Java FAQ series (8)-basic types (built-in types)

Source: Internet
Author: User

The basic type, or built-in type, is a special type different from the class in Java. They are the most frequently used types in programming, so they are always indispensable in the interview questions, in this article, we will review the knowledge related to the basic type from several aspects that are frequently used in the interview.

There are eight basic types, both of which have corresponding packaging classes. For more information, see the following table:

The basic types can be divided into three types: Char, Boolean, and byte, short, Int, long, float, and double. The numeric types include byte, short, Int, long, float, and double. The value types in Java do not have unsigned values. Their value ranges are fixed and will not change as the hardware environment or operating system of the machine changes. In fact, there is another basic type of void in Java, which also has the corresponding packaging class java. Lang. void, but we cannot directly operate on them. We do not need to remember the value range of the basic numeric type, because their values have been defined in the corresponding packaging class as constants. See the following example:

Java code

Public class primitivetypetest {public static void main (string [] ARGs) {// byte system. out. println ("basic type: byte binary digits:" + byte. size); system. out. println ("Packaging class: Java. lang. byte "); system. out. println ("minimum value: byte. min_value = "+ byte. min_value); system. out. println ("maximum value: byte. max_value = "+ byte. max_value); system. out. println (); // short system. out. println ("basic type: Short binary digits:" + short. size); system. out. println ("Packaging class: Java. lang. short "); system. out. println ("minimum value: short. min_value = "+ short. min_value); system. out. println ("maximum value: short. max_value = "+ short. max_value); system. out. println (); // INT system. out. println ("basic type: int binary digits:" + integer. size); system. out. println ("Packaging class: Java. lang. integer "); system. out. println ("minimum value: integer. min_value = "+ integer. min_value); system. out. println ("maximum value: integer. max_value = "+ integer. max_value); system. out. println (); // long system. out. println ("basic type: Long binary digits:" + long. size); system. out. println ("Packaging class: Java. lang. long "); system. out. println ("minimum value: Long. min_value = "+ long. min_value); system. out. println ("maximum value: Long. max_value = "+ long. max_value); system. out. println (); // float system. out. println ("basic type: Float binary digits:" + float. size); system. out. println ("Packaging class: Java. lang. float "); system. out. println ("minimum value: float. min_value = "+ float. min_value); system. out. println ("maximum value: float. max_value = "+ float. max_value); system. out. println (); // double system. out. println ("basic type: double binary digits:" + double. size); system. out. println ("Packaging class: Java. lang. double "); system. out. println ("minimum value: double. min_value = "+ double. min_value); system. out. println ("maximum value: double. max_value = "+ double. max_value); system. out. println (); // char system. out. println ("basic type: Char binary digits:" + character. size); system. out. println ("Packaging class: Java. lang. character "); // convert character to a numeric value instead of a character. min_value is output to the console system. out. println ("minimum value: character. min_value = "+ (INT) character. min_value); // convert character in the numeric form instead of the character form. max_value is output to the console system. out. println ("maximum value: character. max_value = "+ (INT) character. max_value );}}

Running result:

1. Basic Type: byte binary digits: 8
2. Packaging class: Java. Lang. byte
3. Minimum value: byte. min_value =-128
4. Maximum Value: byte. max_value = 127
5,
6. Basic Type: Short binary digits: 16
7. Packaging class: Java. Lang. Short
8. Minimum value: short. min_value =-32768
9. Maximum Value: short. max_value = 32767.
10,
11. Basic Type: int binary digits: 32
12. Packaging class: Java. Lang. Integer
13. Minimum value: integer. min_value =-2147483648
14. Maximum Value: integer. max_value = 2147483647
15,
16. Basic Type: Long binary digits: 64
17. Packaging class: Java. Lang. Long
18. Minimum value: Long. min_value =-9223372036854775808
19. Maximum Value: Long. max_value = 9223372036854775807
20,
21. Basic Type: Float binary digits: 32
22. Packaging class: Java. Lang. Float
23. Minimum value: float. min_value = 1.4e-45
24. Maximum Value: float. max_value = 3.4028235e38
25,
26. Basic Type: double binary digits: 64
27. Packaging class: Java. Lang. Double
28. Minimum value: Double. min_value = 4.9e-324
29. Maximum Value: Double. max_value = 1.7976931348623157e308
30,
31. Basic Type: Char binary digits: 16
32. Packaging class: Java. Lang. Character
33. Minimum value: character. min_value = 0
34. Maximum Value: character. max_value = 65535

The minimum and maximum values of float and double are both output in the form of scientific notation. The ending "e + number" indicates the number before e multiplied by 10. For example, 3.14e3 is 3.14 × 1000 = 3140, and 3.14e-3 is 3.14/1000 = 0.00314.

When you carefully compare the running results with the preceding table, you will find that the minimum values of float and double types are different from those of float. min_value and double. min_value. Why? In fact, float. min_value and double. min_value respectively indicate the smallest positive numbers that float and double can represent. That is to say, the value float between 0 and ± float. min_value cannot be expressed, and the value double type between 0 and ± double. min_value cannot be expressed. This is not surprising because the values in these ranges are beyond their precision ranges.

The basic types are stored in the stack, so their access speed is faster than the instance objects of the corresponding packaging class stored in the heap. From java5.0 (1.5), Java Virtual Machine (VM) can automatically convert the basic types and their corresponding packaging classes. Therefore, when assigning values, passing parameters, and performing mathematical operations, we use their packaging classes like using basic types, but this does not mean that you can call their packaging classes through basic types. In addition, the packaging classes of all basic types (including void) use final modifier, so we cannot inherit from them to extend new classes or override any of their methods.

What are the rules for value assignment and conversion between various numeric types? Let's take a look at the following example:
Java code

Public class primitivetypetest {public static void main (string [] ARGs) {// when assigning values to byte type variables, the number does not need to be suffixed with byte byte_a = 1; // The Compiler checks the range. If the value is out of the range, an error is returned. // byte byte_ B = 1000; // assign a long value to the BYTE variable, an error is reported during compilation, even if the value does not exceed the value range of byte type // byte byte_c = 1l; // when assigning a value to the short type variable, the number does not need to be suffixed with short short_a = 1; // The Compiler checks the range. If the value is out of the range, an error is returned. // short short_ B = 70000; // assign a long value to a short variable. An error is reported during compilation, even if the value does not exceed the short value range. // byte short_c = 1l; // when assigning values to a short type variable, the number does not need to be suffixed with int int_a = 1; // The Compiler checks the range, if the value is out of the range, an error will be reported // int int_ B = 2200000000; // assign a long value to the int type variable. An error will be reported during compilation, even if the value does not exceed the value range of the int type, // int int_c = 1l; // you can directly assign an int value to the long variable, long long_a = 1 without a suffix after a number; // if the value given to the long variable exceeds the int value range, add L after the number (Case Insensitive) long long_ B = 2200000000l; // The Compiler checks the range. If the value is out of the range, an error is returned. // long long_c = 930020.0000000l; // you can assign an int value to the float variable float float_a = 1; // you can assign a long value to the float variable float float_ B = 1l; // floating point numbers without the F (case-insensitive) Suffix are double by default, and cannot be directly assigned to the float variable // float float_c = 1.0; // a float value requires an F (case-insensitive) Suffix to mark float float_d = 1.0f; // assign a double value to a float variable. An error is reported during compilation, even if the value does not exceed the value range of the float type, // float float_e = 1.0d; // The Compiler checks the value range, if the value is out of the range, an error is returned. // float float_f = 3.50020.e38f; // you can assign an int value to the double variable double double_a = 1; // you can assign a long value to the double variable double double_ B = 1l; // you can assign a float value to the double variable double double_c = 1f; // floating point numbers without a suffix are of the double type by default. You can directly assign a value of double double_d = 1.0; // You can also add a suffix of D (Case Insensitive) to the number, the double double_e = 1.0d of the double type is clearly marked; // The Compiler checks the range. If the value is out of the range, an error is reported. // double double_f = 1.80020.000000000e308d; // assign a value of the double type to a byte type variable. An error is reported during compilation, even if the value does not exceed the value range of the byte type. // byte byte_d = 1.0d; // assign a value of the double type to a variable of the short type. An error is reported during compilation, even if the value does not exceed the value range of the short type. // short short_d = 1.0d; // assign a value of the double type to an int type variable. An error is reported during compilation, even if the value does not exceed the value range of the int type. // int int_d = 1.0d; // assign a value of the double type to a long variable. An error is reported during compilation, even if the value does not exceed the value range of the long type. // long long_d = 1.0d; // you can use a character to initialize a char variable char char_a = 'a'; // you can also use an int value to initialize the char variable char char_ B = 1; // assign a long value to a char variable. An error is reported during compilation, even if the value does not exceed the char value range. // char char_c = 1l; // assign a float value to a char variable. An error is reported during compilation, even if the value does not exceed the char value range. // char char_d = 1.0f; // assign a double value to a char variable. An error is reported during compilation, even if the value does not exceed the char value range. // char char_e = 1.0d; // The Compiler checks the range. If the value is out of the range, an error is returned. // char char_f = 70000 ;}}

From the above example, we can draw the following conclusions:

1. integers without a character suffix are of the int type by default. Floating-point numbers without a character suffix are of the double type by default.
2. If the value of an integer exceeds the range that can be expressed by the int type, the suffix "L" must be added (case-insensitive. We recommend that you use uppercase letters, because the lower-case l and Arabic numerals 1 are easily confused), expressed as long.
3. All integers and floating-point numbers with the "f" (case-insensitive) Suffix are float; All integers and floating-point numbers with the "D" (case-insensitive) Suffix are double.
4. the compiler checks the values of byte, short, Int, long, float, double, and char variables during compilation. If the values exceed the value range, an error is returned.
5. Int values can be assigned to all numeric variables; long values can be assigned to long, float, and double variables; A float value can be assigned to a float or double type variable. A double value can only be assigned to a double type variable.

Displays the default logical conversion relationships between several basic types:

The solid line in the figure indicates the conversion without loss of precision, while the dotted line indicates that such conversion may lose a certain degree of precision. If we want to convert a type that can represent a larger range or a higher precision to a type with a smaller range or a lower precision, we need to use cast. However, we should try to avoid this usage because it often causes errors. Please refer to the example below. Can you predict the result of the code without running it?

Java code

Public class primitivetypetest {public static void main (string [] ARGs) {int A = 123456; short B = (short) A; // What is the value of B? System. Out. println (B );}}

Running result:

1.-7616

Influence of Operators on Basic Types

When you use the plus (+), minus (-), minus (*), minus (/), minus (%) operators to calculate the basic type, follow the following rules:

1. As long as one of the two operands is of the double type, the other will be converted to the double type, and the result is also of the double type;
2. Otherwise, as long as one of the two operands is of the float type, the other will be converted to the float type and the result will also be of the float type;
3. Otherwise, if one of the two operands is of the long type, the other will be converted to the long type and the result is also of the long type;
4. Otherwise, both operands (including byte, short, Int, and char) will be converted to the int type, and the result is also of the int type.

When you use the + =,-=, * =,/=, % =, and operator to calculate the basic types, follow the following rules:

1. The value on the right of the operator is first forcibly converted to the same type as the value on the left of the operator, and then executed. The calculation result is of the same type as the value on the right of the operator.

After learning about this, we can answer the frequently asked questions below. See:

Short S1 = 1; S1 = S1 + 1; what is the error? Short S1 = 1; S1 + = 1; what is the error?

At first glance, I think they should all be correct and can run normally. Let's write an example and try it:
Java code

Public class primitivetypetest {public static void main (string [] ARGs) {short S1 = 1; // This line of code reports a compilation error // S1 = S1 + 1; // No error is returned for this line of code S1 = 1 + 1; // No error is returned for this line of code S1 + = 1 ;}}

We can see the result from the example. The rules listed above can also be easily explained. In S1 = S1 + 1;, the result of the S1 + 1 operation is int type. assign it to a short type variable S1, so an error is reported. In S1 + = 1; because S1 is of the short type, 1 is forcibly converted to the short type first, and then involved in the operation. The result is of the short type, so no error is reported. So, S1 = 1 + 1; why is no error reported? This is because 1 + 1 is a constant that can be determined during compilation. The "+" operation is executed during compilation, rather than during program execution, this statement is equivalent to S1 = 2, so no error is reported. As mentioned above, forced type conversion for basic types may produce incorrect results. Therefore, when using operators such as + =,-=, * =,/=, and % =, pay more attention to it.

Math. Round () method

The Java. Lang. Math class has two round () methods. Their definitions are as follows:
Java code

public static int round(float a) {       //other code   }     public static long round(double a) {       //other code   }  

Their return values are all integers, and they all adopt the rounding method. The calculation rules are as follows:

1. If the parameter is positive and the first digit after the decimal point is greater than or equal to 5, the calculation result is the integer part of the parameter + 1.
2. If the parameter is negative and the first digit after the decimal point is greater than 5, the operation result is the integer part of the parameter-1.
3. If the parameter is positive and the first digit after the decimal point is <5; or the parameter is negative and the first digit after the decimal point is <= 5, the operation result is an integer part of the parameter.

We can use the following example to verify:
Java code

Public class mathtest {public static void main (string [] ARGs) {system. out. println ("first digit after decimal point = 5"); system. out. println ("positive number: math. round (11.5) = "+ math. round (11.5); system. out. println ("negative number: math. round (-11.5) = "+ math. round (-11.5); system. out. println (); system. out. println ("first digit after decimal point <5"); system. out. println ("positive number: math. round (11.46) = "+ math. round (11.46); system. out. println ("negative number: math. round (-11.46) = "+ math. round (-11.46); system. out. println (); system. out. println ("first digit after decimal point> 5"); system. out. println ("positive number: math. round (11.68) = "+ math. round (11.68); system. out. println ("negative number: math. round (-11.68) = "+ math. round (-11.68 ));}}

Running result:

1. First digit after decimal point = 5
2. Positive number: Math. Round (11.5) = 12
3. Negative number: Math. Round (-11.5) =-11
4,
5. First digit after decimal point <5
6. Positive number: Math. Round (11.46) = 11
7. Negative number: Math. Round (-11.46) =-11
8,
9. First digit after decimal point> 5
10. Positive number: Math. Round (11.68) = 12
11. Negative number: Math. Round (-11.68) =-12

Based on the running results of the above example, we can also summarize the results as follows, which may be easier to remember:

1. the first digit after the decimal point of the parameter <5. The calculation result is the integer part of the parameter.
2. the first digit> 5 after the decimal point of the parameter. The calculation result is the absolute value + 1 of the integer part of the parameter, and the symbol (that is, positive and negative) remains unchanged.
3. the first digit after the decimal point of the parameter is 5. The positive value is an integer plus 1, and the negative value is an integer.

Switch statement

Which types can be used to determine the switch statement? Let's do a test and we will know:
Java code

Public class mathtest {// Enumeration type. For Java and later versions, static Enum enum_e {a, B} public static void main (string [] ARGs) {// byte byte_n = 0; switch (byte_n) {Case 0: system. out. println ("byte can be used for switch statement"); break;} // byte byte_m = 0; // switch (byte_m) must be supported by Java 1.5 () or later) {Case 0: system. out. println ("Byte Class can be used for switch statement"); system. out. println (); break;} // char char_n = 0; Switch (char_n) {Case 0: system. out. println ("char can be used for switch statement"); break;} // character char_m = 0; // switch (char_m) must be supported by java5.0 (1.5) or later) {Case 0: system. out. println ("character class can be used for switch statements"); system. out. println (); break;} // short short_n = 0; Switch (short_n) {Case 0: system. out. println ("short can be used for switch statement"); break;} // short short_m = 0; // switch (short_m) {Case 0 is supported in Java 1.5 () or later versions: system. out. println ("short class can be used for switch statements"); system. out. println (); break;} // int int_n = 0; Switch (int_n) {Case 0: system. out. println ("int can be used for switch statement"); break;} // integer int_m = 0; // switch (int_m) must be supported by java5.0 (1.5) or later) {Case 0: system. out. println ("integer class can be used for switch statement"); system. out. println (); break;} // long long_n = 0; // compilation error. The long type cannot be used in the switch statement // switch (long_n) {// case 0: // system. out. println ("Long can be used for switch statement"); // break; //} // long long_m = 0l; // compilation error, the long type cannot be used in the switch statement // switch (long_m) {// case 0: // system. out. println ("long class can be used for switch statement"); // system. out. println (); // break; //} // float float_n = 0.0f; // compilation error. The float type cannot be used in the switch statement/switch (float_n) {// case 0.0f: // system. out. println ("float can be used for switch statements"); // break; //} // float float_m = 0.0f; // compilation error, the float type cannot be used in the switch statement // switch (float_m) {// case 0.0f: // system. out. println ("float class can be used for switch statement"); // system. out. println (); // break; // double double_n = 0.0; // compilation error. The double type cannot be used in the switch statement // switch (double_n) {// case 0.0: // system. out. println ("Double can be used for switch statement"); // break; //} // double double_m = 0.0; // compilation error, the double type cannot be used in the switch statement // switch (double_m) {// case 0.0: // system. out. println ("Double class can be used for switch statement"); // system. out. println (); // break; //} // Boolean bool_ B = true; // compilation error. The boolean type cannot be used in the switch statement/switch (bool_ B) {// case true: // system. out. println ("Boolean can be used for switch statement"); // break; //} // Boolean bool_l = true; // compilation error, the boolean type cannot be used in the switch statement // switch (bool_l) {// case true: // system. out. println ("Boolean class can be used for switch statement"); // system. out. println (); // break; // String object string string_s = "Z"; // compilation error. The long type cannot be used in the switch statement // switch (string_s) {// case "Z": // system. out. println ("string can be used for switch statement"); // system. out. println (); // break; //} // Enum (Enumeration type, available for Java or later) Switch (mathtest. enum_e.a) {Case A: system. out. println ("Enum can be used in the switch statement-a"); break; Case B: system. out. println ("Enum can be used in switch statement-B"); break ;}}}

The running result is as follows:

1. byte can be used in switch statements
2. The Byte Class can be used for switch statements.
3,
4. Char can be used in switch statements
5. The character class can be used for switch statements.
6,
7. Short can be used in switch statements
8. The short class can be used for switch statements.
9,
10. Int can be used in switch statements.
11. The integer class can be used for switch statements.
12,
13. Enum can be used in switch statement-

The results have come out. Let's summarize:

1. byte, Char, short, Int, and their packaging classes (supported by Java 1.5/or later) can be used in switch statements.
2. Long, float, double, Boolean, and their packaging classes (in all Java versions) cannot be used for switch statements.
3. The Enum type, that is, the enumeration type can be used for switch statements, but it is only supported in Java 1.5 or later versions.
4. All types of objects (including the string class, but in java5.0/1.5 or later versions, this item should exclude the packaging classes corresponding to the byte, Char, short, and INT types) cannot be used in switch statements.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.