-5

I tried with the following class :

public class EqualMethodTestWithNew {
    public static void main(String[] args) {
        String value = "xxx";
        String name = new String("xxx") ;

        System.out.println("hascode : value : "+value.hashCode());
        System.out.println("hascode : name  : "+name.hashCode());

        if (value == name) {
            System.out.println("equal == 1");
        } else {
            System.out.println("false == 1");
        }

    }
}

though the hasCode is same for the both variable it prints the false == 1. could some one explain the reason why?

thanks

Joe
  • 4,460
  • 19
  • 60
  • 106

5 Answers5

1

String should be compared with equals() method, not ==. You are trying the check the equality of the memory address of both instances (actually they are not) instead of the value in the String instances. So, use

if(value.equals(name)) {
    System.out.println("equal == 1");
}
Maroun
  • 94,125
  • 30
  • 188
  • 241
Keerthivasan
  • 12,760
  • 2
  • 32
  • 53
1

You should be using equals method instead of == opertaor.

if (value.equals(name)) {
   System.out.println("equal == 1");
} else {
   System.out.println("false == 1");
}

Note that:

== tests for reference equality.

.equals() tests for value equality.

Please see here for more information.

Community
  • 1
  • 1
Bhushan
  • 6,151
  • 13
  • 58
  • 91
1

The reason why your code is not working is that == tests whether the reference to the object is the same, and that is not your case. To compare the value of the string, you need to use the .equals(String str) method.

if (value.equals(name)) {
    ...
}
Balduz
  • 3,560
  • 19
  • 35
1

You need to understand what exactly is happening when you execute the 2 string statements.

    String value = "xxx";

The above line creates a new compile time constant string which does into the String intern pool.

    String name = new String("xxx") ;

But in this case, since you're using the new operator, it creates a new String object which goes in the object heap. It does not have the same address as the one which was created in the previous statement.

The hashCode() method is based on the contents of the String which are the same, but that doesn't mean that they both refer to the same String object in the memory.

s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1] // would return same value for all String objects having the same content

To compare the values, you need to use equals() method.

And if you want to compare the object references use the == operator. In your case, since both refer to difference objects, you get the output as false.

Alternatively, you can ask the compiler to check and fetch the reference of a String with the same value already existing in the String pool by using the intern() method.

String value = "xxx";
String name = new String("xxx");
name = name.intern(); // getting reference from string pool

Now you'll get the output as equal == 1 when your do if (value == name) {.

Rahul
  • 44,383
  • 11
  • 84
  • 103
0

Strings are compared using equal() method. == compares the two objects are equal are not.

Rahul
  • 3,479
  • 3
  • 16
  • 28