0

Why this problem print 5?

public static void main(String[] args) { int[]a={4,5,7}; inc(a); System.out.print(a[0]); } static void inc(int[] i){ i[0]++; } “In Java, variables of primitive types (int, float, boolean, etc.) are passed by value and not by the address of the variable when we talk about methods. When you pass a variable of primitive type to a method, you can be sure that the value of the variable will not be changed by the method.” If this is true, then why does the value of position 0 increase through a method?

15th Jun 2020, 5:06 PM
Juan Debenedetti
Juan Debenedetti - avatar
3 Réponses
+ 1
Your statement regarding primitives is true but arrays are objects in Java so the array reference is passed by value. Hope that answers the question.
15th Jun 2020, 6:12 PM
Avinesh
Avinesh - avatar
0
Avinesh In this case it is an array of int integers (primitive) and not Integer objects (object). I'm still confused...
15th Jun 2020, 10:59 PM
Juan Debenedetti
Juan Debenedetti - avatar