[JAVA - 점프 투 자바 #11]

형 변환과 final

형 변환

  • 문자열을 숫자열로 변환
    public class Sample {
      public static void main(String[] args) {
          String num = "123";
          int n = Integer.parseInt(num);
          System.out.println(n);  // 123 출력
      }
    }
    
    public class Sample {
      public static void main(String[] args) {
          int n = 123;
          String num = "" + n;
          System.out.println(num);  // 123 출력
      }
    }
    

final

자료형에 값을 단 한 번만 설정할 수 있게 강제하는 키워드.

public class Sample {
    public static void main(String[] args) {
        final int n = 123;  // final로 설정하면 값을 바꿀 수 없다.
        n = 456;  // 컴파일 오류 발생
    }
}
import java.util.ArrayList;
import java.util.Arrays;

public class Sample {
    public static void main(String[] args) {
        final ArrayList<String> a = new ArrayList<>(Arrays.asList("a", "b"));
        a = new ArrayList<>(Arrays.asList("c", "d"));  // 컴파일 에러 발생
    }
}

카테고리: ,

태그:

업데이트: