Using `==` to compare Strings [duplicate] - java

This question already has answers here:
Getting strange output when printing result of a string comparison
(3 answers)
Behavior of String literals is confusing
(11 answers)
Closed 4 years ago.
String s5="Ram";
String s6="Ram";
System.out.println(" s5==s6 is " + s5==s6); // false
System.out.println(s5==s6); // true
Why the first line is false and the second line is true always?

Because + has a higher precedence than ==,
" s5==s6 is "+s5==s6
evaluates as
" s5==s6 is Ram"=="Ram"
which is obviously false. You could have avoided this particular problem by using parentheses:
" s5==s6 is "+(s5==s6)
Meanwhile,
s5==s6
evaluates as
"Ram"=="Ram"
which is true only because of automatic interning of string literals in Java. If one of those strings was evaluated rather than a literal, it would be false, as == compares object identity, and two different String objects would be evaluated as different even if they had the same content. Compare
String s5 = new String("Ram");
String s6 = new String("Ram");
System.out.println(s5==s6); //false
Use .equals for meaningful comparison of string contents:
System.out.println(s5.equals(s6)); //true

Related

Integer.toString() vs. Integer.parseInt() [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 2 years ago.
How come the first condition is false and the second is true? I was sure they were both true.
System.out.println(Integer.toString(3) == "3");
System.out.println(Integer.parseInt("3") == 3);
Integer.parseInt converts a String to a primitive int and primitives can be compared with ==. However, Integer.toString produces a String object and == for objects checks that they are the exact same reference; use String#equals instead to compare the values of the Strings.
System.out.println(Integer.toString(3).equals("3"));
System.out.println(Integer.parseInt("3") == 3);
The above code outputs:
true
true

Java returning false on comparing two equal strings [duplicate]

This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 3 years ago.
When I execute:
String a = "hello";
String b = "hello";
System.out.println(a==b);
I get output as "true".
But when I run:
String a = new String("hello");
String b = "hello";
System.out.println(a==b);
I get output as "false".
I understand that in the first case, Java makes 'b' point to the same object where 'a' pointed, but why can't it do that in the second case?
String in Java are immutable. Meaning:
String a = "hello";
String b = "hello";
a and b are litterally pointing to the same object in memory
Here you explicitly create a new object
String a = new String("hello");
String b = "hello";
Hence the 2 are not equal.
== compares the memory address. To check if an Object is equal to another in Java you should use the equals method that is on all objects.
If you rewrite System.out.println(a==b); to System.out.println(a.equals(b)); it will be true for both cases
Please read the following well explained answer on stackoverflow:
What is the difference between "text" and new String("text")?

String new Object and comparision [duplicate]

This question already has answers here:
Are two Java objects with same hashcodes not necessarily equal?
(10 answers)
How do I compare strings in Java?
(23 answers)
Closed 4 years ago.
As of i know any changes on String a new Object will create,and for some run time activity if there is a content change then a new object will create in Heap are, but i am confusing on below cases, please give idea...
String s8="abcd";
String s9=s8.toUpperCase();
String s11=s8.toUpperCase();
System.out.println("S9 "+s9.hashCode() +" s10 "+s11.hashCode());//S9 -- 2001986 s10 -- 2001986
System.out.println(s9==s11);//false
In the above scenario the address is printing same but the == operator shaowing false.
please tell why address is same and comparision is false.
String s8="abcd"; : Memory will be allocated from constant pool.
String s9=s8.toUpperCase(); New object will be created on heap
String s11=s8.toUpperCase(); Another New object will be created on heap
If you look at the implementation of toUpperCase
public String toUpperCase(Locale locale) {
..
return new String(result, 0, len + resultOffset);
Hence it creates a new object on heap each time. therefore s9 != s11
Note: If two objects are equal then their hashcodes are equal but vice
versa is not true
UPDATE:
String s11=s9.toUpperCase();
s11==s9 // returns true
Because there are not chars which can be modified and therefore s11 and s9 both points to the same object. I strongly recommend to you to read the implementation
== operator is used for reference comperison. Basically when you create s9 and s11 then only 1 object is created in heap. That's why those 2 hashcode is same and 2 different references are pointing same object. That's why s9==s11 has retured false.

Java - assinging a replaced string , or printing it [duplicate]

This question already has answers here:
Using variables to store values or directly get values from the objects?
(3 answers)
Closed 7 years ago.
Is there a difference between this:
String str1 = "abcabc";
String str2 = str1.replaceAll("a", "");
System.out.print(str2);
And
String str1 = "abcabc";
System.out.print(str1.replaceAll("a", ""));
In terms of memory used, or in other words, will the print method create memory in the heap for the new string?
str1.replaceAll("a", "")
returns a new String instance. So "Yes", it does create it on the heap, & "Yes" they are the same. (memory speaking)

Int and Char comparison by "==" [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 8 years ago.
Why is this ok? What exactly does it compare?
int i = 10;
char c = 10;
if( c == i)
System.out.println("We are Equal");
And the same in this situation:
String s1 = "Null";
String s2 = new String(s1);
if( s1 == s2)
System.out.println("We are Equal");
I get that we're not comparing the contents of the variable.
In the first example, the two literal values of the integers are being compared, I.e. 10 == 10.
In the second example, your are comparing String objects, meaning the value of the actual Objects, not its content, are getting compared. In order to compare the content of these string objects, I.e. "Blah" == "Blah", you should use the string method String.compare(String strToCompare
Strings are objects. You are comparing references for the strings. Char/ints are primitives so no objects.

Categories