루프에서 변수 선언 위치에 대한 궁금중
결론부터 말하자면
단순 지역 변수 선언의 경우 스코프 범위가 적게 하도록 루프안에 선언. 루프 안에서 new class()등을 통한 객체 생성이 있을 경우 메모리 효율(GC효율)을 위해 루프 밖으로.
In my conclousion
If you need local variables which only use in loop, recomend declaring variables in loop for reducing scope.
If you make new objects(ex: A a = new A()), recomend declaring objects outside loop for GC perfomance.
본 글에서 미리 정리할 내용
- 변수와 객체의 메모리 할당 차이 (GC)
본 글 이후 추가적으로 정리할 내용
- new () 객체 선언과 클래스의 스태틱 매서드 호출 시 선언 차이
- new () 객체 선언과 지역 변수 메모리 차이(stack, heap 구조 차이)
I will complete this post soon.