The address of a string after concatenation [duplicate] - java

This question already has answers here:
Are two Java objects with same hashcodes not necessarily equal?
(10 answers)
Hashcode value is same
(9 answers)
How do I compare strings in Java?
(23 answers)
Why do I need to override the equals and hashCode methods in Java?
(31 answers)
Does hashcode number represent the memory address? [duplicate]
(9 answers)
Closed 3 years ago.
I'm trying to see how string object works in Java.
String value1 = "Good";
String value2 = "Good";
System.out.println(Integer.toHexString(value1.hashCode()));
System.out.println(Integer.toHexString(value2.hashCode()));
System.out.println(value1 == value2);
And it shows the same address
21f4dd
21f4dd
true
I know that these 2 variables point to the same object which is stored in heap. But I'm stuck when using concatenation.
String value3 = "Bad";
System.out.println(Integer.toHexString(value3.hashCode()));
value3 += " enough";
System.out.println(Integer.toHexString(value3.hashCode()));
String value4 = "Bad enough";
System.out.println(Integer.toHexString(value4.hashCode()));
System.out.println(value3 == value4);
It shows
103e5
c35f20b
c35f20b
false
After concatenation, there is a new string object whose value is "Bad enough". I assign this object to 2 variables, value3 and value4 also print their address.
My question is that the address of value3 and value4 are the same, so it means they point to the same object but why Java return false when comparing these 2 variables?

When you use + for concatenation, java internally uses StringBuilder. So value3 += " enough"; will be converted to something around the line
value3 = new StringBuilder()
.append(value3)
.append(" enough")
.toString();
here object will be created in the heap memory. Whereas in the value4 case the object is being created in String Pool.
I suggest you to read:
JLS SE8 15.18.1:
An implementation may choose to perform conversion and concatenation
in one step to avoid creating and then discarding an intermediate
String object. To increase the performance of repeated string
concatenation, a Java compiler may use the StringBuffer class or a
similar technique to reduce the number of intermediate String objects
that are created by evaluation of an expression.
Hashcode and equals contract: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode()
Hashcode of both the Strings is the same because String overrides default hashCode implementation. It is calculated on the content of String.
public int hashCode() {
int h = hash;
if (h == 0 && value.length > 0) {
char val[] = value;
for (int i = 0; i < value.length; i++) {
h = 31 * h + val[i];
}
hash = h;
}
return h;
}

The hashcode method of Strings actually doesn't point to the object address in the heap.
To obtain the actual address you can use the Unsafe API. Unfortunately, as far as I know, it isn't officially documented. You can try this to obtain the actual memory address on heap of an object.

Related

return result of Deque pollFirst() in java [duplicate]

This question already has answers here:
How can I properly compare two Integers in Java?
(10 answers)
Closed 3 years ago.
I was coding an algorithm issue, below code can't pass case
public void pop() {
if (s1.pollFirst() == minStack.peekFirst())
minStack.pollFirst();
}
however below can,
public void pop() {
int tmp = s1.pollFirst() ;
if (tmp == minStack.peekFirst())
minStack.pollFirst();
}
the only difference is how I use s1,pollFirst() return result. I can't figure out real difference here.
Thanks
Comparing two Integer Objects with values less than -128 or bigger than 127 using == will always result in false. However, if you compare Integer with primitive int, it will give you true if the actual value is the same.
int n1=128;
Integer n2=127;
Integer n3=127;
Integer n4=128;
Integer n5=128;
System.out.println(n1==n2); //false
System.out.println(n2==n3); //true
System.out.println(n4==n5); //false
System.out.println(n1==n5); //true
In the second example you are assigning the value to the primitive int, therefore it is automatically unboxed.

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.

Why (Integer) 222 != (Integer) 222 in Java? [duplicate]

This question already has answers here:
Why is 128==128 false but 127==127 is true when comparing Integer wrappers in Java?
(8 answers)
What is the difference between an int and an Integer in Java and C#?
(26 answers)
Closed 7 years ago.
It holds true for (Integer) 1 == (Integer) 1, which seems legitimate.
So why it's having excursion for (Integer) 222's equality?
Integer is a class. So to compare objects you need to use equals instead of ==
What actually happens with shorter Integer is that if you get an Integer using the method valueOf you get always the same cached instance for values between -128 and 127. So in this case == works.
It doesn't work if you instead of using valueOf create a new instance explicitly with the operator new.
For To be more clear I write the current implementation of valueOf
public static Integer valueOf(int i) {
final int offset = 128;
if (i >= -128 && i <= 127) { // must cache
return IntegerCache.cache[i + offset];
}
return new Integer(i);
}

Java String comparision depending on object initialisation [duplicate]

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.

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