What happens if I create my own String class in java?

When I create my own String class. If there is another class in one package, the main one ( String [] args) seems to select my String class instead of the one present in rt.jar. My question is: why is the String class from rt.jar not selected by the Bootstrap Class loader in this case. My understanding is the request to load the class, which comes to the application loader, will be delegated to the bootstrap bootloader.

+4
source share
2 answers

Because Stringin your local package takes precedence; you can explicitly use java.lang.Stringor call yours class Stringsomething else 1 .

public static void main(java.lang.String[] args)

To expand above, at compile time, the compiler resolves class names in their full form ( imports does not exist at the bytecode level ). In addition, Java includes a static pool Stringthat is initialized before loading your class (in order for this to function, it java.lang.Stringmust be loaded before any custom classes).

1 What's really better is shadowing classes from java.langrequesting a service nightmare.

+8
source

String rt.jar bootstrap . - , , bootstrap.

. , String , , (, java.lang.String your.pkg.String) . Java , .

+4