Results 1 to 8 of 8
  1. #1
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default References and passing them.

    Hey,

    Just wondering at the moment,

    When you pass a reference to an object. For example calling the constructor of an object and passing an object to it and then assigning it to another instance, Is the memory needed for that instance copied so you have double the memory used for each instance?
    (Don't know if that was actually understandable though...)

    Thanks.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: References and passing them.

    No, you don't double the memory requirement at all. All that is passed is the address of the object (and yes, this does take up memory, but very little), but the memory for the object itself does not increase or change.

  3. #3
    jammas615 is offline Senior Member
    Join Date
    Jul 2011
    Location
    Melbourne, Victoria, Australia
    Posts
    155
    Rep Power
    4

    Default Re: References and passing them.

    So it becomes a pointer in a sense?

    But then why doesn't java have pointers?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: References and passing them.

    Quote Originally Posted by jammas615 View Post
    So it becomes a pointer in a sense?

    But then why doesn't java have pointers?
    One of Java's main philosophies is to simplify memory handling and thereby make coding safer than coding in C/C++. A down side is that you lose some power in this trade.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: References and passing them.

    In another thread you mentioned using C++: a major difference between the languages is the way Java uses references only as the value of (nonprimitive) variables. This explains how you can use variables as arguments to constructors and methods without eating up memory.

    These reference values are pointers in a very real sense. If it was good enough for whoever named the NullPointerException to think of them as pointers, it's good enough for us. The safety Fubarable talks about arises from the fact that there is no pointer arithmetic.

    You can assign them to variables, you can pass them to constructors and methods, you can dereference them (called that in C, too, with respect to its pointers) But what you can't do is increment them or do other arithmetic operations. That way lies buffer overruns and all the rest.

Similar Threads

  1. About references.
    By fatabass in forum New To Java
    Replies: 8
    Last Post: 03-03-2012, 08:17 AM
  2. Forward references
    By Norm in forum Advanced Java
    Replies: 2
    Last Post: 06-30-2010, 02:19 AM
  3. Soft References
    By rickcharles_b in forum Advanced Java
    Replies: 0
    Last Post: 06-20-2007, 08:27 PM

Posting Permissions

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