Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

How many objects created?

 
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Assuming x does not refer to anything before this statement...

String x = "first" + "second";

...how many objects are created?

I have seen two explanations:

1) Answer three: One that x refers to, i.e. "firstsecond". One as a result of "first", which nothing refers to and One as a result of "second" that nothing refers to.
2) Answer one: One that x refers to, i.e. "firstsecond". That is all, since "first" and "second" are string literals and NOT objects.

I'd appreciate a clarification for my upcoming exam.

On your marks...get set...go!

Cheers,

Simon.
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simon three objects, "first" "second" and "firstsecond", will be created in the string pool. If you now try and create another string say String str = "second" it would refer to the already existing one from the shared string pool and no new object would be created. I think so anyway!!
 
John Campbell
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Simon three objects, "first" "second" and "firstsecond", will be created in the string pool. If you now try and create another string say String str = "second" it would refer to the already existing one from the shared string pool and no new object would be created. I think so anyway!!
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

So,why does...

String x = new String("xyz");

...create two string objects according to K&B, page 400 - self test answer to Q 3 say, i.e. one referred to by x and the lost string "xyz"?

I would have thought we were creating one just one String object on the heap, i.e. "xyz" and x is referring to it.

All assistance is warmly welcomed.

Cheers,

Si.
 
Ranch Hand
Posts: 1272
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The string literal "xyz" exists in the string literal pool from the start of the program. The unary operator new always creates a new object. The string literal "xyz" is the single operand of the new operator. The result of the new operation is another object on the heap with the value "xyz".
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike,

Thanks for the feedback, I am grateful.

Excuse me for seeming dim, but I want to be clear on this point

Is the string literal "xyz" an object or not?

Thanks,

Si.
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Si,
"str" is an anynomous String object.

Shiv
SCJP1.4
 
Simon Cockayne
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi again,

So...for...

String x = new String("xyz");

A) "xyz" is a String literal, which IS an object on the heap.
B) new String("xyz") creates a second object on the heap and the reference is assign to the String reference x.

Thank you for clearing that up for me and for the collective patience!

Kind regards,

Simon.
 
reply
    Bookmark Topic Watch Topic
  • New Topic