|
 |
|
 |
10-30-2003, 07:28 AM
|
#1 (permalink)
|
|
Registered User
Join Date: May 2003
Location: CA
Posts: 12
|
Java help?? d00ds??
can you pass by refrence in Java? I know you can in C++ (which is far superior, but alas, the AP computer science class is now in Java *sigh*...(just kidding all you java programmers :-P)). Is doing it anything like doing it in C++? Is it even possible? Thank you!! Gracias!!
~J3r3d d0000d!!:butt:
|
|
|
10-30-2003, 01:38 PM
|
#2 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Australia
Posts: 21
|
Unlike C++, which 'normally' passes by value and has to be specified to pass by reference, Java _only_ passes variables by value and _only_ passes objects by reference. In general this works out fairly well... at least, I can't remember ever wanting a variable passed by reference or an object passed by value.
|
|
|
11-02-2003, 12:48 PM
|
#3 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
I had a discussion about this with my professor a while back, and Java actually does pass by value (as far as objects are concerned), but the value is that of the memory reference, as opposed to the actual memory reference. I'm not sure what use that knowledge is, but there it is.
|
|
|
11-02-2003, 01:17 PM
|
#4 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Australia
Posts: 21
|
Er...
How is the value of the memory reference different from the memory reference itself, given that the memory reference is its value?
I'm unclear as to what you mean here.
As far as I know, a memory reference is the number (OK, simplified a bit here, but ah well) in memory where the thing being referenced starts... so I don't understand what you mean when you say the value of the memory reference... It just seems to me that you're saying that... well, say you had an int of 3, it just seems as if you're saying that instead of passing 3, the language passes 3...
|
|
|
11-02-2003, 08:57 PM
|
#5 (permalink)
|
|
LOAD "*",8,1
Join Date: Feb 2003
Location: la.ca.us
Posts: 254
|
DarkTwilkitri is correct. java is passing a pointer (er, "reference"). it's just an integer. yes, it is passing the value of the pointer that is a reference to the object (which is what passing by reference means).
caution, your professor may be an idiot.
|
|
|
11-03-2003, 03:05 PM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
No it's not that, I misunderstood him. I just talked to him (without mentioning this thread), and he basically said just what DarkTwilkitri said. I just mixed up the difference between primitives and objects (well, combined is a better word).
I need a vacation . . .
|
|
|
11-03-2003, 10:13 PM
|
#7 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
> can you pass by refrence in Java?
nope ...
java passes primatives by value and objects
by reference copy ...
> java is passing a pointer (er, "reference").
> ... yes, it is passing the value of
no it doesn't - it passes a copied object's reference.
otherwise code like this:
Code:
void a(Object obj){
obj = null;
}
void b(){
Object o = new Object();
a(o);
if(null == a){
System.out.println("not possible");
}
}
would print "not possible". - it wont.
__________________
direct entry file specification.
|
|
|
11-04-2003, 12:06 AM
|
#8 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Australia
Posts: 21
|
I'm going to take that example and rewrite it a bit. Requesting a comparison with null is not the best way of demonstrating something - some languages will say that null != null...
Before I do that, though.
Quote:
|
no it doesn't - it passes a copied object's reference.
|
Let's take a real world analogy. The third game in my game shelf is Oracle of Ages. Therefore, we can say that the reference to Oracle of Ages is 3.
If we copy 3, or get a so-called reference copy, we still end up with a reference of 3, which still points to the same game.
I'll edit this after I go and check that example to see how it goes.
-Unless - you mean the reference to a copied object, not the copied reference to an object - in this case your argument makes sense - it's hard to tell.
--That code won't even compile without being edited...
|
|
|
11-04-2003, 12:16 AM
|
#9 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
Quote:
Originally posted by DarkTwilkitri
-Unless- you mean the reference to a copied object,
|
Quote:
Originally posted by npa
it passes a copied object's reference.
|

__________________
direct entry file specification.
|
|
|
11-04-2003, 12:21 AM
|
#10 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
Quote:
Originally posted by DarkTwilkitri
--That code won't even compile without being edited...
|
... yes - it obviously needs to be placed inside a
class, etc ...
__________________
direct entry file specification.
|
|
|
11-04-2003, 12:34 AM
|
#11 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Australia
Posts: 21
|
That sentence is ambiguous, sorry :/
It can be tested simply without having to worry about those pesky nulls.
Code:
public class test
{
int skasaher;
test()
{
skasaher = 3;
}
public void increment(test johan)
{
johan.skasaher++;
}
public static void main(String[] args)
{
test celice = new test();
System.out.println(celice.skasaher);
celice.increment(celice);
System.out.println(celice.skasaher);
}
}
This prints out 3, 4. Not 3, 3 as it would if the change affected a copied object.
My earlier mis-interpretation of your theory may have been the most correct.
It passes a copy of the reference to the function, so when you set the copy to null, it doesn't affect the original because you are just changing the copied location. But when you change something inside the copy, it is changed.
...Of course, this is all just supposition...
And I was referring to
if(null == a)
when there isn't actually any a to compare against null. I think you meant o.
|
|
|
11-04-2003, 04:27 AM
|
#12 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
As my professor explained it, if you do this:
Code:
void foo(){
int a = 4;
addSix(a);
}
void addSix(int input){
input = input + 6;
}
"a" will still be 4 after calling addSix() from foo(). But if you do this:
Code:
void foo(){
Hashtable ht = new Hashtable();
addSomething(ht);
}
void addSomething(Hashtable input){
input.put("foo","bar");
}
"ht" will contain "bar" with a key of "foo" after calling addSomething() from foo().
|
|
|
11-04-2003, 12:05 PM
|
#13 (permalink)
|
|
Code Monkey
Join Date: Jul 2003
Location: canada
Posts: 82
|
Quote:
Originally posted by npa
when there isn't actually any a to compare against null. I think you meant o.
|
dark - yes ... you're right, i made a typo.
and yes i forget how it actually works but it
i think it copies the object in a shallow-copy
type system, so that assigning it to something
else (obj = whatever()) isn't reflected but modifying
its properties (obj.x = 1) is.
i'll try and find the actual terms for it ...
__________________
direct entry file specification.
|
|
|
11-04-2003, 03:30 PM
|
#14 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,139
|
Shallow copies and deep copies are terms used when cloning. You can do what you're describing with immutables like Strings, but most Objects you can't, you'll be passing around the Object reference. I know because I recently had that problem and had to implement cloning of Objects. If you say "Object x = y;", 'x' and 'y' are the same object.
|
|
|
11-04-2003, 05:15 PM
|
#15 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Australia
Posts: 21
|
Actually, this is starting to make sense...
Java always passes by value. But, what we think of as an object is actually a reference...
So when you pass int a = 6 to a function, you're passing a copy of 6 - this copy is altered within the function, and therefore does not affect the original six.
And when you pass Integer intwrapper = new Integer(6) to a function, all intwrapper is, is a reference to the Integer created in memory. So the reference gets copied. So, if you do something which affects the reference, ie:
Code:
void alterInteger(Integer anInt)
{
anInt = new Integer(7);
}
the value of the object reference anInt is changed to that of the new Integer, without changing the original reference intwrapper. Then it's lost when it loses scope.
But if you do... OK, Integer was a bad example because it doesn't have any set methods. Hmm. Just say that Integer had a setInt method...
Anyway, if you do something which alters something the reference refers to...
Code:
void alterInteger(Integer anInt)
{
anInt.setInt(7);
}
This will go to the object referenced by the copy anInt, which is the same object referenced by intWrapper, and alter the values inside the object, which will affect both copies since they are merely references to the object.
Of course, there's every chance that I'm completely wrong and I'm just making a fool out of myself. But this is the only conclusion I can draw 
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:28 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|