References(Pointers) in JAVA. Applying reference variables

There is a task: To implement a function that reads a long number. I did everything, but I just need to solve it with the help of links.That is, so that instead of char[] s and int[] a there are links and their value changes. In C++, it looks like this void ReadLong(char *s,int *a); And how to do it in java?

public static int size;

    public static void ReadLong(char[] s,int[] a) {
        int cnt;
        if (s.length%4==0) {
            cnt=s.length/4;
        }
        else {
            cnt = (s.length+4-s.length%4)/4;
        }

        a[0]=cnt;
        int num=1;
        int ost=s.length%4;
        int i=0;
       while (s.length-i>ost) {
            a[num] = Character.getNumericValue(s[i]) + Character.getNumericValue(s[i + 1]) * 10 + Character.getNumericValue(s[i + 2]) * 100 + Character.getNumericValue(s[i + 3]) * 1000;
            i+=4;
            num++;
        }
        switch (ost){
            case 1:
                a[num]=Character.getNumericValue(s[i]);
                break;
            case 2:
                a[num]=Character.getNumericValue(s[i])+Character.getNumericValue(s[i+1])*10;
                break;
            case 3:
                a[num]=Character.getNumericValue(s[i])+Character.getNumericValue(s[i+1])*10+Character.getNumericValue(s[i+2])*100;
                break;
            default:break;
        }
        for (int j=1;j<=cnt;j++) System.out.println(a[j]);
        }


    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
       String s=in.nextLine();
       StringBuilder S = new StringBuilder(s);
       S.reverse();
       char[] ss = S.toString().toCharArray();
        int cnt = s.length()/4+2;
        size=cnt;
        int[] a = new int[cnt];
        ReadLong(ss,a);
    }

}
Author: post_zeew, 2017-12-03

1 answers

All arguments in Java are passed to when called by the value.

When a reference to an object is passed to the method, the reference itself is passed the method of calling by the value. But since the passed value refers to an object, a copy of that value will still be referenced to the same object as the corresponding argument.


That is, so that instead of char[] s and int[] a there are links and they are changed value

In this case, copies of the references are passed to the method. When you change these arrays in the method, these changes will be visible from the outside (You can check this yourself by displaying the array elements before and after to the console).

It is not possible to pass the parameter when calling by reference in Java.

Example 1:

public class Main {

    public static void main(String[] args) {
        int[] array = new int[3];

        for (int i = 0; i < array.length; i++) {
            array[i] = 1;
        }

        printArray(array);

        System.out.println("changeArray");

        changeArray(array);

        printArray(array);
    }

    private static void printArray(int[] array) {
        System.out.println(Arrays.toString(array));
    }

    private static void changeArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = 2;
        }
    }
}

Output to the console:

[1, 1, 1]
changeArray
[2, 2, 2]

From this example, you can see that after changing the array using the changeArray(...) method, these the changes are visible in the main(...) method.

Example 2:

public class Main {

    public static void main(String[] args) {
        int[] array = new int[3];

        for (int i = 0; i < array.length; i++) {
            array[i] = 1;
        }

        printArray(array);

        System.out.println("changeArray");

        changeArray(array);

        printArray(array);
    }

    private static void printArray(int[] array) {
        System.out.println(Arrays.toString(array));
    }

    private static void changeArray(int[] array) {
        array = new int[3];
        for (int i = 0; i < array.length; i++) {
            array[i] = 2;
        }
    }
}

Output to the console:

[1, 1, 1]
changeArray
[1, 1, 1]

This example clearly demonstrates the fact that the argument is passed when called by value (and not when called by reference). If the array was passed when called by reference, the change in the reference in the changeArray(...) method would be visible in the main(...) method, but it is not visible, since the copy of the reference to the array was changed in thechangeArray(...) method.

 4
Author: post_zeew, 2017-12-03 11:39:22