Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the Java main interview questions?

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 Java main interview questions?" in the operation of the actual case, 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 using the main method?

No, we can't run the Java class without the main method.

Before Java7, you can run the Java class by using static initialization. However, it won't work since Java7.

2. The parameter required by the main () method is not a string array.

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.

Packagecom.instanceofjava

PublicclassMainMethod

{

Publicstaticvoidmain (Stringargs [])

{

}

}

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.

Packagecom.instanceofjava

PublicclassA

{

Publicstaticintmain (String [] args)

{

Return1;//runtimeerror:Nomainmethodfound

}

}

4. Why must the main () method 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"?

Packagecom.instanceofjava

PublicclassA

{

PublicMainMethod (inti)

{

/ / Constructortakingoneargument

}

Publicvoidmain (String [] args)

{

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

Packagecom.instanceofjava

PublicclassA

{

Publicvoidmain (String [] args)

{

System.out.println ("indhu"); / / Runtimeerror

}

}

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 "publicstaticvoidmain (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.

Packagecom.instanceofjava

PublicclassA

{

Publicstaticvoidmain (String [] args)

{

System.out.println ("Indhu")

}

Voidmain (intargs)

{

System.out.println ("Sindhu")

}

Longmain (inti,longd)

{

System.out.println ("Saidesh")

Returnd

}

}

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.

Packagecom.instanceofjava

PublicclassA

{

Privatestaticvoidmain (String [] args)

{

/ / Runtimeerror

}

}

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, the main method can be synchronized in Java, and the synchronized modifier allows it to be used in the declaration of the main method, so that the main method can be synchronized in Java.

This is the end of the content of "what are the Java main interview questions"? 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report