Type conversion of int and String in JAVA

int->String

int n= 12345;
String s= "";
the first type: s += n; //will generate two String objects
The second type : s += String.valueOf(n); //directly use the static state of the String class method, yields only one object

String->int

int n = 0;
Sting s = "123456";
n = Integer.parseInt(s); //Use the static method directly, no redundant objects will be generated, but an exception will be thrown
n = Integer.valueOf(s).intValue (); //Integer.valueOf(s) is equivalent to new Integer(Integer.parseInt(s)), which will also throw an exception, but will generate one more object

String String -> Integer int

A. There are two methods:

1). int i = Integer.parseInt([String]); 或
i = Integer.parseInt([String],[int radix]);

2). int i = Integer.valueOf(my_str).intValue();

Note: The method of converting a string to Double, Float, Long is similar.

Integer int -> String String

A. There are three methods:

1.) String s = String.valueOf(i);

2.) String s = Integer.toString(i);

3.) String s = “” + i;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325927025&siteId=291194637