In Java book, it is said that Integer, Long, Double ,.......... are reference type. But I can not pass by reference in method. Why?
Printable View
In Java book, it is said that Integer, Long, Double ,.......... are reference type. But I can not pass by reference in method. Why?
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
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.Quote:
Originally Posted by ComputerJy
No it doesn't.Quote:
Originally Posted by crptcblade
You can re-initialize the object any time and set the new value
Ummm...but then its a different object. :confused:Quote:
Originally Posted by ComputerJy
Unless I'm missing something (wouldn't be the first time :afrog:)
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
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?
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:
It's not elegant.Code:void wantToModify(int[] oneInt)
{
oneInt[0] = 4;
}
void other()
{
int[] oneInt = new int[1];
wantToModify(oneInt);
}
Oh, and SysErr, String is not mutable.
Quote:
Originally Posted by CornedBee
I meant imutable or whatever the right word is.
Immutable.
Yeah, yeah ;)Quote:
Originally Posted by CornedBee
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.
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.