Results 1 to 12 of 12

Thread: Reference Type

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    167

    Smile Reference Type

    In Java book, it is said that Integer, Long, Double ,.......... are reference type. But I can not pass by reference in method. Why?

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Reference Type

    You can pass any object, array byref only primitive data types cannot be passed byref.
    And for example.. for the double you can create an Double(class) object and pass it byref
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Reference Type

    Quote Originally Posted by ComputerJy
    for the double you can create an Double(class) object and pass it byref
    But after you initialize any of the primitive wrappers (Integer, Double, etc), you can't modify the primitve value. So it really defeats the purpose of using it for byref.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Reference Type

    Quote Originally Posted by crptcblade
    But after you initialize any of the primitive wrappers (Integer, Double, etc), you can't modify the primitve value. So it really defeats the purpose of using it for byref.
    No it doesn't.
    You can re-initialize the object any time and set the new value
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: Reference Type

    Quote Originally Posted by ComputerJy
    No it doesn't.
    You can re-initialize the object any time and set the new value
    Ummm...but then its a different object.

    Unless I'm missing something (wouldn't be the first time )
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  6. #6
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Reference Type

    I know it's a different object, but neither you nor the machine will notice that.
    because java uses the same memory fragment when re-initializing
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  7. #7
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Reference Type

    Should it be a different object? I don't think anything besides the String object is mutable, so it shouldn't create a new object unless you do so for some reason. Simply change the value?

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Reference Type

    There's no real pass-by-ref in Java. You can modify objects that allow you to modify them, but you can never modify the object references and have the changes visible outside.
    The Apache Jakarta project has pre-written mutable wrappers for the primitives. Or you can pass a one-element array of the primitive:
    Code:
    void wantToModify(int[] oneInt)
    {
      oneInt[0] = 4;
    }
    
    void other()
    {
      int[] oneInt = new int[1];
      wantToModify(oneInt);
    }
    It's not elegant.

    Oh, and SysErr, String is not mutable.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Reference Type

    Quote Originally Posted by CornedBee
    Oh, and SysErr, String is not mutable.

    I meant imutable or whatever the right word is.

  10. #10
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Reference Type

    Immutable.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  11. #11
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Reference Type

    Quote Originally Posted by CornedBee
    Immutable.
    Yeah, yeah


    So what you were saying earlier is that ALL objects are not passed by reference?


    I find it interesting that String objects (maybe other object) arrays are not passed by reference. I kind of had a feeling they wouldn't be, but I didn't know for sure until I tested it. I just thought since it was an array it would be passed by reference, but I was wrong.... Kind of weird, but understandable.

  12. #12
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Reference Type

    All variables are passed by value. However, there are only primitives and references. That is,
    String str;
    or
    JTable table;
    are not String objects or JTable objects, but references to these. This reference is passed by value.
    This means that within a function, this has no effect on the outside:
    str = "other";
    table = new JTable();
    That's because you're only modifying a copy of the reference. However, modifications to the object will be visible outside:
    table.setPreferredSize(new Dimension(200, 100));
    The problem is that String doesn't provide any functions that modify its data - it's immutable, like the primitive wrappers Integer, Long, etc.

    Similarly, you can modify the contents of an array, but you cannot replace it - arrays are immutable insofar as that you cannot change their size. You'd have to allocate a new array and make the reference point to this, but references are passed by value, so the outside view doesn't change.

    Yes, it's quite confusing. That's what I don't like about Java and C# - they blur the (important, IMO) distinction between values and references.
    A working knowledge of C++ (or even C) makes all of this far easier to understand.
    Last edited by CornedBee; Feb 16th, 2006 at 04:10 PM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width