On Sep 5, 4:57 pm, "Krupinski Norbert"
<[EMAIL PROTECTED]> wrote:
> Miga, I'm sorry but I can't understand your explanation, maybe I'm too
> stupid
Certainly not! :-)
> - could explain it once again with different way?
Your example is great to understand what happens under the hood.
The fact is class String is not so good as class Something for
example, as it has an overrided method for print. Yours has none, so
we will print only the objects, so that we can see which object is
referenced in all part of the program.

I've modified a bit your example, just to print the objects (and a
Main2 class, not main2, sorry I stick to Java rules).

1 - First version with element_a = element_b commented in swap

package swappingstrings3;

/**
 *
 * @author miga
 */
class Something {

    public String a;

    public Something() {
    }

    public Something(String a) {
        this.a = a;
    }
}

public class Main2 {

    public static void main(String[] args) {

        Something element_a = new Something("aaa");
        System.out.print("In Main2/main before swap element_a
Something object: ");
        System.out.println(element_a);
        Something element_b = new Something("bbb");
        System.out.print("In Main2/main before swap element_b
Something object: ");
        System.out.println(element_b);
        System.out.println("Before swap element_a=" + element_a.a + "
element_b=" +
                element_b.a);
        swap(element_a, element_b);
        System.out.print("In Main2/main after swap element_a Something
object: ");
        System.out.println(element_a);
        System.out.print("In Main2/main after swap element_b Something
object: ");
        System.out.println(element_b);
    }

    static void swap(Something element_a, Something element_b) {
        Something temporary = new Something();
        System.out.print("in swap Temporary Something object: ");
        System.out.println(temporary);
        System.out.print("in swap at the beginning element_a Something
object: ");
        System.out.println(element_a);
        System.out.print("in swap at the beginning element_b Something
object: ");
        System.out.println(element_b);
        //element_a = element_b;
        //System.out.print("in swap after first changing references
element_a Something object: ");
        //System.out.println(element_a);
        //System.out.print("in swap after first changing references
element_b Something object: ");
        //System.out.println(element_b);
        temporary.a = element_a.a;
        System.out.print("in swap at the end Temporary Something
object: ");
        System.out.println(temporary);
        element_a.a = element_b.a;
        System.out.print("in swap at the end element_a Something
object: ");
        System.out.println(element_a);
        element_b.a = temporary.a;
        System.out.print("in swap at the end element_b Something
object: ");
        System.out.println(element_b);
    }
}

When run, it gives:
run:
In Main2/main before swap element_a Something object:
[EMAIL PROTECTED]
In Main2/main before swap element_b Something object:
[EMAIL PROTECTED]
Before swap element_a=aaa element_b=bbb
in swap Temporary Something object: [EMAIL PROTECTED]
in swap at the beginning element_a Something object:
[EMAIL PROTECTED]
in swap at the beginning element_b Something object:
[EMAIL PROTECTED]
in swap at the end Temporary Something object:
[EMAIL PROTECTED]
in swap at the end element_a Something object:
[EMAIL PROTECTED]
in swap at the end element_b Something object:
[EMAIL PROTECTED]
In Main2/main after swap element_a Something object:
[EMAIL PROTECTED]
In Main2/main after swap element_b Something object:
[EMAIL PROTECTED]
BUILD SUCCESSFUL (total time: 2 seconds)

You see, that element_a in Main2 before the swap is effectively passed
to swap, as well as element_b, then you use the capability of the
Something class to swap the value of element_a and element_b, that is
you apply the implicit method of class Something (this) to each
element, hence you don't change the reference to the object, just the
value of the object. And at the end of swap, you can check that the
references to the objects have not changed.
To make it easier to read:
element_a = 10d448
element_b = 0e1c6
And again in Main2, you retrieve the same references for element_a and
element_b.


2 - Second version with element_a = element_b commented out in swap

(same program just uncomment the four lines commented out)

run:
In Main2/main before swap element_a Something object:
[EMAIL PROTECTED]
In Main2/main before swap element_b Something object:
[EMAIL PROTECTED]
Before swap element_a=aaa element_b=bbb
in swap Temporary Something object: [EMAIL PROTECTED]
in swap at the beginning element_a Something object:
[EMAIL PROTECTED]
in swap at the beginning element_b Something object:
[EMAIL PROTECTED]
in swap after first changing references element_a Something object:
[EMAIL PROTECTED]
in swap after first changing references element_b Something object:
[EMAIL PROTECTED]
in swap at the end Temporary Something object:
[EMAIL PROTECTED]
in swap at the end element_a Something object:
[EMAIL PROTECTED]
in swap at the end element_b Something object:
[EMAIL PROTECTED]
In Main2/main after swap element_a Something object:
[EMAIL PROTECTED]
In Main2/main after swap element_b Something object:
[EMAIL PROTECTED]

What happens here?
element_a is 10d448 in Main2/ as well as at the beginning of swap,
good, that's normal.
element_b is e0e1c6 in Main2/ as well as at the beginning of swap,
good, that's normal again.
After element_a = element_b:
element_a points to element_b (e0ec16), element_b also (normal not
changed).
then after assigning new value to element_a;
actually, you don't change element_a, you change element_b, because
both point to element_b.
And as you change it twice, you don't change its value.
And in Main2, you retrieve element_a (which was not changed) and
element_b which was changed to itself.
End result, you think that it does nothing when you print the value,
but actually it has made something to the objects, inside the swap.

Hope this is more easier to understand.



--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to