A question in the object's size.

Discussion in 'Java' started by Bruce .J Sam, Jul 7, 2005.

  1. Bruce .J Sam

    Bruce .J Sam Guest

    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?
     
    Bruce .J Sam, Jul 7, 2005
    #1
    1. Advertising

  2. Bruce .J Sam

    Guest

    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
     
    , Jul 7, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Madhanmohan S
    Replies:
    5
    Views:
    7,842
    Madhanmohan S
    Sep 3, 2004
  2. Andreas Klemt
    Replies:
    6
    Views:
    11,419
    Peter Theill
    Nov 28, 2004
  3. =?Utf-8?B?QmlkYXJrb3Rh?=

    How to set the DataGrid Row Size to a fixed size.

    =?Utf-8?B?QmlkYXJrb3Rh?=, May 19, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    859
    =?Utf-8?B?QmlkYXJrb3Rh?=
    May 19, 2005
  4. Neil Zanella
    Replies:
    4
    Views:
    513
    Gianni Mariani
    Oct 9, 2003
  5. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,742
    Michael Jung
    May 25, 2008
Loading...

Share This Page