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?
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.
0
Avinesh In this case it is an array of int integers (primitive) and not Integer objects (object). I'm still confused...



