Results 1 to 4 of 4
  1. #1
    Daxan is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default ¿Values by Reference? I dont understand...

    First, sorry about my english :).

    I have a question:

    I have:

    [...]
    static long tiempo = 0;
    public static void main(String[] args)
    {
    int test2 [] = {435,544,33,22,4,23,54,12323,5,3,0,121,4,65};
    ordenar_array_Burbuja(test2);
    mostrar_array(test2);
    }

    public static void ordenar_array_Burbuja(int array_a_ordenar[]) // Función que devuelte un array ordenado
    {
    mostrar_array(array_a_ordenar);
    tiempo = System.currentTimeMillis();
    int n = array_a_ordenar.length;
    for (int pass=1; pass < n; pass++) { // count how many times
    // This next loop becomes shorter and shorter
    for (int i=0; i < n-pass; i++) {
    if (array_a_ordenar[i] > array_a_ordenar[i+1]) {
    // exchange elements
    int temp = array_a_ordenar[i]; array_a_ordenar[i] = array_a_ordenar[i+1]; array_a_ordenar[i+1] = temp;
    }
    }
    }
    long total = 0;
    total = System.currentTimeMillis()-tiempo;
    System.out.println("He tardado "+ total +" en terminar");
    mostrar_array(array_a_ordenar);
    }
    [...]

    If I invoke "ordenar_array" and then I print "test2", the array dont have the initial values (435,544,33,22,4,23,54,12323,5,3,0,121,4,6), the array is sorted. ¿why? I dont return de array sorted to test2.


    This example:

    public class Test{
    public static void sum (Integer i){

    int val = i;
    val+=4;
    i = val;

    }

    public static void main (String args[]){

    Integer i = new integer (5);
    sum (i);
    System.out.println(i);

    }

    }


    Is the opposite, ¿why?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: ¿Values by Reference? I dont understand...

    Arrays are like objects. When you pass them as parameters to a method, the address of the array is passed, not a copy of the array. There is only one array and no copies, so any changes you make to the array are to the one array.

  3. #3
    Daxan is offline Member
    Join Date
    Feb 2012
    Posts
    2
    Rep Power
    0

    Default Re: ¿Values by Reference? I dont understand...

    Thanks Norm !!

    With your explication, now I understand the difference ;).

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default Re: ¿Values by Reference? I dont understand...

    You're welcome.

Similar Threads

  1. HELP ...DONT UNDERSTAND Error
    By ask4soteria in forum New To Java
    Replies: 6
    Last Post: 11-26-2010, 10:43 AM
  2. Replies: 3
    Last Post: 07-28-2010, 03:49 PM
  3. 8 questions I dont understand while studying for SCJP
    By shankhas in forum Java Certification
    Replies: 5
    Last Post: 05-19-2010, 08:53 AM
  4. Dont understand Return Statement.
    By ocean in forum New To Java
    Replies: 6
    Last Post: 10-22-2009, 01:06 PM
  5. Data Files - A problem that I dont understand :D
    By Exhonour in forum New To Java
    Replies: 7
    Last Post: 01-20-2009, 06:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •