Showing posts with label string. Show all posts
Showing posts with label string. Show all posts

Thursday, February 14, 2013

Java - String comparison

Does it give you a bellyache?
String gg1 = "goldengate";
String gg2 = "goldengate"; //s2 is stored in a string pool.
String gg3 = new String("goldengate");
System.out.println(gg1==gg2); //is true, because they share
//the same memory addr
System.out.println(gg1==gg3); //false, because of 'new'
view raw StringPool.java hosted with ❤ by GitHub
Never mind, you can compare them like this-

System.out.println(gg1.compareTo(gg3));