Я играл с Classname.class и Classname.class.toString() и обнаружил что-то необычное..class == .class vs .class.toString() vs .class.toString()
. Класс, кажется, приравнивается к классу при работе в одном классе. Хотя .class.toString() не приравнивается к .class.toString() в том же классе. Почему это было бы так.
Пожалуйста, смотрите мой код ниже
public class HelloWorld{
public static void main(String []args){
if(String.class.toString() == String.class.toString())
System.out.println("toString(): Yes they are the same");
else
System.out.println("toString(): They are not the same ?");
System.out.println("=============================");
if(String.class == String.class)
System.out.println(".class: Yes they are the same");
else
System.out.println(".class: They are not the same");
}
}
Выход:
sh-4.3# javac HelloWorld.java
sh-4.3# java -Xmx128M -Xms16M HelloWorld
toString(): They are not the same ?
=============================
.class: Yes they are the same
возможно дубликат [Как сравнить строки в Java ?] (http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) –