• Post Reply Bookmark Topic Watch Topic
  • New Topic

String Object Creation  RSS feed

 
Sreeni Aluru
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String s = new String("thisisthefirststringobject");
--how many string objects the above statement creates?.
two or one
Thanks.
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It creates two objects. However, the details behind String literals and how they are managed will not be tested on the exam.
Corey
 
S Goyal
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone tell me how 2 objects will be created?
 
Corey McGlone
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
String literals are treated in a special way in Java. As Strings are immutable objects, there are some optimizations that can be done.
Without going into too great of detail, whenever you use a String literal is your code (i.e. "thisisthefirststringobject"), a new String object is created and a reference is created to it in the string literals table. Also, when you use the new String() command, you're creating a new String with the same contents as the one that was created earlier. We now have two distinct String objects with the same contents. If you'd like to test this, just execute this code:

As I mentioned earlier - this won't be on the exam. However, if you're really interested, do a search in this forum for String Literals and I'm sure you'll find lots of information. This is a well discussed topic here.
I hope that helps,
Corey
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After all, the constructor for String takes another String as argument.
 
Vince Hon
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try it:
class Must129_String {

public static void main(String args[]) {
String a = "abc"; //1
String b = new String("abc");//2
System.out.println(a==b);
}
}
/*
The process is that:

During compiliation, a string with literal "abc" is created and put in the system String pool.
In //2, the system will search in the pool to find if any literal match with "abc", finally, it
finds a string "abc" referenced by "a" matches the value, therefore, no new String object is
created. At this point "a" refer to the String object "abc" in the pool and there is only one
String object being created.

However, during runtime, the "new" command run in //2, a total new String object with literal
"abc" is created "outside" the pool. Thereafter, it is referenced by "b".
As a result "a" and "b" refer to 2 different String objects.

*/
 
Sreeni Aluru
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just for Clarification---
system.out.print("firststringobject" + "secondstringobject");
-- how many objects it creates?
3 or 1 or none

String s = new String("first");
How many objects the following line creates?.
s.concat(" second");
1 or 2 or none
Thanks.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Sreeni.

system.out.print("firststringobject" + "secondstringobject");
String s = new String("first");
s.concat(" second");

three
two
two
 
Badri Sarma
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Frome the above question which is posted
s.concat(" second");
how this creates 2 objectes, becasue s is changed its states it creates only new object.
If this types of question are seen in the SCJP exam the what would be the answer, for example
String str = new String("concat");
A) 1
B) 2

Badri
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However concat method takes another string object as argument. Thus two string objects are created.
On the other hand:

will create only three because all the string literals with the same information point to the same string object.
___________________________________________
I would answer two. But it is likely they are more interested in testing that the returned string object is a different object than s (as long as the argument length is greater than 0).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!