What is call by value and call by reference in java with example?

CALL BY VALUE: 


Look at first example:



class Hello          
{
void disp(int i,int j)
{
i*=2;
j/=2;
}  
}


class T7
{
public static void main(String as[])
{
int a=99;
int b=77;

System.out.println("before call");
System.out.println("a="+a);
System.out.println("b="+b);

Hello h=new Hello();
h.disp(a,b);
System.out.println("AFTER call");
System.out.println("a="+a);
System.out.println("b="+b);

}
}

OUTPUT:

before call
a=99
b=77
AFTER call
a=99
b=77


When you pass a simple type to a method, it is passed by value. Thus what occurs to the parameter that receives the argument has no effect outside the method. Here the operation occur inside disp() have no effect on the values of a and b used in the call. Their values didn't change to 198 and 38.

CALL BY REFERENCE:



class Hello
{
int a,b;
Hello(int i,int j)
{
a=i;
b=j;
}
void disp(Hello o) //pass an object
{
o.a*=2;
o.b/=2;
}
}

class T6
{
public static void main(String as[])
{
Hello h=new Hello(15,20);
System.out.println("before call");
System.out.println("a="+h.a);
System.out.println("b="+h.b);
h.disp(h);
System.out.println("AFTER call");
System.out.println("a="+h.a);
System.out.println("b="+h.b);

}
      }

OUTPUT:
before call
a=15
b=20
AFTER call
a=30
b=10



When you pass an object to a method the situation change dramatically. Because objects are passed by Reference. When you creating a variable of a class type ,  you are only creating a reference to an object. change to the object inside the method do effect the object used as an argument. When object reference is passed to a method, the reference itself is passed by use of call-by-value.



 


13 comments :

  1. I can do this program without passing reference object.
    class Hello
    {
    int a,b;
    Hello(int i,int j)
    {
    a=i;
    b=j;
    }
    void disp() //pass an object
    {
    a*=2;
    b/=2;
    }
    }

    class T6
    {
    public static void main(String as[])
    {
    Hello h=new Hello(15,20);
    System.out.println("before call");
    System.out.println("a="+h.a);
    System.out.println("b="+h.b);
    h.disp();
    System.out.println("AFTER call");
    System.out.println("a="+h.a);
    System.out.println("b="+h.b);

    }
    }

    ReplyDelete
    Replies
    1. Indeed and only thing which confused is how object is modified in Java. Here is one example which clears my doubt on Java pass by value and pass by reference

      Delete
    2. Krunch and Munch

      Delete
    3. for your information java does not support call by reference but it create clone of java

      Delete
    4. call by reference possible when we perform address(pointer)based operation

      Delete
  2. Then why we are use reference object passing as a parameter.

    ReplyDelete
  3. How to pass a method in one class to another class in java interface??

    ReplyDelete
  4. thanx for simple example

    ReplyDelete
  5. As far as I know Java supports "Pass By value" only.
    Explain.?

    ReplyDelete
  6. Your blog is absolutely worth to read if anybody comes throughout it. I'm lucky I did because now I've got a whole new view of this. I didn't realize that this issue was so important and so universal. You certainly put it in perspective for me.

    www.imarksweb.org

    ReplyDelete
  7. 精力剤:http://www.zzleshirts.com
    RU486:http://www.zzleshirts.com/p59.html
    巨人倍増:http://www.zzleshirts.com/p2.html
    D10:http://www.zzleshirts.com/p74.html
    D10媚薬:http://www.zzleshirts.com/p74.html
    D10催情剤:http://www.zzleshirts.com/p74.html
    淫インモラル:http://www.zzleshirts.com/p651.html
    インモラル:http://www.zzleshirts.com/p651.html
    紅蜘蛛:http://www.zzleshirts.com/p35.html
    レディーチャイナ:http://www.zzleshirts.com/p293.html
    威哥王:http://www.zzleshirts.com/p250.html
    花痴:http://www.zzleshirts.com/p28.html
    ハナチ:http://www.zzleshirts.com/p28.html
    絶對高潮:http://www.zzleshirts.com/p1.html
    絶對高潮カプセル:http://www.zzleshirts.com/p1.html
    魔鬼天使性欲粉:http://www.zzleshirts.com/p348.html
    Muira Puama:http://www.zzleshirts.com/p348.html
    中華牛鞭:http://www.zzleshirts.com/p60.html
    FLY D5:http://www.zzleshirts.com/p293.html
    D5原液:http://www.zzleshirts.com/p292.html
    蒼蝿水:http://www.zzleshirts.com/p293.html
    花之欲:http://www.zzleshirts.com/p26.html
    美人豹:http://www.zzleshirts.com/p338.html
    妻之友:http://www.zzleshirts.com/p357.html
    福源春:http://www.zzleshirts.com/p372.html
    福源春カプセル:http://www.zzleshirts.com/p372.html
    催情丹:http://www.zzleshirts.com/p411.html
    D8 催情剤:http://www.zzleshirts.com/p170.html
    蟻王:http://www.zzleshirts.com/p206.html
    アリ王:http://www.zzleshirts.com/p206.html
    淫インモラル:http://www.zzleshirts.com/p651.html
    インモラル:http://www.zzleshirts.com/p651.html
    妖姫:http://www.zzleshirts.com/p650.html
    情愛芳香劑:http://www.zzleshirts.com/c65.html
    芳香劑:http://www.zzleshirts.com/c65.html
    芳香劑rush:http://www.zzleshirts.com/c65.html
    狼1号:http://www.zzleshirts.com/p100.html
    狼一号:http://www.zzleshirts.com/p100.html
    三鞭粒:http://www.zzleshirts.com/p433.html
    三体牛鞭:http://www.zzleshirts.com/p402.html
    三體牛鞭:http://www.zzleshirts.com/p402.html
    新一粒神:http://www.zzleshirts.com/p429.html
    五便宝:http://www.zzleshirts.com/p238.html
    三便宝:http://www.zzleshirts.com/p288.html
    蟻力神:http://www.zzleshirts.com/p128.html
    VigRx:http://www.zzleshirts.com/p198.html
    vigrxプラス:http://www.zzleshirts.com/p198.html
    ビグレックス:http://www.zzleshirts.com/p198.html
    勃動力三體牛鞭:http://www.zzleshirts.com/p456.html
    蔵八宝:http://www.zzleshirts.com/p430.html
    中華牛鞭:http://www.zzleshirts.com/p60.html
    リドスプレー:http://www.zzleshirts.com/p212.html
    LIDO SPRAY:http://www.zzleshirts.com/p212.html
    D5 催情剤:http://www.zzleshirts.com/p164.html
    SPANISCHE FLIEGE D5:http://www.zzleshirts.com/p164.html

    ReplyDelete
  8. see in callbyvalue program we using in main class,to declare a and b value, and print it.ok it will print the a and b value.but after object creation you call the method
    h.disp();ok. after than you also print the a and b value.because the value does't change.i want the doubt why did't you will print the value h.a and h.b? because thats way the value entering the method.or else the value only print the main class variable declaration a and b value.

    ReplyDelete
  9. Best one- https://www.youtube.com/watch?v=L94X_orDVWw&t=420s

    ReplyDelete