-3
public class EqualsCheck {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        Scanner sc1= new Scanner(System.in);
        String s1 = sc.next();
        String s2 = sc1.next();
        equalCheck(s1,s2);
    }

    private static void equalCheck(String s1, String s2) {
        //Using Assignment
        System.out.println(s1 == s2);

        //Using equals

        System.out.println(s1.equals(s2));

        //Printing HashCode

        System.out.println("s1 :"+s1.hashCode()+" s2: "+s2.hashCode());
    }
}

Passed string are :

 s1 = "abc";
 s2 = "abc";

If I do a s1==s2, it returns false. Why string pooling doesn't work here.

KittMedia
  • 7,368
  • 13
  • 34
  • 38
ravikant
  • 67
  • 1
  • 1
  • 11

1 Answers1

1

String pooling doesn't work here because you're not putting strings in the string pool.

This doesn't happen automatically for strings constructed at runtime. You would need to call String.intern() to do so.

Andy Turner
  • 137,514
  • 11
  • 162
  • 243