How many objects will get created for the piece of code shown below?
String str1 = new String("nitesh");
String str2 = new String("nitesh");
String str3 = "nitesh";
String str4 = str3;
String str5 = str2;
How many objects will get created for the piece of code shown below?
String str1 = new String("nitesh");
String str2 = new String("nitesh");
String str3 = "nitesh";
String str4 = str3;
String str5 = str2;
String str1 = new String("nitesh");
String str2 = new String("nitesh");
String str3 = "nitesh";
String str4 = str3;
String str5 = str2;
TreeSet<Integer> set = new TreeSet<Integer>();
set.add(System.identityHashCode(str1));
set.add(System.identityHashCode(str2));
set.add(System.identityHashCode(str3));
set.add(System.identityHashCode(str4));
set.add(System.identityHashCode(str5));
System.out.println(set.size());
// => 3
The three objects are:
"nitesh"
created during compilationString
constructor.3 Objects (Wrong Answer). "Well, correct number, but wrong explanation".
String str1 = new String("nitesh"); // Creates a new String object
String str2 = new String("nitesh"); // Creates a new String object
String str3 = "nitesh"; // Creates a new String object
String str4 = str3; // Holds the same reference as str3
String str5 = str2; // Holds the same reference as str2
Seems like the third line does not create a new String object, my bad.
I will go try it out in an IDE in the meantime..
Just keeping the answer here for now, for the sake of the constructive comments going on below.
Seems like the right explanation for 3 Objects will be something along the line of..
First line creates 2 objects, 1 on the heap and 1 within the String pool?
Second line creates 1 object on the heap.
Yes 3 Objects will be created.
1) String str1=new String("nitesh"); // String object "str1" is created
2) String str2=new String("nitesh"); // String object "str2" is created
3) String str3="nitesh"; // String object "str3" is created
String str4=str3; // Will not be created a new object; But will hold the value of "str3"
String str5=str2; // Will not be created a new object; But will hold the value of "str2"
Actually 2 objects. str1
and str2
hold object references. While str3
, str4
and str5
do not hold string objects but are string literals.
In your example,str1
and str2
point to different objects in the Java String Pool
. These strings, if mutated, will still point to same memory locations where they pointed before getting mutated.
Though these string object cannot be mutated directly by using the =
operator, as strings in java are immutable, but stringbuffer objects can be made using the string object and then that string buffer object can be mutated with the help of methods in java.lang.StringBuffer
class.
But if str3
, str4
and/or str5
are mutated, ie. if you do this somewhere later in the code:
str3="narendra modi";
A new memory location will be reserved in the java heap
and not the java string pool
, with the string "narendra modi" stored in it and str3
will point to it. While str4
and str5
will still be pointing to location where the string "nitesh" is stored.
Citation added: string buffers CAN be mutated...