Hello, everyone. Today, let's share with you about the data type extension of Java
As long as you read my previous blog, you will have a clear understanding of java data types
In that case, let's share the data type extension of Java today
Extensions to java basic data types
Let's take a direct look at my extension code for Java data types
public class ShuXi { public static void main(String[] args) { //Integer extension: binary 0b decimal octal 0x int m =10; int m2 =010; //Octal 0 int m3 =0x10; //Hex 0x System.out.println(m); System.out.println(m2); System.out.println(m3); System.out.println("=================================");//This is to display the split line of each part on the console //============================================= //Floating point extension? Industries requiring high data accuracy //float: // double float f = 0.1f; //0.1 double d = 1.0/10; //0.1 //The above uses two different data types to assign values to variables f and d // f=0.1 d=1.0/10=0.1 //So in computational logic, f and d are equal System.out.println(f==d); //Compare whether f and d are equal //true if the condition holds, false if the condition does not hold System.out.println("==================="); //Show split lines //Let's write another one below float r1 =21212125242545452f; //We define r1 as 21252425452f float r2=r1+1; //Define a value where r2 equals r1 plus 1 //So in arithmetic logic, r1 is not equal to r2 System.out.println(r1==r2); //Determine whether r1 is equal to r2 //Once again, we were surprised to find that r1 is equal to r2 //We were surprised to find that they were not equal //It is concluded that the characteristics of float are finite and discrete. Rounding error, close to but not equal to approximately //It is best to avoid floating-point comparisons //BigDecimal we can use this tool class //========================================= //Character extension: //================================ System.out.println("============================"); char c1 = 'a'; //Take char as the data type and assign a value to our custom variable c1 char c2 = 'in'; //Take char as the data type and assign "medium" to our custom variable c2 System.out.println(c1); //Assignment corresponding to output c1 System.out.println((int)c1); //Cast (char as) System.out.println(c2); //Assignment corresponding to output c1 System.out.println((int)c2); //Force conversion //At this time, we can see that the assignment corresponding to c1 is a, which is an English character, and the assignment of c2 is medium, which is a Chinese character //All characters are essentially numbers //In this example, cast converts characters to numbers //char is a data type, which involves coding //Encoding Unicode table 97 = a 65 = a 2 bytes 0-65536 excel 216 = 65536 //U0000 UFFFF char c3 = '\u0061'; System.out.println(c3); //a System.out.println("==================="); //Show split lines //Escape 2 characters // \t tab // \n line feed System.out.println("hello\tworld"); //There is a space between two words System.out.println("hello\nworld"); // Two lines are written between two words System.out.println("==================="); //Show split lines //Boolean extension boolean flag = true; if (flag==true){} if (flag){} } }
1. The above is a whole code with a large amount of content
2. I wrote notes in almost every part
3. I will show you the effect of each part of the code on the console below
4. I will explain a lot of things inside
First:
public class ShuXi { public static void main(String[] args) { //This is the beginning. It is an overall framework of the Java program. The class is followed by the class name, that is, ShuXi is the class name, which must be consistent with the java file name
second
//Integer expansion: binary 0b decimal octal 0x hexadecimal 0x10 int m =10; int m2 =010; //Octal 0 int m3 =0x10; //Hex 0x System.out.println(m); //Assignment of output m System.out.println(m2);//Assignment of output m2 System.out.println(m3);//Assignment of output m3
This is an extension of the integer type
1. Understand the different manifestations of the number system, that is, writing
2. The custom variables m, M2 and m3 are assigned as the special writing method of the corresponding base system
Look at the console effect
Second:
Floating point extension
//Floating point extension? Do not use in industries requiring high data accuracy //float: // double float f = 0.1f; //0.1 double d = 1.0/10; //0.1 //The above uses two different data types to assign values to variables f and d // f=0.1 d=1.0/10=0.1 //So in computational logic, f and d are equal System.out.println(f==d); //Compare whether f and d are equal //true if the condition holds, false if the condition does not hold //We were surprised to find that they were not equal System.out.println("==================="); //Show split lines //Let's write another one below float r1 =21212125242545452f; //We define r1 as 21252425452f float r2=r1+1; //Define a value where r2 equals r1 plus 1 //So in arithmetic logic, r1 is not equal to r2 System.out.println(r1==r2); //Determine whether r1 is equal to r2 //Once again, we were surprised to find that r1 is equal to r2 //It is concluded that the characteristics of float are finite and discrete. Rounding error, close to but not equal to approximately //It is best to avoid floating-point comparisons //BigDecimal we can use this tool class
Look at the console effect
1. The first floating-point code we wrote is correct in mathematical logic, and the result shows false
2. The first floating-point code we wrote is wrong in mathematical logic, and the result shows true (right)
3. Conclusion: float characteristics: finite, discrete. Rounding error, close to but not equal to approximately
4. It is best to avoid using floating-point number comparison
Third: character extension
//Character extension: //================================ System.out.println("============================"); char c1 = 'a'; //Take char as the data type and assign a value to our custom variable c1 char c2 = 'in'; //Take char as the data type and assign "medium" to our custom variable c2 System.out.println(c1); //Assignment corresponding to output c1 System.out.println((int)c1); //Cast (char as) System.out.println(c2); //Assignment corresponding to output c1 System.out.println((int)c2); //Force conversion //At this time, we can see that the assignment corresponding to c1 is a, which is an English character, and the assignment of c2 is medium, which is a Chinese character //All characters are essentially numbers //In this example, cast converts characters to numbers //char is a data type, which involves coding //Encoding Unicode table 97 = a 65 = a 2 bytes 0-65536 excel 216 = 65536
//U0000 UFFFF char c3 = '\u0061'; System.out.println(c3); //a
See the console effect:
//Escape character // \t tab // \n line feed System.out.println("hello\tworld"); //There is a space between two words System.out.println("hello\nworld"); // Two lines are written between two words System.out.println("==================="); //Show split lines
See the console effect:
//Boolean extension boolean flag = true; //It's written by a vegetable bird if (flag==true){} if (flag){} //Written by an expert
`
The extension of these data types may be frequently asked in interviews. After mastering the eight basic data types of java, you need to look at these things
Well, that's all for the extension of data types
If you have any questions, please chat in private. If you have steps, please give advice. Thank you