• 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

How many Objects?

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

how many objects are created when this line is executed??

String s= new String("string");

in KnB it is given as two..please explain me how come??


secondly..

regarding == if the two references that are being compared are in hierarchy but not same ..then will it give compilation error..
similarly ..for equals if there are in hierarchy will it be false ??/
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Srikanth,
I assume that you know well about the concept of String pool. (Just goback the same pages of K&B book).
So,
When you decare like,


String s= new String("string");



This statement will make 2 object entries. One object will be created in Stirng pool and another object will be created with the same string value and will be refered by the reference s.

Regarding 2nd squestion,


for equals if there are in hierarchy will it be false ??



That all depend on the implementation of the equals() method!!
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
srikanth question was easy but what about this:

ok, we have two references but how many objects are created in the
"String Constant Pool" and how many on the "Heap"?

consider that:
s1==s2will return FALSE
s1.equals(s2)will return TRUE
[ October 25, 2005: Message edited by: Ner min ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ner ..i suppose ..both will have one object on pool and one on heap...

right??
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi


ok, we have two references but how many objects are created in the
"String Constant Pool" and how many on the "Heap"?



String Constant Pool : 1
Heap : 1
 
Ner min
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi, yeah u r right

i remember i was a bit confused with that question when i was doing SCJP so i thought...

here is another tricky one:

String s1 = new String("immutable");
String s2 = "immutable";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
s1=s2.intern();
System.out.println(s1==s2);
System.out.println(s1.equals(s2));

What ist the output?:___________________

String s1 = new String("immutable");
String s2 = "immutable";
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
s2=s1.intern();
System.out.println(s1==s2);
System.out.println(s1.equals(s2));

What ist the output?:___________________
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ner ..as far as my first doubt is concerned ..

for ==
at line 1 there wont be any compiler error as there are in same hierachy whereas it occurs if there is no inheritence relationship like String,StringBuffer...
thats fine..
but for equals method if there are in hierarchy they still return false ...
like in the below code..
 
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

String Constant Pool : 1
Heap : 1



Runtime Constant Pool: 0
Heap: 2

All objects are stored on the heap. A reference to the String object is stored in the constant pool (along with the characters contained in the literal).
[ October 25, 2005: Message edited by: Steve Morrow ]
 
Ner min
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#Steve no only one on the Heap
s2 ist placed on the stack and references directly the pool.
the proof for this is that s2==s2.intern() returns TRUE, which would NEVER BE the Case if u used new String()
--------------------

hmm, i think i know what u r talking about , but that is not the case/question. The s1 and s2 r simple(no frills) Strings
so what is th output?

false true true false
or
false true false true
or
...

for example 1 and 2
[ October 25, 2005: Message edited by: Ner min ]
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ner ..
regarding first one..
it prints false true true true..
second false true false true ..

coz..s1.intern means it will check in the pool whether this string exists (as equals) and if it finds then it will return that string ..
s2=s1.intern();
will return the string and assign it to the same one which is already in the pool..so there wont be any shift ...
 
Ner min
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#srikanth, right, u know all that , how comes that u get confused with u'r initial question?
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

s2 ist placed on the stack and references directly the pool.


Not really. The 's2' reference variable is stored on the stack and is equal to the 's1' reference stored in the pool.

I think you misunderstood what I was saying: no objects are created "in the pool". All objects are stored on the heap. References (and other constants) are stored in the pool.

That said, my point is probably outside the scope of the SCJP...
[ October 25, 2005: Message edited by: Steve Morrow ]
 
Ner min
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#Steve
ok there was misunderstending of term "object", strictly in javatermms saying objects r really in the one and only place, the HEAP. I just mixed the "objects" and "objects representations"
 
Steve Morrow
Ranch Hand
Posts: 657
Spring VI Editor Clojure
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ner min:
#Steve
ok there was misunderstending of term "object", strictly in javatermms saying objects r really in the one and only place, the HEAP. I just mixed the "objects" and "objects representations"



No problem. I was probably being entirely too nitpicky, anyway...

Cheers!
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/QB]

but you didnt override equals
 
You ridiculous clown, did you think you could get away with it? This is my favorite tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic