Core Java Tutorial

 
 

Sample java HelloWorld Programme

 
 

To create a simple java program, you need to create a class that contains main method.Let us look at a simple code that will print the words Welcome to Java Tutorial by Jr Kallam.

        public class FirstJavaProgram {

              /*This is my first java program.
                This will print 'Hello World' as the output */
                 

         public static void main(String []args) {
                System.out.println("Welcome to Java Tutorial by Jr Kallam");   // prints Welcome to Java Tutorial by Jr Kallam
   }
} 

 
 

Let's look at how to save and run the code

  1. Open note pad and add the code as above
  2. Save the file as a FirstJavaProgram.java
  3. Open a command prompt window and go to directory where you saved the class
  4. Now, we need to compile oyr code to see whether is there any mistakes in our code, for that type javac FirstJavaProgram.java , if there are no errors command prompt will take you to the next line
    what happens at complie time?

    Initially our file is saved with ".java" extension. Once if you compile your .java file then you will get the .class file(Which is in byte code format).

    Compilation
  5. Now, we need to run our code to get the output, for that type java FirstJavaProgram , now you can see output as a "Welcome to Java Tutorial by Jr Kallam".
    what happens at Run time?

    After getting .class file, if you run yur code then interpreter wil read byte code and executes instructions

Output

     c:\> javac FirstJavaProgram.java
     c:\> java FirstJavaProgram
      Welcome to Java Tutorial by Jr Kallam

Understanding what we wrote above

   "Class" is a keyword used to declare a class in java

   "public" keyword is an access modifer which represents visibility,if you use public as a keyword this class is visible to all.

   "static" os a keyword, if we declare any method as static, it is known as static method. The core advantage of static method is that there is no need to create object to invoke the static method. The main method is executed by the JVM, so it doesn't require to create object to invoke the main method. So it saves memory.

   "void" is return type, it returns null.

   "main", from where java compilation starts

   "String[] args" is used for commandline argument.

   "System.out.println()" is used to print output.

java main method declaration

     You can declare main method of java in different formats

1)public static void main(String[] args)  
2)public static void main(String []args)  
3)public static void main(String args[])  
4)public static void main(String... args)  
5)static public void main(String[] args)  
6)public static final void main(String[] args)