CodeGym /Blog Jawa /Acak /Kepiye cara ngowahi int dadi String ing Jawa
John Squirrels
tingkat
San Francisco

# Kepiye cara ngowahi int dadi String ing Jawa

Ing artikel iki kita bakal ngrembug ngowahi int (jinis primitif) lan jinis Obyek (wrapper) Integer menyang String. Ana akeh cara kanggo nindakake ing Jawa.

## Carane ngowahi int dadi string ing Jawa

1. Ngonversi kanthi nambah string kosong.

Cara paling gampang kanggo ngowahi int dadi String gampang banget. Cukup nambahake int utawa Integer string kosong "" lan sampeyan bakal entuk int minangka String. Mengkono amarga nambah int lan String menehi String anyar. Tegese yen sampeyan duwe `int x = 5`, mung nemtokake `x + ""`lan sampeyan bakal entuk String anyar.

Punika conto:

``````
//  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);
}
}
``````

Outpute yaiku:

``````
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 ngowahi int dadi string nggunakake Integer.toString(int)

Kelas obyek minangka kelas root ing Jawa. Tegese saben kelas Jawa diwarisake langsung utawa ora langsung saka kelas Obyek lan kabeh metode kelas Obyek kasedhiya kanggo kabeh kelas Jawa.

Obyek nduweni cara khusus toString () kanggo makili obyek minangka senar. Dadi, saben kelas Jawa uga marisi metode iki. Nanging, ide sing apik yaiku ngilangi metode iki ing kelas sampeyan supaya entuk asil sing cocog.

Metode toString() kelas Integer ngasilake obyek String sing makili parameter int utawa Integer sing ditemtokake.

Sintaksipun:

``````
public static String toString(int i)
``````

Cara ngowahi argumen i lan ngasilake minangka conto string. Yen nomer negatif, tandha bakal katahan.

``````
//  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
``````

Sampeyan bisa nggunakake cara toString kanggo ngowahi Integer (jinis pambungkus) uga.

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

Hasile yaiku:

Ngonversi Integer kanggo String: -7

 Sampeyan bisa nggunakake khusus `Integer.toString method toString(int i, int base)`sing ngasilake perwakilan senar saka nomer i karo basa basa lan saka String. Tuladhane Sintaksis yaiku: `````` public static String toString(int i, int base) `````` Punika conto: `````` int a = 255; // binary String customString = Integer.toString(a, 2); System.out.println(customString); `````` Output minangka representasi biner String saka angka desimal 255: `````` 11111111 ``````
3. Ngonversi int dadi String nggunakake String.valueOf(int)

Metode `String.valueOf(int)`ngasilake representasi string saka argumen int.

Sintaks metode kasebut yaiku:

``````
public static String valueOf(int i)
``````

Ing ngisor iki conto Java convert int to String nggunakake `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
``````

Sampeyan bisa uga nindakake perkara sing padha karo Integer (jinis pambungkus int):

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

Output bakal:

Ngonversi Integer kanggo String: -7

4. Ngonversi nggunakake DecimalFormat

`java.text.DecimalFormat`punika kelas ditetepake ing `java.text`paket lan subclass saka `NumberFormat`. Iki digunakake kanggo ngowahi format angka desimal menyang string sing nuduhake pola tartamtu. Kita uga bisa nggunakake kanggo Integers.

``````
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);
}
}
``````

Outpute yaiku:

``````
31415
``````

5. Ngonversi nggunakake String.format()

String.format () iku salah siji cara liyane kanggo ngowahi Integer kanggo String Obyek.

Sintaksis

``````
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);
}
}
``````

Outpute yaiku:

``````35
``````

Wacan liyane:

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION