*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes parameterpassing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "parameterpassing " Watch "parameterpassing " New topic
Author

parameterpassing

sailajauppalapati
Greenhorn

Joined: Jul 10, 2000
Posts: 5
import java.util.*;
class Over
{
public static void main(String args[])
{
Vector v=new Vector();
Vector v1=new Vector();
Over o=new Over();
o.set(v,v1);
System.out.println("value of v",+v);
System.out.println("value of v1",+v1);
}
public void set(Vector v,Vector v1)
{
v.addElement("100");
v1=v;
System.out.println("value of v",+v);
System.out.println("value of v1",+v1);
}
}
sorry Kai i typed it wrongly.
thanks Kai Li for your reply.actually I want to know what will be the output in each
println in detail.if you can please explain
me .I am little bit confused.please reply
any one .sorry Iam asking silly one.
Kai Li
Greenhorn

Joined: Jun 26, 2000
Posts: 29
Originally posted by sailajauppalapati:
1. import java.util.*;
2. class Over
3. {
4. public static void main(String args[])
5. {
6. Vector v=new Vector();
7. Vector v1=new Vector();
8. Over o=new Over();
9. o.set(v,v1);
10. System.out.println("value of v",+v);
11. System.out.println("value of v1",+v1);
12.}
13. public void set(Vector v,Vector v1)
14. {
15. v.addElement("100");
16. v1=v;
17. System.out.println("value of v",+v);
18. System.out.println("value of v1",+v1);
19. }
}
sorry Kai i typed it wrongly.
thanks Kai Li for your reply.actually I want to know what will be the output in each
println in detail.if you can please explain
me .I am little bit confused.please reply
any one .sorry Iam asking silly one.

You still have one typing error:
System.out.println("value of v",+ v); no comer!!!
I try to explain clearly:
1. Line 6 and line 7 create two object references for two different objects.
suppose v point to the first object,v1 point the second object.
2. Line 9 when you call set method, you pass the copy of object reference. It means for the first object, now you have two reference, one reference in the main method and another one in the set method(both called v), and both reference point to the same object. The same thing as the second object and there are also two reference point to the same object(both called v1).
3. in set method,when you call v.addElement method you actually operate on the first object and the content of object change. But two reference(v) still point to the first object(already changed). Now when you implement line 16,it means the reference of second object in set method point to the first object(it means now we have three reference point to the first object). The reference of second object in the main method still point to the second object(it doesn't change since you doesn't do anything on it)now only one reference point to the second object.
4. now let's see the printout:
since in main method you have one reference v, in set method you have one reference v and v1,all three reference point to the first object. so any print related to these three reference must be same. So line 17,18,10 print the same thing:
value of v[100]
value of v1[100]
value of v[100]

since in main method only v1 refer to the second object and you don't operate on it, the line 11 print out should be:
value of v1[]
hope it will make you sense. sorry for my bad english..
Kai,
[This message has been edited by Kai Li (edited July 15, 2000).]
jafarali
Ranch Hand

Joined: Jul 15, 2000
Posts: 37
Look at the following code it may give you some clue :
class look {
public static void main(String sx[]) {
look l = new look();
String x = "JAVA";
String y = "is";
StringBuffer z = new StringBuffer("That is really ");
l.confuse(x,y,z);
System.out.println("Inside main => "+x + " " + y + " " + z);
}
public void confuse(String a, String b, StringBuffer c) {
System.out.println("Inside confuse#1 => " + a + " " + b + " " + c);
a = "JSP"; //Trying to change object reference but it is just a copy
c.append("great"); //Trying to change value and it will reflect
//because it is pointing same location
System.out.println("Inside confuse#2 => " + a + " " + b + " " + c);
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: parameterpassing
 
Similar Threads
hashtable with Vector
getting error when converting vector to string
How can I pass vector in java?
Overloading
Difference between two programs