Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-05-2004, 07:57 AM   #1 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
passing by reference

ok, i'm about to go read up on this, but i thought i'd post this question so when i find the resolution it will help someone in the future.

in my class i have this method:
Code:
public boolean setCustomerByID(customer cust,String id)
{
  // if found set customer object
  return true;

  // if customer not found
  return false;
}
i need this method to return a boolean value, and this is how i want to call it:
Code:
customer c = new customer();
if( myclas.setCustomerByID(c,"1234") ){
  out.println(c.firstName + " " c.lastName);
}else{
  out.println("customer not found");
}
in that example, i would like the setByCustomerID method to return true/false, but i alway want my new customer object 'c' to be set in the process.

at this point, it doesn't seem like customer is passing by reference.

i'm going to go google it now but any insight would be appreciated. i'll post my findings later.
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2004, 08:15 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
ok, am i correct in assuming the reference will hold until i assign the object to a new object inside the method?

here's the best example i found:
Code:
class ValHold{
        public int i = 10;
}

public class ObParm{
public static void main(String argv[]){
        ObParm o = new ObParm();
        o.amethod();
        }
        public void amethod(){
                int i = 99;
                ValHold v = new ValHold();
                v.i=30;
                another(v,i);
                System.out.print( v.i );
        }//End of amethod

        public void another(ValHold v, int i){
                i=0;
                v.i = 20;
                ValHold vh = new ValHold();
                v =  vh;
                System.out.print(v.i);
        System.out.print(i);
        }//End of another

}
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2004, 08:17 AM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
I dont know Java but I found this interesting:
http://www.yoda.arachsys.com/java/passing.html

From there you can build further I think. I'm cetain it will give you an edge.
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 08:35 AM   #4 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
So in your code, "c" is nothng but an "object reference". So when you pass it to your ID function, you will pass a COPY OF THE REFERENCE.
Aha, now I see what Java is about .

So you could "alter" the value(s) in "c" but you cannot interchange it with other objects if passed to the same function (like a "b" for example).
You need to use the original references for that.

That's what I understood from that text.

So try your experiment like this:
- Make a swap function.
- Give objects b and c a value in main. (your current customer objects will do just fine).
- pass them to your swap function.
- swap them.
- test in main: fails.
- in swap function: assign new value (name and id) to "c".
- test in main: success, value of c has changed.

Watch it, strings are passed by VALUE. No copies of references are made.
And it is certainly not a reference like in C++.

That's what I understood from that story. But you gotta test it in the swap function I proposed. I don't have Java.
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 08:38 AM   #5 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
So
Code:
 v =  vh;
should fail if you test it in main().

That's what I understood. You just copied a copy of a reference. Not the object reference itsefl.

I wonder if I was even close to the facts.
If I am, give me a candy .
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 08:43 AM   #6 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
...will hold until i assign the object to a new object inside the method?
That's right.
We're in sync. If you're wrong (the java experts will come soon hopefully), then I hope you didn't catch some of my stupidity .
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 09:44 AM   #7 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
I didn't quite follow what you were trying to do, but in Java, Val is correct in that as far as Objects, a copy of the reference was passed. That means you're still working on the same object that was passed in. Primitives are passed by value, so changing an int inside a method won't change the value of that int at wherever it is outside of that method.
__________________
GitS
Belisarius is offline   Reply With Quote
Old 05-05-2004, 10:24 AM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
well my method searches a database and creates a new customer object.

when i assign the new customer object to the object i passed in the method, .. the new value is not accessable from outside the method.

for now i just work-around by returning the customer, .. but i'd rather make the method boolean and assign a new value to the object inside the method.
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2004, 10:26 AM   #9 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
That's what I understood too. But I have a question as well now, to test my theory (I don't have java remember?):
If a primitive (like an int) defines the state of an object then code like
Code:
a.myIntegerValue = 5;
inside a function would change the the state of an object and therefore
Code:
out.print(a.myIntegerValue);
will yield "5" outside the function?
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 12:06 PM   #10 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
i think i have this down now .. are my comments correct here?
Code:
private void doSomething(MyObject myObj){
  // this is the same object that got passed in
  myObj.param = 1;

  MyObject newObj = new MyObject();
  newObj.param = 2;

  myObj = newObj;

  // at this point, the object passed in
  // from outside 'param' = 1 and i can 
  // no longer maniuplate the object passed in
}
sorry to beat a dead horse, but is that right?
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2004, 12:16 PM   #11 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Once you exit "doSomething", the result of this:
Code:
myObj = newObj;
will be discarded.
__________________
Valmont is offline   Reply With Quote
Old 05-05-2004, 12:31 PM   #12 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
because nothing will be using myObj anymore as we knew it inside doSomething .. ok, it makes much more sense now.
__________________
Mike
sde is offline   Reply With Quote
Old 05-05-2004, 01:20 PM   #13 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
Val, you're correct both times. Because the method passes a copy to the reference, as opposed to the reference itself, you cannot reassign the reference. Hence, setting the input to a new object won't affect the object outside the method.

sde, why not just return null instead of false, and check to see if the object returned is null?
__________________
GitS
Belisarius is offline   Reply With Quote
Old 05-06-2004, 06:22 AM   #14 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,545
Valmont is on a distinguished road
Quote:
because nothing will be using myObj anymore as we knew it inside doSomething...
Inaccurate sde, sorry.
You never used myObj to start with...
You used a copy of myObj's reference. Not myObj.

There were two myObj-references in your program. Your function used one of them: the body-double in the world of memories...

Hey! I gotta remember this one...
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add an Assembly Reference - Mono twixntwix MS Technologies ( ASP, VB, C#, .NET ) 4 07-08-2004 08:00 AM
Java help?? d00ds?? zergmuncher Java 19 11-15-2003 02:35 AM
Passing form data to PHP with Javascript bdl PHP 5 07-03-2002 10:18 AM


All times are GMT -8. The time now is 01:08 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting