Java Tutorial – Java Hello World (part II)

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:

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:

publicclass HelloWorld

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:

publicstaticvoid main(String[] args)

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:

publicclass HelloWorld
{

public class HelloWorld
{

This is the Class definition.

publicstaticvoid main(String[] args){

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");

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!

It’s a matter of preference, I bet half of the Java developers will say to do it in the same line, and the other half will say on a new line.

The way to configure it in Spring STS in Window -> Preferences. Then in the popup screen choose Java -> Code Style -> Formatter and create a new Profile. Inside the profile you’ll see tabs for Indentation and Braces (etc.)

I just want to say thank you for providing these tutorials! I always had a bit of trouble understanding certain things in programming. My professors never really explained in detail the things you have covered or if they did, they didn’t explain it in a way that was easy to understand. As you mentioned in your tutorials the “scary” parts such as classes and methods is what always held me back as moving forward with developing applications/software. Now not anymore. Thanks!

I bought your book too now…You java tutorial is just amazing!!!!! I would recommend it to all my friends..u wont believe i wont sit up this late before my pc reading anything technical..trust me..u changed it for me..here Iam sitting and reading your Java tutorial with so much of interest..your simple language style is really motivating me to read …Excellent Job Trevor!

Thanks so much for your feedback Preethi, I’m very happy to hear that you find my teachings to be so inspiring.
I will hopefully be recording a new podcast episode in the next 24 hours, so stay tuned for that!

Hey!
I have taken two years of computer science, but i’ve never really grasped the subject and couldn’t memorize it. But after looking through this website, i feel as though this will be a huge help. i’m looking forward to learn using this.
I did have a quick question though, what exactly is the difference of getting the book and also going through this website?

Thinking about programming as a new career and I’m going through these tutorials to see if I not only like it but can grasp the languages. I have to say that you have done a great job in presenting this subject matter and I’m becoming more convinced that this is something I’d like to do. Do you have a post on which languages are in most demand?

You’ll see that Java is still a top contender when it comes to programming languages. Java’s being slightly beat by Python, and all the other “languages” above Java are web technologies that actually go hand in hand with Java. My next book will be covering those topics: HTML/CSS, Javascript, Ajax

Wow, I loved the way you chopped the commands, you could be a very good author in programming, or perhaps youve written a lot already. I also liked the way the you made a sample of the wrong vs right code.keep it up

Eventually I will be… At the moment I’m super busy with another business venture that’s keeping me in full programming mode, once that’s done I’ll be able to focus my efforts on getting more blog posts up.

Thanks for your post about Java Hello World! I want to ask you 2 questions.

1/. “This could represent an Array of Strings, and this Array would be referenced by the variable name “args”” <- So, when (and why) do we use "args" variable? I read some codes of Java and it was always beginning by the line of code "public static void main (String[] args)" <- Is it default setting in Java programs?

1) the “args” variable is used when the program is launched. If you need to pass in any information for the program to run properly, then this is what you would use. For example, let’s say you write a program that’s meant to read text files and parse them in some way… you could use the “args” variable to pass in the PATH for the file (i.e. C:\windows\textfile.txt).

2) The main method isn’t actually explicitly called in code, it’s called via your IDE when you tell your IDE to “Run as Java Application”. Since you’re telling your IDE to run your code as a Java Application, it knows to invoke the “main” method.

Trevor you’re the most amazing writer I have ever Came across. I bet you that if you write a book in java programming then you’ll be the bestseller. Tell me one thing how much do I need to make apps like whatsapp or instagram ?

Thanks for the kind words… I actually did publish a book and it actually did hit the Amazon best seller list.

As for your question, mobile apps like whatsapp and instagram require the knowledge of not only Java, but platforms like Android (which is based on Java). The eclipse IDE has a decent set of plugins that allow you to do Android development… but like I said, you need a solid understanding of Java before you’ll want to dive into Android development.

Wow marvelous web page man i mean ur doing great stuff to the society , blindness about coding its everywhere with your help people could actually learn and be a coder in java and etc , thks man im hooked on with ur tutorials

I have been trying to learn java for over a month, have watched so many tutorials,,, I found your tutorial just when i was about to give up,,,never thought there’s someone who could make it sound and look so easy and interesting,, I feel like you are out of this world man bravo!!!! Awesome work,, thanks for making this whole thing an effortless learning experience for us!!!!

Hello Trevor… It’s a great explanation. I’m learning alot from your tutorials.. I wanna do Automation software testing with “Selenium”. It requires Java to learn Selenium. How much and what level of Java do i need to know to use Selenium?. What topics should I study to learn it? Your advice would be greatly appreciated. I want to study Java according to your advice. Thank you very much.