Miga, I'm sorry but I can't understand your explanation, maybe I'm too
stupid - could explain it once again with different way?

For example lets consider the program below. When you uncomment line labaled
as a MODIFICATION in it works as I expected (method swap changes the order
two times - so in fact it does nothing ):

class Something {
 public String a;

 public Something() {
 }

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


    

public class main2 {

static void swap (Something element_a, Something element_b){
 
Something temporary = new Something();     
//element_a =  element_b;   MODIFICATION
temporary.a=element_a.a;
element_a.a=element_b.a;
element_b.a=temporary.a;


    
}    
    
public static void main(String[] args) {

Something element_a = new Something("aaa");     
Something element_b = new Something("bbb");       

System.out.println("Before swap element_a=" + element_a.a + " element_b=" +
element_b.a);
swap(element_a,element_b);
System.out.println("After swap element_a=" + element_a.a + " element_b=" +
element_b.a);

 }    
    
    
}


-----Original Message-----
From: [email protected]
[mailto:[EMAIL PROTECTED] On Behalf Of miga
Sent: Friday, September 05, 2008 3:43 PM
To: Free Java Programming Online Training Course By Sang Shin
Subject: [java programming] Re: Passing parameters by reference question




On Sep 5, 2:44 pm, "Krupinski Norbert"
<[EMAIL PROTECTED]> wrote:
>  Great!
>
> But could you explain why the following modification:
>
>   static void swap(StringBuffer firstStringBuffer, StringBuffer
> secondStringBuffer) {
>         firstStringBuffer=secondStringBuffer;  //MODIFICATION
>         StringBuffer temporaryStringBuffer = new StringBuffer();
>         temporaryStringBuffer.append(firstStringBuffer);
>         firstStringBuffer.replace(0, secondStringBuffer.length(), 
> secondStringBuffer.toString());
>         secondStringBuffer.replace(0, temporaryStringBuffer.length(), 
> temporaryStringBuffer.toString());
>
> }
>
> don't influence the result and change it into:
>
> Before swap - firstString = aaa secondString = bbb After swap - 
> firstString = bbb secondString = bbb
>
It does not give the same result by me:
Before swap - firstString = aaa secondString = bbb After swap - firstString
= aaa secondString = bbb

That is easy to explain; in the swap method, you just make the
firstStringBuffer points to the secondStringBuffer, that changes its
internal value, but you have sort of clone of firstStringBuffer, that's not
the same one as in Main method. Hence when you assign the value of
firstStringBuffer to firstString in Main, you don't assign the value of
firstStringBuffer as it was in swap method, but as it is in Main method. End
result you don't change it.

Now, be aware that the equality symbol for String or the like is very
dangerous, it is always a copy of the String or the like, not the same
String or the like. That's why you see this behaviour.

You can observe the changes while debugging in Netbeans, put a break on all
lines except printing in swap, then put a break on both lines after the swap
call in Main, step into each step, and observe carefully what happens to
local variables in swap, then in Main. That helps to understand you are not
dealing with the same references though they have the same names.

You may see what happens, this way:

static void swap(StringBuffer firstStringBuffer, StringBuffer
secondStringBuffer) {
        System.out.println("firstStringBuffer: " +
firstStringBuffer.toString() +
                " secondStringBuffer: " + secondStringBuffer.toString());
        firstStringBuffer = secondStringBuffer;
        System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
                + " secondStringBuffer: " + secondStringBuffer.toString());
        StringBuffer temporaryStringBuffer = new StringBuffer();
        temporaryStringBuffer.append(firstStringBuffer);
        System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
                + " secondStringBuffer: " +
secondStringBuffer.toString()
                + " temporaryStringBuffer: " +
temporaryStringBuffer.toString());
        firstStringBuffer.replace(0, secondStringBuffer.length(),
secondStringBuffer.toString());
        System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
                + " secondStringBuffer: " +
secondStringBuffer.toString()
                + " temporaryStringBuffer: " +
temporaryStringBuffer.toString());
        secondStringBuffer.replace(0, temporaryStringBuffer.length(),
temporaryStringBuffer.toString());
        System.out.println("firstStringBuffer: " +
firstStringBuffer.toString()
                + " secondStringBuffer: " +
secondStringBuffer.toString()
                + " temporaryStringBuffer: " +
temporaryStringBuffer.toString());
    }



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