| 일 | 월 | 화 | 수 | 목 | 금 | 토 | 
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | 3 | 4 | 5 | 6 | 7 | 8 | 
| 9 | 10 | 11 | 12 | 13 | 14 | 15 | 
| 16 | 17 | 18 | 19 | 20 | 21 | 22 | 
| 23 | 24 | 25 | 26 | 27 | 28 | 29 | 
| 30 | 
- input
 - string
 - html
 - CMD
 - 배열
 - Eclipse
 - 인텔리제이
 - 문자열
 - list
 - js
 - 이탈리아
 - table
 - 자바스크립트
 - replace
 - json
 - CSS
 - map
 - Button
 - 정규식
 - IntelliJ
 - javascript
 - 이클립스
 - 자바
 - ArrayList
 - Visual Studio Code
 - HashMap
 - vscode
 - Java
 - date
 - Array
 
- Today
 
- Total
 
어제 오늘 내일
[Java] String을 int로, int를 String으로 변환하기 (문자열 숫자 변환) 본문
String -> int (문자열을 숫자로)
String 문자열을 int로 변환하기 위해서는
java.lang.Integer 클래스의 parseInt()와 valueOf() 메소드를 사용할 수 있습니다.
Integer.parseInt()
static int parseInt(String s)
java.lang.Integer 클래스의 static 메소드인 parseInt() 메소드는
파라미터로 숫자로 변환할 문자열을 입력받고,
입력받은 문자열을 integer로 변환한 int 값을 리턴합니다.
코드
public class StringToInt {
    public static void main(String[] args) {
        String str1 = "123";
        String str2 = "-123";
        int intValue1 = Integer.parseInt(str1);
        int intValue2 = Integer.parseInt(str2);
        System.out.println(intValue1); // 123
        System.out.println(intValue2); // -123
    }
}
결과
123
-123
Integer.valueOf()
static int valueOf(String s)
parseInt() 메소드와 마찬가지로
valueOf() 메소드는 java.lang.Integer 클래스의 static 메소드이고,
파라미터로 숫자로 변환할 문자열을 입력받습니다.
parseInt() 와 가장 큰 차이점은,
valueOf() 메소드는 문자열을 변환하여 Integer Object를 리턴한다는 것입니다.
parseInt() 메소드는 primitive type인 int를 리턴합니다.
(parseInt()와 valueOf()는 이 외에도 입력받는 파라미터의 타입 등의 차이점이 더 있습니다.)
코드
public class StringToInt {
    public static void main(String[] args) {
        String str1 = "123";
        String str2 = "-123";
        int intValue1 = Integer.valueOf(str1).intValue();
        int intValue2 = Integer.valueOf(str2).intValue();
        System.out.println(intValue1); // 123
        System.out.println(intValue2); // -123
    }
}
결과
123
-123
예제를 살펴보면,
int intValue1 = Integer.valueOf(str1).intValue();
Integer.valueOf() 메소드는 Integer Object를 리턴하기 때문에,
이 Integer Object를 primitive type인 int로 변환하기 위해,
Integer 클래스의 intValue() 메소드를 다시 한번 호출하였습니다.
(사실, intValue() 메소드를 따로 호출하지 않아도,
위 케이스의 경우 자동으로 형변환이 일어나지만,
여기에서는 valueOf() 메소드가 Integer를 리턴한다는 사실을 강조하기 위해서,
명시적으로 intValue() 메소드를 호출하여 주었습니다.)
int -> String (숫자를 문자열로)
int를 String으로 변환하기 위해서는
Integer.toString(), String.valueOf() 메소드를 이용할 수 있고,
간단하게는 빈 문자열과 int를 '+'연산자로 연결하여 문자열로 변환할 수 있습니다.
Integer.toString()
코드
public class IntToString {
    public static void main(String[] args) {
        int intValue1 = 123;
        int intValue2 = -123;
        String str1 = Integer.toString(intValue1);
        String str2 = Integer.toString(intValue2);
        System.out.println(str1);
        System.out.println(str2);
    }
}
결과
123
-123
Integer 클래스의 toString() 메소드를 사용하면 int를 문자열로 바꿀수 있습니다.
String.valueOf()
코드
public class IntToString {
    public static void main(String[] args) {
        int intValue1 = 123;
        int intValue2 = -123;
        String str1 = String.valueOf(intValue1);
        String str2 = String.valueOf(intValue2);
        System.out.println(str1);
        System.out.println(str2);
    }
}
결과
123
-123
java.lang.String 클래스의 valueOf() 메소드를 사용하여도,
int를 String으로 변경할 수 있습니다.
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
문자열에 int를 이어붙이면,
문자열이 리턴되는 속성을 이용한 방법입니다.
Java의 String을 int로, int를 String으로 변환하는 방법을 알아보았습니다.
'IT > Java' 카테고리의 다른 글
| [Java] 10진수 <-> 2진수, 8진수, 16진수로 변환하기 (0) | 2021.04.24 | 
|---|---|
| [Java] 문자열 앞뒤 공백 제거하기 (trim() vs strip() 차이) (1) | 2021.04.21 | 
| [Java] 문자열에서 따옴표 제거 하기 (0) | 2021.04.21 | 
| [Java] 큰 따옴표 출력하는 3가지 방법 (0) | 2021.04.20 | 
| [Java] 문자열을 배열로 변환하는 2가지 방법 (1) | 2021.04.20 |