How many object created when creating object of String [duplicate] - java

This question already has answers here:
Questions about Java's String pool [duplicate]
(7 answers)
Closed 8 years ago.
Actually I am bit confused that how many object created in below "code processing".
String s=new String("A");
s=s+"B";
Actually someone said that here 4 objects will be created but in whole processing but how not understand.
Please anyone can give me detail description also included memory part such string pool etc.

The first String created is literal "A", that is, if not interned
prior.
The second String is the instance generated by the new keyword.
The third one is literal "B", again, if not interned prior.
The last one is the concatenation of s and "B".

You have two String literal Objects, namely "A" and "B". Then you explicitly instantiate a new instance of "A" with new String("A");. Finally, the fourth instance is created when you perform the String concatenation s+"B"

Related

If Strings in java are immutable, why I can do this? [duplicate]

This question already has answers here:
Immutability of Strings in Java
(26 answers)
Closed 3 years ago.
I realize that the hashCode of the variable name, is different after the "update", but objectively what makes a String object in fact immutable ?
public static void main(String[] args) {
String str = "AB";
System.out.println(str ); // AB
str = str .replace(str .charAt(0) ,'W');
System.out.println(str );//WB
}
EDIT 1 : The hashCode is based on the value of the variable and have no relation with memory adress.
EDIT 2 : I now understand that Strings are references and not Objects in it self.
I read back all the answers for this same question and found out good answers in topics like [this] (Immutability of Strings in Java). Thank you whos tried to help me and my excuses for any silly misunderstood.
I also recommend this articles here to who wants better understand how Strings works in Java :
https://www.pushkarrajpujari.com/article/strings-in-java/
and how references works :
https://javaranch.com/campfire/StoryPassBy.jsp
EDIT 3: I cannot DELETE this topic anymore, according with Stackoverflow "You cannot delete this question as others have invested time and effort into answering it." which I agree.
If you look at the documentation of replace(), it mentions:
Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.
Therefore, the replaced String is an entirely new String.

Object created while using String methods in java [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
public class StringDemo
{
public static void main(String [] args)
{
String s = "Sachin";
s.concat(" Tendulkar");
s.toLowerCase();
System.out.print(s);
}
}
This example giving output as : Sachin
then how many objects have been created?
The answer is: an indeterminate number.
on the face of it, the two operations on s each create a single String object,
two more String objects are created at load time to represent the literals,
objects may be created when print is called: internally to the System.out PrintWriter and the stream stack that it wraps,
each String object may (or may not) have a distinct char[] object inside it,
it is possible that the operations on s could be optimized away, since they actually have no effect on the output of the program (!!),
when the application is called, it will be passed a String[] argument, potentially populated with multiple String, and (finally),
an arbitrary number of objects will be created during JVM bootstrapping and class loading ... prior to the application starting.
So, depending on what objects you count, how you count them, and the other assumptions that you make, the answer could be some number from zero to a very large number of objects.
Note: the normal quiz answer for this would be "2 Strings are created", but as you can see the answer is a lot more complicated than that.
Note 2: the concat and toLowerCase methods do NOT create strings in the string pool. In fact, the only String operation that puts strings into the pool is intern. (It is easy to verify this experimentally, or by reading the Java class library source code.)
String in java is a immutable type.
s.concat(" Tendulkar");
s.toLowerCase();
these 2 lines return 2 distinct strings and doesn't affect the original string.
In java String is considered as immutable which means that it cannot be changed once its created, so if you count how many you have, on the first line you declared the first one, when you did s.concat("SE 6") you created a new object, and finally s.toLowerCase() created the 3rd object, therefore 3 string objects are created.

Java String initialization as primitive type [duplicate]

This question already has answers here:
What is the difference between "text" and new String("text")?
(13 answers)
Closed 7 years ago.
String is an Object. Why it is possible to initialize it the same way as primitive type: String str = "my string";
I was expecting to see initialization by using constructor only: new String("my string");
This is just a simplification provided by java. The other alternative would be enormous ugly. Your alternative solution has one simple logical mistake:
new String("my string");
Just aswell uses a string-literal as simply "my string". The real alternative would be
new String(new char[]{'m','y',' ',...,'n','g'});
Or just the same example using a byte[] (deprecated), which would look even worse.
You can go to the javadocs:
Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable.

Comparing two string literals [duplicate]

This question already has answers here:
Comparing strings with == which are declared final in Java
(6 answers)
Closed 9 years ago.
while comparing to strings we can do using == or .equals()
In == we know that it checks for references but in .equals() it checks for contents.
So suppose if there are 2 strings say
String s="SO";
String s1="SO";
so in this case s1==s and s.equals(s1) both will give true.
But here it gives me false
So what I assume is + is high priority than ==
so in this case
System.out.println(""+s1==s);
it will be splitted like (""+s1)==s and now ""+s1 will be a new String and hence the new String will never be equal to s so its printing false
I am just interested to know whether I thought is right or not
""+s1 creates a new String Object on the heap (since it is not declared as final). So, the references will not be same.

String s = "a" + "b" + "c"; Can anyone tell for this statement how many object will be created [duplicate]

This question already has answers here:
How many Java string objects created in the code String s="abc"+"xyz";?
(3 answers)
Closed 9 years ago.
Code:
String s = "a" + "b" + "c";
I want to know how many objects will be created for this statement.
There will be one string object in the string pool. "a" + "b" + "c" is resolved to "abc" at compile time (see JLS ยง15.28), so what you have is equivalent to
String s = "abc";
There are no StringBuilders involved here, contrary to what the accepted answer of the duplicate question asserts. You can even see this in the bytecode:
LDC "abc"
ASTORE 1
From the JLS link above:
Compile-time constant expressions of type String are always "interned" so as to share unique instances, using the method String.intern.

Categories