Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > A question in the object's size.

Reply
Thread Tools

A question in the object's size.

 
 
Bruce .J Sam
Guest
Posts: n/a
 
      07-07-2005
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?

 
Reply With Quote
 
 
 
 
david@tribble.com
Guest
Posts: n/a
 
      07-07-2005
Bruce J Sam wrote:
> 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?



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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments