A data type conversion
1.1 automatic conversion
/* When the data is not the same type, data type conversion will occur. Automatic type conversion (implicit) 1, Features: Mother does not require special handling, automatically. 2. Rules: data range from small to large. */ public class Demo01DataType{ public static void main(String[] args){ System.out.println ( 1024); // this is an integer, the default type is int System.out.println (3.14); // this is a floating-point number, the default type is double // long num1 = 100L; // left is long type, the right is the default type int, left and right are not the same // a equals sign represents the assignment, the int constants on the right, to the left of the long variable to store // int -> long, the range of data in line with the requirements of small to large // this line of code type conversion occurs automatically. Long num1 = 100 ; System.out.println(num1);// 100 // left is double type, the right is of type float around different // float -> double, from small to large in line with the rules // also undergone automatic type conversion double num2 = 2.5F ; System.out.println(num2);// 2.5 // left is the float type, the type of right is long, left and right are not the same // long -> float, float range is larger, from small to large in line with the rules // also undergone automatic type conversions float num3 = 30L ; System.out.println(num3);// 30.0 } }
1.2 cast
/* Cast 1, features: It needs to be a special formatting can not be done automatically. 2, the format: a small range of types of small-scale variable name = (small type) had a large range of data; * / Public class Demo02DataType { public static void main (String [] args) { // left is int type, the type of right is long, is not the same // long -> int, not from small to large // does not happen automatically type conversion // format: small small range of types of variable name = (small type) had a large range of data; int NUM = ( int ) 100L ; System.out.println(num); } }
1.3 Conversion Notes
/* Precautions: 1, cast generally not recommended because of the potential loss of precision occurs, data overflow. 2, byte / short / char these three types of mathematical operations can occur, for example: "+." 3, bytp / short / char in the operation of these three types of time, will be promoted to become first type int, then in calculations
4, boolean data type conversion type can not happen * / public class Demo02DataType { public static void main (String [ ] args) { // Long forcibly converted into an int int num2 = ( int ) 6000000000L ; // overflow System.out.println (num2); // 1705032704; if there is already data that exceeds the range int int below it is not fit, do not hold open. // Double -> int: loss of precision fractional decimal place will be lost to integer int num3 = ( int ) 3.5 of ; System.out.println (num3); // 3, this is not a rounding all decimal places will be discarded. char zifu1 = 'A'; // this variable is a character, which is uppercase A System.out.println (+ zifu1. 1); // 66 is uppercase A is treated as 65 // underlying computer will use a digital (binary) to represent the characters a, is 65 // Once char types of mathematical operations, then the character will be translated into a number according to certain rules byte Num4 = 40; // Note! Value can not exceed the size of the right type of range to the left byte Num5 = 50 ; // byte + byte ---> int + int ---> int int RESULT1 + = Num4 Num5; System.out.println(result1);// 90 Short Num6 = 60 ; // byte Short --- +> + int int ---> int // int + result2 = Num4 Num6; // int cast short: Note logic must ensure that the real size would have no more than short range, or data overflow may occur short result2 = ( short ) (+ Num4 Num6); System.out.println(result2);// 100 } }
1.4 ASCII code table
/* Control numbers and characters table (code table) ASCII code table: American Standard Code for Information Interchange, American Standard Code for Information Exchange Unicode code table: it played, but also the relationship between the control numbers and symbols, a part of the beginning of 0-127 ASCII exactly the same, but from the beginning contains 128 more characters. 48 - '0' 65 - 'A' 97 - 'a' */public class Demo03DataTypeChar{ public static void main(String[] args){ char zifu1 = '1'; System.out.println(zifu1 + 0);//49 char zifu2 = 'A'; char zifu3 = 'C' ; // left is of type int, char type is the right // char ---> int, from small to large indeed // automatic type conversions occurred int NUM = zifu3; System.out.println(num); // 99 char zifu4 = 'and'; // correct wording System.out.println (zifu4 + 0); // 20013 } }