[JAVA] int <-> String 변환

박진서·2023년 5월 22일

JAVA

목록 보기
14/15

1. int를 String으로 변환

 int intValue1 = 123;
 int intValue2 = -123;
 
 // 1. Integer.toString()
 String str1 = Integer.toString(intValue1);
 String str2 = Integer.toString(intValue2);
 
 // 2. String.valueOf()
 String str1 = String.valueOf(intValue1);
 String str2 = String.valueOf(intValue2);
 

2. String을 int로 변환

String str1 = "123";
String str2 = "-123";

// 1. Integer.parseInt()
int intValue1 = Integer.parseInt(str1);
int intValue2 = Integer.parseInt(str2);

// 2. Integer.valueOf().
int intValue1 = Integer.valueOf(str1).intValue();
int intValue2 = Integer.valueOf(str2).intValue();

3. 꿀팁

// 문자열 + 숫자 = 문자열로 계산
int aLong = Integer.parseInt(""+a+b);
profile
백엔드 개발자

0개의 댓글