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

How to understand the main function in java

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to understand the main function in java. The editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.

Preface

Some time ago, I saw an interview question: "can the main function be overloaded?" At that time, there was a hoop. How could there be this kind of interview questions? are all interviewers so sick now? Although the pervert is still very interested, so I reviewed the basic problem [bad smile here]

Main introduction

When we started to learn the java program, the first part of the code must be the main function. The format of the main function is as follows:

Public static void main (String [] args) {}

First, for the format definition of the main method:

"public": the main method is loaded by JVM at startup, and public has the highest access, so it needs to be declared as public.

"static": the method is called either through the object or through the class, while the main method does not need to generate an object because it is called by a virtual machine, so it can be declared as static

"main": as for why the method name is called main, I think it should refer to the method name in C language. After all, the ancestor of java originally wrote C.

"void": when the main method exits, there is no need to return a relevant return value, so it is void

"String []": this string array is used to accept parameters entered by the user at run time

Have you noticed at this time that the main method is the same as other static methods.

Can main be reloaded?

Go directly to the code

It runs successfully, so except for the main method specified by JVM as the entry point of the application, other main methods are relatively common.

Can main be inherited?

Continue with the code to define a HelloMeta inheritance parent class HelloWorld

Obviously, the subclass still runs the parent class's main method. So let's rewrite the method of the subclass.

You can still run the main method in the subclass

Can main be called?

Let's go to the code.

You can see that the main method is called by the callVerification method, but there is no exit method in the middle, resulting in an endless loop and the program crashes.

The above is how to understand the main function in java. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.

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