aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Object query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Object query" Watch "Object query" New topic
Author

Object query

Prasun Howlader
Ranch Hand

Joined: Oct 21, 2007
Posts: 89
How many Object are created following this line?

StringBuilder ob = new StringBuilder("Java");

I think 2 is it true? One for Java literal and others for StringBuilder.


"Control time instead of letting time control you."
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Originally posted by Prasun Howlader:
How many Object are created following this line?

StringBuilder ob = new StringBuilder("Java");

I think 2 is it true? One for Java literal and others for StringBuilder.


Yes. That is for String literal and NOT Java literal.

You may further refer to a very good article about String Literals and objects by Corey McGlone here.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

Tell you in Detail from memory point of View :

StringBuilder s = new StringBuilder("Kathy");

Now compile will create 2 Objects :
>> a String Object "kathy" and put it into "String constant Pool"

>> A StringBuilder Object on the heap which will reference to "kathy" Object on SCP.

>> so Overall s (this one on stack) ---> StringBuilder (on Heap) ---->"kathy" ( on SCP)

Now what is SCP (String Constant Pool) :
Generally when a java program runs, We create a lots of string object that waste a lot of memory, So in order to make JAVA Memory Efficient, JVM set aside some area of Heap called String Constant pool, in which all object of String literals aside.

Please reply again if you need some more light on SCP.


Thanks and Regards,
SCJP 1.5 (90%), SCWCD 1.5 (85%), The Jovial Java, java.util.concurrent tutorial
Dean Jones
Ranch Hand

Joined: Dec 29, 2007
Posts: 129
>> so Overall s (this one on stack) ---> StringBuilder (on Heap) ---->"kathy" ( on SCP)
---------------------------------------------------------------------------
Dear Sunny, I have few doubts:

Where do object reference variables live? On the stack or on the heap? What happens if the object reference variable is a method local variable?

Please clarify.
[ January 14, 2008: Message edited by: Dean Jones ]
Sunny Jain
Ranch Hand

Joined: Jul 23, 2007
Posts: 433

HI,



I know the syntax is wrong, but concept is right !!!

Cheers!
Sunny
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Howdy Ranchers,

yes, the instance variables live along with the instances in *heap* memory. Whereas the local variables do live in *stack* memory.

For a better clarification, you may refer this thread
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Object query