Data type conversions:
(1). Automatic conversion
Low-level variables can be converted directly to advanced variables, which are called automatic type conversions. Like what:
byte b; int b; long b; float B; double b;
The above statement can be passed directly in Java.
(2). If the low-level type is char, the conversion to the advanced type (int) is converted to the corresponding ASCII value, for example:
char c = ' c '; int i = C; System.out.println ("Output:" +i);
Output: output:99
(3). For Byte/short/char three types, they are the same level, so you cannot automatically convert types to each other, and you can use forced type conversions.
Short i=99; Char c= (char) i; System.out.println ("Output:" +c);
Output: Output:c
(4). Advanced type conversions to low-level types can cause a loss of precision.
(5). Packaging transition type conversion (to compensate for the loss of accuracy in 4)
Java has 6 packaging classes are boolean,character,integer,long,float and double respectively;
String and date itself are classes, and there is no wrapper class.
e.g: float--->double
float f1 = 100.00f;
float F1 = new float (F1);
Double D1 = F1.doublevalue ();
e.g: double--->int
Double D1 = 100.00;
Double D1 = new double (d1);
int i1 = D1.intvalue ();
(6) Conversion of string types to other data types
Class Test {
public static void Main (String args[]) {
int i1 = 10;
float f1 = 3.14f;
Double D1 = 3.1415926;
Integer I1 = new Integer (i1);
float F1 = new float (F1);
Double D1 = new double (d1);
String SI1 = i1.tostring ();
String sf1 = F1 . toString ();
String SD1 = D1. toString ();
System.out.println ("SI1" +SI1);
System.out.println ("sf1" +SI1);
System.out.println ("sd1 " +SI1);
}
}
(7) Convert character type directly to other data types as numeric values
Using the character getnumericvalue (char ch) method
Java Fundamentals Hardening Data type conversions in 22:java