Perhaps the problem is that in Java
parameter are passed by reference, so you can modify the object but not the reference to object.
If you know C, you want modify the pointer to an object and not the value of the pointed
object.
Bye
On Fri, 5 Sep 2008 03:48:07 -0700 (PDT)
Norman Ho <[EMAIL PROTECTED]>wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---
Norman Ho <[EMAIL PROTECTED]>wrote:
>
> 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
-~----------~----~----~----~------~----~------~--~---
- [java programming] Passing parameters by reference quest... Norman Ho
- [java programming] Re: Passing parameters by refere... miga
- [java programming] Re: Passing parameters by re... Norman Ho
- [java programming] Re: Passing parameters by re... Norman Ho
- [java programming] Re: Passing parameters b... miga
- [java programming] Re: Passing paramete... Norman Ho
- [java programming] Re: Passing par... miga
- [java programming] Re: Passing par... Marco Severino
- [java programming] Re: Passing par... Krupinski Norbert
- [java programming] Re: Passing... miga
- [java programming] Re: Pas... miga
- [java programming] Re: Pas... Krupinski Norbert
- [java programming] Re: Pas... miga
- [java programming] Re: Pas... Krupinski Norbert
- [java programming] Re: Pas... miga
- [java programming] Re: Pas... miga
- [java programming] Re: Passing parameters by re... SS~
