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

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...

Regarding class files

Class files:- Regarding class file or dot class file also known as byte code of class, in a java programming language we can use any number of classes in our program. After compilation all classes convert into individual   .class   file. Example:- class A { } class B { } class C { } class Student { public static void main(String[]args) { System.out.println("Well-come to my java blog"); } } We can save above program to any   classname.java   or we can use any other   name.java,   there is no compulsion for java file name. But recommended to save by that class name which having main method. I saved the above file to   Student.java   name .   Before compilation my   C:\JAVATECH   folder contain only one java file. Student.java For  compilation. C:\JAVATECH>javac  Student.java After compilation   my   C:\JAVATECH   folder contains five files, on...

inheritance

INHERITANCE:- Inheritance is the process of code re-usability, using this property software or applications can develop in less time or can develop rapidly. Inheritance increases the readability of code and decrease redundancy or repetition of code or reduces size of code. Use of previously developed code in future any number of times is called inheritance. Through inheritance variables and methods of class can inherited. Types of Inheritance:- 1)         Single or Single level inheritance 2)         Multilevel inheritance 3)         Hierarchical inheritance Java does not support to multiple inheritance. Single or single level inheritance:- In a single inheritance a class is inherited by a single class. In a single or single level inheritance a sub class having only one super class directly or indirectly. Syntax: -        class Subclass ex...