Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

parameterpassing

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
}
}
 
He repaced his skull with glass. So you can see his brain. Kinda like this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic