❓전역변수를 제외한 나머지 스코프의 변수 종류는 쪼개져 있나?
참조 범위를 줄여서 여러군데서 접근할 경우 발생 할 수 있는 사이드 이펙트를 줄이기 위해
인스턴스 변수의 경우 객체를 생성하는것이라 메모리 점유를 하기때문에 무분별한 객체생성을 피해야한다.
❓final키워드 없이 상수값을 변수로만 선언했다면 어떤 일이 발생할 수 있나?
멀티스레드 환경에서 상수로 선언하지 않은 경우 다른 스레드에서 접근해서 값의 변경이 일어나 변수가 스레드 세이프 하지않을 수 있다.
→ 꼭 멀티스레드 환경이 아니더라도 런타임 시 값에 대한 변경이 이루어 지지 않았다는것을 보장할 수 없다.
❓wrapper타입은 무엇이고 wrapper 타입의 기본값과 기본 자료형의 기본값은?
기본 타입 데이터를 객체 타입으로 포장하는 클래스
클래스 파일이므로 기본값은 null이다.
primitive type을 사용해야하는 경우라면 primitive type을 사용하는것을 권장한다. wrapper타입을 사용했을 경우 NullPointerException에 대한 위험이 있기때문이다. 또한 primitive type은 원시적인 값을 가지고 연산하고 wrapper타입의 경우 객체의 참조값을 들고있다보니 연산이 불가하다. 그렇다면 autounboxing작업을 진행하게되는데 이 또한 객체를 primitive type으로 한번 더 변환하다보니 원시적인값만 가지고 연산하는것보다 시간이 소요하게 된다.
**The Integer class wraps a value of the primitive type int in an object.**
**An object of type Integer contains a single field whose type is int.**
In addition, this class provides several methods for converting an int
to a String and a String to an int, as well as other constants and methods
useful when dealing with an int.
Integer클래스의 java doc을 봤을때 int → String으로 변환한다는 얘기가 있다. 평소에는 아무생각없이 썼었는데 primitive type → object로 변경하기 때문에 Integer클래스에 구현된거같다.
int intValue = 1;
Integer.toString(intValue);
private static long sum() {
Long sum = 0L;
for (long i = 0; i <= Integer.MAX_VALUE ; i++) {
sum += i;
}
return sum;
}
public static void main(String[] args) {
long start = System.nanoTime();
long x = sum();
long end = System.nanoTime();
System.out.println((end - start) / 1000000 + "ms");//7236ms
System.out.println(x);
}
private static long sum() {
long sum = 0L;
for (long i = 0; i <= Integer.MAX_VALUE ; i++) {
sum += i;
}
return sum;
}
public static void main(String[] args) {
long start = System.nanoTime();
long x = sum();
long end = System.nanoTime();
System.out.println((end - start) / 1000000 + "ms");//797ms
System.out.println(x);
}
Memory Consistency & Memory Visibility