#12 Object Oriented Java Tutorial: Call by Value vs Call by Reference

///#12 Object Oriented Java Tutorial: Call by Value vs Call by Reference

#12 Object Oriented Java Tutorial: Call by Value vs Call by Reference

FavoriteLoadingAdd to favorites

Access 7000+ courses for 15 days FREE:

Java468x60

Java tutorial for beginners where I will talk about What is CALL BY VALUE and CALL BY REFERENCE, importance, significance and how does it takes place. Detail difference has been covered along with demo code.

Source code files:

Next Video :
Previous Video :

Check out my website:

Donate and support us
[Only if you think our courses are better than Udemy or Udacity]
➤ Patreon:
➤ Paypal/Payoneer: sriyank123@gmail.com
➤ UPI (only for India): smartherd@okaxis

Programming courses:
➤ Ruby Programming:
➤ Dart Programming:
➤ Kotlin Programming:
➤ Java Programming:

Flutter course:
➤ Flutter App Development:

Android courses:
➤ Android using Kotlin:
➤ Android using Java:
➤ Android Material Design:
➤ Android Jetpack Architecture:
➤ Android Multiple Screen Support:
➤ Android Retrofit:

More programming courses:

Follow us on
Facebook:
Twitter:
Github:

—- Thank you for your love and support —-

source

By |2020-02-09T01:39:41+00:00February 9th, 2020|Java Video Tutorials|37 Comments

37 Comments

  1. manoj reddy February 9, 2020 at 1:39 am - Reply

    ohh man..explanation is super….I have an Interview tomorrow, What if You comes as a Interviewer and asks this question. I will explain perfectly and I will get a job…..hahaa

  2. Vandana Nayak Nayak February 9, 2020 at 1:39 am - Reply

    thanks! this is helpful

  3. Anupam Sen February 9, 2020 at 1:39 am - Reply

    Very good and simpler way of explanation. Tremendously liked it..

  4. Vimarsh February 9, 2020 at 1:39 am - Reply

    Great video
    Thanks for clearing by confusion!

  5. Sachin Chinnannavar February 9, 2020 at 1:39 am - Reply

    very nice thankyou .i got it

  6. Varun Deoghare February 9, 2020 at 1:39 am - Reply

    Dude… Stop teaching wrong concepts. Java supports only call by value and not call by reference.

  7. Adam February 9, 2020 at 1:39 am - Reply

    thank you! so that means we have to use cloning or make a new object inside the receiving method to change the object values

  8. saloni vaish February 9, 2020 at 1:39 am - Reply

    Can you plz tell why you put the modify() in different block but not in the main method block.

  9. Ramesh Kumar February 9, 2020 at 1:39 am - Reply

    Wrong explanation. Java doesn't support call by reference. Since r1 and r2 are pointing to the same location. That's way the value of r1 also gets 20 on execution.

  10. stark Tshering February 9, 2020 at 1:39 am - Reply

    nice explanation. ☺ thank you👑

  11. Diniș Alexandru-Cătălin February 9, 2020 at 1:39 am - Reply

    The explain is partially correct, and actually it shows that Java only supports pass by value. When sending the parameter to the function, both of them are pointing to the actual object. So what' s changed it's actually the object, not the reference. Try to set the reference to null inside the modify method. If pass by reference, the value of r2 should be null, so you should get a NullPointerException.

  12. jimmy Ryan February 9, 2020 at 1:39 am - Reply

    Great video. Very clear. What I don't understand is any time I look this answer up all the answers I find contradict themselves. They say java ALWAYS passes by value (they emphasise this) and then go onto explain how it passes objects by reference.

  13. Mukesh February 9, 2020 at 1:39 am - Reply

    Java doesn't support call by reference

  14. ujjwal singh February 9, 2020 at 1:39 am - Reply

    Right?

  15. Mi Gene February 9, 2020 at 1:39 am - Reply

    Nice explanation! 👍

  16. Radio Emission February 9, 2020 at 1:39 am - Reply

    Very ,very nice video my friend ….
    You are doing a great job….
    This video is very productive for the begainers ….
    It helped me aswelll…………👌👌👌👌👍👍👍👍👍

  17. Prince Kathait February 9, 2020 at 1:39 am - Reply

    Awesome video sir it make my concept clear

  18. vamshi medishetty February 9, 2020 at 1:39 am - Reply

    What trigger means?

  19. Official website February 9, 2020 at 1:39 am - Reply

    bahut badhiya

  20. Vicky Kumar February 9, 2020 at 1:39 am - Reply

    Confusing

  21. vasu dev upadhyay February 9, 2020 at 1:39 am - Reply

    well explained Sriyank

  22. Sayan NANDY February 9, 2020 at 1:39 am - Reply

    In the last program, why 40 i.e value of r2 came first while S.O.Pln of r1 was first on the program?
    Plz explain me sir

  23. Srinath Nirmal February 9, 2020 at 1:39 am - Reply

    good

  24. PRIYA SURYAWANSHI February 9, 2020 at 1:39 am - Reply

    Excellent

  25. Sukhman Saini February 9, 2020 at 1:39 am - Reply

    Veryyy Helpful

  26. Arun Bakshi February 9, 2020 at 1:39 am - Reply

    Thank you for such nice and easy explanation finally i got the visual representation that i was looking for. Amazing.

  27. Jayakumar Nachimuthu February 9, 2020 at 1:39 am - Reply

    Nice Explanation …

  28. Vishal Shinde February 9, 2020 at 1:39 am - Reply

    Well explained

  29. Sunil Gupta February 9, 2020 at 1:39 am - Reply

    I understood call by value and call by reference concept but please make me clear that what java support call by value or call by reference.

  30. siddharth jain February 9, 2020 at 1:39 am - Reply

    nice video buuoooyyyy

  31. Bogadipreetham Reddy February 9, 2020 at 1:39 am - Reply

    This video is awesome. Keep posting such videos. Its really helpful

  32. Bunny83 February 9, 2020 at 1:39 am - Reply

    While the examples are correct and you correctly stated that "r1" and "r2" are also located on the stack, both of your examples are call by value . The Java language doesn't have the concept of passing a variable by reference. People always confuse "reference types" or "references" with the concept of passing a variable by reference.

    Here's an actual example that shows that the variable is actually passed by value. So inside your method you get a copy of the variable and not a reference:

    Rectangle r1 = new Rectangle();
    r1.length = 10;
    modify(r1);
    System.out.println(r1.length); // prints "10"

    void modify(Rectangle r2)
    {
    r2 = new Rectangle();
    r2.length = 20;
    }

    As you can see the variable is actually passed by value*. Because *changing the variable inside the method has no effect on the variable that was passed in. The "value" of r1 is copied into r2. However since Rectangle is a reference type, the value of the variable is just an address, the reference to the actual object on the Heap.

    Again, this is not called pass by reference.

    C# for example does have true reference parameters. So when calling the method you don't copy the value of the variable but instead you pass a reference / pointer to the variable location on the stack.

    //C#
    Rectangle r1 = new Rectangle();
    r1.length = 10;
    modify(ref r1);
    print(r1.length); // prints "20"

    void modify(ref Rectangle r2)
    {
    r2 = new Rectangle(); // we change the variable r2
    r2.length = 20;
    }

    Of course if you're a pure Java programmer you don't really come in contact with the concept of passing by reference (as it's not supported by Java). However calling "using a parameter to access the referenced object" as "call by reference" is just wrong. See wikipedia:

    https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference

  33. Fun Stuff February 9, 2020 at 1:39 am - Reply

    is it for class 12 ip ?

  34. Raghav Narang February 9, 2020 at 1:39 am - Reply

    Confused 😓

  35. someoneintheshadow45 February 9, 2020 at 1:39 am - Reply

    Thank you!!!

  36. insane karma February 9, 2020 at 1:39 am - Reply

    good useful……better used…..best ever…….

  37. HallivanGalli ( ͡° ͜ʖ ͡°) February 9, 2020 at 1:39 am - Reply

    Very helpful ! Thanks.

Leave A Comment

*