+ 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
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*/
+ 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*/
+ 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..
+ 6
so basicly the value is the reference to the array
+ 5
Everything is passed by value in java
+ 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 :
+ 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.
+ 2
Thank a lot bhai, now the concept is crystal clear
+ 1
no frnd