Chris Lamb

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'?


Chris Lamb is a freelance software developer and the current Debian Project Leader. You can read other posts by me, see software I have written or read more about me. You can also follow me @lolamby.


Planets: ALUG UWCS WUGLUG

Thursday 27th December 2007


Eight comments