Java Tutorial – Java Hello World (part II)

by Trevor Page on July 11, 2012

 

Java Tutorial – Java Hello World (part II)

In this post, we will look at the first Java program we created in the last post. So if you haven’t yet read the previous Java hello world post, please do so now so you’re all caught up.

Now that we’ve created our first Java program, we need to figure out what all that code actually means right!? Surely you’re looking at it and saying, “How does anyone understand that stuff?”, I assure you, it’s not as scary as it looks. All we need to do is break down each part of the code and understand what each part does and why it’s important. So let’s do that! Here’s the code:

package com.helloworld;
 
public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello World");
  }
}

For now let’s skip over the first line:

package com.helloworld;

It was created automatically by our IDE (the Spring STS program you’re using to code) when we created the HelloWorld.java file. We’ll talk about why this line is important later, for now, just know that it needs to be there otherwise we’ll get errors.

The second line is:

public class HelloWorld

This line is important for two reasons. The first reason is that it represents the Class. Now when I say Class, I don’t mean a class of students with a teacher. I mean a Java Class file. These Class files represent a ‘blueprint’ for an Object in Java. Objects, in Java, are the foundation of the programming language, because Java is an Object Oriented programming language. Objects in Java represent exactly what they sound like, an Object in real life. Think of an Object like a noun (person, place or thing). Our example of HelloWorld.java isn’t a great one, as HelloWorld doesn’t really mean anything in the real world. Putting that aside, I’ll have an entire post about Object Oriented languages soon.

So, the second reason that this line is important, is that the name “HelloWorld” has to be exactly that name, because that’s the name of the file that we created (HelloWorld.java). We had chosen to name this file “HelloWorld.java” for no particular reason, but since we chose that name, the name of our declared Class needs to match. One thing to note, is that Java is case sensitive, so that means “HelloWorld” is not the same as “helloworld”. Capital and non-capital letters matter.

One thing I haven’t yet touched on with this line of code is the first word, “public”. In Java, there are four levels of visibility for your code: public, protected, package and private. Essentially all this means is whether or not other Class files will be able to have access to this particular file. Since we say public, that means that any other file can look and interact with this Class file. This concept of visibility is also used with something called methods. What’s a method you ask? Good segue into the next line of code:

  public static void main(String[] args)

The first thing you may notice, is that word “public” again. It means the same thing as it did before, except now it’s saying that this particular chunk of code (the code between the curly braces {}) is also public. Which again, means that any other Class files can access this particular chuck of code! Seems like a pretty easy concept right?

Now, I said that I would segue into what a method was… well, you see how I keep referring to this code as a “chunk” of code. Well, now it’s time for me to use the technical term for this chunk of code, it’s called a method! Methods are essentially “chunks” of code that you can run over and over again by “calling” that “chunk” of code. “Calling a method” means that I wish to execute all the lines of code that are present between those curly braces {} of that method. Furthermore, we always gives names to our methods, just like we always give names to our variables. For this particular piece of code, the method’s name is “main”.

So now allow me to re-iterate:

public class HelloWorld
{

This is the Class definition.

  public static void main(String[] args)
  {

This is a method within our Class definition.

We establish this concept of something being “within” our Class definition by looking at the curly braces {}. The outer set of curly braces are the Class definition, the inner set is the method definition. This is more or less constant in any Class file, so keep that in mind.

The two words on the left of this method “static void” are what’s known as modifiers, they modify how the method will work. For now let’s ignore them, as I will expand on what they mean in a future java tutorial. So that just leaves us with (String[] args), this is known as the “parameter” or “argument” section of the method. You see, in Java’s syntax, a method can have variables passed in so we can use them and modify them inside the method if we wish. This is denoted with the parenthesis () after the method name. The implementation of this and the usefulness of this I will also outline in future posts, as there’s only so much teaching I can do in one java tutorial (and more importantly only so much learning you can do).

The last thing I want to quickly point out is the String[] args. I’ve mentioned what a String was in our first lesson on variables, but why are there square brackets [] after the String variable type? This is just Java’s syntax for an Array. I mentioned what an Array was in the Data Structures lesson, to refresh your memory, just think of it as a list of Strings. Think of it like a kind of grocery list: “Lettuce”, “Tomato”, “Bananas”, “Pasta sauce”, etc… This could represent an Array of Strings, and this Array would be referenced by the variable name “args”! See? String[] args

Now, our final line of code to analyze:

    System.out.println("Hello World");

This one is pretty simple to explain. All this code does is output whatever is in quotes into your Console window in your IDE (Spring STS). This line of code is used most often when you just want to quickly take a look at the state of a variable at any given moment. So, for example, if you had a variable called outsideTemperature, and this variable just updated its own value every 30 minutes, and let’s say you want to see what the variables state was (in other words, see what the outside temperature is), you could write System.out.println(outsideTemperature). Then the value that the variable has would display in your Console window in your IDE.

Alrighty! I think that’s enough for today’s Java tutorial, I hope I helped to take the mystery out of this Java hello world code. Give yourself a pat on the back for making it this far, you’re doing great! Again, I will ask you to leave comments if there’s anything that’s not clear to you, or if you just want to share any sort of feedback with me. I love to get comments and I’ll respond to all of them to the best of my ability :)

Oh, and if you want to get notified as soon as my next posts are published, just sign up to my mailing list below and I’ll be sure to email you when these Java tutorials are hot off the press!




{ 32 comments… read them below or add one }

Leave a Comment

Powered by sweet Captcha

Previous post:

Next post: