Let’s start with a little program in java to print a line “Hello World”

public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World");
}
}
  • Line 1
    public : One of Access Modifiers. It makes this piece of code visible to the world.
    class : A class is a blueprint from which individual objects are created.
    HelloWorld : Name of the class
  • Line 2
     static : static is a keyword that allows main( ) to be called without having to instantiate a particular instance of the class.
    void : void simply tells the compiler that main( ) does not return a value
    main : main( ) is the method called when a Java application begins. Please note “Java is case-sensitive”. Thus, Main() is different from main(). Java compiler will compile classes that do not contain a main( ) method. But the Java interpreter has no way to run these classes. So, if we had typed Main instead of main, the compiler would still compile your program. However, the Java interpreter would report an error because it would be unable to find the main( ) method.
    Parameter : Any information that you need to pass to a method is received by variables specified within the set of parentheses that follow the name of the method. These variables are called parameters.
  • Line 3
    System.out.println() : Java statement that prints the argument passed, into the System.out which is generally stdout. Let’s break them
    System => The System class contains several useful class fields and methods. It cannot be instantiated. System class can be used for standard input, standard output, and error output streams. It can also be used to externally defined properties and environment variables.
    Dot Operator (.) => In Java, the dot operator can be used to call methods and variables.
    out => If we look into System class file, we will find
public final static PrintStream out = null;

So out is a public final static object of PrintStream class.
println => PrintStream class defines a function println

public void println(String x) {
synchronized(this) {
print(x); // prints message
newLine(); // brings cursor to new line
}
}
Categories: JAVA

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *