简体   繁体   English

如果我在java中创建自己的String类会发生什么?

[英]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.Stringyour.pkg.String )。 At runtime, Java class loaders always load classes using a full class name. 在运行时,Java类加载器始终使用完整的类名加载类。

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

相关问题 Java Android-创建我自己的类 - Java Android - Create My Own Class 通过在Java中使用String创建我自己的BigInteger - Create my own BigInteger by using String in Java 如果我初始化一个已经在 J​​AVA 中初始化的 Class 对象会发生什么? - What will happen If I initialize a Class object which is already initialized in JAVA? Java-如何将对象从自己的类转换为主类中的字符串? - Java - How can I convert an object from my own class to a string in the main class? 我应该使用什么数据结构来创建自己的“BigInteger”类? - What data-structure should I use to create my own “BigInteger” class? 创建我自己的注释Java - Create my own Annotation Java 我必须在自己的Java类中使用getter方法吗? - Must I use a getter method inside of my own class in Java? 如何在Android上的C中加载我自己的Java类? - How do I load my own Java class in C on Android? 如何在Java中为AES算法创建自己的密钥? - How can I create my own keys for the AES algorithm in Java? 当我使用尚未编写的超类方法时,在Java中会发生什么? - What will happen in Java when I use a method of the super class which has not been written?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM