공부/기본지식

[Java] Primitive type & Reference type

porkbellyYam 2023. 3. 15. 22:50

원시타입은 값 자체를 복사하기 때문에, 원본 데이터의 값이 바뀌더라도 기존 데이터의 값을 유지한다.

let origin = 100;
let copy = origin;

console.log(copy); // 100

origin = 200;
console.log(copy); // 100

참조 타입은 주소 값을 참조하기 때문에, 원본 데이터의 값이 바뀌면 복사한 데이터의 값도 변경된다.

let origin = {
	name: 'Jinny'
}
let copy = origin;

console.log(copy.name); // Jinny

origin.name = 'Mr.Lee';

//origin과 copy는 동일한 주소값을 참조하기 때문에 같은 객체를 나타낸다.
console.log(copy.name); // Mr.Lee

 

출처: https://cocobi.tistory.com/155