+ 2

Equality using == for Java string object. Why str1==str2 is true, while str1==str4 is false in my code.

As addition str1.hashCode() is exactly same with str4.hashCode((). It means, str1 and str4 is the same object in the constant string pool. https://code.sololearn.com/cSC237t03ctn/?ref=app. Thank you friends.

5th Aug 2023, 2:06 AM
Oliver Pasaribu
6 Respuestas
+ 6
In Java, having the same hash code for two strings doesn't necessarily mean they are pointing to the same memory location. Hash codes are calculated based on the content of the strings. Hopefully it is evident to you that by comparing strings with == it compares only the object references. To compare their contents use the equals() method. Try System.out.println(str4.equals(str1)); That will print true.
5th Aug 2023, 2:40 AM
Brian
Brian - avatar
+ 4
Okay, I understand better the direction of what you are asking. After a little research, I learned some rules about string interning. When str1 and str2 are created, the JVM recognizes they are the same, and it efficiently assigns the same string pool object. When str4 is made, it dynamically allocates a new string object and does not check whether it should be interned to the string pool. I think the expectation is that a new dynamically-allocated string is likely to be temporary, so it should not be retained in the string pool. However, you can force str4 to be interned like this: String str4 = str3.toLowerCase().intern(); Now the comparision (str1==str4) will be true.
5th Aug 2023, 4:57 AM
Brian
Brian - avatar
+ 3
Brian, I do understand about reference and content COMPARATION using == and equals() method. However, talking about constant string pool, are certain region in memory, head for dynamic or object and stack for primitive variable. If the string str1= "party" already exist, then, str4 = "party" is refer to existing string strq itself. And also str4= "Party".toLowerCase() result "party" itself which already exist innthe string pool. And also both "PARTY".toLowerCase() results "party" itself which exist in pool and referred by str1 variable. But why "PARTY".toLowerCase() == "party" return false. Despite hashcode is just an unique number, we know that if two object have same hashcode, it can be said that we are no referring to the sdme object. Please explain more to make it clear to us.
5th Aug 2023, 2:59 AM
Oliver Pasaribu
+ 1
The reason str1 == str4 is false in your code is due to how Java handles String objects and comparisons using the == operator. In Java, the == operator compares object references, not the content of the strings. When you create a string using string literals (like "party"), Java's string interning mechanism comes into play, which reuses the same reference for the same string literal to save memory. In your code:str1 and str2 both refer to the same string literal "party", so str1 == str2 is true.str3 and str4 also refer to the same string literal "PARTY", but when you call str3.toLowerCase(), a new string with the content "party" is created. This new string is not the same reference as the original string "party", so str1 == str4 is false.If you want to compare the content of the strings, you should use the .equals() method: System.out.println(str1.equals(str4)); This will compare the content of str1 and str4, and it will print true since their content is the same.
6th Aug 2023, 7:21 PM
ÃrShåD KHãÑ
ÃrShåD KHãÑ - avatar
0
So, the string method toUpper() and tolower() return newly created string? Thank you, I Will check it later to verify or to make it clear.
6th Aug 2023, 11:37 PM
Oliver Pasaribu
0
Yes. You do any operation on string it will create new Object on Heap memory. It's not particular for toLower Or toUpper().
14th Aug 2023, 4:45 AM
ÃrShåD KHãÑ
ÃrShåD KHãÑ - avatar