December 27th 2007

Java Quiz 2

/wp-content/2007/java-time.jpeg

Yes, it's Java quiz time again.

"Everyone" knows you shouldn't compare strings by reference equality (==), but can you explain why...

public class Test1 {
    public static void main(String args[]) {
        System.out.println(("use" == "use") ? "True" : "False");
    }
}

...always prints 'True', and then...

public class Test2 {
    public static void main(String args[]) {
        System.out.println((new String("mention") == new String("mention")) ? "True" : "False");
   }
}

...always prints 'False'?




You can subscribe to new posts via email or RSS.