aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many Objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How many Objects?" Watch "How many Objects?" New topic
Author

How many Objects?

srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
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 ??/


Thanks & Regards<br /> <br />-Srikanth
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
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!!


Manivannan
Ner min
Ranch Hand

Joined: Sep 14, 2005
Posts: 76
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

Joined: Jul 28, 2005
Posts: 252
ner ..i suppose ..both will have one object on pool and one on heap...

right??
hema gopalakrishnan
Greenhorn

Joined: Sep 01, 2005
Posts: 6
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

Joined: Sep 14, 2005
Posts: 76
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

Joined: Jul 28, 2005
Posts: 252
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..
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Sep 14, 2005
Posts: 76
#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

Joined: Jul 28, 2005
Posts: 252
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

Joined: Sep 14, 2005
Posts: 76
#srikanth, right, u know all that , how comes that u get confused with u'r initial question?
Steve Morrow
Ranch Hand

Joined: May 22, 2003
Posts: 657

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

Joined: Sep 14, 2005
Posts: 76
#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

Joined: May 22, 2003
Posts: 657

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!
sarath chala
Greenhorn

Joined: Sep 28, 2004
Posts: 2
[/QB]

but you didnt override equals
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How many Objects?