알아두면쓸데있는신기한잡학사전/고군분투흔적들
[Java] String을 int로 변환하는 법/int를 String으로 변환하는 법
대범하게
2022. 7. 27. 11:07
반응형
String을 int로 변환하는 법
Integer.parseInt(String값)
자바에서 String 자료형을 int 자료형으로 바꾸고 싶을 때가 있다.
이때 Integer.parseInt(str) 함수로 String을 int로 변환한다.
str 부분에 원하는 스트링 값을 넣어주면 된다.
String str = "99";
int i = Integer.parseInt(str);
System.out.println(i);
//결과
//99
int를 String으로 변환하는 법
방법1)
String.valueOf(int값)
자바에서 int 자료형을 String 자료형으로 바꾸고 싶을 때가 있다.
이때 String.valueOf(num) 함수로 int를 String으로 변환한다.
num 부분에 원하는 int값을 넣어주면 된다.
int i = 99;
String str = String.valueOf(i);
System.out.println(str);
//결과
//99
방법2)
Integer.toString(int값);
int i = 99;
String str = Integer.toString(i);
System.out.println(str);
//결과
//99
방법2)
int + ""
문자열에 int를 이어붙이면, 문자열이 리턴이 된다.
public class IntToString {
public static void main(String[] args) {
int intValue1 = 123;
int intValue2 = -123;
String str1 = intValue1 + "";
String str2 = intValue2 + "";
System.out.println(str1);
System.out.println(str2);
}
}
// 결과
// 123
// -123
반응형