Java Type conversion Integer String Long Float Double Date
1 How do I convert string string to 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 string turns into a Double, Float, Long with a similar method.
2 How do I convert an integer int to string string?
A. There are three approaches:
1.) String s = string.valueof (i);
2.) String s = integer.tostring (i);
3.) String s = "" + I;
Note: Double, Float, and Long turn into strings are very similar.
Package cn.com.lwkj.erts.register;
Import Java.sql.Date; public class Typechange {public Typechange () {}//change the ' string type to ' 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 in
Teger (value);
return integer.tostring (); }//change the the string type to the float type public static float Stringtofloat (string floatstr) {float Floa
Tee
Floatee = float.valueof (FLOATSTR);
return Floatee.floatvalue (); The//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) {RET Urn Java.sql.Date.valUeof (DATESTR); }//change the Sqldate type to the string type public static string datetostring (Java.sql.Date datee) {retur
n 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); }/* Cloud Habitat Community www.jb51.net * *
Common data type conversion functions in Java
Although all can be found in the Java API, organize 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
The basic types have the following four kinds:
int length data types are: Byte (8bits), short (16bits), int (32bits), Long (64bits),
Float length data types are: Single precision (32bits float), double precision (64bits double)
Boolean-type variables have the values: Ture, False
Char data types are: Unicode characters, 16-bit
Corresponding class type: Integer, Float, Boolean, Character, Double, short, Byte, Long
Conversion principles
Converting from low precision to high precision
BYTE, short, int, long, float, double, char
Note: Two char operations, automatically converted to int, when char and other types of operations, will be automatically converted to int type, and then do other types of automatic conversion
Base type conversions to class type
Forward conversion: To create a new class-type variable by using the class wrapper
Integer a= new Integer (2);
Reverse conversion: Converting via class wrapper
int B=a.intvalue ();
class type to string conversion
Forward conversions: Because each class is a subclass of the object class, and all object classes have a ToString () function, you can use the ToString () function to convert
Reverse conversion: A variable of a new class type is created by the class wrapper
Eg1:int i=integer.valueof ("123"). Intvalue ()
Description: The above example converts a string into an integer object, and then calls the object's Intvalue () method to return its corresponding int value.
Eg2:float f=float.valueof ("123"). Floatvalue ()
Description: The above example converts a string into a float object, and then calls the object's Floatvalue () method to return its float value.
Eg3:boolean b=boolean.valueof ("123"). Booleanvalue ()
Description: The above example converts a string into a Boolean object, and then calls the object's Booleanvalue () method to return its corresponding Boolean value.
Eg4:double d=double.valueof ("123"). Doublue ()
Description: The above example converts a string into a double, and then calls the object's Doublue () method to return its double value.
Eg5:long l=long.valueof ("123"). Longvalue ()
Description: The above example converts a string to a long object, and then calls the object's Longvalue () method to return its long value.
Eg6:char=character.valueof ("123"). Charvalue ()
Description: The above example converts a string into a character object, and then calls the object's Charvalue () method to return its corresponding char value.
Conversion of base type to string
Forward conversion:
such as: int a=12;
String b;b=a+ "";
Reverse conversion:
Through the class wrapper
Eg1:int I=integer.parseint ("123")
Note: This method can only be applied to strings converted to integer variables
Eg2:float f=float.valueof ("123"). Floatvalue ()
Description: The above example converts a string into a float object, and then calls the object's Floatvalue () method to return its float value.
Eg3:boolean b=boolean.valueof ("123"). Booleanvalue ()
Description: The above example converts a string into a Boolean object, and then calls the object's Booleanvalue () method to return its corresponding Boolean value.
Eg4:double d=double.valueof ("123"). Doublue ()
Description: The above example converts a string into a double, and then calls the object's Doublue () method to return its double value.
Eg5:long l=long.valueof ("123"). Longvalue ()
Description: The above example converts a string to a long object, and then calls the object's Longvalue () method to return its long value.
Eg6:char=character.valueof ("123"). Charvalue ()
Description: The above example converts a string into a character object
The above all kinds of Java conversion of all types of summary (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.