변환 중 정수를 문자열로 Integer 클래스를 사용하는 것과 관련됩니다. toString() 또는 문자열.값() 직접 변환을 위해. 문자열.형식() 유연한 서식 옵션을 제공하는 또 다른 방법입니다. 사용 스트링빌더 또는 문자열 버퍼 광범위한 문자열 조작에는 정수 값을 문자열로 추가하는 것이 효율적입니다.
우리는 일반적으로 이러한 변환 기사에 반대합니다. 왜냐하면 문자열에 대해서는 많은 작업을 수행할 수 있지만 정수에 대해서는 제한이 있기 때문입니다. String 클래스에는 번거로움 없이 작업을 수행하는 데 도움이 되는 다양한 내장 메서드 목록이 있습니다.
두 개의 정수를 연결해야 한다고 가정하면 숫자 체계 내에서 수학을 플레이할 해당 숫자 체계를 처리해야 하기 때문에 이 작업은 지루한 작업이 될 것입니다. 하지만 ~하기 위해 Java에서 정수를 문자열로 변환할 때 작업을 너무 쉽게 만들어주는 몇 가지 내장 메서드와 클래스가 있습니다.
팁: 우리는 일반적으로 래퍼 클래스 개념을 가지고 있지만 기본 클래스 데이터 멤버 유형을 문자열로 변환합니다. 왜냐하면 Java의 실제 프로그래밍에서는 문자열을 다루기 때문입니다.
Java에서 int를 문자열로 변환하는 방법은 무엇입니까?
정수를 문자열로 변환하는 특정 방법이 아래에 언급되어 있습니다.
- 사용하여 toString() Integer 클래스의 메서드
- 사용하여 가치() String 클래스의 메소드
- 사용 정수(int).toString() Integer 클래스의 메소드
- 빈 문자열과 연결을 사용합니다.
Integer 클래스의 toString 메소드 사용
Integer 클래스에는 정적 메소드가 있습니다. toString() 이는 지정된 int 매개변수를 나타내는 String 객체를 반환합니다. 인수는 변환되어 문자열 인스턴스로 반환됩니다. 숫자가 음수이면 부호가 유지됩니다.
예:
자바
// Java Program to Illustrate Integer to String Conversions> // Using toString() Method of Integer Class> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input integers> > int> a => 1234> ;> > int> b = -> 1234> ;> > > // Converting integer to string> > // using toString() method> > String str1 = Integer.toString(a);> > String str2 = Integer.toString(b);> > > // Printing the above strings that> > // holds integer> > System.out.println(> 'String str1 = '> + str1);> > System.out.println(> 'String str2 = '> + str2);> > }> }> |
>
>산출
String str1 = 1234 String str2 = -1234>
String 클래스의 valueOf() 메소드 사용
String 클래스에는 정적 메소드 Java가 있습니다.
// Java Program to Illustrate Integer to String Conversions> // Using valueOf() Method of String class> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom integer input> > int> c => 1234> ;> > > // Converting above integer to string> > // using valueOf() Method> > String str3 = String.valueOf(c);> > > // Printing the integer stored in above string> > System.out.println(> 'String str3 = '> + str3);> > }> }> |
>
>산출
BFS 대 DFS
String str3 = 1234>
Integer 클래스의 toString() 메소드 사용
이는 위에서 제안한 방법 1과 다릅니다. 이 방법에서는 Integer 클래스의 인스턴스를 사용하여 toString() 메서드를 호출합니다.
위 방법의 구현은 다음과 같습니다.
자바
// Java Program to Illustrate> // Integer to String Conversions> // Using toString() Method of> // Integer Class> > // Importing required classes> import> java.util.*;> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input integer> > int> d => 1234> ;> > > // Converting integer to string> > // using toString() method of Integer class> > String str4 => new> Integer(d).toString();> > > // Printing the integer value stored in above string> > System.out.println(> 'String str4 = '> + str4);> > }> }> |
>
>
산출
출력 설명: 변수가 기본 유형(int)인 경우 Integer.toString(int) 또는 String.valueOf(int)를 사용하는 것이 더 좋습니다. 그러나 변수가 이미 Integer(기본 유형 int의 래퍼 클래스)의 인스턴스인 경우 위에 표시된 대로 해당 toString() 메서드를 호출하는 것이 더 좋습니다.
메모: 변환이 수행되기 전에 Integer 클래스의 인스턴스가 생성되므로 이 방법은 효율적이지 않습니다.
빈 문자열과 연결 사용
접근하다: 여기서는 빈 문자열을 선언하고 '+' 연산자를 사용하여 결과를 문자열로 저장합니다. 이제 이를 통해 이러한 문자열을 성공적으로 추가하고 연결할 수 있습니다.
위 메소드를 구현하면 다음과 같습니다.
자바
// Java Program to Illustrate Integer to String Conversions> // Using Concatenation with Empty String> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom integer values> > int> a => 1234> ;> > int> b = -> 1234> ;> > > // Concatenating with empty strings> > String str1 => ''> + a;> > String str2 => ''> + b;> > > // Printing the concatenated strings> > System.out.println(> 'String str1 = '> + str1);> > System.out.println(> 'String str2 = '> + str2);> > }> }> |
온스에 10ml
>
>산출
String str1 = 1234 String str2 = -1234>
int를 문자열 Java로 변환하는 고급 방법
특정 사전 방법이 아래에 언급되어 있습니다:
- DecimalFormat 클래스 사용
- StringBuffer 클래스 사용
- StringBuilder 클래스 사용
- 특수 기수 및 사용자 정의 기수 사용
DecimalFormat 클래스 사용
십진수 형식 숫자를 문자열로 형식화하는 클래스입니다.
위 메소드를 구현하면 다음과 같습니다.
자바
// Java Program to Illustrate> // Integer to String Conversions> // Using DecimalFormat Class> > // Importing required classes> import> java.text.DecimalFormat;> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Input integer value> > int> e => 12345> ;> > > // Creating an object of DecimalFormat class> > // inside main() method> > DecimalFormat df => new> DecimalFormat(> '#,###'> );> > > // Converting above integral value to string> > String Str5 = df.format(e);> > > // Printing the value stored in above string> > System.out.println(Str5);> > }> }> |
>
>
산출
팁: 이 방법을 사용하면 가독성을 위해 소수 자릿수와 쉼표 구분 기호를 지정할 수 있습니다.
StringBuffer 클래스 사용
StringBuffer는 여러 값을 문자열로 연결하는 데 사용되는 클래스입니다.
예시 1:
자바
// Java Program to Illustrate> // Integer to String Conversions> // Using StringBuffer Class> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Integer input value> > int> f => 1234> ;> > > // Creating an object of StringBuffer class> > StringBuffer sb => new> StringBuffer();> > sb.append(f);> > > String str6 = sb.toString();> > > System.out.println(> 'String str6 = '> + str6);> > }> }> |
>
>산출
String str6 = 1234>
예시 2:
자바
// Java Program to Illustrate> // Integer to String Conversions> // Using StringBuffer Class> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > String str6> > => new> StringBuffer().append(> 1234> ).toString();> > > System.out.println(> 'String str6 = '> + str6);> > }> }> |
>
베드페이지 같은 사이트
>산출
String str6 = 1234>
StringBuilder 클래스 사용
스트링빌더 유사하게 작동하지만 StringBuffer처럼 스레드로부터 안전하지 않습니다.
예시 1:
자바
// Java Program to Illustrate> // Integer to String Conversions> // Using StringBuilder Class> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Input integer> > int> g => 1234> ;> > > // Creating an object of StringBuilder class> > // inside main() method> > StringBuilder sb => new> StringBuilder();> > sb.append(g);> > > String str7 = sb.toString();> > > // Printing the value stored in above string> > System.out.println(> 'String str7 = '> + str7);> > }> }> |
>
>
선택 정렬산출
String str7 = 1234>
예시 2:
자바
// Java Program to Illustrate Different Ways for> // Integer to String Conversions> // Using StringBuilder Class> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > String str7> > => new> StringBuilder().append(> 1234> ).toString();> > > // Printing the value stored in above string> > System.out.println(> 'String str7 = '> + str7);> > }> }> |
>
>산출
String str7 = 1234>
메모: 위의 모든 예제에서는 기수(radix) 10을 사용합니다. 다음은 2진수, 8진수, 16진수 시스템으로 변환하는 편리한 방법입니다. 임의의 사용자 정의 번호 시스템도 지원됩니다.
A. 특수 기수 사용
예: 바이너리
자바
// Java Program to Illustrate Integer to String Conversions> // Using Special Radix In Binary Numbers> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Input integer> > int> h => 255> ;> > String binaryString = Integer.toBinaryString(h);> > > // Printing the binary number stored in above string> > System.out.println(binaryString);> > }> }> |
>
>산출
11111111>
출력 설명: 11111111은 숫자 255를 이진수로 표현한 것입니다.
예: 8진수
자바
// Java Program to Illustrate Integer to String Conversions> // Using Special Radix In Octal Numbers> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input integer> > int> i => 255> ;> > String octalString = Integer.toOctalString(i);> > > // Printing the octal number stored in above string> > System.out.println(octalString);> > }> }> |
>
>산출
377>
377은 숫자 255를 8진수로 표현한 것입니다.
예: 16진수
자바
// Java Program to Illustrate Integer to String Conversions> // Using Special Radix In Hexadecimal Numbers> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Custom input integer> > int> j => 255> ;> > String hexString = Integer.toHexString(j);> > > // Printing the hexadecimal number> > // stored in above string> > System.out.println(hexString);> > }> }> |
>
32비트 아키텍처와 64비트 비교
>산출
ff>
그만큼 ff 숫자 255를 16진수로 표현한 것입니다.
B. 사용자 정의 베이스/기수
접근하다: Integer 클래스의 toString() 메서드를 사용하여 문자열로 변환하고 추가적으로 기수라는 인수로 값을 전달합니다. int를 문자열로 변환할 때 다른 사용자 정의 기본/기수를 사용할 수 있습니다. 아래 예에서는 설명을 위해 기본 7 숫자 시스템을 고려하고 있습니다.
예:
자바
// Java Program to Illustrate Integer to String Conversions> // Using Custom Radix> > // Main class> class> GFG {> > > // Main driver method> > public> static> void> main(String args[])> > {> > // Input integer value> > int> k => 255> ;> > > // Setting base as 7, converting integer to string> > // using toString() method and> > // storing it into a string> > String customString = Integer.toString(k,> 7> );> > > // Printing value stored in above string> > System.out.println(customString);> > }> }> |
>
>
산출
메모: 513은 7진수 체계로 쓰여졌을 때 숫자 255를 표현한 것입니다.
요약
Java에서 정수를 문자열로 변환하는 프로세스에는 다음을 사용하는 메서드가 포함됩니다. toString() 및 valueOf() 직접 변환을 위한 Integer 클래스의 메서드, 문자열.형식() 사용자 정의 가능한 서식 옵션 StringBuilder 또는 StringBuffer 효율적인 문자열 통합을 위해.