Show Mobile Navigation

Webring

Powered by WebRing.

Call by value and Call by reference in Java

Sachin R Kukale - 20:36

Java supports only call by value or pass by value. In call by value you pass the actual values of data to the process. Arguments and primitives are always passed by value in Java.
While passing the object the object reference itself will be passed by value so original reference and copy both are associated to same object. So Java does not support pass by reference.
Java Supports only call by value in which copy of actual arguments is passed to formal argument.
Ex:
class MakeChange{
      int d=50;
      void change(int d){
            d=d+100;    //change will be to local variable.
      }
}
public class CallByV {
      public static void main(String[] args) {
            MakeChange mc=new MakeChange();
            System.out.println("Before change:"+mc.d);
            mc.change(100);
            System.out.println("After Chnage:"+mc.d);
      }
}


0 comments:

Post a Comment