DEV Community

Cover image for Intro to Java
Matthew Palmer
Matthew Palmer

Posted on

Intro to Java

Introduction

Java has been all the buzz for awhile now. 2020 seems to be the year of heightened popularity with a forecast in only gaining higher momentum in 2021. It's especially a popular language with prestigious banking companies such as JP Morgan Chase & Co. As luck would have it, Java is currently ranked higher than even JavaScript as far as job openings go (demand).

So, what better time for us to get started than now? This blog will cover how to get started and introduce some basic conceptual elements of Java.

Setup

For the sake of only speaking from direct experience, I'd like to stick with Visual Studio Code as the primary IDE being used for this blog.

  1. Once downloaded and installed, you'll want to head over to Oracle's JDK download page for the latest version of Java (as of the time this blog was posted) which should provide resources in regards to the correct Java Development Kit (JDK) for your system. Download & install! :)

  2. Head over to your Visual Studio Code and click the "Extensions" tab, Type in "Java Extension Pack" and install this extension.

NOTE: This pack comes with some nifty shortcuts to speed up your coding, and leaves enough mental space for understanding some of the initial syntax moving forward.

To be sure everything is set up properly, try typing java or javac into your terminal to make sure you get something returned. This is what I get on my system:

macbookpro@MacBook-Pro Java-GettingStarted % java
Usage: java [options] <mainclass> [args...]
           (to execute a class)
   or  java [options] -jar <jarfile> [args...]
           (to execute a jar file)
   or  java [options] -m <module>[/<mainclass>] [args...]
       java [options] --module <module>[/<mainclass>] [args...]
           (to execute the main class in a module)
   or  java [options] <sourcefile> [args]
           (to execute a single source-file program)

 Arguments following the main class, source file, -jar <jarfile>,
 -m or --module <module>/<mainclass> are passed as the arguments to
 main class.

 where options include:

    -cp <class search path of directories and zip/jar files>
    -classpath <class search path of directories and zip/jar files>
    --class-path <class search path of directories and zip/jar files>
                  A : separated list of directories, JAR archives,
                  and ZIP archives to search for class files.
    -p <module path>
    --module-path <module path>...
                  A : separated list of directories, each directory
                  is a directory of modules.
    --upgrade-module-path <module path>...
                  A : separated list of directories, each directory
                  is a directory of modules that replace upgradeable
                  modules in the runtime image
    --add-modules <module name>[,<module name>...]
                  root modules to resolve in addition to the initial module.
                  <module name> can also be ALL-DEFAULT, ALL-SYSTEM,
                  ALL-MODULE-PATH.
    --list-modules
                  list observable modules and exit
    -d <module name>
    --describe-module <module name>
                  describe a module and exit
    --dry-run     create VM and load main class but do not execute main method.
                  The --dry-run option may be useful for validating the
                  command-line options such as the module system configuration.
    --validate-modules
                  validate all modules and exit
                  The --validate-modules option may be useful for finding
                  conflicts and other errors with modules on the module path.
    -D<name>=<value>
                  set a system property
    -verbose:[class|module|gc|jni]
                  enable verbose output for the given subsystem
    -version      print product version to the error stream and exit
    --version     print product version to the output stream and exit
    -showversion  print product version to the error stream and continue
    --show-version
                  print product version to the output stream and continue
    --show-module-resolution
                  show module resolution output during startup
    -? -h -help
                  print this help message to the error stream
    --help        print this help message to the output stream
    -X            print help on extra options to the error stream
    --help-extra  print help on extra options to the output stream
    -ea[:<packagename>...|:<classname>]
    -enableassertions[:<packagename>...|:<classname>]
                  enable assertions with specified granularity
    -da[:<packagename>...|:<classname>]
    -disableassertions[:<packagename>...|:<classname>]
                  disable assertions with specified granularity
    -esa | -enablesystemassertions
                  enable system assertions
    -dsa | -disablesystemassertions
                  disable system assertions
    -agentlib:<libname>[=<options>]
                  load native agent library <libname>, e.g. -agentlib:jdwp
                  see also -agentlib:jdwp=help
    -agentpath:<pathname>[=<options>]
                  load native agent library by full pathname
    -javaagent:<jarpath>[=<options>]
                  load Java programming language agent, see java.lang.instrument
    -splash:<imagepath>
                  show splash screen with specified image
                  HiDPI scaled images are automatically supported and used
                  if available. The unscaled image filename, e.g. image.ext,
                  should always be passed as the argument to the -splash option.
                  The most appropriate scaled image provided will be picked up
                  automatically.
                  See the SplashScreen API documentation for more information
    @argument files
                  one or more argument files containing options
    -disable-@files
                  prevent further argument file expansion
    --enable-preview
                  allow classes to depend on preview features of this release
