Java data type conversion detailed explanation

zhaozj2021-02-16  86

Java data types are divided into three categories, namely,, name, character, and numeric, and numeric types are intellectual and floating point; relative to data type, Java variable type is Boolean; character type Char; integer, short, int, long; floating-point float, double. Four of four integer variables and two floating-point variables correspond to different precision and ranges, respectively. In addition, we often use two types of variables, names, and DATE. Mutual conversion between these variable types is often used in our programming, in the discussion below, we will explain how to implement these conversions. 1 Type / R java data type of data type transition is generally divided into three types, respectively: (1). Conversion between simple data types (2). String and other data types (3). Other practical Data Type Conversion Let's discuss these three types of conversion respectively. 2 The conversion between simple data types is intellectual, real, and character types in Java are treated as simple data types, these types are [center] (Byte, Short, Char), respectively, from low to advanced - Int - Long - FLOAT - DOUBLE [/ center] Conversion between simple data types can also be divided into: ● Low-level to advanced automatic type conversion ● Advanced to low-level mandatory type conversion ● Packaging class transition type can be converted 2.1 automatic type conversion low level Variables can be directly converted to advanced variables, and the author is called automatic type conversion, for example, the following statement can pass directly in Java:

BYTE B; INT i = B; long L = B; float f = b; double d = B;

If the low-level type is a CHAR type, it is converted to the advanced type (integer) converted to the corresponding ASCII code value, such as / r

CHAR C = 'c'; INT I = C; System.out.Println ("Output:" i);

Output: Output: 99; For three types of Byte, Short, Char, they are flat, so they cannot automatically switch each other, and the forced type conversion can be used.

Short i = 99; char c = (char) i; system.out.println ("Output:" C);

Output: Output: C; But according to the author's experience, byte, short, int, all types are intellectual, so if you operate integer data, it is best to use the INT type. 2.2 Forced Type Conversion When the advanced variable is converted to a low-level variable, the situation is complex, you can use forced type conversion. That is, you have to use the following statement format:

INT i = 99; BYTE B = (Byte) I; char C = (char) i; float f = (float) i;

Imagine, this conversion may definitely lead to overflow or accuracy, so the author does not recommend using this conversion. 2.3 Packaging Type Transition When we discuss mutual conversion between other variable types, we need to understand the Java packaging class, so-called packaging class, you can directly represent the simple type variable as a class, execute variable types When transitioning each other, we will use these packaging classes. Java has six packaging classes, namely Boolean, Character, Integer, Long, Float and Double, which we can see them from the literal, which correspond to Boolean, CHAR, INT, LONG, FLOAT, and DOUBLE. String and Date itself are class. So there is no concept of packaging classes. When transitions between simple data types (automatic conversion or forced conversion), we can always use the package class to perform intermediate transitions. In general, we first declare a variable, then generate a corresponding package class, you can use the various methods of the packaging class to convert. For example: Example 1 Return to Double Value Method

When you want to convert Double type to int type:

Double D1 = 100.00; Double D1 = New Double (D1); INT I1 = D1.intValue ();

When you want to convert the INT type to a Double type, automatic conversion:

INT I1 = 200; Double D1 = I1;

Simple type variables are converted to the corresponding packaging class, which can utilize the constructor of the package class. That is: Boolean (Boolean Value), Character (CHAR Value), Integer (Float Value), Double (Double Value), in each packaging class, total tangible × × Value () Method to get its corresponding simple type data. With this method, the conversion between different numerical variables can be achieved, for example, for a double-precision real type, intValue () can obtain its corresponding integer variable, while DoubleValue () can get its corresponding double precision Type variable. 3 Conversions of String Types and Other Data Types You can see the membership methods provided by each class in the class library, and all classes derived from the java.lang.object class provide the toString () method to convert the class into characters. string. For example: Tostring () method for Characrer, Integer, Float, Double, Boolean, Short, etc. is used to convert characters, integrals, floating point numbers, double precision numbers, logic numbers, short interstitations such as strings. . As follows:

INT I1 = 10; float f1 = 3.14f; Double D1 = 3.1415926; Integer I1 = New Integer (I1); // Generate Integer Class / RFLOAT F1 = New Float (F1); // Generate FLOAT / RDOUBLE D1 = New Double (D1); // Generate Double Class / R / / Supreme TSTRING () method to call the package class 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); 5, do word symbol Conversion to other data types To other data types, the characteristic variable is converted to the numerical variable. In fact, there are two corresponding relationships. In the conversion of our first part, it is actually converted into a corresponding ASCII code, but We sometimes need another conversion relationship, for example, '1' refers to the value 1, not its ASCII code, for this conversion, we can use the Character's GetNumericValue (CHAR CH) method. 6. There is no direct correspondence between the DATE and other data types and the DATE class, but you can use the int type to represent the year, month, day, time, minute, second, so There is a correspondence between the two. When doing this conversion, you can use the three forms of the Date class constructor: Date (int year, int month, int Date): year, month, day Date, Int HRS, INT MIN: Take the INT type, month, day, time, minute Date, Int Year, Int Month, Int Date, Int HRS, INT min, Int Sec Sec Sec Sec Sec Sec Sec Sec Sec Sec S : In the INT type, there is a very interesting correspondence between the year, month, day, time, minute, second / R, between long integer and DATE, is to expressed a time as a distance from Greenwich, 1970. January 0:0:0:0:0:0:0:0:0:0am. For this correspondence, the Date class also has its corresponding constructor: Date (long Date) Getting the year, month, day, time, minute, seconds and weeks you can use the Date class getYear (), getMonth ( ), GetDate (), getHours (), getminutes (), getseconds (), getDay () method, you can also understand it to convert the DATE class into int. The DATE class's getTime () method can get a long-intensive number corresponding to the previous time, like the package class, and the Date class also has a toString () method can convert it to the String class. Sometimes we want to get a specific format of Date, such as 20020324, we can use the following methods, first introduced in the file,

转载请注明原文地址:https://www.9cbs.com/read-16391.html

New Post(0)