Int-> String
Int I = 12345;
String s = "";
Method 1: s = I + "";
Method 2: s = String. valueOf (I );
What are the differences between the two methods? Is the role the same? Is it interchangeable under any circumstances?
String-> int
S = "12345 ";
Int I;
Method 1: I = Integer. parseInt (s );
Method 2: I = Integer. valueOf (s). intValue ();
What are the differences between the two methods? Is the role the same? Is it interchangeable under any circumstances?
The answer is as follows:
Method 1: s = I + ""; // two String objects are generated.
Method 2: s = String. valueOf (I); // directly use the static method of the String class to generate only one object
Method 1: I = Integer. parseInt (s); // directly use the static method. No redundant objects are generated, but an exception is thrown.
Method 2: I = Integer. valueOf (s ). intValue (); // Integer. valueOf (s) is equivalent to new Integer (Integer. parseInt (s), an exception is thrown, but an object is generated.
--------------------------------------------------------------------
1 how to convert a 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 methods for converting strings to Double, Float, and Long are similar.
2. How do I convert an integer int to a String?
A. There are three methods:
1.) String s = String. valueOf (I );
2.) String s = Integer. toString (I );
3.) String s = "" + I;
Note: the conversion of Double, Float, and Long into strings is similar.
JAVA data type conversion
This is an example of data type conversion in JAVA.
Package shenmixiaozhu;
Import java. SQL. Date;
Public class TypeChange {
Public TypeChange (){
}
// Change the string type to the int type
Public static int stringToInt (String intstr)
{
Integer integer;
Integer = Integer. valueOf (intstr );
Return integer. intValue ();
}
// Change int type to the string type
Public static String intToString (int value)
{
Integer integer = new Integer (value );
Return integer. toString ();
}
// Change the string type to the float type
Public static float stringToFloat (String floatstr)
{
Float floatee;
Floatee = Float. valueOf (floatstr );
Return floatee. floatValue ();
}
// Change the float type to the string type
Public static String floatToString (float value)
{
Float floatee = new Float (value );
Return floatee. toString ();
}
// Change the string type to the sqlDate type
Public static java. SQL. Date stringToDate (String dateStr)
{
Return java. SQL. Date. valueOf (dateStr );
}
// Change the sqlDate type to the string type
Public 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 );
}
}
Common data type conversion functions in JAVA
Although it can be found in java api, sort out and make a backup.