Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / July 2005

Tip: Looking for answers? Try searching our database.

A question in the object's size.

Thread view: 
Bruce .J Sam - 07 Jul 2005 01:31 GMT
  In order to show my question, I have written three simple class.
class A {
 int id;
 String name;
 public void f() {}
 public void g() {}
}
class B {
 int id;
 public void f() {}
}
class C extends B {
 String name;
 public void g() {}
}
   Is it the size of the object of class C equal to the object of
class A, or bigger than it?
david@tribble.com - 07 Jul 2005 17:26 GMT
Bruce J Sam wrote:
> In order to show my question, I have written three simple class.
> class A {
[quoted text clipped - 14 lines]
> Is it the size of the object of class C equal to the object of
> class A, or bigger than it?

Well, class A contains an int and a String member variable.  Class C
contains an int and a String variable (one of which is inherited from
B), so it should be the same size as A.

This all assumes, of course, a very simple and straightforward Java
compilation model.  At any rate, this is what you'd expect from what is
actually contained in the resulting compiled Java bytecode and symbol
table.

It also assumes that there are no alignment issues to deal with, and
that an int and a String variable are both one 32-bit "word" in size.
If a object reference is bigger (which could be the case on 64-bit
CPUs), then the order in which the base and derived members occur could
cause one class to be larger than the other because it contains padding
bytes to force proper alignment of the members.  There is no general
rule about whether inherited members occur before or after derived
members in memory.  In fact, there is no general rule about what order
the class members occur in at all; the JVM has a lot of
leeway in deciding how to arrange the member variables.

How the JVM actually chooses to lay out the object types in memory may
differ from this, for a variety of reasons.  Generally, though, you're
safe in assuming that every object takes up at least one word, which is
its hidden class type pointer (similar to a C++ object's _vtbl
pointer), and possibly another word for a synchronization lock.
Implementing one or more interfaces may also complicate the memory
layout.  In general, though, inheritance should not add any extra space
to objects other than the base class member variables.

-drt


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.