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
Post a Comment