Pass by Value vs. Pass by Reference

p.p1 { margin: 0; font: 12px “Helvetica Neue”; color: rgba(69, 69, 69, 1) }
p.p2 { margin: 0; font: 12px “Helvetica Neue”; color: rgba(69, 69, 69, 1); min-height: 14px }
p.p3 { margin: 0; font: 12px “Helvetica Neue”; color: rgba(228, 175, 10, 1) }

Pass by Value vs. Pass by Reference

 

https://www.youtube.com/watch?v=BHtfb3lfc-g

 

https://www.youtube.com/watch?v=h9uD7ipqu3w

 

https://www.youtube.com/watch?v=N8IeMYsdgAY

 

 

Java always pass by value

Value is the copy

Reference is the original address in the memory

 

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).

 

 

 Stack + Heap with Reference & Instance Variables & local variables 

https://www.youtube.com/watch?v=UcPuWY0wn3w

 

 

https://www.youtube.com/watch?v=I6yo9V5nvPw

 

Stack: primitive type, reference of var

 

Heap: object 

Class Car{

 Int hp;

}

Car myCar;

myCar = new Car();

 

my2Car = new Car();

my2Car.hp = 120;

 

 

myCar is the reference var , the address or the reference var is in the stack 

The object new Car() is in the heap

Garbage collection runs on the heap. clean objects which has no reference points to 

Them, this saves memory for future 

 

my2Car() is a reference var which has the 

Address of the object new Car()

 

The object new Car() resides on the heap

And it has an instance variable hp 

Also resides on the heap inside of the new Car()

 

Now the instance  var we have seen is a primitive type 

 

22:00

Confusing part when we have a non primitive

Type as instance var

 

 

 

 

 

 

 

Java for beginner playlist 

https://www.youtube.com/watch?v=bwUWEioRlgE&list=PLlxmoA0rQ-LzrFmbcHy2vUYIr9ZRo83yv

Start from # 9.1

版权声明:本文为tobeabetterpig原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tobeabetterpig/p/9451048.html