parseInt()와 intValue()

이승현·2022년 11월 11일
0

parseInt()

<개념>


  • static method
  • Integer을 생성하지 않고 parameter만 넣어주면 메소드를 수행할 수 있음
  • string 객체에서 int형 값을 뽑아내는 메소드

*Integer 생성 안함
== 즉 Integer(Object)라는 박스를 만들지 않고 String -> int 교체

<사용 이유>


  • String형 객체에서 int형 값을 뽑아 내는 메소드
  • 문자형을 정수형으로 만듦

<사용 예제>


int 변수 = Integer.parseInt(숫자값으로 된 String);

String str = "119";
int i = Integer.parseInt(str);
System.out.println(i);
--> i = 119

intValue()

<개념>


  • static method가 아님
  • Integer객체에서 int형 값을 뽑아내는 메소드
  • Integer : int Value와 String Value 두 가지가 있음

<사용 이유>


  • 객체에 내용 값을 정수로 변환하기 위함

<사용 예제>


Integer i = new Integer(150);
i.intValue();

int number = Integer.valueOf(str).intValue();

<정리>

  1. 값 -> 값 형 변환 Integer.parseInt()
  2. 객체 -> 내용물 변환 intValue()

0개의 댓글