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.
- First of all, it declares the class name which must be that same thing as the file name.
- 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:
- public states that this is a public method. More on this later.
- static states that this is a static method. More on this later.
- 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.
- 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.
- System.out.println("Hello world!"); This line specifies that it should output the text specified. In this case, the specified text is "Hello world!
- This line closes the main method.
- 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.