2.2 forced type conversion Converting advanced variables to low-level variables is complicated. You can use forced type conversion. You must use the following statement format: Int I = 99; byte B = (byte) I; char c = (char) I; float f = (float) I; As you can imagine, this kind of conversion will certainly lead to overflow or reduced precision. Therefore, I do not recommend this kind of conversion. 2.3 conversion of packaging type transition When we discuss the conversion between other variable types, we need to understand the Java packaging class. The so-called packaging class can directly represent a simple type variable as a class, when performing conversions between variable types, we will use these packaging classes in large quantities. Java has six packaging classes: Boolean, Character, Integer, Long, Float, and Double, literally, we can see that they correspond to boolean, char, int, long, float, and double respectively. String and Date are classes. So there is no concept of packaging. When converting between simple data types (automatic or forced conversion), we can always use the packaging class for intermediate transition. In general, we declare a variable first, and then generate a corresponding packaging class, we can use various methods of the packaging class for type conversion. For example: Example 1: If you want to convert the float type to the double type: Float f1 = 100.00f; Float F1 = new float (f1); Double d1 = F1.doubleValue (); // method that returns the double value type for a Float class whose F1.doubleValue () is When you want to convert the double type to the int type: Double d1 = 100.00; Double D1 = new Double (d1); int i1 = D1.intValue (); If you want to convert the int type to the double type, automatic conversion is performed: Int i1 = 200; double d1 = i1; You can use the constructor to convert a simple type variable to a corresponding packaging class. That is: Boolean (boolean value), Character (char value), Integer (int value), Long (long value), Float (float value), Double (double value) In each packaging class, there is always a tangible method of ×× Value () to get the corresponding simple type data. This method can also be used to convert different numeric variables. For example, for a double-precision real-type class, intValue () can get the corresponding integer variable, while doubleValue () you can get the corresponding double-precision real variables.
3 Conversion between string type and other data types By checking the member methods provided by various classes in the class library, we can see that almost all classes derived from the java. lang. Object Class provide the toString () method, which converts the class to a string. For example, the toString () method toString () of Characrer, Integer, Float, Double, Boolean, Short, and other classes () this method is used to convert characters, integers, floating-point numbers, double-precision numbers, logical numbers, and short integers into strings. As follows:Int i1 = 10; float f1 = 3.14f; double d1 = 3.1415926; Integer I1 = new Integer (i1); // generates the Integer class/rFloat F1 = new Float (f1 ); // generate Float class/rDouble D1 = new Double (d1); // generate Double class/r // respectively call the toString () of the packaging class () method to convert to String si1 = I1.toString (); String sf1 = F1.toString (); String sd1 = D1.toString (); Sysytem. out. println ("si1" + si1); Sysytem. out. println ("sf1" + sf1); Sysytem. out. println ("sd1" + sd1 ); |
|
4. Convert the numeric type directly as a value to another data type. There are actually two mappings between converting a numeric variable to a numeric variable. In the conversions we mentioned in the first part, we actually convert it into the corresponding ASCII code, however, we sometimes need another Conversion Relationship. For example, '1' refers to the value 1 rather than its ASCII code. For such conversion, we can use the getNumericValue (char ch) method of Character.
5. Conversion Between the Date class and other data types There is no direct correspondence between the integer type and the Date type, but you can use the int type to indicate year, month, day, hour, minute, and second, respectively, in this way, a ing relationship is established between the two. In this conversion, you can use the Date class constructor in three forms: Date (int year, int month, int date): indicates the year, month, and day in the int type. Date (int year, int month, int date, int hrs, int min): indicates the year, month, day, hour, and minute in the int type. Date (int year, int month, int date, int hrs, int min, int sec): year, month, day, hour, minute, second/r in int type There is an interesting ing between long integer and Date classes, that is, to express a time as the number of milliseconds from 00:00:00, January 1, January 1, 1970, GMT. For this correspondence, the Date class also has its corresponding constructor: Date (long date) You can use the getYear (), getMonth (), getDate (), getHours () the getMinutes (), getSeconds (), and getDay () methods can also be interpreted as converting the Date class to an int. The getTime () method of the Date class can obtain the long integer number corresponding to the time we mentioned earlier. Like the packaging class, the Date class also has a toString () the method can be converted to the String class. Sometimes we want to get a specific Date format, such as 20020324. We can use the following method to first introduce the Date in the file,Import java. text. simpleDateFormat; import java. util. *; java. util. date date = new java. util. date (); // if you want to obtain the YYYYMMDD format SimpleDateFormat sy1 = new SimpleDateFormat ("yyyyMMDD"); String dateFormat = sy1.format (date); // if you want to obtain the year separately, month, day SimpleDateFormat sy = new SimpleDateFormat ("yyyy"); SimpleDateFormat sm = new SimpleDateFormat ("MM"); SimpleDateFormat sd = new SimpleDateFormat ("dd "); string syear = sy. format (date); String smon = sm. format (date); String sday = sd. format (date ); |
|