public class Test7{ public Test7(){} public Test7(Test7 ref){ this (ref,"Hai"); } public Test7(Test7 ref,String str){ ref.Test7(str); System.out.println("Hi"); } public void Test7(String str){ System.out.println(str); } public static void main(String[] args){ Test7 t = new Test7(); Test7 t7 = new Test7(t); } }
can anyone explain to me the output of this programme in more details...I am having a problem when it comes to passing by value and pass by reference...
Sandip Sarkar
Ranch Hand
Joined: Nov 25, 2007
Posts: 55
posted
0
Your program is as follows:
public class Test7{ public Test7(){}//1 public Test7(Test7 ref){//2 this (ref,"Hai");//3 } public Test7(Test7 ref,String str){ //4 ref.Test7(str);//5 System.out.println("Hi");//6 } public void Test7(String str){//7 System.out.println(str);//8 } public static void main(String[] args){ Test7 t = new Test7();//9 Test7 t7 = new Test7(t); //10 } }
Now Line 10 calls for Line 2. Line 3 passes it on to Line 4 . Line 5 calls function at Line 7. Line 8 prints "Hai". Line 6 prints "Hi".
So the output is : "Hai" "Hi"
I hope it helped.
Nicholas Jordan
Ranch Hand
Joined: Sep 17, 2006
Posts: 1282
posted
0
[vuthlarhi donald:] I am having a problem when it comes to passing by value and pass by reference...
What problem, your code does not demonstrate the passing by value v pass by reference design challenge efficiently:
"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Originally posted by vuthlarhi donald: can anyone explain to me the output of this programme in more details...I am having a problem when it comes to passing by value and pass by reference...
In Java, everything is always passed by value. There is no pass by reference in Java.