[英]What will happen if i create my own String class in java?
When i create my own String class .If there is another class in same package , main( String [] args) seems to pick my String class instead of the one present in rt.jar. 当我创建自己的String类时。如果在同一个包中有另一个类,main( String [] args)似乎选择我的String类而不是rt.jar中的类。 My question is , why String class from rt.jar is not picked by Bootstrap Class loader in this case. 我的问题是,为什么在这种情况下,Bootstrap类加载器不会选择rt.jar中的String类。 My understanding is a class loading request that comes to Application loader will be delegated to bootstrap class loader. 我的理解是一个类加载请求来到Application loader将被委托给bootstrap类加载器。
Because the String
in your local package takes precedence; 因为本地包中的String
优先; you can explicitly use java.lang.String
or name your class String
something else 1 . 你可以显式地使用java.lang.String
或命名你的class String
其他1 。
public static void main(java.lang.String[] args)
To expand on the above, at compile time the compiler resolves class names to their fully qualified form (at the byte-code level, there aren't any import
s). 为了扩展上述内容,在编译时编译器将类名解析为完全限定的形式(在字节码级别,没有任何import
)。 Further, Java includes a String
intern pool that is initialized before your class is loaded (in order for that to function, java.lang.String
has to be loaded before any user classes). 此外,Java包含一个String
intern池 ,它在加载类之前被初始化(为了使其起作用,必须在任何用户类之前加载java.lang.String
)。
1 Which is really a better idea, shadowing classes from java.lang
is asking for a maintenance nightmare. 1 这真的是一个更好的主意,来自java.lang
阴影类要求维护噩梦。
Why is the String class from rt.jar not picked by the bootstrap class loader in this case. 为什么在这种情况下,引导类加载器不会选择rt.jar中的String类。 My understanding is a class loading request that comes to Application loader will be delegated to bootstrap class loader. 我的理解是一个类加载请求来到Application loader将被委托给bootstrap类加载器。
The name resolution actually happens at compile time. 名称解析实际上发生在编译时。 The compiler decides what String
means in the context of the source code file that is using it and then uses the full name for the resolved class (eg java.lang.String
or your.pkg.String
) in the class file. 编译器决定在使用它的源代码文件的上下文中String
意味着什么,然后在类文件中使用已解析类的全名(例如java.lang.String
或your.pkg.String
)。 At runtime, Java class loaders always load classes using a full class name. 在运行时,Java类加载器始终使用完整的类名加载类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.