This question already has answers here:
What is the Java string pool and how is "s" different from new String("s")? [duplicate]
(5 answers)
Closed 7 years ago.
Following code:
String a = new String("aaa");
String a2 = new String("aaa");
System.out.println(a == a2);
String b = "bbb";
String b2 = "bbb";
System.out.println(b == b2);
Produces following output:
false
true
Why there is difference in output for comparision a==a2 and b==b2 depending from type of String creation ?
When you declare a and a2 you explicitly create new (different) Strings. The use of the constructor causes a copy to be made. Hence == fails as a and a2 point to different values.
When you declare b and b2, b2 can re-use the same string from the pool. Hence they actually point to the same value and == returns true.
Take a look here or here for a detailed answer.
Related
This question already has answers here:
What is the difference between == and equals() in Java?
(26 answers)
Closed 2 years ago.
What is the difference between these two cases? Would the equality check be wrong? If not, which one is faster?
If you use wrapper class (Byte instead of byte), you should use equals method. The code below doesn't work (not likely to happen) as == compare reference not the value.
public static void main(String[] args) {
Byte b1 = new Byte((byte) 0);
Byte b2 = new Byte((byte) 0);
System.out.println(b1 == b2);
}
Some explanation here.
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")?
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.
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)
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.