Java Program Structure

To understand structure and features of class I have used example of Hello Java program here. There is few lines in program and the major task of the program is to print “Hello Java” text.
Example:
public class Hello
{   
    public static void main(String[] args)
    {
        System.out.println("Hello Java");  
    }
}
Program Output:
HelloJava
The above example has been used to print “Hello Java” on the screen.
You have to keep in mind that, Java code is case sensitive.
Java Program Structure

public class Hello

  • This creates a class called Hello.
  • All class names required to start with a capital letter.
  • The word public means that it is accessible by any other classes.

{…}

The 2 curly brackets are used to group all the commands together so it is known that the commands belong to that class.

public static void main

  • The word public means that it is accessible by any other classes.
  • The word static means that it is unique.
  • The word void means this main method has no return value.
  • main is a method where the program starts.
You will notice that the main method code has been moved to some spaces left. This is called indentation which used to make a program easier to read and understand.

System.out.println();


  • System is a final class name and cannot be instantiated. Therefore all its members (fields and methods) will be static, and we understand that it is an utility class. As per javadoc, “…Among the facilities provided by the System class are standard input, standard output, and error output streams; access to externally defined properties and environment variables; a means of loading files and libraries; and a utility method for quickly copying a portion of an array…”
  • out is static member in System class which is instance of java.io.PrintStream. Its access specifiers are public final. This gets instantiated during startup and gets mapped with standard output console of the host. This stream is open by itself immediately after its instantiation and ready to accept data. When running a program from windows command line, it is the standard console.
  • println method prints text on the screen with newline. There are multiple println methods with different arguments (overloading). Every println makes a call to print method and adds a newline. print calls write() and the story goes on like that.
  • The data that is to be printed is put inside brackets. You will also notice that the words in inverted commas because they are what is called a string. Each letter is a character and a series of characters grouped together is called a string. Strings always be put between inverted commas. You must have to put a semi-colon after every command to show that it is the end of the command.


                                                                                  ⏪ PREVIOUS PAGE          NEXT PAGE ⏩

2 comments:

  1. NIIT java assignment go to =>http://mkniit.blogspot.in

    ReplyDelete
  2. JAVA TUTORIAL, JAVA STUDY, JAVA TECH, JAVA PROJECTS, JAVA ASSIGNMENTS, NIIT PROJECTS, NIIT ASSIGNMENMENTS, NIIT CYCLE TESTS, NIIT LAB@HOME, NIIT EXAMS MT, NIIT PMR, NIIT NOTES AND MUCH MORE GO TO https://solutionrk.blogspot.in/

    ReplyDelete