Need help in Java
can anyone explain me the difference between first two questions?

1 Like

Because Arrays and Objects are “pass by reference” in Java

and Primitives are “pass by value”

In first question, you are passing the array (Object) to the local function and whatever changes you are doing in that function is taking place in the actual object. In main() method, the same object is pointed by both arrays a1 and a2

But In second question, as boolean is primitive variable, the changes made in the local function will not affect the primitive value (of boolean) present in main method.

Please read this for more info :

Reading the above document, makes it more clear.