相信很多人在运行第一个可以运行的 Java 程序的时候都会要求写一个主函数。
然后很多人都会照葫芦画瓢的写一个下面的函数:
public static void main(String[] args) {
}
IJ 甚至非常贴心的给你 main 的代码提示:
这个函数里面有几个定义和关键字,估计可能会有些绕,和为什么这么写?
近期在学习的时候才翻出来再看看,其中可能比较难理解的是关键字 static
public
这个关键字在这个函数中很好理解,就是表示这个函数是可以被其他类访问到。
void
表示这个函数不返回任何数据。
这个也比较好理解,因为这个函数是主程序的入口,通常是不需要返回结果的。使用 void 也没关系。
String[] args
这个表示的是这个函数可以从执行的时候获得的输入参数。
就是告诉这个函数在执行的时候,你可以在命令行中定义一些参数,然后这个函数通过获得这些参数来对运行进行调整。
通常我们对输入参数的控制会使用。
Apache 的 commons-cli-1.4.jar 来进行处理,因为这个 API 帮我解决了对出入参数进行处理的常用方法,能够简化我们的操作。
这个内容我们在其他地方再单独拿出来说。
static
这个就是我们需要说明一下的重点了。
我们都知道 static 是表示静态的意思,为什么在 main 函数 里面需要静态关键字修饰?
static 主要用途是告诉编译器 main 函数是一个静态函数。同时也就是说main 函数中的代码是存储在静态存储区的,即当定义了类以后这段代码就已经存在了。
这个所谓的静态存储区,就是我们常说 JVM 中的堆(Heap),这个 Heap 就是 JVM 垃圾回收管理的区域,也是你经常看到内存溢出的区域。
static关键字主要用于内存管理。我们可以应用 ava static关键字在变量,方法,块和嵌套类中。 static关键字属于类,而不是类的实例。
被 static 关键字修饰的不需要创建对象去调用,直接根据类名就可以去访问。
在 main() 方法中使用静态(static)这个关键字,JVM 将会为这个方法开辟内存空间,你不需要对这个方法进行实例化,因此能够节省不必要的开销。
如果 main 方法不声明为静态的,JVM 就必须创建main类的实例,因为 构造器 可以被重载,JVM就没法确定调用哪个 main 方法。
因此,在这里这个 static 关键字是必须要有的,否则你的程序可以编译,但是无法运行。
如果使用上面的代码:
public void main(String[] args) {
System.out.println("RUN");
}
运行的时候将会有错误:
Error: Main method is not static in class com.ossez.codebank.algorithm.Main, please define the main method as:
public static void main(String[] args)
Process finished with exit code 1
但是编译是不会有错误的。
At 5 and 7 dpl, when there are few or no oligodendrocytes within the lesion, we identified Sox2 cells as being OPCs by colabeling with the OPC markers NG2, Nkx2 Enrique IIMwiMCVUki 6 17 2022
0 before crossover SABCS 2015 Denosumab Improves Outcomes for Patients Taking Aromatase Inhibitors
Donny mLVHECnKLvYWXvHmr 6 28 2022 In certain embodiments of a compounds of Formula 1, R can be a chemotherapeutic moiety selected from N CH CH R, CH N CH CH R, N O CH CH R, CH N O CH CH R, N O CH CH R CH CH R, CH N O CH CH R CH CH R, O N CH CH R, CH O N CH CH R, CO N CH CH R, CH CO N CH CH R, O CO N CH CH R, and CH O CO N CH CH R, wherein each R is independently selected from Cl, Br, I, OSO CH, OSO CF, and hydroxyl OH; R can be selected from hydrogen, halogen, N R, N O R, N R OR, NO, NO, CN, COOR, CON R, OH, C alkyl, C alkoxy, C alkylsulfanyl, C alkylsulfinyl, C alkylsulfonyl, C heteroalkyl, C heteroalkoxy, C fluoroalkyl, C fluoroalkoxy, C cycloalkyl, C cycloalkyloxy, and C cycloalkylalkyl; wherein each R can be independently selected from hydrogen, deuterio, C alkyl and C alkoxy, or two geminal R together with the nitrogen to which they are bonded form a 3 to 6 membered heterocyclic ring; each of R, R, and R can be hydrogen; R can be selected from COOH, COOR, S O OH, P O OH H, and 1H tetrazol yl, wherein R is selected from C alkyl; each R can be independently selected from hydrogen, methyl, hydroxyl, and fluoro; R can be selected from hydrogen, methyl, ethyl, propyl, isopropyl, cyclopropyl, butyl, isobutyl, cyclobutyl, tert butyl, C alkoxy, C fluoroalkyl, and C fluoroalkoxy; and L can be selected from a bond, CH, C OH H, CHCH, C CH, CF, O, SO, NR, CO, CH CH, CH CHCH, CHCH CH, CH CHOH, CHOH CH, CH CF, CF CH, CO NR, NR CO, CH NR, NR CH, CH O, O CH, CH S, S CH, CH SO, SO CH, CH CO, and CO CH, wherein R is selected from hydrogen, methyl, and ethyl
zanaflex duphaston price philippines mercury The ability to set the reader s grey cells whirring is a sine qua non of crime fiction, but these days you are permitted to do so through means other than authorial legerdemain
It s also used to balance and regulate hormone production