To specify an argument for a long option, you can use --<name>=<value> or
--<name> <value>.
Enter fullscreen mode Exit fullscreen mode

If you're getting this message, you're ready to go!

Hello World

Now you've got Java installed and you're ready to get started! Create a folder on your computer and open it with Visual Studio Code. Inside of your Visual Studio Code, you should have the option to create a file within your new folder. Name it whatever you want, but I suggest helloworld.java for the sake of keeping it logical.

Within your new helloworld.java, start with typing class and choose the second suggestion down. It looks like a box with a dotted border at the bottom. This will create your initial class which will be named after your file. If you called your file helloworld.java, you can expect your code to look like this:

/**
 * helloworld
 */
public class helloworld {


}
Enter fullscreen mode Exit fullscreen mode

Within your class, start typing main and choose the first option. It also has the dotted bottom-border box. This is the main method that is executed when your Java application is compiled and ran. Your code should now look like this:

public class helloworld {

    public static void main(String[] args) {

    }
}
Enter fullscreen mode Exit fullscreen mode

Let's talk about what's going on here. public is a primitive data-type keyword in Java. Primitive meaning "built-in". Using a great resource online to define what it is from w3schools -> "The public keyword is an access modifier used for classes, attributes, methods and constructors, making them accessible by any other class."

static is another primitive data-type meant to represent a constant. static is a non-access modifier. static keyword can be used with a class level variable, block, method and inner class or nested class.

void is another primitive data-type used for when we don't want to return anything. This keyword can be changed depending on what you want to return. If you want to return a string, void is removed and replaced with String.

Let's take a look at how that works!

/**
 * helloworld
 */

public class helloworld {

    public static void main(String[] args) {
        String sayHello = addExclamationMark("Hello world");
        System.out.println(sayHello);
    }

    public static String addExclamationMark(String s) {
        return s + "!";
    }
}
Enter fullscreen mode Exit fullscreen mode

In our main method, we are defining a String labeled as sayHello. This is assigned to a method addExclamationMark() which is taking a string "Hello world" as an argument. addExclamationMark() is defined below our main method. This dynamic method is replacing void with String because a string is what we will be returning. The argument in parenthesis is taking a String as an argument with s as the dynamic value. s is what is being passed in. In main, "Hello world" is what we are passing in. Lastly, we are returning that string with a "!" added to the end.

Our final output will end up being Hello world!

Run your code

Test it out! In your Visual Studio Code, look to the left-hand side of your IDE and choose that icon that looks like a bug sitting on the bottom corner of a play button (or sideways triangle, if you will). Click this and then click the "Run and Debug" button.

Your results should appear in the terminal!

Conclusion

Congratulations, you're now ready to embark on your continued journey with Java! There is so much more out there to learn, but I'll be happy to recommend a free Udemy course which will push you in the right direction. You can find that course at this link.

Happy coding, my friends!

Don't forget to connect with me: 😄
LinkedIn
Github
Twitter
My Portfolio

Top comments (0)