[Java] 참조 (1) (feat. 복제)

SeongEon Kim·2022년 6월 9일
0
  1. 복제
package org.opentutorials.javatutorials.reference;
 
public class ReferenceDemo1 {
 
    public static void runValue(){
        int a = 1;
        int b = a;
        b = 2;
        System.out.println("runValue, "+a); 
    }
 
    public static void main(String[] args) {
        runValue();
    }
 
}

결과는 아래와 같다.

runValue, 1

값을 변경한 것은 변수 b이기 때문에 변수 a에 담겨있는 값은 그대로이다. 변수 b의 값에 변수 a의 값이 복제된 것이다.
아래 그림을 확인하면 더 이해가 쉽다.

profile
꿈을 이루는 사람

0개의 댓글