miga said that malti sharma added more println() method. It only displays and displays, here I want to display the result by own two println() methods in main(). No addition in swap. If that, nothing to say! It's OK but is not my requirement ^^!
Maybe we can't do that, because when code *swap(int a, int b)*, then we
indicate that *PASS BY VALUE => a, b can't be changed*
I think swap(a, b) is impossible, must be swap() ^^! (too shy :p)
public class Swap {
static int a = 8;
static int b = 5;
public static void swap() {
int temp = a;
a = b;
b = temp;
}
public static void main(String[] args) {
System.out.println("Before: a = " + a + ", b = " + b);
*swap();*
System.out.println("After: a = " + a + ", b = " + b);
}
}
run:
Before: a = 8, b = 5
After: a = 5, b = 8
BUILD SUCCESSFUL (total time: 2 seconds)
Think a simple ques, but not true ... [?][?][?]
Thanks again !
On Wed, Apr 22, 2009 at 6:58 PM, pavan kumar <[email protected]>wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---
<<inline: 330.gif>>
