How many objects are created when we write [duplicate] - java

This question already has answers here:
String s = new String("xyz"). How many objects has been made after this line of code execute?
(21 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 5 years ago.
String str=new String ("xyz");
I read somewhere JVM create 2 object 1st in pool and 2nd in heap.
Is this true ? If true then why JVM create 2 object when one is already there can anyone explain?

Yes, you are right. It creates two objects. One in String Constant Pool and another object in Heap pointing to the String pool.
If we consider String str= "Hello" //String literal, it creates only one object by JVM in String Constant Pool.
As per your syntax(String str = new String("xyz") //String object) JVM creates 2 objects. One in string pool and another in heap).
For further reference, please check the following discussion:
String s = new String("xyz"). How many objects has been made after this line of code execute?

"xyz" is in the constant pool once the class has been loaded
str is put on the heap when that line actually runs
see
Where does Java's String constant pool live, the heap or the stack?

Related

What is the advantage of declaring String s= new String("abc").intern(); over String s="abc"(or vice versa) [duplicate]

This question already has answers here:
String object creation using new and its comparison with intern method
(2 answers)
Closed 6 years ago.
As I understand that
1) String s="abc"; creates "abc" in string pool and
2) String s=new String("abc").intern(); string pool reference is returned irrespective of whether "abc" exists in pool before or not.
My question is
What is the advantage of 1) over 2) (or vice versa) since both of these returns the reference from pool.
Which syntax is preferred?
In 2) irrespective of "abc" present in pool, will the object gets created in heap initially and then its lost? (eligible for gc)
This is simplest and fastest
String s = "abc";
Using new String("abc").intern() is not only slower but much more complex and confusing.
In short, don't make the code any more complicated than needed.

how am i able to change value of same String? [duplicate]

This question already has answers here:
Java String Immutability and Using same string value to create a new string
(4 answers)
Closed 7 years ago.
i know Strings are immutable in java and i just read some theory about it
Once a String is declared it cannot be changed . i declared a String and changed it's value just by appending some value to it . it should not have happened
String s = "amol";
s = s + "abc";
System.out.println(s); //output is amolabc
The String s didn't get changed. When you did s + "abc", it created a new String object with the result of the operation.
You need to understand the concept of String Pool in Java. Please go through the link
http://www.javaranch.com/journal/200409/ScjpTipLine-StringsLiterally.html
You are right that Strings are immutable. Here, You didn't change the String s. All distinct Strings are stored in the Heap Memory. So, all the variables (say 10 variables ) containing the same String (say "Hello" ) will occupy only 5 bytes of memory. They all will point to the same location. Separate instances will not be stored for each of those variables.
Here, when you write s = s + "abc", a new string "amolabc" is created in the heap,and now the variable s just points to the new string in the heap. You didn't modify the value of "amol". You just made a new String.
The meaning of immutable is not like you thought to be.Being immutable guarantees that hashcode will always the same, so that it can be cashed without worrying the changes.That means, there is no need to calculate hashcode every time it is used.
here you are appending another string value to it,which can be done.But you cannot concatenate with another string.
By appending it is creating another string.
Refer here

JAVA : How many Objects will be created ? why? [duplicate]

This question already has answers here:
String s = new String("xyz"). How many objects has been made after this line of code execute?
(21 answers)
Closed 7 years ago.
I was going through SCJP examination and found one line in one book.
String s = new String("abc");
And it was written that, two objects will be created on above line. one on HEAP and one on STRING POOL.
I am not satisfied by the declaration given there. Can someone make me understand why two objects are created ?
Thanks in advance.
Author is correct. When ever you used String literal, that literal goes to constant pool first.
Hence "abc" created in constant pool.
And when you use new key word a new Object called s created on heap.
The literal "abc" is created and interned ("string pool").
The new operator will create a new String that is not interned.
Author is correct:
one object will be created in the string pool for "abc" literal; and
another object will be created on the heap for new String(...)
Object 1 - "abc"
Object 2 - new String("abc")

