java在main函数中建立函数,谁在java中调用main函数?

Java程序的入口点是main方法,它由JVM调用。当main方法使用return时,JVM会退出,除非有非守护线程运行或注册了关闭钩子。Java命令(java或javaw)启动JVM并执行指定类的main方法。程序的执行不涉及直接调用main方法的其他函数。
摘要由CSDN通过智能技术生成

public static void main(String[] args)

{

boolean t=true;

System.out.println("Before return");

if(t) return;

System.out.println("not execute");

}

In the above code when the return is used then it should return to the function which calls the main function. Who exactly calls the main function?

解决方案

Java classes are executed within a larger context (a particular JVM as others have noted). Below are some possibilities:

you are working within Eclipse and use debug/run to execute a particular class's main() method.

In all cases the main() method is the canonical entry point to executing code given a particular class. From the docs on the java JVM:

DESCRIPTION

The java tool launches a Java application. It does this by starting a Java runtime environment, loading a specified class, and invoking that class's main method. The method declaration must look like the following:

public static void main(String args[])

The method must be declared public and static, it must not return any value, and it must accept a String array as a parameter. By default, the first non-option argument is the name of the class to be invoked. A fully-qualified class name should be used. If the -jar option is specified, the first non-option argument is the name of a JAR archive containing class and resource files for the application, with the startup class indicated by the Main-Class manifest header.

The Java runtime searches for the startup class, and other classes used, in three sets of locations: the bootstrap class path, the installed extensions, and the user class path.

Non-option arguments after the class name or JAR file name are passed to the main function.

The javaw command is identical to java, except that with javaw there is no associated console window. Use javaw when you don't want a command prompt window to appear. The javaw launcher will, however, display a dialog box with error information if a launch fails for some reason.

You state:

In the above code when the return is used then it should return to the function which calls the main function.

There may not be any other Java function (in fact there usually isn't) which calls the main() function. It's the convention for declaring a well-known entry point. If the JVM is launched to run your class's main() method, then when main() returns, the JVM exits, except in a few special cases, e.g. there are other non-daemon threads running or there is a shutdown hook.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值