How Array is Passed in Java Functions?

0
17
Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share


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

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 1
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
    1
    Share

LEAVE A REPLY

Please enter your comment!
Please enter your name here