CodeGym /Java Blog /무작위의 /Java에서 int를 문자열로 변환하는 방법
John Squirrels
레벨 41
San Francisco

Java에서 int를 문자열로 변환하는 방법

무작위의 그룹에 게시되었습니다
이 기사에서는 int(기본 유형) 및 객체 유형(래퍼) Integer를 문자열로 변환하는 방법에 대해 설명합니다. Java에서 이를 수행하는 방법에는 여러 가지가 있습니다.

Java에서 int를 문자열로 변환하는 방법

  1. 빈 문자열을 추가하여 변환합니다.

    int를 String으로 변환하는 가장 쉬운 방법은 매우 간단합니다. int 또는 Integer에 빈 문자열 ""을 추가하면 int를 문자열로 얻을 수 있습니다. int와 String을 추가하면 새로운 String이 생성되기 때문에 발생합니다. 즉 int x = 5, 가 있는 경우 정의만 하면 x + ""새 문자열을 얻을 수 있습니다.

    다음은 예입니다.

    
    //  converting int to string, Java 
    public class Demo {
       public static void main(String[] args) {
    
           int x = 5;
           //  java int to string
           String xText = x + "";
           //  the result output
           System.out.println("convert int to String, Java: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("adding int = 5 and String = \"5\". The result is a new String = " + xText + x);
           //  integer to string, Java code
           Integer y = 7;
           String yText = y + "";
           System.out.println("convert Integer to String: " + yText);
           System.out.println("our Integer: " + y);
           System.out.println("adding Integer = 7 and String = \"7\". The result is a new String = " + y + yText);
       }
    }
    

    출력은 다음과 같습니다.

    
    convert int to String, Java: 5
    our int: 5
    adding int = 5 and String = "5". The result is a new String = 55
    convert Integer to String: 7
    our Integer: 7
    adding Integer = 7 and String = "7". The result is a new String = 77
    

  2. Java는 Integer.toString(int)을 사용하여 int를 문자열로 변환합니다.

    객체 클래스는 Java의 루트 클래스입니다. 즉, 모든 Java 클래스는 Object 클래스에서 직접 또는 간접적으로 상속되며 모든 Object 클래스 메서드는 모든 Java 클래스에서 사용할 수 있습니다.

    Object에는 모든 객체를 문자열로 나타내는 특수 메서드 toString()이 있습니다. 따라서 모든 Java 클래스도 이 메서드를 상속합니다. 그러나 좋은 생각은 적절한 결과를 얻기 위해 자신의 클래스에서 이 메서드를 재정의하는 것입니다.

    Integer 클래스의 toString() 메서드는 지정된 int 또는 Integer 매개변수를 나타내는 String 객체를 반환합니다.

    구문:

    
    public static String toString(int i)
    

    메서드는 인수 i를 변환하고 문자열 인스턴스로 반환합니다. 숫자가 음수이면 부호가 유지됩니다.

    예:

    
    //  java integer to string using toString method
    
    public class Demo {
       public static void main(String[] args) {
    
           int x = -5;
           //  java convert int to string using Integer.toString
           String xText = Integer.toString(x);
           //  the result output
           System.out.println("convert int to String: " + xText);
           //  the int output
           System.out.println("our int: " + x);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + xText + Integer.toString(x));
    
    
       }
    }
    

    
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    

    toString 메소드를 사용하여 Integer(래퍼 유형)를 변환할 수도 있습니다.

    
    Integer number = -7;
    String numberAsString = Integer.toString(number);
    System.out.println("convert Integer to String: " + numberAsString);
    

    결과는 다음과 같습니다.

    정수를 문자열로 변환: -7

    Integer.toString method toString(int i, int base)기본 밑과 String보다 숫자 i의 문자열 표현을 반환하는 특수를 사용할 수 있습니다 . 예를 들어

    구문은 다음과 같습니다.

    
    public static String toString(int i, int base)
    

    다음은 예입니다.

    
    int a = 255;
    //  binary
    String customString = Integer.toString(a, 2);
    System.out.println(customString);
    

    출력은 십진수 255의 문자열 이진 표현입니다.

    
    11111111
    

  3. String.valueOf(int)를 사용하여 int를 문자열로 변환

    메서드는 String.valueOf(int)int 인수의 문자열 표현을 반환합니다.

    메서드의 구문은 다음과 같습니다.

    
    public static String valueOf(int i)
    

    다음은 Java를 사용하여 int를 String으로 변환하는 예입니다 String.valueOf(int).

    
    public class Demo {
       public static void main(String[] args) {
           int z = -5;
           //  Java int to String converting 
     String zText = String.valueOf(z); 
           //  the result output
           System.out.println("convert int to String: " + zText);
           //  the int output
           System.out.println("our int: " + z);
           //  adding int and String gives the new String
           System.out.println("converting int = -5 and adding to String = \"-5\". The result is a new String = " + zText + z);
       }
    }
    

    
    convert int to String: -5
    our int: -5
    converting int = -5 and adding to String = "-5". The result is a new String = -5-5
    

    Integer(int의 래퍼 유형)에 대해 동일한 작업을 수행할 수 있습니다.

    
    Integer number = -7;
    String numberAsString = String.valueOf(number);
    System.out.println("convert Integer to String: " + numberAsString);
    

    출력은 다음과 같습니다.

    정수를 문자열로 변환: -7

  4. DecimalFormat을 사용하여 변환

    java.text.DecimalFormat는 패키지 에 정의된 클래스 java.text이며 의 하위 클래스 입니다 NumberFormat. 특정 패턴을 따르는 것을 나타내는 문자열로 십진수를 형식화하는 데 사용됩니다. 정수에도 사용할 수 있습니다.

    예:

    
    import java.text.DecimalFormat;
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 31415;
           DecimalFormat decimalFormat = new DecimalFormat("#");
           String myNumberAsString = decimalFormat.format(myNumber);
           System.out.println(myNumberAsString);
       }
    }
    

    출력은 다음과 같습니다.

    
    31415
    

  5. String.format()을 사용하여 변환

    String.format()은 정수를 문자열 객체로 변환하는 또 하나의 방법입니다.

    통사론

    
    public static String format(String format, Object... args)
    

    
    public class Demo {
       public static void main(String[] args) {
           int myNumber = 35;
           String myNumberAsString = String.format("%d", myNumber);  //  %d converter defines a single decimal integer variable.
           System.out.println(myNumberAsString);
       }
    }
    

    출력은 다음과 같습니다.

    35
    

코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION