Results 1 to 3 of 3

Thread: Type conversion

  1. #1
    hannes is offline Member
    Join Date
    Nov 2009
    Posts
    54
    Rep Power
    0

    Default Type conversion

    Hello,

    I have a question about type conversion. If you convert an object T to an oject S is there allocated new memory for T, or is it the same memory that is reallocated?

    Thanks,
    Hannes

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,003
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by hannes View Post
    Hello,

    I have a question about type conversion. If you convert an object T to an oject S is there allocated new memory for T, or is it the same memory that is reallocated?

    Thanks,
    Hannes
    Nothing really happens; just the type refernce is changed. All classes form a tree with the Object class as the root. You can only 'upcast' a type T to a type S when S is nearer to the root than T on the same path or your can 'downcast' a type T to a type U where T is nearer to the root on that path from the root to U.

    Nothing happens to the object itself because for an 'upcast', a sub class "is a" super class" and the compiler is able to figure out that relationship and for a "downcast" a check is performed at runtime (the superclass has to lie on the path from the root to the target class of the cast operation).

    kind regards,

    Jos

  3. #3
    renamed is offline Member
    Join Date
    Dec 2009
    Location
    Rio de Janeiro
    Posts
    38
    Rep Power
    0

    Default

    Quote Originally Posted by hannes View Post
    Hello,

    I have a question about type conversion. If you convert an object T to an oject S is there allocated new memory for T, or is it the same memory that is reallocated?

    Thanks,
    Hannes
    In fact nothing related to memory happens.
    Remember that in java, when we do the following:

    Java Code:
    T abc = new T();
    We create two dimensions in memory: one that we will call "abc" which is a pointer that points to the new object created by operator new.

    Either "abc" or the new object created have a size in memory... if you do the following:

    Java Code:
    S s = (S)abc;
    Two things will happen:

    1) Is the size of "s" larger than or equals to the size of "abc", which actually points to the object "T"?
    2) Does "s" and "abc" have anything in common?

    If the answer of the two questions above is positive, then the conversion can be done.
    Please don't laugh at my English... I'm trying my best! :)

Similar Threads

  1. XLS to PDF conversion
    By nitin2k2k in forum Advanced Java
    Replies: 18
    Last Post: 07-02-2013, 11:17 AM
  2. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 06:24 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  4. runtime type conversion
    By sardare in forum Advanced Java
    Replies: 3
    Last Post: 08-18-2008, 08:51 AM
  5. Conversion from wav to vox
    By bozovilla in forum Advanced Java
    Replies: 1
    Last Post: 07-31-2008, 05:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •