why the static method not change the value of two value of the two string?(scjp02_Q8
Gong James
Ranch Hand
Joined: Oct 15, 2001
Posts: 77
posted
0
Q8 What is the output of the following code? 1. public class Note { 2. public static void main(String args[]) { 3. String name[] = {"Killer","Miller"}; 4. String name0 = "Killer"; 5. String name1 = "Miller"; 6. swap(name0,name1); 7. System.out.println(name0 + "," + name1); 8. swap(name); 9. System.out.println(name[0] + "," + name[1]); 10. } 11. public static void swap(String name[]) { 12. String temp; 13. temp=name[0]; 14. name[0]=name[1]; 15. name[1]=temp; 16. } 17. public static void swap(String name0,String name1) { 18. String temp; 19. temp=name0; 20. name0=name1; 21. name1=temp; 22. } 23. } // end of Class Note a. Killer,Miller followed by Killer,Miller b. Miller,Killer followed by Killer,Miller c. Killer,Miller followed by Miller,Killer d. Miller,Killer followed by Miller,Killer
the given ans is :C. I had compile and run the code ,"Killer,Miller Miller,Killer" is output ,which proved that the ans C is right. But I think the static method swap(String name0,String name1) access the class variable ,so it change the original value of the class.
I am wrong on this opinion,but I can't find where's the wrong ?
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
Originally posted by Gong James: Q8 What is the output of the following code? 1. public class Note { 2. public static void main(String args[]) { 3. String name[] = {"Killer","Miller"}; 4. String name0 = "Killer"; 5. String name1 = "Miller"; 6. swap(name0,name1); 7. System.out.println(name0 + "," + name1); 8. swap(name); 9. System.out.println(name[0] + "," + name[1]); 10. } 11. public static void swap(String name[]) { 12. String temp; 13. temp=name[0]; 14. name[0]=name[1]; 15. name[1]=temp; 16. } 17. public static void swap(String name0,String name1) { 18. String temp; 19. temp=name0; 20. name0=name1; 21. name1=temp; 22. } 23. } // end of Class Note a. Killer,Miller followed by Killer,Miller b. Miller,Killer followed by Killer,Miller c. Killer,Miller followed by Miller,Killer d. Miller,Killer followed by Miller,Killer
the given ans is :C. I had compile and run the code ,"Killer,Miller Miller,Killer" is output ,which proved that the ans C is right. But I think the static method swap(String name0,String name1) access the class variable ,so it change the original value of the class.
I am wrong on this opinion,but I can't find where's the wrong ?
The static method swap(String name0, String name1) swaps the references only which are copies that within the swap method only. When the method returns back to main() the references in main() still point to the same String objects. Hope this help.
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
more... name0 and name1 in swap() are copies of main()'s name0 and name1. In swap(), it change name0 and name1 which are references not the actually object itself.
Gong James
Ranch Hand
Joined: Oct 15, 2001
Posts: 77
posted
0
I know what you mean ,if what you said are right ,How can you explain ,with the same feature ,the method swap(String name[]) swap the elements of array?
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
Originally posted by Gong James: I know what you mean ,if what you said are right ,How can you explain ,with the same feature ,the method swap(String name[]) swap the elements of array?
with name[0] .. name is pointing to the array. name is a copied reference and name[0] is the really reference point to the String object. if you do changes on name[0], thats not a copy, thats the actually String object outside of swap(). if you do changes on name, thats a copy, it won't change the name reference in main(). hope this help
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
sorry.... more. in another word. name holds the actally, really references pointing to the String objects. hope this help.
Gong James
Ranch Hand
Joined: Oct 15, 2001
Posts: 77
posted
0
Mr FEi NG Can you clearly tell me how can I change the code so that swap the String name0 name1 value in the method swap()?
Fei Ng
Ranch Hand
Joined: Aug 26, 2000
Posts: 1241
posted
0
Hi James. Since java pass by values only you can't change code in swap(String name0, String name1) to swap them. Put the String objects in a array like the first swap(String name[]) method then it will work. Changing the copies doesn't affect the String object. You need to change what the copies are pointing to. And since String object is immutable you can't swap them on the second swap method, just can't.