EDITORIAL : I was sailing to the sea , the nickname comes from the name of my girlfriend's name as well. I love technology, open source love, love programming.
技术是开源的、知识是共享的
.
This blog is a little summary and record their own learning, if you have the Java , algorithms interested, you can focus on my dynamic, we learn together.
用知识改变命运,让我们的家人过上更好的生活
.
Article Directory
main
The first method is a method to know when we learn the Java programming language, have you ever wondered why main
the method is public、static、void
of. Of course, many people first learn that C and C ++, but some subtle differences with the former, it will not return any value in Java main method, why main
approach public、static、void
, this article attempts to find some answers.
mian
The method is the entry Java program, remember that we are not here to discuss Servlet, MIDlet and any other java application container management, the core java programming, JVM looks for class public static void main (String [] args), If you can not find the method throws an error NoSuchMethodError: main program terminates.
main
Methods must strictly follow its grammatical rules, the method signature must be public static void
the parameter is a string array type, if it is Java1.5 and later can use variable parameters:
public static void main(String... args)
1. Why main method is static (static)
-
Because the main method is static, JVM calls this method does not need to create any instance that contains the main method.
-
Because the C and C ++ the same as the main method similar entry program execution.
-
If the main method is not declared as static, JVM must create an instance of the main class, as the constructor can be overloaded, JVM would not be able to determine which main method calls.
-
Static methods and static data loaded into memory can be called directly without the need to create the same methods as instance after instance to call, if the main method is static, then it will be loaded into the JVM context methods become enforceable.
2, why the main method is public (public)
Java specifies the number of modifiers can be accessed, such as: private, protected, public, any method or variable can be declared as public, Java can be accessed from places outside the class. Because the main method is public, JVM can easily access execute it.
3, why the main method does not return a value (Void)
Because the main return any value to the program did not make any sense, it is designed to be void, meaning there will be no main value is returned.
4, summary
-
main
The method must be declaredpublic、static、void
, otherwise JVM can not run the program -
If you can not find the JVM
main
method throws NoSuchMethodError: main exceptions, such as: If you run the command: java HelloWrold, JVM will search HelloWorld.class filepublic static void main (String[] args)
method. -
main
The entrance is a program, at the beginning of program execution. -
main
The method is a specific thread "main" run, the program will run until the end of the main thread or non-daemon thread terminates. -
When you see "Exception in Thread main" such as: Excpetion in Thread main: Java.lang.NullPointedException, it means an exception from the main thread.
-
You can declare
main
a method using a variable parameter java1.5 of ways, such as:
public static void main(String... args)
-
In addition
static、void、和public
, you can usefinal
,synchronized
andstrictfp
modifiers in themain
signature method, such as:
public strictfp final synchronized static void main(String[] args)
-
main
Like other methods it can be overloaded methods in Java, but the JVM will call the above signature of this standard main method. -
You can use the
throws
clause in the method signature, you can throw any checked and unchecked exceptions. -
JVM static initialization block call
main
is executed before the method, they are loaded into memory when the JVM in the class was executed.