String s = new String("abc") memory allocation [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.
String s = new String("abc")
I know that this would create a new String object in Heap.
But i am confused about a statement in SCJP book by Kathy Sierra.
It states that the above statement would create an object in heap and at the same if the String "abc" is not present in the String pool , it would also add "abc" to the String pool.
Could anyone please tell me if the object "abc" is also created in the String pool in the above case.
String s = new String("abc")
Yes,above line will create two objects, one in string pool and other in Heap.
So, now
1) If you create a string literal like:-
String s1="abc"; //abc value will be taken from string pool which is previously added
2) If you create a String object and call intern method, no new object will be created instead it will just refer to "abc" present in string pool.
String s2=new String("abc").intern();
Note: When we are creating new String object based on existing one, it reuses char[] value.
Since new String(someStringValue) creates an exact copy of existing string and strings are immutable, there is clearly no reason for the two to exist at the same time.
Even if you have two String objects, they might still point to the same content.
You can refer this:- https://dzone.com/articles/string-memory-internals
Yes, it will be created as you are not having the same string before this line in your code. Now consider this example.
String temp = "abc"; // line1
String s = new String("abc"); // line2
In this case "abc" is not recreated. s will point to "abc" that was created at line 1.
sujith in comment : But in that case i do not see any difference between String temp ="abc" and String temp = new String("abc").
In line 1 a new "abc" will be created in heap. And a new String object will be created and added into the stack. And in line2 a new object of String will be created which will be referring to the "abc" that was created at line1 in heap. To better understand what thing goes to stack and what goes to heap visit here.
Yes it does. To optimize memory use, it does so.
In case you create another variable with same value "abc",
new variable will still point to earlier rather than creating a new one.
Somehow it is trying not to create two equal objects

String s=new String("Rohit"); Does this statement creates an object in heap only or it makes an entry in string pool as well? [duplicate]

This question already has answers here:
Difference between string object and string literal [duplicate]
(13 answers)
What is the difference between "text" and new String("text")?
(13 answers)
Closed 8 years ago.
I attended interview and i was asked this question.
String s=new String("Rohit");
Does this statement creates an object in heap only or it makes an entry in string pool as well?
I answered it does not make entry in pool. I think with .intern() it would make entry in string pool. Interviewer's thought was opposite.
Could you please guide me if i was wrong or interviewer?
Thanks in Advance.
EDIT:
String s1=new String("Rohit");
String s2="Rohit";
String s3=new String("Rohit").intern();
System.out.println(" "+(s2==s3)+" "+(s1==s2)+" "+(s1==s3)+" "+(s2==s3));
results as :true false false true
This makes me to think that without using intern() with new, there is no entry in pool for this object
Several things wrong with what you say he said:
First, doing new String always returns a new string, and never one that is interned.
Second, while it is true that the presence of the string literal "Rohit" might cause a String of that value to be "interned" (what is erroneously referred to as placing in the "string pool" or "string constant pool"), that would be done (if it was done) when the class was loaded, not when the statement was executed.
Third, since there can only ever be one copy of a String with a given pattern in the interned string table, even loading the class is not guaranteed to add a new entry, since one might already be there.
Of course, as is often the case, there may have been some misunderstanding on the part of one or both of you, or the question (or your answer) may have been poorly/unclearly worded.
I agree with #Hot Licks, but the behaviour of when a String literal is loaded changed in Java 7 AFAICS.
String literals are loaded when the class is loaded in Java 6 but in Java 7 they changed this to be when the first line which uses the string is executed. You can detect this by looking at the String returned by String.intern(); The first time it is called for a String it will return the same object, however when called again with an equals() String it will return the previous object.
StringBuilder sb = new StringBuilder("Hell");
sb.append("o");
String s = sb.toString();
String si = s.intern(); /* same string if not loaded. */
String s2 = "Hello";
System.out.println( System.getProperty("java.version")+" " + (s == si) + " "+(s2 == s));
prints
1.6.0_45 false false
as you expect, but in Java 7+
1.7.0_45 true true
If s2 is loaded first, the intern() string si will be the same as it, and thus different to s However, if s2 is loaded after, all the Strings use the same object.

Categories