• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

String object using new operator

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

How many object are created in each of the following statements.



Please explain me. I know in line 1 only one object is created. What about Line 2 and Line 3?
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 Objects will be created.

Rahul Ba wrote:
I know in line 1 only one object is created. What about Line 2 and Line 3?


Irrespective of what is there in the String pool, if you are using "new" an Object will be created.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



One object, it is just that you are re-assigning the same String object on the heap to the same String reference. line 1 and line 2 are two alternatives of creating and initializing a string so one object.


The string reference s is now re-assigned to a completely new object on the heap denoted by "jkl". The String instance created above may be garbage collected.....

Do read up on String immutability in Java. You may find it easy to tackle these kinda-questions..

Regds,
Abhijit.

[SCJP 4/5, Zend Certified Engineer for PHP 5, OCPWCD 5, MySQL Certified Associate, Python Certified by OReilly]
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 objects, one per line.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, I'd go for four. Every time you use new String you get a new one. But also, how many different String literals are you creating?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijit Ghatnekar wrote:
The string reference s is now re-assigned to a completely new object on the heap denoted by "jkl". The String instance created above may be garbage collected.....


Not relevant to the question, though.
 
Rahul Ba
Ranch Hand
Posts: 215
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My answer is as follows please correct me If I am wrong




 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Ba wrote:My answer is as follows please correct me If I am wrong


Sounds about right to me, but the fact is that the most important thing is when they're created.
Pool objects (the literals) are created by the compiler and loaded at program initialization; new objects
are created at runtime, probably around the time the statement is actually executed (hard to say
exactly, because of all the optimization that goes on).

Winston
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So Winston, how many objects do you think are created by the 3 lines of code?
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijit Ghatnekar wrote:One object, it is just that you are re-assigning the same String object on the heap to the same String reference. line 1 and line 2 are two alternatives of creating and initializing a string so one object.


That's not completely correct: lines 1 and 2 do not do exactly the same.

In the first line, s will refer to a String object which is in the string pool.

In the second line, you are creating a new String object explicitly which will copy its contents from the String object that's in the string pool.

Note that it is wasteful and never necessary to create a String object explicitly by passing it a String literal.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Ba wrote:
How many object are created in each of the following statements.



Please explain me. I know in line 1 only one object is created. What about Line 2 and Line 3?



Executing that code creates two String objects. Line 1 just assigns to variable s a reference to a String object that was created in the pool when the class was loaded (if some other class hadn't already caused it to be created).

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Abhijit Ghatnekar wrote:


One object, it is just that you are re-assigning the same String object on the heap to the same String reference. line 1 and line 2 are two alternatives of creating and initializing a string so one object.



No.

First we don't assign objects, we assign references. Second, the new operator always creates a new object.

Line 1 doesn't create any objects, and line 2 creates one.

Do read up on String immutability in Java.



That's only relevant to the extent that for the constant pool to make any sense, the objects in it need to be immutable. It has no bearing on how many objects are created by this code.
 
James Boswell
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Executing that code creates two String objects. Line 1 just assigns to variable s a reference to a String object that was created in the pool when the class was loaded (if some other class hadn't already caused it to be created).



Thanks for this clear explanation Jeff.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic