Java Tutorial - Passing by Value (Object References)
ฝัง
- เผยแพร่เมื่อ 26 ก.ค. 2024
- Passing by Value (Object References).
In an earlier video, I discussed a common difficulty that every beginner Java programmer encounters - does Java pass primitive data variables (i.e. int, float, char, boolean, etc) to method parameters by value or by reference. Recall Java always passes by value (passes a copy) and never by reference (passes the original).
In this video, I explain using examples how Java passes object references by value (that is to say - passes a copy of the object variables, arrays, etc) . In particular, I illustrate in detail how the passing by value of object references can exhibit behavior that is very similar to passing by reference (i.e. passing the original object memory address).
I hope you find this video beneficial. Please leave any comments or feedback in the comments section and I will respond as soon as I can. Thank you.
WATCH SOME MORE JAVA TUTORIALS
Enhanced For Loop Explained and Illustrated
• Java Tutorial - Enhanc...
For Loops Explained and Common Pitfalls Highlighted
• Java Tutorial - For Lo...
How to use the Shortcut Operators
• Java Tutorial - How to...
How to use the Conditional Operator ( ? : )
• Java Tutorial - How to...
Compare Objects - Equality Operator Vs Equals Method
• Java Tutorial - Compar...
Command Line Arguments
• Java Tutorial - Comman...
How To Write A Method Part II
• Java Tutorial - How To...
How To Write A Method Part I
• Java Tutorial - How To...
Passing by Value (Primitive Data Types)
• Java Tutorial - Passin...
Division and Modulo Operators Explained
• Java Tutorial - Divisi...
Operator Precedence Explained
• Java Tutorial - Opera...
What are Data Types, Variables and Constants?
• Java Tutorial - Introd...
How to use Nested If Statements
• Java Tutorial - How to...
How to use simple If Statements
• Java Tutorial - How to...
What are Control Flow Statements
• Java Tutorial - What a...
How to Write, compile and run your first Java program on Windows
• Java Tutorial - How to...
How to write, compile and run Java programs all within Notepad++
• Java Tutorial - How to...
How to download and install the Notepad++ editor on Windows
• How to download and in... - วิทยาศาสตร์และเทคโนโลยี
This 2015 video is better than a number of tutorials in 2021. Thx!!
Thank you :)
I have been banging my head to understand this for last hour but you explained it so simply. Now I understand why java is only said to have passing by value.
You're very welcome :)
this is the best video on youtube. if you can't understand it now, leave programming, leave earth, leave this universe.
I am humbled - thank you :)
you have successfully explained what is called passing by reference for decades and then you name it passing by value.
Java is passing by sharing "Thus the behaviour of call by value Java or Visual Basic and call by value C or Pascal are significantly different: in C or Pascal, calling a function with a large structure as an argument will cause the entire structure to be copied and mutations to the structure are invisible to the caller. However, in Java or Visual Basic only the reference to the structure is copied and mutations to the structure are visible to the caller."
Excellent! I think this is the best explanation given for passing objects as parameters and passing those by value!
+RomanNagel
Thanks for the positive feedback :)
Last 5 minutes of your video exactly explains why we get confused with pass by value and pass by reference in java while dealing with objects. Good job man! Thank you !
You are very kind - thank you :)
Thank you so much!
5 years later and still very helpful!
Glad it helped!
Sir I really appreciate your effort. You made it look so easy. My concept is clear now.
Thanks for making this video!!!
+Rohit Jaiswal Thank you and you are welcome! :)
wow at last I got it. here it is: when passing object types you are not passing the data of the object but the address of the object. so you are passing a copy of the reference address of the object.
Thanks for clearing it up, You've done a better job at explaining this than all these other Java tutorials that somehow expect you to understand this stuff automatically.
You're very welcome! :)
At last somebody explains it plain and clear!. I read and read about this but never understood it quite clear. Now I realize I had the concept all the way round. Thank you!
You're welcome - thank you for the feedback :)
This is a great, great tutorial and explains what it should in the best possible way. It is a shame that you have only couple of tutorials about java and not much more because you could be one of the best java lecturer. Thanks for making this video and of course like, share, subscribe...
Best video on youtube to explain this concept!
Wow, thank you!
Simple and Clear explanation. Thank you!
Martin you explained this so well, very much appreciated!!!
I finally understood without getting confused what this is about. Thank you so much for this.
Love this tutorial. It is helpful. I have ADHD, however, and there are just a few too many commercial breaks for me to focus. Luckily I can watch this several times and take notes.
Thank you.
Such a good explanation of this. I was baffled before and now I'm confident I could explain this to someone new. Thanks a ton!
That's great - glad I could help
Excellent explanation, thank you! If you're still making videos and want to expand, I think an in depth video explaining deep copying would be greatly appreciated.
The explanation was clear and concise in the best possible way. Well done.
That visual makes it so much easier to learn!!
WoW. Thank soooooooooooooo much. Just learning Java, watched many videos but was more confused with Pass by value and Pass by reference. Your video is OUTSTANDING, CLEAR AND WELL EXPLAINED.
I am delighted my video could be of help. :)
The best explanation on the internet, this is amazing. Thank you!
You're very welcome!
You're a hero. I have a CS midterm tom. This was a great watch!
Thank you Nikhil! I am glad I could be of some help :)
This was the best explanation I have watched. Thank you for making this so simple
Glad it was helpful!
This video is very easy to understand the concept. Thanks for this video
Brilliant vid, thanks Martin!
Did a great job in the Java interview i've had today thanks to you
I am delighted to hear that. I wish you every success. :)
Amazing explanation! Appreciated!
you have explained it in best possible way. thanks :-)
Beautifully explained!
Excellent tutorial and the explanation is great!
Thank you very much from Brazil. ...very straightforward...
Thank you :)
Wow, you are asome, the best explanation I have ever seen, congratulations and thank you very much, finally after some time I understood how to pass an object in a method
You are welcome! :)
Such a great video ! I really appreciate it . Thank you !
+boris runakov
You are welcome! :)
one of the better tutorials (have not seen a better one yet) on this topic.
Thank you :)
You have made it clear for me. Thank you for your excellent and brief presentation.
You're welcome :)
Very simple and clear explanation, thank you very much sir!
Glad it was helpful!
That was really helpful! Thanks
Great example. Thanks.
Thank you. really clear. i don't why other post don't directly point out passing by value for object means passing address.
+Yichen Lin
Delighted that my videos could be of some help. :)
Because it doesn't mean that. Passing by value means copying, it just happens that what you copy is the bits of the value of the reference in java. If it's a primitive data type these bits contain the value of the primitive data type itself
Very clear explanation. Thank you!
You're welcome :)
I love your videos. Please, can you make more videos on object references with more than 2 classes.
Thanks so much for the explanation, very clear/descriptive and now I understand.
Cheers :)
thank you very very very much. now everything is cleared to me.
Perfectly explained. Thanks very much.
Glad it was helpful! :)
thanks...very simple & clear explanation...
very good mate!! i got the idea for the first time .
+LawZist
Thank you :)
Wowwww! Such an amazing explanations. This video has blown my brain! You are incrediable teacher. I wish you could make videos on Java. Thanks
Thank you :)
Mate, you have explained it very clearly. Thank you
You're welcome! :)
The best explanation on this topic! Thanks
Thank you. You're very welcome.
Great explanation! I appreciate your effort.
You are very welcome - thank you for the feedback. :)
Thanks for the awesome tutorial
Thanks for this tutorial. Very clear.
Thank you.
Really good video thank you.
Very good explained! Thx!
Thank you Mohai for the feedback. :)
Wow awesome explanation thank you
Great explanation...
Awesome.. your way of explaining.. concept cleared...
Cheers :)
素晴らしい!
ありがとうございました (with the help of Google Translate) :)
Very good explanation ....you helped me a lot
thanks bro finally i understood it
fantastic video and a great way of teaching, thanks for all the help
You're very welcome! :)
Great explanation!
Glad it was helpful!
Excellent job explaining this topic
Glad you liked it :)
Good explaination, I think the reason it looks quiet like pass by reference is that changeDetails method is actually changing the memory
Wow beautifully explained. 🏆🏆🏆🏆🏆
Thank you 🙂
This is great. Thank you so much
Glad you enjoyed it!
Great explanation. Thanxs :-)
+ceanorme
Thank you - greatly appreciated! :)
Great explanation Sir!
Glad it was helpful!
Thank you , now is clear to me.
Glad I could help.
It was amazing thank you for your help.
You're very welcome! :)
great explanation
thank you! bless your heart
Thank you and God Bless. :)
Thank you very much.
Perfect explanation
Thank you :)
Legend! Thankyou!
Glad I could be of help :)
thank you!
Niecely done!
Bless you man!
and you too! :)
Thank you sir, this helped a lot :)
You're welcome :)
Excellent explanation
Thank you.
thank you very much
EXCELLENT- super
shamin mohammed Thank you :)
thank you very much beautiful explanation. sir can you do tutorial about oop concepts and collections in java ?
Interview question! thanks
great video really enjoyed it, is it possible you could edit your videos with numbers and put them in order so i can go through them as though its like a step by step course? i find your way of teaching far better to understand than most other videos i have seen so would like to learn what java i can from you! thank you
great tutorial.
You're welcome :)
thank you very much!!!
You're welcome!
Well explained great sir😁😊
Thank you! :)
Thank you Sir.
You're very welcome.
Thank you sir!!
Cheers. :)
Thanks for video
Most welcome
Sir, i have a question.
Did the person1 and person2 store in the Stack memory and the values of them are the address refer to the same Object store in the Heap memory?
thank you ,,
woow its realy helpfull
You're very welcome :)
thank sir !
You are welcome :)
Excellent, you need to know C++ (specificly shared_ptr) to understand Java 'references'. Everything boils down to C and C++ I guess...
How is passing by the "value of a reference" considered to be passing by value, instead of passing by reference? It seems to me that this IS passing by reference. Wouldn't passing by value mean to copy the data state of person1 to person2 at that point in the program, so that they are two different objects and would reflect different data when manipulated differently?
I mean I understand what is happening, I just don't get why copying memory addresses doesn't qualify as a pass by reference when the memory address is what is used to reference an object.
Thank you
Glad to hear it helped. :)