Hello World
public class Hello {
     public static void main(string[] args) {
          System.out.println("Hello world!");
     }
}

Let's take a look at this code line by line.

  1. First of all, it declares the class name which must be that same thing as the file name.
  2. The second line states that this is the main method. It is what is run when this program is run later. Lets break this line piece by piece:
    1. public states that this is a public method. More on this later.
    2. static states that this is a static method. More on this later.
    3. void states that this returns no result. Something that would return a result would be like a method that calculates the area of a rectangle. It would return the area.
    4. main(string[] args) { that was long, but hold on. The main part of it says that when this class is run, this is the part that is run. The part in the parenthesis states that it's an array of strings. The "args" part of it takes the commands on the command line. Every argument that is taken by a method, if any, is described as the variable type and then the name for it where it is going to be used in the argument. Something to note is that it is a string argument, but why are those brackets there? The brackets specify that is in array of strings. Since it is the main method, it takes the arguments directly off of the command line.
  3. System.out.println("Hello world!"); This line specifies that it should output the text specified. In this case, the specified text is "Hello world!
  4. This line closes the main method.
  5. This line closes the Hello class

Wait, so now we no how the java program works, how does it run? Well I'll explain. Look here to learn how compile and run java programs.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License