Variables in Java are passed by value. If the variable contains a reference 
(not a primitive) then that reference may be changed and the change will appear 
outside the function.

Consider:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package test;

/**
 *
 * @author Administrator
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        String [] sa = {"hi", "Bob"};
        int [] sa2 = {1, 2};
        testMutableArray(sa, sa2);
        System.out.println(sa[0]+ " " + sa[1]);
        System.out.println(sa2[0] + " " + sa2[1]);
    }

    private static void testMutableArray(String[] sa, int [] sa2) {
        System.out.println(sa[0]+ " " + sa[1]);
        System.out.println(sa2[0] + " " + sa2[1]);
        String [] sat = {"hello", "William"};
        sa = sat;
        System.out.println(sa[0]+ " " + sa[1]);
        sa2[0] = 3;
        sa2[1] = 4;
        System.out.println(sa2[0] + " " + sa2[1]);
    }

}

Output:

hi Bob
1 2
hello William
3 4
hi Bob
3 4

Date: Thu, 22 Jan 2009 20:42:21 +0100
Subject: [java programming] Is it possible to Pass-By-Value an array?
From: [email protected]
To: [email protected]

Hi!I'm wondering if it's possible to pass-by-value an array to a method?
Thanks.Jose
-- 

/**************************************************/
      Jose Maria Dueñas Quesada
      e-mail: [email protected]
      web: www.joseduenas.com
/**************************************************/

Computer science is not about computers any more than astronomy is about 
telescopes. -- E.Dijkstra 



_________________________________________________________________
Hotmail® goes where you go. On a PC, on the Web, on your phone. 
http://www.windowslive-hotmail.com/learnmore/versatility.aspx#mobile?ocid=TXT_TAGHM_WL_HM_versatility_121208
 
--~--~---------~--~----~------------~-------~--~----~
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