In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what are the interview questions of the Java main method?" in the operation of actual cases, many people will encounter such a dilemma, and then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. How do you define a class without the main method?
No, we can't run the Java class without the main method.
Before Java 7, you could run the Java class by using static initialization. But it didn't work since Java 7.
The argument required by the 2.main () method is not an array of strings?
No, the argument to the main () method must be an array of strings.
However, when introducing arguments, you can pass string-type arguments as parameters to the main () method. The parameter must be an array.
Package com.instanceofjava; public class MainMethod {public static void main (String args []) {}}
3. Can we change the return type of the main () method?
No, the return type of the main () method can only be empty. Any other type is unacceptable.
Package com.instanceofjava; public class A {public static int main (String [] args) {return 1; / / run time error: No main method found}}
Why does the 4.main () method have to be static?
The main () method must be static.
If main () is allowed to be non-static, then JVM has to instantiate its class when calling the main method.
When instantiated, you also have to call the constructor of the class. If the constructor of this class has arguments, then ambiguity will occur.
For example, in the following program, what parameter does JVM pass when instantiating class "A"?
Package com.instanceofjava; public class A {public MainMethod (int I) {/ / Constructor taking one argument} public void main (String [] args) {/ / main method as non-static}
5. Can we declare the main () method non-static?
No, the main () method must be declared static so that JVM can call the main () method without instantiating its class.
If you remove the declaration of "static" from the main () method, although the compilation can still be successful, it will cause the program to fail at run time.
Package com.instanceofjava; public class A {public void main (String [] args) {System.out.println ("indhu"); / / Run time error}}
6. Can we overload the main () method?
Yes, we can overload the main () method. A Java class can have any number of main () methods.
In order to run the java class, the class's main () method should have a declaration such as "public static void main (String [] args)". Compilation can also be successful if you make any changes to this declaration. However, the Java program cannot be run. You will get a runtime error because the main method cannot be found.
Package com.instanceofjava; public class A {public static void main (String [] args) {System.out.println ("Indhu");} void main (int args) {System.out.println ("Sindhu");} long main (int I, long d) {System.out.println ("Saidesh"); return d;}}
7. Can we declare the main () method to be private or protected, or not to access the modifier?
No, the main () method must be public. You can't define the main () method as private and protected, and you can't do without access modifiers.
This is to allow JVM to access the main () method. If you do not define the main () method as public, although the compilation will be successful, you will get a run-time error because the main method cannot be found.
Package com.instanceofjava; public class A {private static void main (String [] args) {/ / Run time error}}
8. Can we override the main method in Java?
No, you can't override the main method in Java. This is because main methods are static methods, and static methods are combined at compile time in Java, so you cannot override static methods in Java.
9. Can we terminate the main method in Java?
You can terminate the main method in Java. JVM has no problem with that.
10. Can we synchronize main methods in Java?
Yes, main methods can be synchronized in Java, and the synchronized modifier is allowed to be used in the declaration of main methods, so that main methods can be synchronized in Java
This is the end of the content of "what are the interview questions of the Java main method". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.