This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills: The software developer's life manual and have John Sonmez on-line!
See this thread for details.
Win a copy of Soft Skills: The software developer's life manual this week in the Jobs Discussion forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in creating strings...

 
thambi durai
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can any one explain the two way of String creation ...

String s="hello";
String s=new String("hello");
how many object and reference is created in heap and pool.

WHAT IS OUTPUT FOR TH FOLLOWING CODE?

public String makinStrings()
{
String s = �Fred�;
s = s + �47�;
s = s.substring(2, 5);
s = s.toUpperCase();
return s.toString();
}
How many String objects will be created when this method is invoked?
A. 1
B. 2
C. 3
D. 4
E. 5
F. 6
 
Ian Edwards
Ranch Hand
Posts: 107
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, if we go through the code line by line:

We are creating a String object here and giving s a reference to it.


"47" is a String object, and we are creating a new string object by concatenating "47" to "Fred". We lose the reference to "Fred" and point s to "Fred47".


In this line we are creating a new String object, "ed4", and assign it to s.


Here we create a new String object, "ED4", and assign it to s.


Lastly, we create a new String object using the toString() method.


In total we are creating 6 string objects.
 
thambi durai
Greenhorn
Posts: 6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the answer is 3...
how it's possible..
 
Dean Jones
Ranch Hand
Posts: 129
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Thambi,

please see the link
 
ABHIJEET BUDKE
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic