java type conversion Integer String Long Float Double Date
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.
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 floate; floate = Float.valueOf(floatstr); return floate.floatValue(); } //change the float type to the string type public static String floatToString(float value) { Float float = new Float(value); return floate.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); } } /* Wulin.com www.VeVB.COM */Commonly used data type conversion functions in JAVA
Although it can be found in the JAVA API, organize it 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)
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Data Type
There are four basic types:
Int length data types are: byte(8bits), short(16bits), int(32bits), long(64bits),
float length data types include: single precision (32bits float), double precision (64bits double)
The values of boolean type variables are: ture, false
The char data types are: unicode characters, 16-bit
Corresponding class types: Integer, Float, Boolean, Character, Double, Short, Byte, Long
Conversion principle
Convert from low precision to high precision
byte , short , int , long , float , double , char
Note: When two char-type operations are automatically converted to int type; when char-type operations are used with other types, it will also be automatically converted to int type first, and then perform other types of automatic conversion.
Convert basic types to class types
Forward conversion: new variable of a new class type through a class wrapper
Integer a= new Integer(2);
Reverse conversion: Convert through class wrapper
int b=a.intValue();
Convert class type to string
Forward conversion: Because each class is a subclass of the object class, and all object classes have a toString() function, you can convert it through the toString() function.
Reverse conversion: new class type variable through class wrapper
eg1: int i=Integer.valueOf("123").intValue()
Description: The above example is to convert a string into an Integer object, and then call the intValue() method of this object to return its corresponding int value.
eg2: float f=Float.valueOf("123").floatValue()
Description: The above example is to convert a string into a Float object, and then call the floatValue() method of this object to return its corresponding float value.
eg3: boolean b=Boolean.valueOf("123").booleanValue()
Description: The above example is to convert a string into a Boolean object, and then call the booleanValue() method of this object to return its corresponding boolean value.
eg4:double d=Double.valueOf("123").doublue()
Description: The above example is to convert a string into a Double object, and then call the double() method of this object to return its corresponding double value.
eg5: long l=Long.valueOf("123").longValue()
Description: The above example is to convert a string into a Long object, and then call the longValue() method of this object to return its corresponding long value.
eg6: char=Character.valueOf("123").charValue()
Description: The above example is to convert a string into a Character object, and then call the charValue() method of this object to return its corresponding char value.
Conversion of basic types to strings
Forward conversion:
For example: int a=12;
String b;b=a+””;
Reverse conversion:
By class wrapper
eg1:int i=Integer.parseInt("123")
Note: This method can only be used for converting strings into integer variables
eg2: float f=Float.valueOf("123").floatValue()
Description: The above example is to convert a string into a Float object, and then call the floatValue() method of this object to return its corresponding float value.
eg3: boolean b=Boolean.valueOf("123").booleanValue()
Description: The above example is to convert a string into a Boolean object, and then call the booleanValue() method of this object to return its corresponding boolean value.
eg4:double d=Double.valueOf("123").doublue()
Description: The above example is to convert a string into a Double object, and then call the double() method of this object to return its corresponding double value.
eg5: long l=Long.valueOf("123").longValue()
Description: The above example is to convert a string into a Long object, and then call the longValue() method of this object to return its corresponding long value.
eg6: char=Character.valueOf("123").charValue()
Description: The above example is to convert a string into a Character object
The above summary of various types of Java conversions (recommended) is all the content shared by the editor. I hope it can give you a reference and I hope you can support Wulin.com more.