How String is immutable? [duplicate] - java

This question already has answers here:
Immutability of Strings in Java
(26 answers)
Closed 6 years ago.
If a String object is immutable then why is it printing "Help"?
The String object s1 shouldn't be modified according to its immutability feature. I am confused for years, please help me understand this:
Code
public static void main(String[] args) {
String s1 = "Hello";
s1 = "Help";
System.out.println(s1);
}
Output
Help

Your second assignment actually is changing the String that s1 references.
There is still a String of "Hello" in existence (in the pool) which cannot be changed.
The behavior you described would be achieved by making s1 final - in which case you would get a compiler error if you tried to change the value the String s1 references.

Related

why is jvm returning false when comparing two objects of same type? [duplicate]

This question already has answers here:
How do I compare strings in Java?
(23 answers)
how to check reference equality in an object which implements content equality?
(5 answers)
Closed 4 years ago.
public class Test
{
public static void main(String[] args)
{
String s = new String("test");// ***
String s1 = s.toUpperCase();
String s2 = s.toLowerCase();
String s3 = s1.toLowerCase();
System.out.println(s==s1);//first case
System.out.println(s==s2);//second case
System.out.println(s==s3);//third case
}
}
1) Why does it return false for the third case(commented). Both s3 and s are pointing test yeah? But it seems JVM creates another object named test for s3 in the heap memory. But it is not the same for the second case(commented). It uses the object which was previously created as s(commented as *** in the code). Why is that?
2) And what happens to s1 object TEST because s3 is created from s1. Will s1 be destroyed or will it be in heap?
Here String s = new String("test"); object will be created in heap area (not inside String pooled area) but any other string returned after any operation will be created in String pooled area. To answer your question:
s==s3 returned false as they are two different objects.
s1 won't be collected by garbage collector until it is being referenced.

Why I am getting different output for string pool while comparing two objects in java? [duplicate]

This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 4 years ago.
I am running this program. To check string pool concept. I refer this link and added two more lines with equals method from Object class in java. Why I am getting different output for objRef1==objRef2 vs objRef1.equals(objeRef2). In my opinion they should be same.
public class Abc2 {
public static void main(String[] args) {
String s1 = "Cat";
String s2 = "Cat";
String s3 = new String("Cat");
System.out.println("s1 == s2 :"+(s1==s2));
System.out.println("s1.equals(s2) :"+(s1.equals(s2)));
System.out.println("s1 == s3 :"+(s3==s2));
System.out.println("s1.equals(s3) :"+(s3.equals(s2)));
}
}
String is a complex datatype so == find two different refetences.
Strings equals() compares the content instead of the reference.
So two "Cat"s has the same content but arent the same object.
Its like you try to say C:\test.txt and C:\test2.txt where the same file because they have the same content.

How a string present in a string pool and a string present in a heap outside stringpool have same hashCode? [duplicate]

This question already has answers here:
Use String hashCode() Method? [closed]
(4 answers)
Closed 4 years ago.
Consider the piece of code given below.
I was wondering how can a string present in string pool "s1" or "s2" have the same hashCode as a string present in heap as "s3" but outside string pool.
class Test{
public static void main(String[] args) {
String st1 = "Shiva";
String st2 = "Shiva";
String st3 = new String("Shiva");
System.out.println(st1 == st2);
System.out.println(st1 == st3);
System.out.println(st2 == st3);
System.out.println(st1.hashCode());
System.out.println(st2.hashCode());
System.out.println(st3.hashCode());
}
}
input: deep (master *) LanguagePackageInJava $ javac Lecture3.java
output: deep (master *) LanguagePackageInJava $ java Test
true
false
false
79855167
79855167
79855167
I have searched a lot regarding this question. Please do tell me where am I wrong in my thought process.
hashcode and equals method have contract , so if two string are true for equals then their hashcode should be same.

Does Java Operator "==" really compare two objects based on memory reference? [duplicate]

This question already has answers here:
will two strings with same content be stored in the same memory location?
(9 answers)
How do I compare strings in Java?
(23 answers)
Closed 5 years ago.
As the java-doc tells Java Operator == tests for reference equality (whether they are the same object). so "==" operator will return true only if two object reference it is comparing represent exactly same object otherwise "==" will return false.
But while running a piece of code all i found is that this statement doesn't satisfy the Output of the code.
Here's the code:
public class Test2 {
public static void main(String[] args)
{
String s="Sachin";
String t="Sachin";
System.out.println(s==t);
}
}
And Surprisingly i found a output "true".
Please Help me understand why it is so?
Here's a Screenshot to my program output:
https://i.stack.imgur.com/OZ0PW.jpg
You assuption is that
String s="Sachin";
String t="Sachin";
creates two string objects, but this is not true.
Java optimises the usage of string so that is puts literal strings in the string pool so that it assinges an already created string object from that pool if the compiler finds the same string a second time. This is called string interning.
You better try this:
public class Test2 {
public static void main(String[] args)
{
String s="Sachin";
String t=new String(s);
System.out.println(s==t);
}
}

String constant pool query [duplicate]

This question already has answers here:
When does the pool change?
(3 answers)
Closed 8 years ago.
public class D2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="java";
String s2="ja".concat("va");
System.out.println(s1==s2);
}
}
On concatenating two String constants in s2 doesn't result in a new String object(String objects are only created when new is used or a perm reference like s1 on concatenation). Kindly shed light on why the o/p is false.
On concatenating 2 String constants in s2 doesn't result in a new String object
That's true when it's performed with the string concatenation operator - but that's not what you're doing here. You're manually calling String.concat, so this is not a constant expression, and not evaluated at compile time.
If you used:
String s2 = "ja" + "va";
then the concatenation would be performed by the compiler, and s1 and s2 would refer to the same object.
JLS section 15.28 gives details around what leads to a constant expression, and that doesn't include method calls.

Categories