Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

To Katherine Sierra & Bert Bates:

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bert, PLEASE TRY TO READ & REPLY..
Here is the hot question of these days...

--> String s=new String("Bert");

1) How many objects r created in above statement?// One or Two?
2) Whether StringPool contains 'Objects' or 'References to Objects'?

Some books r making contrary statement with urs. So I am bit confused..
Thanx in advance..
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, the answer to question 1 is: one. Only one string object exists on the heap.

Not sure about question 2.
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


1) How many objects r created in above statement?// One or Two?



There are two String objects created by this line of code.

The first is "Bert" and the second is the String object created by the new operator.

The first string is created when the class is loaded and the second is created when the line of code executes.


2) Whether StringPool contains 'Objects' or 'References to Objects'?



The String pool contains references to Strings.


There is a good article on this subject here.
[ November 13, 2005: Message edited by: Scott Johnson ]
 
Ranch Hand
Posts: 257
Hibernate Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Mahendar Reddy:
Hi Bert, PLEASE TRY TO READ & REPLY..
Here is the hot question of these days...

--> String s=new String("Bert");

1) How many objects r created in above statement?// One or Two?
2) Whether StringPool contains 'Objects' or 'References to Objects'?

Some books r making contrary statement with urs. So I am bit confused..
Thanx in advance..




first think about the follwong

new String("bert");

what it will do.

it will create an object with name bert in heap

then what about this

String k=new String("bert");

String k= (new String("bert"));


both are same. ....and in each case that will create two object and the GC will collect the first object after assiging it to the new object because it will not be referenced by any other object.....

I got this answer from java faq.
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rangvallika,

both are same. ....and in each case that will create two object and the GC will collect the first object after assiging it to the new object because it will not be referenced by any other object.....



I would suggest you to go through http://www.javaranch.com/journal/200409/Journal200409.jsp , and re-think about the above GC statement. Because as per the article the first obejct will never be eligible for GC.

Regards,
Mausam
 
Let's go to the waterfront with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic