Day2 : Compiling Java Program with Examples

The First Java Program 

class Test

{

        public static void main(String [] args)

            {

                System.out.println("Hello User");

            }

}


  • Always Write class which you want to make, the first letter in capital letter like Test.
  • Here public is access modifier, static is used for memory management mainly, void is Return Type, main() is method, String [] args is command line argument, System is predefined method, out is object refinement, Println is predefined method.

Compiling Java Program

To compile a java program we can command Javac having with following Syntax:
Javac Name of the program

 For Example : Javac Test.java

Whenever we compile a java program the compiler generates byte code files and these byte code files have 3 special properties:

  1. The Extension of the byte code file is always class.
  2. There main is always same as name of the class defined by programmer.
  3. Number of byte code files is always equal to the number of class defined by the programmer. For Example: If we've created three class called student, Faculty & College then the compiler will generate 3 byte code files called as student.class, Faculty.class, college.class.

Running of Java Code 

To Run Java program we use following syntax :
Java Name of the Class

The word main class means the class having main method.

For Example: Java Test 

Where to save Java program 

If you're Beginner then go with Notepad to save in bin folder because you're not know how to set path. But this is not recommended because in bin file there are too many files comes when you're installing JDK. But as a beginner we're not recommend to go directly on IDE.

Secondly, if you don't want to save program in java first you go to set the java path. After setting path of java you can access java from any folder, any driver.

class  Test
{
    public static void main(Strings [] args)
        {
            System.out.println("Hello user");
            System.out.println();
            System.out.println("I'm learning Java");
        }
}

OUTPUT of above code:
                                  
                        1)Hello User
                        2)
                        3)I'm learning Java

One file name containing Two Or More classes

Step1 : Open Notepad with Run as administrator and save File name with mp.java bin folder.

class Bhopal 
{
    public static void main(String [ ] args)
    {
        System.out.println("Hello from Bhopal");
    }
}

class Indore
{
 public static void main(String [ ] args)
    {
        System.out.println("Hello from Indore");
    }
}

Step2 : Open command prompt with run as administrator.
Step3: Type following syntax:
                javac mp.java
                dir Bhopal
                dir Indore

                Java Bhopal
                Java Indore

//If you want to print both just write:
In Bhopal class, in next line after Syetm.out.println("Hello from Bhopal"); write Indore.main(null);
and this method is called in java as Indirect Recursion.


 



Comments

Popular posts from this blog

Day1: Introduction to Java and its feature

What does System.out.printf( "%-15s%03d\n", s1, x) do?