+ 5

Why I am getting false??

Array are passed by reference , so actual parameters should also change , but check out this code. https://code.sololearn.com/cRaqEON09Lcr/?ref=app

2nd Feb 2018, 8:25 AM
Mr.Curious
Mr.Curious - avatar
10 Answers
+ 21
//👉1) class B { public static void main(String[]args){ int a1[] ={1,4,5}; int a2[]={1,6,7}; for(int a=0;a<3;a++) a2[a]=a1[a]; System.out.println(a2[0]+" "+a1[0]+"\n"+a2[1]+" "+a1[1]+"\n"+a2[2]+" "+a1[2]+""+(a1==a2)); } } /*In this corresponding elements of both arrays are same , but reference will be different bcz both are different objects ... so a1==a2 will return false*/ //👉2) class B { public static void main(String[]args){ int a1[] ={1,4,5}; int a2[]={1,6,7}; for(int a=0;a<3;a++) a2[a]=a1[a]; System.out.println(a2[0]+" "+a1[0]+"\n"+a2[1]+" "+a1[1]+"\n"+a2[2]+" "+a1[2]+""+(a1==a2)); } } /*here the reference will be same , so a1==a2 will return true //👉3) class B { static void m(int a1[], int a2[]){ a2=a1; System.out.println(a2[1]+" "+a2[1]); } public static void main(String[]args){ int a1[] ={1,4,5}; int a2[]={1,6,7}; m(a1, a2); System.out.print(a1[1]+" "+a2[1]+(a1==a2)); } } /*here , if u will print the elements , then after the method m (a1,a2) , in main method , then u see that corresponding elements will not be equal ... so a1==a2 will return false */ //👉4) //how abt this class B { static void m(int a1[], int a2[]){ for(int a=0;a<3;a++) a2[a]=a1[a]; System.out.println(a2[0]+" "+a1[0]+"\n"+a2[1]+" "+a1[1]+"\n"+a2[2]+" "+a1[2]); } public static void main(String[]args){ int a1[] ={1,4,5}; int a2[]={1,6,7}; m(a1,a2); System.out.println(a2[0]+" "+a1[0]+"\n"+a2[1]+" "+a1[1]+"\n"+a2[2]+" "+a1[2]); } } /*here corresponding elements of a1 and a2 will be equal , but since both are different objects & having different reference ... again the result will be false*/ /*dhyan se dekh lo 3rd wale ko , isme corrsponding elements barabar nahi honge , assign karna padhta ha ... jaise 4th wale ma kiya ha loop chala kar*/
5th Feb 2018, 5:28 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 18
//how abt this class B { static void m(int a1[], int a2[]){ for(int a=0;a<3;a++) a2[a]=a1[a]; System.out.println(a2[0]+" "+a1[0]+"\n"+a2[1]+" "+a1[1]+"\n"+a2[2]+" "+a1[2]); } public static void main(String[]args){ int a1[] ={1,4,5}; int a2[]={1,6,7}; m(a1,a2); System.out.println(a2[0]+" "+a1[0]+"\n"+a2[1]+" "+a1[1]+"\n"+a2[2]+" "+a1[2]); } } /*it will make elements of each array equal , but still a1==a2 equates to false , might bcz these are pointing towards different memory location*/
5th Feb 2018, 10:56 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 6
found this.... Everything in Java are passed-by value. . In case of Array(Which is nothing but an Object), array referenceis passed by value.. (Just like an objectreference is passed by value).. When you pass an array to other method, actually the reference to that array is copied..
2nd Feb 2018, 9:36 AM
D_Stark
D_Stark - avatar
+ 6
so basicly the value is the reference to the array
2nd Feb 2018, 9:39 AM
D_Stark
D_Stark - avatar
+ 5
Everything is passed by value in java
2nd Feb 2018, 8:31 AM
D_Stark
D_Stark - avatar
+ 2
But I have read that composite data types are passed by reference, and the value of actual parameter will be changed if there is a change in formal parameter. check out this code :
2nd Feb 2018, 9:45 AM
Mr.Curious
Mr.Curious - avatar
2nd Feb 2018, 10:16 AM
Mr.Curious
Mr.Curious - avatar
+ 2
@ gaurav Then why it will return true if I do a1=a2 in main method while , if I do the same thing by passing in other method , I get false, as in both case ,they should point to same memory location.
5th Feb 2018, 4:53 PM
Mr.Curious
Mr.Curious - avatar
+ 2
Thank a lot bhai, now the concept is crystal clear
6th Feb 2018, 3:07 PM
Mr.Curious
Mr.Curious - avatar
+ 1
no frnd
2nd Feb 2018, 9:21 AM
Mr.Curious
Mr.Curious - avatar