public class x0002swap {
public x0002swap() {
Integer a = new Integer(8);Integer b = new Integer(5);
System.err.println(a + "---before---" + b);
swap(a, b);
System.err.println(a + "---after---" + b);
}
public void swap(Integer first, Integer second) {
Integer temp = first;first = second;second = temp;
}
public static void main(String[] args) {
new x0002swap();
}
}
output:
8---before---5
8---after---5
-----------------------------------------------------------
Memory:("@100 ->" means : variable is pointing from location 100 in
memory to some other location)
(a...@100) ->(8...@5000)
(b...@200) ->(5...@6000)
now in the method swap:
(fi...@300) ->(8...@5000)
(sec...@400) ->(5...@6000)
(t...@500) ->(8...@5000)
(fi...@300) ->(5...@6000)
(sec...@400) ->(8...@5000)
now thas is what has happened. first and second variables are now
swapped. but this in no way has resulted in a and b variables to swap.
-----------
ofcourse we can solve this by sending an array and returning the
reversed array or
by using collections...
-----------
If still there is some confusion then draw a diagram of the above show
memory map. connect the pointers correctly. then everything will be
clear.
hope this helps...
reg
pavan
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---