Skip to main content

java print

Syntax of main method in java is   public static void main(String[]args) before understanding about main method we have to understand that who is responsible for main method.if we write a simple program or write a simple java class without main method like

     Test.java
   
      class Test
     {

     }

above program will compile successfully.for compilation of  java file use javac tool or javac.exe file.

javac Test.java

it means compiler checks only syntax of class that means compiler does not check the main method in the file or program,so that compiler is not responsible for main method in java.

After successfully compilation of above program we have to run the generated byte code.

java Test

Error: Main method not found in class Test, please define the main method as:
   public static void main(String[] args)

So JVM (java virtual machine) or java interpreter is responsible for main method,first jvm check that the main method is present or not in class file.
second if main method is present than it is in proper format or not.

Why jvm always check main method as public static void main(String[]args).
Because in side jvm main method is call as public static void main(String[]args) .

for example :- jvm is a software or program so in side jvm.


                              jvm()
                             {
                              public static void main(String[]args);
                             }

Why main method is declared as public:-In side jvm main method is declared as a public because of jvm has to access main method from outside , so for always availability purpose of main method to jvm,main method is declared as public .

Why main method is declared as static:-we know that jvm is take care about the main method,so jvm has to execute or run main method without creating any object that's why main method is declared as a static.for static function or variable there are two ways to access.

  • by using the object.
  • by using the class name. 

Why void:-In java void is not a data type here void is a keyword and mainly used for function.
here void is return type of main method,it does not return any value to jvm. if main method return some data or value to jvm that will be useless for developer because developer can not use that value,that's why main has a void as return type.
In the syntax of main method public and static both are access modifier than the order of access modifier can be change so we can write main method as

static public void main(String[]args)

Why String[]args as parameter:-
here String is a java class why use String type parameter because in java language String can be convert into any primitive type,why String array type because if it is not array type then we can pass only single value to main method from command line,if we want to provide multiple values then it must be array type that's why.

args:-Here args is a array name it not compulsory to use args only we can write any valid identifier in place of args.


Valid Syntax of main method is given below:-

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

Valid command line argument declaration is given below:-
1)    main(String[]args)
2)    main(String args[])
3)    main(String…args)
4)    main(String[]  args)
5)    main(String   []args)
6)    main(String[]a)

In place of args we can use any valid identifier.



About String class:-public final class java.lang.String implements java.io.Serializable, java.lang.C
omparable<java.lang.String>, java.lang.CharSequence
{
}
In a java String is class which is present in java.lang package ,java.lang package is by default available to all the classes. String objects are mostly used in java because it can easily convert into primitive types.

String class is declared as a public so any where we can use String class facility like constants, constructors and methods of String class. String class is declared as a final so this class cannot inherit by other classes and cannot override methods of this class. String class implements three interfaces these are Serializable, Comparable and CharSequence , for providing more facilities. String is immutable class we cannot change the contents of object or in other words object cannot modify.

Comments

Popular posts from this blog

Inner classes

Inner Classes:- class inside class or interface called inner class or nested class. Advantage of inner classes is grouping multiple classes and interfaces into a single class. Another big advantage of inner class is that inner class can happily access all the data or variables of outer class directly. Types of Nested classes:- There are two types of nested classes static and non-static nested classes. (A)Non –static inner classes:- 1)     Member inner class 2)     Anonymous inner class 3)     Local inner class (B)Static nested class Example1:-Outer.java class Outer { class Inner { void disp() { System.out.println("Hello This is Inner class Method"); } } public static void main(String[]args) { Inner in=new Inner(); in.disp(); } } Output:- C:\JAVATECH>javac Outer.java Outer.java:12: error: non-static variable this cannot be referenced from a static context Inner in=new Inner(); ...

Inheritance-4

Example10:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(null); System.out.println("This is sub class Zero argument constructor"); } public static void main(String[]args) { Test t=new Test(); } } Output:- C:\JAVATECH>javac Test.java C:\JAVATECH>java Test This is super class One argument constructor This is sub class Zero argument constructor Example11:- class Student { Student() { System.out.println("This is super class Zero argument constructor"); } Student(String s) { System.out.println("This is super class One argument constructor"); } } class Test extends Student { Test() { super(); super(null); System.out.println("This is su...

Object,Data Hiding

Object:- Object is real time entity exist in the real world , the basic purpose of object is to transfer data from one location to another location.object is small memory , allocated inside primary memory(RAM).in java object created inside heap memory,actually in java RAM memory  is divided into some segment according to the type of variables for example. RAM==>Heap area+Stack area+String constant pool+class area  etc. when object created then inside object only non static class variables occupied memory and initialize with default values. non static class variables also known as instance variable reside into object in heap area,static variables occupied memory in class area at the time of class loading from HARD disk to RAM and all local variables occupied memory in stack area when reaching to variables. object  created from the class then what object contains is depend on class structure or class members.             ...