Java Sample program:
Let us start to write with a "Hello World" program using Java. A Java program has to be written which will display the message "Hello world" on to the screen.
println() method is the one which is used to display any value provided to it, as an argument.
Therefore, println("Hello World");
This Method prints the value and also a '\n' character at last. But this println() method is a non-static member of the class 'PrintStream'. So, an object of 'PrintStream' class should be used to invoke the println() method. Moreover, that object has to be associated with the standard output device (Monitor), in order for the message to be displayed on the screen.
A partial definition of the two classes 'System' and 'PrintStream' are shown above. In 'System' class, there is a static member called 'out' which is an object of 'PrintStream' class. This refers to the Standard Output Stream.
Note: Similarly, there are two other variables in 'System' class - 'in' and 'err' which refer to a standard input stream and standard error stream respectively.
Thus the static variable 'out' which is an object of 'PrintStream' can be used to invoke the 'println()' method.
However, 'out' is a member of 'System' class and that that too a static member. So, the class name 'System' should be used to access the variable 'out'.
Thus, System.out.println("Hello World"); should be used to display the message.
Every program begins from main() method. So, include the previous statement inside the main() method. As said earlier, Java is true object-oriented language; so place the above code, inside a class.
class Sample //name specified by the user
public ststic void main(String args)
Type the above program in notepad and save as "Sample.java". Filename is given as same as that of class name.