One, string conversion to other types
1. Convert string to int type
(1) Method one
int i = integer.parseint (String str);
(2) Method two
int i = integer.valueof (String str). Intvalue ();
Note: Integer.parseint and integer.valueof are different, the former generates an integral type, and the latter is an object, so the value of the object is obtained by Intvalue () .
The string turns into Double, Float, and Long in a similar way.
2. Convert string to double type
(1) Method one
Double i=double.parsedouble (String str);
(2) Method two
Double i=double.valueof (String str). Doublevalue ();
3. Convert string to float type
(1) Method one
Float i=float.parsefloat (String str);
(2) Method two
Float i=float.valueof (String str). Floatvalue ();
4. Convert a string to a long type
(1) Method one
Long I=long. Parselong (String str);
(2) Method two
Long i=long.valueof (String str). Longvalue ();
Second, convert other types to strings
1. convert int type to string
(1) Method one
String str = string.valueof (int i);
(2) Method two
String str = integer.tostring (int i);
(3) method three
String str = "" + I;
Note: Double, Float, Long is similar in the same way.
2. Converting a Double type to a string
(1) Method one
String str = string.valueof (int i);
(2) Method two
Double. toString (int i);
(3) method three
String str = "" + I;
3. Convert the float type to a string
(1) Method one
String str = string.valueof (int i);
(2) Method two
String str = float.tostring (int i);
(3) method three
String str = "" + I;
4. Converting a long type to a string
(1) Method one
String str = string.valueof (int i);
(2) Method two
String str = Long. toString (int i);
(3) method three
String str = "" + I;
Conversions between three, Long, String, date types
1.convert java.util.Date type to long type
Date date=new date (); System.out.println (Date.gettime ());
parsing: where GetTime () is returned as a long type, with a length of 13, for milliseconds, and if you want to get the number of seconds, you only need to divide by 1000.
long mseconds=date.gettime ()/1000;
2, long type conversion to java.util.Date type
New SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"); New Date (Mseconds *); = Sdf.format (date); System.out.println (str);
The previous mseconds is the number of seconds, so the number of milliseconds to multiply by 1000, and then into the java.util.Date type, so that the conversion of long to date is completed;
In order to format the output date, you can call the format method of the SimpleDateFormat output date
3, such as "2015-08-31 21:08:06" format string type conversion to Java.util.Date type
String str= "2015-08-31 21:08:06"new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"= (Date) Sdf.parse (str); System.out.println (Date.gettime ());
It outputs the result of
1441026486000
It is sometimes necessary to convert CST time, such as: Wed Sep 11:26:23 CST 2009, which can also be resolved with the SimpleDateFormat parse
String str= "Wed Sep 11:26:23 CST"new SimpleDateFormat ("EEE MMM dd HH:mm:ss zzz yyyy"= ( Date) Sdf.parse (str);
Iv. conversion of type float, double type, int
1. Float type is converted to double type
(1) Method one
/***/float f1=100.00f; float F1=new float (F1); // F1.doublevalue () is a method of returning a double value type for the float class double D1=f1.doublevalue ();
(2) Method two
/** * Convert float type to string and convert to BigDecimal, with its Doublevalue () method */ float f = 127.1f; New BigDecimal (string.valueof (f)); Double
2. Double type conversion to float type
double d = 3.14; float f = (float) D;
3, the double type is converted to the INT type
double d1=100.00;D ouble D1=new double (d1); int i1=d1.intvalue ();
4. convert int type to double type
int i1=200; double d1=i1;
V. Other small instances of application
1, a long type is converted to an int type
(1) Method one: Forced conversion
Long L = 300000int i= (int) L;
(2) Method two: Call the Intvalue () method
Long L = 300000intnew
(3) method Three: First converts a long string into strings, and then transforms into an integer
2, a long type is converted to an integer type
Integer i=New long (long L). Intvalue ();
Vi. Summary
1. Conversion between strings and other types
(1) Switching of other types to strings
A, call the class string conversion method: X.tostring ();
B. Automatic conversion: x+ "";
C. Method of using String: string.valueof (X);
(2) string as a value, to other types of conversions
A. Convert to a corresponding wrapper instance before calling the corresponding method to convert to another type
For example, the character "32.1" converts the value of the double type to the format:new Float ("32.1"). Doublevalue ().
B. Static Parsexxx method
String s = "1"; byte b = Byte.parsebyte (s); Short T = short.parseshort (s); int i = integer.parseint (s); long l == = double.parsedouble (s);
C.character getnumericvalue (char-ch) method
Returns the value represented by the specified Unicode character int
.
(3) Other useful data type conversions
2. Data type
There are four basic types of the following:
(1) int length data types are: Byte (8bits), short (16bits), int (32bits), Long (64bits),
(2) Float length data types are: Single precision (32bits float), double precision (64bits double)
(3) The value of a Boolean variable is: Ture, False
(3) Char data types are: Unicode characters, 16 bits
(4) corresponding class type: Integer, Float, Boolean, Character, Double, short, Byte, Long
3. Conversion principle
(1) from low-precision to high-precision conversion
BYTE, short, int, long, float, double, char
Note: Two char operations are automatically converted to int, and when char and other types are operated, they are automatically converted to int, and then other types of auto-conversions
(2) Basic type conversion to class type
A. Forward conversion: A new class-type variable with a class wrapper
Integer a= new Integer (2);
B. Reverse conversions: Converting through a class wrapper
int B=a.intvalue ();
(3) class type to string conversion
A. Forward conversion: Because each class is a subclass of the object class, and all object classes have a ToString () function, the ToString () function can be converted to
B. Reverse conversions: A new class-type variable with the class wrapper new
Eg1:int i=integer.valueof ("123"). Intvalue ()
Note: The previous 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 ()
Note: The previous example converts a string into a float object and then calls the object's Floatvalue () method to return its corresponding float value.
Eg3:boolean b=boolean.valueof ("123"). Booleanvalue ()
Note: The previous 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"). Doublevalue ()
Note: The previous example converts a string into a double object, and then calls the object's Doublevalue () method to return its corresponding double value.
Eg5:long l=long.valueof ("123"). Longvalue ()
Note: The previous example converts a string into a long object, and then calls the object's Longvalue () method to return its corresponding long value.
Eg6:char=character.valueof ("123"). Charvalue ()
Note: The previous example converts a string into a character object, and then calls the object's Charvalue () method to return its corresponding char value.
(4) Conversion of basic types to strings
A. Forward conversion:
such as: int a=12;
String b;b=a+ "";
B. Reverse conversions:
Through the class wrapper
Eg1:int I=integer.parseint ("123")
Description: This method can only be used to convert a string into an integer variable
Eg2:float f=float.valueof ("123"). Floatvalue ()
Note: The previous example converts a string into a float object and then calls the object's Floatvalue () method to return its corresponding float value.
Eg3:boolean b=boolean.valueof ("123"). Booleanvalue ()
Note: The previous 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"). Doublevalue ()
Note: The previous example converts a string into a double object, and then calls the object's Doublevalue () method to return its corresponding double value.
Eg5:long l=long.valueof ("123"). Longvalue ()
Note: The previous example converts a string into a long object, and then calls the object's Longvalue () method to return its corresponding long value.
Eg6:char=character.valueof ("123"). Charvalue ()
Note: The previous example converts a string into a character object, and then calls the object's Charvalue () method to return its corresponding char value.
Data type conversion Encyclopedia in Java (personal summary)