Wednesday, November 12, 2014

Getting Started with Java (Hello, World)

I spend some time on the subreddit /r/javahelp, and see many of the same questions and errors crop up again time after time.  People who are trying to learn how to program get caught up in the syntax, and wind up down the rabbit hole.

So let's take a look at the very basics of Java.

The basic unit of Java programming is the class.  We define a class by creating a text file (with a .java extension) according to some strict rules, then we compile the .java file to a .class file using the javac command.  Better yet, we use an Integrated Development Environment, but I will discuss that in a future post.

Your source code files, which is what we call the .java text files, can be created with any text editor.  Notepad will work fine for these early examples, but there are certainly better ones out there.  Even for your first efforts, you will want a clean work area free of clutter, so it is best to create a new directory somewhere on your hard drive for each project on which you will work.  I prefer to base everything under a "\Dev" directory so that all of my work is in one easily identifiable place.  Since my main system at home uses a solid state drive plus a storage drive, that means my work area is inside of "D:\Dev".  I'm going to write everything here as though I'm using the C: drive, though, since that's more likely to be what you are using.

We want to have a directory for our project, which will be where we go to do all of our work on any given task.  Finally, while this may not seem very important right now, it's best not to cluster your source files right there in the project directory.  So I like to have a 'src' directory in which to save my source code files.

So, since by convention our first project is "Hello World", we wind up saving our .java to the following directory:

C:\Dev\HelloWorld\src

There is one additional complication relating to source, which is what we call "packages".  I'll defer discussion of this for the time being, but they are set up in your workspace as directories off of the src directory above.

So, that being said, what does Hello, World look like in Java?  It quite literally cannot get any simpler than the following example:

HelloWorld.java

Everything here is necessary to the task at hand.  Let's go through it line by line:

public class HelloWorld {

Every java class needs to be declared.  While there are a few other possibilities for this line, this is the most common way in which you will declare your classes.  The word 'public' means this class can be seen (and ultimately used to do work) by any other class that knows about this project.  We are defining a class, and giving it the name 'HelloWorld'.  By convention we always start our classes with an upper case letter, and follow that with what is called "Camel Case", which means lower case words with upper case initial letters.

Finally, there is an opening curly bracket, which matches the curly bracket on the very last line.  Curly brackets (or braces) are going to be your close companions as you work with Java, so get used to them.  They are always 'balanced', meaning that each opening bracket '{' has to be matched by a closing bracket '}'.

public static void main(String[] args) {

Next up, we have 'main'.  Any command line Java program will start at main, and main is always declared the same way.  The only thing you can really change here is the name 'args', which is arbitrary.  Command line switches and parameters will be found in args, and although we aren't making use of them in this program, we still have to mention the possibility.  Again, we have an opening bracket.  Any 'method' (or 'function') you create in a Java class will have a set of brackets around its code.

System.out.println("Hello, world!");

Here's the only line that actually does something in our little demonstration.  It writes the literal value Hello, world! to the console output.  All Java programs (and indeed, all programs in most languages have 'standard output' and 'standard input' available by default for simple user interaction.

}
}

These two lines are just needed to balance the brackets.  The first closes the definition of "main", the second closes the definition of the class HelloWorld.

Presuming that a Java Development Kit is available and configured correctly, this can be built with the 'javac' command.:
Compiling HelloWorld
If we get no error messages, we should now have the file "HelloWorld.class" sitting here as well.


Now we can run it with the 'java' command:
Running HelloWorld

In a future installment, I will discuss further organization of the project space.  There are good reasons not to mix all the .java and .class files in one place.  Integrated Development Environments make all of these tasks simpler, and I definitely recommend obtaining IntelliJ IDEA or Eclipse or NetBeans (or all three).

View code here

No comments:

Post a Comment