How Array is Passed in Java Functions?



In my post Array , I have explained how Array works. In this post we will see how Array is passed to a function.The technique is called Pass by reference. Java Arrays are passed to the function by reference or as a pointer to the original Array.This means anything we do to the Array inside the function affect the original.
Here is an example how Array elements affects when we pass by reference.

class MyArray{
public static void main(String args[])
{
String b[]={"Love","Lovely","Lovable"};
System.out.println("Before change"+b[0]);
MyArray.passByReference(b);
System.out.println("After change"+b[0]);
}
public static void passByReference(String a[]){
a[0]="Loving";
}
}
 
Output of the code:
Loving
How Array is Passed in Java Functions? How Array is Passed in Java Functions? Reviewed by Animesh Chatterjee on October 17, 2018 Rating: 5

No comments:

Powered by Blogger.