Skip to content

[6주차] 이예림_item 44#32

Merged
023-dev merged 2 commits intoeffective-java-3e-study:mainfrom
yerim123456:main
Feb 20, 2025
Merged

[6주차] 이예림_item 44#32
023-dev merged 2 commits intoeffective-java-3e-study:mainfrom
yerim123456:main

Conversation

@yerim123456
Copy link
Copy Markdown
Contributor

No description provided.

@yerim123456 yerim123456 changed the title [7장] [6주차] 이예림_item 44 Feb 6, 2025
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분이 잘 이해가 되지않아서, 혹시 예시 같은 것을 통해 좀 더 추가 설명가능할까요?

Copy link
Copy Markdown
Contributor Author

@yerim123456 yerim123456 Feb 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

public static void main(String[] args) {
   Function<Integer, Integer> square = x -> x * x; // Integer → Integer

   int result = square.apply(5);
   System.out.println(result);
}

해당 예시를 참고해봐주시면 좋을 것 같습니다. 지금 입력값(Integer x)을 받아서 x * x를 계산한 뒤 Integer로 반환하는 함수 객체를 만들고, square.apply(5) 를 호출하였습니다.

  1. int 값으로 들어간 5는 Integer로 오토박싱
  2. 연산 수행(x * x) 시, Integer이 언박싱 되어 다시 int로 변환
  3. 다시 오토박싱 되어 Integer(25) 결과 출력

따라서 박싱과 언박싱이 불필요하게 여러 번 일어나서 성능이 저하될 가능성이 있다고 합니다.
덕분에 저도 더 상세히 생각해 볼 수 있었습니다. 질문해 주셔서 감사합니다:)

@023-dev 023-dev merged commit ce608a9 into effective-java-3e-study:main Feb 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants