• 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

confused

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Here are 2 questions from Valiveru's exam:
Q1.
How many String objects are created when run this code?
String s1,s2,s3,s4;
s1="hello";
s2=s1;
s3=s2+"pal";
s4=s3;
I think the answer is 2 but the it is 3, any reason???/

Q2.
can we initilize one innerclass object like:

Outer$Inner i = this.new Inner()?
thanks.
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
s1="hello";
s2=s1;
s3=s2+"pal";
s4=s3;
It will create 3 strings :
1. "hello"
2. "pal"
3. s2+"pal" = "hellopal"
Think...String is non-mutable object and so s2+pal will create another object.
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Helen,
Q1
s1="hello" //1st String
The following is where I believe your problem exists..
s3=s2+"pal";
Now as you know String's are immutable, so whenever String concatenation takes place, the append() method of StringBuffer is called. Its prototype is given below(This one will be called)
StringBuffer append(String str)
"pal" is therefore your 2nd String.
Consider what the append method returns. It returns a StringBuffer containing the cocatenated value and not a String. The compiler inserts a call to toString() to turn it back to a String. This is how the 3rd String is formed.

Now for Q2.
I would suppose that you are reffering to the fact that the compiler turns the Outer class having an Inner inner class into Outer$Inner. Well the thing here to remember is that the compiler is working on a .java file which has an Outer class and an Inner inner class. The .java file has no Outer$Inner class. Its only after the code has compiled that the .class file have the Outer$Inner class. There still exists no such class in the .java file. Therefore you cant have any references to Outer$Inner class.
Hope I could be of some help.
Please correct me if I am wrong.
Jayesh

[This message has been edited by jayesh bindra (edited July 18, 2000).]
 
Helen Yu
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much to you all.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic