Java Call By Value
When a method is called by value, a copy of the original argument is passed to the called method. The copied argument occupy separate memory location than the actual argument. If any changes were done to values inside the method, it is only visible inside the method. Their values remain unchanged outside it.
Example 1:-
In this example call by value original value does not changed.
public class Callbyvalue
{
int amt=60;
void interest(int amt){
amt=amt+100;
}
public static void main(String args[])
{
Callbyvalue cv=new Callbyvalue();
System.out.println("Before Interest Amount "+cv.amt);
cv.interest(250);// call by value
System.out.println("After Interest Amount "+cv.amt);
}
}
Example 2:-
In this example we are passing object as a value.
public class Callbyvalue1
{
int amt=60;
void interest(Callbyvalue1 cv){
cv.amt=cv.amt+100;
}
public static void main(String args[])
{
Callbyvalue1 cv=new Callbyvalue1();
System.out.println("Before Interest Amount "+cv.amt);
cv.interest(cv);// passing object as a value
System.out.println("After Interest Amount "+cv.amt);
}
}
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com