This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Cloud Application Architecture Patterns: Designing, Building, and Modernizing for the Cloud and have Kyle Brown, Bobby Woolf and Joseph Yodor on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

interesting problems about swap string object

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

the answer is killer,miller followed by miller killer .
who can explain the how two swap() work?
 
Ranch Hand
Posts: 2676
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by jason rex:

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

the answer is killer,miller followed by miller killer .
who can explain the how two swap() work?


When you pass an object to a method what you are really passing is the object reference, not the object itself. The reference outside the method will remain unchanged. When you pass the arguments to swap( String name0 , String name1 ) you are creating method variables called name0 and name1 and giving them copies of the references used outside the method. When you make the changes inside the method you are changing the variables that are declared inside the method, not the ones outside the method.
In the example where you pass an array to a method, the same principle applies. When you make changes to name[0] and name[1], you are changing the object itself. This is done through the copy of the reference that the method has.
Matthew Phillips
 
jason rex
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am still confused .
by your explain,the name[0],name[1] will print miller,killer
but the answer is not like that ,???
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jason,
This might not address your question directly..but do try to follow the discussion in this thread http://www.javaranch.com/ubb/Forum24/HTML/012833.html
Hope this helps
Shyam
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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


Lets see if I can explain:
Line 4 : name0 is a ref to object "Killer" in the memory
Line 5 : name1 is a ref to object "Miller" in the memory
On line 6 function swap(name0, name1) is called and inside function swap(name0, name1) we pass copies of name0 and name1
so now the object and ref looks like
Main.name0 --- "Killer"
/
swap.name0 ---
Main.name1 --- "Miller"
/
swap.name1 ---
Inside swap we changed the values of swap.name0 and swap.name1 and do not change the values of main.name0 and main.name1 so now at the end of the function swap the ref looks like
Main.name0 --- "Killer"
/
swap.name1 ---
Main.name1 --- "Miller"
/
swap.name0 ---
Notice that the main.name0 and main.name1 are not changed so on line 8 it prints "Killer", "Miller"
Now in case of the other swap function the reference inside the function looks like
main.name ----- {"Killer", "Miller"}
/
swap.name ----/
Notice that both references point to the same object so when you make the swap of swap.name[0] and swap.name[1] the array changes to {"Miller", "Killer"}
and hence the output on the line 9 "Killer", "Miller"
Vinay
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vinay jain:


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


Notice that both references point to the same object so when you make the swap of swap.name[0] and swap.name[1] the array changes to {"Miller", "Killer"}
and hence the output on the line 9 "Killer", "Miller"
Vinay



I think output on the line 9 will be "Miller", "Killer"
CMIW

------------------
Regards
Ravish
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic