简体   繁体   English

java源文件中的主类?

[英]the main class in a java source file?

im new to java. 我刚接触java。

the standard code that is created after i created a new project in netbeans is: 在netbeans中创建新项目后创建的标准代码是:

package helloworldapp;

public class Main {

    public static void main(String[] args) {
        int[] array = new int[10];
        array[9] = 1;

        System.out.println(array[9]);
    }
}

so you see that it uses the System class, but i dont see that class has been imported. 所以你看到它使用System类,但我没有看到该类已被导入。 Is there another code somewhere that does that? 是否有其他代码可以做到这一点? How can i use it if its not imported. 如果没有导入,我该如何使用它。

The System class is, along with everything else in java.lang , imported automatically. System类与java.lang所有其他内容一起自动导入。 Other classes you probably use that are automagically imported include String and Exception . 您可能使用的其他自动导入的类包括StringException

Additional info: 附加信息:

How can i use it if its not imported. 如果没有导入,我该如何使用它。

you can use any class in another package, without importing it, by using the fully qualified name. 您可以使用完全限定名称在另一个包中使用任何类,而无需导入它。 Example: 例:

    java.util.Date now = new java.util.Date();

instead of 代替

import java.util.Date;  // or java.util.*;
...
    Date now = new Date();

Update : 更新
import is only used at compile time to determine the fully qualified name of classes. import仅在编译时用于确定类的完全限定名称。

System位于java.lang包中,可在所有Java程序中自动使用。

Directories containing all standard Java classes are part of the default search algorithm used by both the compiler (javac) and java commands. 包含所有标准Java类的目录是编译器(javac)和java命令使用的默认搜索算法的一部分。 As already described in other answers, java.lang and java.util are examples of the System classes being automatically available for the code you're writing. 正如在其他答案中已经描述的那样,java.lang和java.util是可以自动用于您正在编写的代码的System类的示例。

The JAR file containing these classes can be found on the currently used JDK installation directory of your machine 包含这些类的JAR文件可以在当前使用的计算机JDK安装目录中找到

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM