String to integer
1. Use the Intger. parseInt (String) method:
// Using Integer. parseInt
Int I = Integer. parseInt ("123 ");
System. out. println ("I:" + I );
When the string is not a correct value (such as "a123"), the Integer. parseInt () method may throw a NumberFormatException. Other data types have similar methods, such
Float. parseFloat () and Double. parseDouble (). Other data types have similar methods, such
2. Use the Integer. valueOf () method
The Integer. valueOf () method is implemented as the Factory method. As follows:
// How to convert numeric string = "000000081" into Integer value = 81
Int I = Integer. valueOf ("000000081 ");
System. out. println ("I:" + I );
This method ignores meaningless 0 (the leading zeros, leading zero). For example, after converting the string "000000081", 81 is returned. If the string is not in the correct numeric format, a NumberFormatException is thrown.
Converts an integer to a string.
1. Use a string connector
String price = "" + 123;
This method is inefficient but convenient. This method is equivalent:
New StringBuilder (). append (""). append (10). toString ();
The StringBuilder (String) constrtor allocates a buffer containing 16 characters. Then the append method adds the new attachment content to the buffer. The StringBuilder. toString () method creates a string to save the content in the StringBuider buffer. This means that we need to allocate: a StringBuilder, a character array char [16], a string, and a char [] that can put all input strings down. No String. valueOf () is convenient.
2. Use String. valueOf ()
String a = String. valueOf ("10 ");
3. Use String. format ()
String price = String. format ("% d", 123 );
This is an example of data type conversion in JAVA.
Package cn.com. lwkj. erts. reGISter;
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 they can all be found in java api, sort out and make a backup.
String-> byte
Byte static byte parseByte (String s)
Byte-> string
Byte static String toString (byte B)
Char-> string
Character static String to String (char c)
String-> Short
Short static Short parseShort (String s)
Short-> String
Short static String toString (Short s)
String-> Integer
Integer static int parseInt (String s)
Integer-> String
Integer static String tostring (int I)
String-> Long
Long static long parseLong (String s)
Long-> String
Long static String toString (Long I)
String-> Float
Float static float parseFloat (String s)
Float-> String
Float static String toString (float f)
String-> Double
Double static double parseDouble (String s)
Double-> String
Double static String toString (Double d)