Pass by Value vs. Pass by Reference

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

猜你喜欢

转载自www.cnblogs.com/tobeabetterpig/p/9451048.html