0

I have written a java recursive method and the code is working as expected. But I'm not sure how same method is behaving differently for int and char[] . I'm invoking method1 from main method.

Code 1

void helperMethod(char[] a)
{
    a[0]= 'x';
}

void method1()
{
    char a[] = {'a','b','c'};
    helperMethod(a);

    System.out.println(a[0]);
    System.out.println(a[1]);
    System.out.println(a[2]);
}

Output -

x
b
c

Code 2 -

void helperMethod(int a)
{
    a=3;
}

void method1()
{
    int a =10;
    helperMethod(a);

    System.out.println(a);
}

Output -

10

Why char[] was updated while integer not updated by the helper Method? Is it because I'm passing reference to same object in case of char[] and a copy of object in case of int?

0 Answers0