Verson with StringBuffer doesn't work either:


public class smiec {

static StringBuffer a =new StringBuffer ("aaa");
static StringBuffer b = new StringBuffer ("bbb");

static void swap(StringBuffer x, StringBuffer y){
String temp = x.toString();
x =new StringBuffer ("jj");
y = null;
}
    
    
   
public static void main(String args[]) {
    
    

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

}    
    
} 



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


Settting global variables doesn't work either, I just thought it would, but
I was wrong.

public class SwapString {
static String a = "aaa", b = "bbb";
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {


        System.out.println("Before swap "
                + "a = " + a
                + " b = " + b);
        swap(a, b);
        System.out.println("After swap "
                + "a = " + a
                + " b = " + b);
    }

    static void swap(String a, String b){
        String temp = a;
        a = b;
        b = temp;
    }

}

On Sep 5, 8:40 pm, miga <[EMAIL PROTECTED]> wrote:
> On Sep 5, 12:07 pm, Norman Ho <[EMAIL PROTECTED]> wrote:
>
>
>
> > I know this works:
>
> > public static void main(String[] args) {
>
> >         String[] arr = {"aaa","bbb"};
> >         int i = 0, j = 1;
> >         System.out.println("Before swap "
> >                 + "arr[0] = " + arr[i]
> >                 + " arr[1] = " + arr[j]);
> >         swap(arr, i, j);
> >         System.out.println("After swap "
> >                 + "arr[0] = " + arr[i]
> >                 + " arr[1] = " + arr[j]);
> >     }
>
> >     static void swap(String[] arr, int i, int j){
> >         String temp = arr[i];
> >         arr[i] = arr[j];
> >         arr[j] = temp;
> >     }
>
> > But I don't understand why, they are both void.
>
> Because the array is known before sending it to the static method, 
> therefore the swap works. In the method, you just change the internal 
> values of the array, a bit as if you were using an int declared before 
> the method, and changing it in the method; well, that's just an 
> analogy.
>
> > If I go to the trouble of creating a class object with instance 
> > variables, and a method swapString, would it have worked?
>
> Provided that you construct an array of those strings before the call 
> to the swapString method, yes. Otherwise, static or not static, you 
> will have the same trouble.
>
> The fact is you cannot return more than one thing from a method, so if 
> you want to return two things, you have to create a collection of 
> them, and work with that collection.- Hide quoted text -
>
> - Show quoted text -



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