[자바 실수] String과 int 간 형변환

오서영·2022년 5월 3일
0

JAVA 실수 모음

목록 보기
10/10
post-thumbnail

목표

Run Configuration의 argument로 입력받을 때 String[] args를 통해 String값으로 전달받는다. 이를 int형으로 받는 게 목표였다.

실수

처음에는

int a = (int)args[0];

과 같이 형변환하려고 했다. 결과적으로 오류가 떴다.

잘못된 이유

String은 int나 float와 같은 기본 타입이 아니다. 그렇기 때문에 다른 방법을 써야 한다.

해결

String to int

int a = Integer.parseInt(args[0]);

int to String

String str = Integer.toString(a);
profile
개발과 보안에 관심 있는 대학생입니다😎

0개의 댓글