-2

Actually am testing the functionality of Scanner.

I have a very simple program which has a String variable which contains "abc".Then am reading other String(value "abc") from Scanner using next() method(even i tried with nextLine()).

Then am comparing using the ==, to check whether they are equal according to ==(i know i can compare with equals method which is working as excepted), the strange thing is that it is returning false when i compare using == , even though their hashcode() s are equal and equals() method returning true..

import java.util.Scanner;

public class Tester1234 {

    public static void main(String[] args) {

        Scanner scanner=new Scanner(System.in);
        String str1="abc";
        System.out.println("Eneter abc");
            String str2=scanner.next();

        System.out.println("str1.hascode()"+str1.hashCode()+"\tstr2.hascode()"+str2.hashCode()+"\tstr2.equals(str1)"+str1.equals(str2));
        if(str1==str2)
        {
            System.out.println("equal");
        }
        else
        {
            System.out.println("not equal");
        }

    }
}

I want to know why it is behaving like this ??

Thanks...!

Naren
  • 1,447
  • 1
  • 10
  • 11
  • 1
    @mc10 i know that... I want to know why that is not working with ==... i know Strings with same content will point to same object in StringPool...so if i do == why it is returning flase ? – Naren Feb 25 '14 at 05:28
  • Opps sorry, thing is that when you do new String it will copy the orginal value to internal `final char value[]` and hashcode is generated on the basis of `value[]` that is why you get same equals and hashcode but `==` fails as it wrap under new Object which resides in heap. – Subhrajyoti Majumder Feb 25 '14 at 05:45

3 Answers3

1

If we create an String like this...

String str1 = "abc";

object str1 created in String-Pool, and if we create

String str2 = new String("abc");

then, it created in heap, means another new Object.

this is the reason that your condition if(str1==str2) returns false, because these both are different objects.

But there hashcode is equal because both are "abc", so equals method returns true.

earthmover
  • 4,395
  • 10
  • 43
  • 74
1

you are right that hashcode and equals returning true, hence it will return true for str2.equals(str1). But str2 is new String and hence it is having difference memory address thats why it will not work for str2==str1. here you are comparing memory address and not content of the string.

Bhushan Kawadkar
  • 28,279
  • 5
  • 35
  • 57
1

You are wrong as for the statement that

i know Strings with same content will point to same object in StringPool...

It is true as for literals whitch are hardcoded strings in the code. Compiler creates string pool on compile time and use existing references. But if you are building string on runtime using StringBuilder/StringBuffer (what i belive the Scanner does internally) you are NOT USING entities in string pool, thats why you will get two different objects (references) for the same string content. Workaround for such behaviour is to use intern() however it is performance hit, as interned strings are goint into permgen, and will not be garbage collected.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99