-5

In the below code, by hashCode() it seems 2 objects got created. Then although s1 == s3 is giving true, but why s1 == s4 is giving false ?

public class Main {

public static void main(String[] args) {
    String s1 = new String("jordi") ;
    String s2 = s1.toUpperCase() ;
    String s3 = s1.toLowerCase() ;
    String s4 = new String("jordi") ;
    System.out.println(s1.hashCode());
    System.out.println(s2.hashCode());
    System.out.println(s3.hashCode());
    System.out.println(s4.hashCode());
    System.out.println(s1==s2);
    System.out.println(s1==s3);
    System.out.println(s1==s4);
}

}

This gives output as :

101312786

70775026

101312786

101312786

false

true

false

Vikas
  • 6,868
  • 4
  • 27
  • 41
realdeb
  • 1,118
  • 12
  • 12
  • Because `new String` will always create a different `String` object. `s1` and `s4` are guaranteed to be unique objects. – Elliott Frisch May 02 '19 at 04:25
  • 1
    Please don't rely on `hashCode` to tell you about object identity. That's not what it's for, and not what it does. The values of `s1` and `s4` are references to different strings objects with equal content. There are four objects in the code you've shown: three with the content "jordi" and one with the content "JORDI". The new operator *always* creates a new object. (There are three with the content "jordi" because there's the one that the string literal itself refers to - the reference passed to the `String` constructor twice.) – Jon Skeet May 02 '19 at 04:26
  • Thanks for your response Jon Skeet sir. You addressed my point perfectly. hashCode() does not tell about whether new object got created or not. Whenever we use new operator always new object will be created, understood. But then how 4 objects can be here as s1 == s3 is giving true ? Aren't both reference variables pointing to the same object ? – realdeb May 02 '19 at 05:56
  • As I said, there are three objects with the content "jordi" - one of them isn't assigned to any variables, but you could do so with `String s0 = "jordi";`. That addition wouldn't create any more objects, but then it's easy to say that the four objects can be referred to as: { s0; s1 and s3; s2; s4 } – Jon Skeet May 02 '19 at 06:44

4 Answers4

0
s1 == s4

This performs reference comparison. So you're comparing the is both s1 and s4 point to the same object or not (which they don't - since you explicitly created two different objects)

rdas
  • 20,604
  • 6
  • 33
  • 46
0

When we use == operator for s1 and s4 comparison then it is false since both have different addresses in memory. It compares for memory address

Chiran K.
  • 406
  • 3
  • 16
0

Value objects (objects whose identity is unimportant and whose value is the only important thing about them, such as String and Integer), override equals and hashCode so that, for example, they can be used as keys in a HashMap. The hashCode value for String is specified in detail specifically so that any two strings with the same content will hash to the same value.

Your s1 and s3 are the same object because String#toLowerCase() has an optimization that returns this and avoids creating a new object if the entire string is already lowercase.

chrylis -cautiouslyoptimistic-
  • 75,269
  • 21
  • 115
  • 152
-3

Use the .equals method instead of == for strings. Java gets a little screwy when you try to compare two strings with == instead of .equals()

Here's your code but fixed.

public static void main(String[] args) {

        String s1 = new String("jordi");
        String s2 = s1.toUpperCase();
        String s3 = s1.toLowerCase();
        String s4 = new String("jordi");
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());
        System.out.println(s3.hashCode());
        System.out.println(s4.hashCode());
        System.out.println(s1.equals(s2));
        System.out.println(s1.equals(s3));
        System.out.println(s1.equals(s4));
    }
  • 3
    "Java gets a little screwy" - no it doesn't, it behave entirely consistently. When the OP is trying to detect whether two values are references to the same object or not, and how many objects have been created, `==` is exactly what's required. – Jon Skeet May 02 '19 at 04:32