What will be the output when the following program is run?
public class Main{ char c;/*from w w w.ja v a 2 s. c o m*/ public void m1 (){ char [] myArray = { 'a' , 'b'}; m2 (c, myArray); System.out.println(((int)c) + ", " + myArray [1] ); } public void m2 (char c, char [] myArray){ c = 'b'; myArray [1] = myArray [0] = 'm'; } public static void main (String args []){ new Main ().m1 (); } }
Select 1 option
Correct Option is : C
Arrays are Objects (i.e. myArray instanceof Object is true) so are effectively passed by reference.
So in m1 () the change in myArray [1] done by m2 () is reflected everywhere the array is used.
c is a primitive type and is passed by value.
In method m2 () the passed parameter c is different than instance variable 'c' as local variable hides the instance variable.
So instance member 'c' keeps its default (i.e. 0) value.