|
-
Feb 9th, 2006, 09:50 PM
#1
Thread Starter
Addicted Member
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?
-
Feb 10th, 2006, 06:57 AM
#2
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
-
Feb 10th, 2006, 07:39 AM
#3
Re: Reference Type
 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
-
Feb 10th, 2006, 09:45 AM
#4
Re: Reference Type
 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
-
Feb 11th, 2006, 09:51 PM
#5
Re: Reference Type
 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
-
Feb 12th, 2006, 01:26 AM
#6
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
-
Feb 14th, 2006, 09:02 PM
#7
Frenzied Member
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?
-
Feb 16th, 2006, 01:32 PM
#8
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.
-
Feb 16th, 2006, 03:35 PM
#9
Frenzied Member
Re: Reference Type
 Originally Posted by CornedBee
Oh, and SysErr, String is not mutable.
I meant imutable or whatever the right word is.
-
Feb 16th, 2006, 03:38 PM
#10
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.
-
Feb 16th, 2006, 03:56 PM
#11
Frenzied Member
Re: Reference Type
 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.
-
Feb 16th, 2006, 04:07 PM
#12
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|