The specific details are as follows:
int -> Stringint i=12345;String s="";
The first method: s=i+"";
The second method: s=String.valueOf(i);
What is the difference between these two methods? Is the function the same? Can it be interchanged under any circumstances?
String -> ints="12345";int i;
The first method: i=Integer.parseInt(s);
The second method: i=Integer.valueOf(s).intValue();
What is the difference between these two methods? Is the function the same? Can it be interchanged under any circumstances?
Here are the answers:
The first method: s=i+""; //It will generate two String objects
The second method: s=String.valueOf(i); //Use the static method of the String class directly to generate only one object
The first method: i=Integer.parseInt(s);//Use the static method directly, no redundant objects will be generated, but exceptions will be thrown
The second method: i=Integer.valueOf(s).intValue();//Integer.valueOf(s) is equivalent to new Integer(Integer.parseInt(s)), and it will also throw exceptions, but one more object will be generated.
--------------------------------------------------------------------
1How to convert a string String to an integer int?
A. There are two methods:
1). int i = Integer.parseInt([String]); or
i = Integer.parseInt([String],[int radix]);
2). int i = Integer.valueOf(my_str).intValue();
Note: The method of converting strings to Double, Float, Long is similar.
2 How to convert integer int into string String?
A. There are three methods:
1.) String s = String.valueOf(i);
2.) String s = Integer.toString(i);
3.) String s = "" + i;
Note: The method of converting Double, Float, Long to strings is similar.
JAVA data type conversion
This is an example, which refers to the conversion of data numerical types in JAVA. For everyone to learn
package shenmixiaozhu;import java.sql.Date;public class TypeChange {public TypeChange() {}//change the string type to the int typepublic static int stringToInt(String intstr){Integer integer;integer = Integer.valueOf(intstr);return integer.intValue();}//change int type to the string typepublic static String intToString(int value){Integer integer = new Integer(value);return integer.toString();}//change the string type to the float typepublic static float stringToFloat(String floatstr){Float floate;floatee = Float.valueOf(floatstr);return floate.floatValue();}//change the float type to the string typepublic static String floatToString(float value){Float float = new Float(value);return floate.toString();}//change the string type to the sqlDate typepublic static java.sql.Date stringToDate(String dateStr){return java.sql.Date.valueOf(dateStr);}//change the sqlDate type to the string typepublic static String dateToString(java.sql.Date datee){return datee.toString();}public static void main(String[] args){java.sql.Date day ;day = TypeChange.stringToDate("2003-11-3");String strday = TypeChange.dateToString(day);System.out.println(strday);}}The above content is the two methods of int and string type conversion introduced to you by the editor. I hope it can help you.