public static void main (String[] args) { m1(i1); System.out.print(i1[0] + "," + i2[0]); } } -----------------------------------------------------------------------
Inside m1() int i1 has a method scope, while i2 is not local variable i.e it is not defined anywhere inside the method, so when we initialize i2 with the reference of i1, i2 starts pointing to i1. That means now both i1 and i2 are refering to the same object. So the Output comes out to be:
Code: ----------------------------------------------------------------------- public class Casting21 { static int[] i1 = {1}, i2 = {3}; //1
static void m1(int[] i1) {//creates a local variable i1.contains value {1) int[] i3 = i1; //class variable i3 now contains value {1} i1 = i2;// Variable hiding occurs and local variable i1 is assigned to value of i2-{3}. i2 = i3; //class variable i2 is assigned i3's value which is {1} }
public static void main (String[] args) { m1(i1); System.out.print(i1[0] + "," + i2[0]); } } -----------------------------------------------------------------------
Hello,The Answer Is Correct. I'll Tel U One Trick 2 Solve This Type Of Problems. Let, Object Reference Being Passed obj Is Same As Local Reference obj Then Olways Consider Original Argument As 'obj .Now Do Manipulation.Now Outside Method If U Wanna Access obj Retrieve It As 'obj And Not As obj. This 'll Help U
Agrah Upadhyay 3rd Year B.tech Preparing For SCJP1.4 SASTRA,Tamilnadu
She's brilliant. She can see what can be and is not limited to what is. And she knows this tiny ad:
a bit of art, as a gift, the permaculture playing cards