Input:
class helloprog //change the helloprog with what you named on your class
{
public static void main(String args[])
{
System.out.println("Hello World!");
System.out.println("Welcome to CJ's Blog Spot :)");
}
}
When you saw what is on the output of the program, I want you to compare it with the codes and try to examine it.
Here is the output if you don't have Eclipse:
Output:
Hello World!
Welcome to CJ's Blog Spot :)
1.)
class helloprog
{
........
{
........
........
}
}
The first line of the program which is class helloprog, is the class definition of our program. It indicates the name of the class which is helloprog. But you can name your class anything that you want. In Eclipse, when you create a class, the class automatically appears with the name you put into it.
Well, a class is the basic unit of a Java Program and it can have one or more methods. It's very important that you have a class because you can't run the program without it. By the way, I will explain more about classes on the next Basic Java Program Lesson.
The second line of the program which consists of the left brace, and which is matched with the second right brace which is at the final line of the program indicates the beginning and end of the body of class.
2.)
class helloprog
{
public static void main(String args[])
{
........
{
public static void main(String args[])
{
........
........
}
}
}
}
Well, every Java Class must have a main method and it must only be one. It's very important because when you run a program, your computer will always going to find first and begin the execution with the main method. So if you don't have this method to your program, your computer wouldn't know where to begin the execution of your program.
Now, we created this method because we want it to print the Hello World!.
3.)
class helloprog {
public static void main(String args[])
{
System.out.println("Hello World!");
System.out.println("Welcome to CJ's Blog Spot :)");
}
}
On the fifth line of the program, or after the first/left brace of the main method, you can see the method body, and this is where our instruction for the method is located. As you can see, everything that is inside the parentheses of the System.out.print(....); was printed on the output.
Well, the reason behind that is the print or println(next line) method. It is a built-in method which prints what you have inserted inside the parentheses. As you can see also, we entered a double quotation marks inside the parentheses but it is not printed. Well, it's because everything that was inside the double quotation marks means that it's a String. A String evaluates to itself, that's why the values are the String itself. But if there's no quotation marks there, the computer will not print what is inside the parentheses(unless it is a number because it's going to be an integer).
By the way, I'm just going explain to you more about of what is a String, Integer and Character later .The important now is you know what is a class, main method, and how to construct a simple Hello World! program in Java.(YEAH!) :)
If there's something that is not clear for you or if there's something wrong, don't hesistate to leave a comment down below or email me. Thanks :)