1). There are two ways to convert a simple type of data: Auto-conversion and casting , which typically occurs in an expression or when a method's arguments are passed.
Automatic conversion
When a smaller data is calculated with a larger data, the system will automatically convert "small" data into "big" data, and then perform operations
In the case of a method call, the actual parameter is "small", and the method called by the form parameter data is more "large" (if there is a match, of course, will call the matching method directly), the system will automatically convert "small" data into "big" data, and then the method calls, naturally, for multiple overloaded methods of the same name, will be converted to the most " Close the "big" data and make the call.
These types are from "small" to "large" respectively
(byte,short,char)--int--long--float-double
The word "big" and "small" here do not refer to the number of bytes occupied, but rather the size of the range that represents the value.
① The following statements can be passed directly in Java:
byte b; int Long float double d=b;
② if the low-level type is char, converting to the advanced type (int) is converted to the corresponding ASCII value, for example
Char int i=c; System.out.println ("output:" +i); output: output:99;
③ for the Byte,short,char three types, they are lateral and therefore cannot be automatically converted to each other, and can use the following coercion type conversions.
Short Char c= (char
System.out.println ("Output:" +c); output: output:c;
Forced conversions
When converting "large" data to "small" data, you can use forced type conversions.
That is, you must use the following statement format:
int n= (int) 3.14159/2;
As you can imagine, this conversion can certainly lead to a drop in overflow or precision.
2) The data type of the expression is automatically promoted, and the following rules are noted for automatic elevation of the type.
① all the values of the Byte,short,char type will be promoted to the int type;
② if one operand is long, the result is a long type;
③ if one of the operands is a float type, the result is a float type;
④ if one of the operands is double, the result is a double type;
byte b; b=3; b= (byte) (B*3); // you must declare byte.
3) Package Transition type Conversion
In general, we declare a variable first, and then generate a corresponding wrapper class, you can use the wrapper class of various methods for the type conversion. For example:
① when you want to convert the float type to double type:
float f1=100.00f; float F1=new float (F1); double D1=f1.doublevalue (); // F1.doublevalue () is a method of returning a double value type for the float class
② when you want to convert a double type to an int type:
double d1=100.00;D ouble D1=new double (d1); int i1=d1.intvalue ();
A simple type of variable is converted to the appropriate wrapper class, which can take advantage of the wrapper class's constructor. That
Boolean (boolean value), Character (char value), Integer (int value), Long (long Value), float (float value), double (double value)
And in each packing class, the total tangible is xxvalue () method, to obtain its corresponding simple type data. This method can also realize the conversion between different numerical variables, for example, for a double-precision real class, intvalue () can get its corresponding integer variable, and Doublevalue () can get its corresponding double-precision real variable.
4) conversion between strings and other types
Conversion of other types to strings
① The string conversion method of the Calling class: X.tostring (); ② automatic conversion: X+ ""; ③ method using string: String.volueof (X);
string as a value, to other types of conversions
The ① is converted to the corresponding wrapper instance before the corresponding method is converted to another type.
For example, the character "32.1" converts the value of the double type to the format:
New Float ("32.1"). Doublevalue ().
You can also use:
Double.valueof ("32.1"). Doublevalue ()
② 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);
Methods of ③character
Character.getnumericvalue (char ch)
5) Conversion of date class to other data types
There is no direct correspondence between the integer and date classes, except that you can use the int to represent the year, month, day, time, minute, and second, thus establishing a correspondence between the two, in which you can use the three forms of the date class constructor:
①date (intint int Date): Represents the year, month, Day ②date (intintint) in int intint min): Represents the year, month, day, time, Minute ③date (intint int) in int int int int sec): Type int for year, month, day, time, minute, second
There is an interesting correspondence between the long and the date classes, which is to represent a time as a number of milliseconds from 0:0 GMT, January 1, 1970, 0 seconds.
For this correspondence, the date class also has its corresponding constructor:
A date (long date).
Gets the year, month, day, time, minute, second, and week of the date class that you can use with the date class's
GetYear (), GetMonth (), GetDate (), getHours (), getminutes (), getseconds (), GetDay ()
method, you can also interpret it as converting the date class to int.
The GetTime () method of the date class can get the number of long integers that we said earlier, and as with the wrapper class, the date class has a ToString () method that can convert it to a string class.
Sometimes we want to get a specific format for date, for example 20020324, we can use the following methods, first introduced in the file,
ImportJava.text.SimpleDateFormat;ImportJava.util.*; java.util.Date Date=Newjava.util.Date ();//If you want to get the YYYYMMDD formatSimpleDateFormat sy1=NewSimpleDateFormat ("YyyyMMDD"); String DateFormat=Sy1.format (date);//If you want to get a separate year, month, daySimpleDateFormat Sy=NewSimpleDateFormat ("yyyy"); SimpleDateFormat SM=NewSimpleDateFormat ("MM"); SimpleDateFormat SD=NewSimpleDateFormat ("DD"); String Syear=Sy.format (date); String Smon=Sm.format (date); String Sday=sd.format (date);
Summary: Only Boolean does not participate in conversion of data types
(1). Conversion of automatic type: A. Constant is capable of automatic type conversion within the range of the number of tables
B. Small data range capable of automatic conversion of large data types (note exceptions)
int to Float,long to Float,long to double is not automatically converted, otherwise the precision will be lost
C. The reference type can be automatically converted to the parent class's
D. Basic types and their packaging types are capable of converting to each other
(2). Coercion type conversion: Enclose the target type in parentheses, before placing the variable
Java Fundamentals-Data type conversions