• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

intersting question

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How many String objects are created when the following class is run?
public class TestClass
{
public static void main(String args[])
{
String hello = "Hello"; //1
String world = new String("World!"); //2
String helloworld = hello +" "+world; //3
String alias = helloworld; //4
System.out.println(alias); //5
}
}
well I think its 5
1 at line 1
2 at line 2
2 at line 3
what do u say?
thanku
 
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This question is current "Today's Qestion" and a detailed explanation is also given. Check it out at http://www.jdiscuss.com/index.jsp?tab=scjp
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
[This message has been edited by Paul Anil (edited July 07, 2001).]
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think u r right the answer should be 5. Can some one please explain this?
------------------
Cheers
Tamanna :-)
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I went to JDiscuss and was not able to understand the following explanation at first till I looked at it more closely


String helloworld = "Hello" +" "+"World!";
This actually creates 3 new strings : " ", "Hello " and "Hello World!".
"Hello" and "World!" were already created in the previous 2 LOCs. (All go to String pool and will not be GCed)


The point here is + operator is syntactically left associative so hello and " " will concatenate together and create a new string object "Hello " and then this object and world will concatenate to give us the new object "Hello World!". Tough question indeed .
[This message has been edited by Anshul Manisha (edited July 07, 2001).]
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anshul, + does not behave like when it was an arithmatic operator when it's concatentaor of Strings. + in a String context actually creates a StringBuffer object, which will then call its append(String) method on the subsequent String operants. For instance,
String hello="Hello";
String world="World";
String HelloWorld = hello+" "+world;//here
The commented line actually is
String HelloWorld = new StringBuffer().append(hello)
.append(" ")
.append(world)
.toString();
There was no "Hello " String created at anytime.
 
Paul Anilprem
Enthuware Software Support
Posts: 4803
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Cameron on this. However, it's not that straight forward either.
JLS says:


15.18.1 String Concatenation Operator +
If only one operand expression is of type String, then string conversion is performed on the other operand to produce a string at run time. The result is a reference to a newly created String object that is the concatenation of the two operand strings. The characters of the left-hand operand precede the characters of the right-hand operand in the newly created


And then,


15.18.1.2 Optimization of String Concatenation
An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.


So, the answer is really implementation dependent.(Which should have been given at jdiscuss.)
-Paul.
[This message has been edited by Paul Anil (edited July 07, 2001).]
 
reply
    Bookmark Topic Watch Topic
  • New Topic