≡ Menu

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:

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!




Don't code another minute until you have these tools.
Get this 1-page PDF that outlines the top 7 Tools that Java Professionals use every day.

{ 33 comments… add one }

  • farida July 13, 2012, 10:25 am

    i enjoyed..it didn’t bore me at all…

    • Trevor Page July 13, 2012, 11:28 am

      I’m glad I could keep your attention! Programming can get a bit dry sometimes

  • Belinda July 17, 2012, 2:29 pm

    thanks for the share, doing a good job.

  • flo August 9, 2012, 10:10 am

    Hi Trevor,
    as someone who learned C before, how is it with the curly braces in java?
    You put them in a new row, Spring in the same row.
    What is the “right” or better way?
    Can I change it in Spring?

    Thanks a lot.

    • Trevor Page August 9, 2012, 10:17 am

      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.)

      • flo August 9, 2012, 10:36 am

        thanks. now I can go on with this great tutorial.
        Have a nice day.

  • Dave August 10, 2012, 12:19 pm

    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!

  • gav August 12, 2012, 8:07 am

    hey, thanks for doing these tutorials. I started them last night just for kicks, and it’s been really interesting! :)

    • Trevor Page August 12, 2012, 3:37 pm

      My pleasure! Thank YOU for taking the time to go through them :)

  • faiqa September 24, 2012, 9:14 am

    i learn it clearly..thank you trevor

  • Aurora November 3, 2012, 12:55 pm

    Hi.
    A friend suggested your blog because I am interested in learning Java.. I wanted to say I am enjoying it. Thank you!!!!

    • Trevor Page November 3, 2012, 1:29 pm

      Glad to hear it :) If there’s anything I can do to help you out along the way, do not hesitate to ask!

      Happy Learning

  • Preethi November 26, 2012, 5:59 pm

    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!

    • Trevor Page November 27, 2012, 9:22 pm

      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!

  • Ramya Rajagopalan December 29, 2012, 1:08 pm

    hello sir this site is amazing……… your way of teaching made me to love java thank u so much

    • Trevor Page December 29, 2012, 4:45 pm

      My pleasure Ramya, best of luck with your learning and please leave more comments if you have any questions along the way!

  • Muhammad Bilal January 2, 2013, 4:59 pm

    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?

    Thanks so much!

    • Trevor Page January 2, 2013, 6:15 pm

      Hi Muhammad,

      Thanks very much for you feedback!
      The book has some additional content not available on the website (and it’s formatted in an easier to read fashion).

  • Ivan Legends January 26, 2013, 12:04 pm

    Hi,is it possible to programme without downloading the java tools?

    • Trevor Page January 28, 2013, 1:30 pm

      You need the tools to compile the code and run it. So if your goal is to just write out code and never run it then all you’ll need to use is Notepad and save the files as “.java” files.

      Though, like I mentioned, you won’t be able to compile and test this code without the Java tools (including IDE and JDK/SDK)

  • John Bruin February 2, 2013, 11:08 pm

    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?

    • Trevor Page February 3, 2013, 8:46 am

      I haven’t posted about it exactly, but here’s a recent article about programming languages:

      http://gigaom.com/2007/09/07/top-10-programming-languages-of-the-future-you-voted/

      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

  • Deepa February 21, 2013, 12:59 am

    ILove the way you teach…

  • apple April 30, 2013, 12:50 am

    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

  • Azri May 5, 2013, 4:38 am

    Suberb explanation! god bless u!

  • abhishek July 9, 2013, 3:43 am

    sir are u also provide ur blogs on advance java

    • Trevor Page July 9, 2013, 5:29 pm

      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.

  • Sammy October 7, 2013, 3:59 am

    Hi Trevor!

    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?

    2/. When we call the method "main", how do we call it?

    • Trevor Page October 8, 2013, 8:11 pm

      Great questions Sammy,

      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.

      I hope that helps :)

      • Sammy October 9, 2013, 10:38 pm

        Thanks you :D

  • saquib ahmed July 8, 2014, 8:36 am

    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 ?

    • Trevor Page July 9, 2014, 10:54 am

      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.

  • Anonymous October 6, 2014, 6:16 pm

    I’ve listened to the entire podcasts twice,and now i’m going through all of these matching blog posts..and watching the video’s/taking the tests.Just wanted to say,great job trevor!

Leave a Comment

Powered by sweet Captcha

Next post:

Previous post: