JAVA basic data type initialization learning Notes

Source: Internet
Author: User

Except boolean, which defaults to false, char -->/u0000, all values are 0.

The difference between the basic data type and the reference type is that the basic data type is allocated on the stack, and the reference type is allocated on the stack.

Therefore, let's analyze the difference between "=" and equals.

First, I will take two String objects

The code is as follows: Copy code

String a = "abc ";

String B = "abc ";

Then

If (a = B) {System. out. println ("a = B ");}

Else

{System. out. println ("! = B ");}

Program output! = B

Cause: The addresses of a and B are different. a = B compares the addresses of two variables.

Define two basic types

The code is as follows: Copy code

Int a = 4;

Int B = 4;

If (a = B) {System. out. println ("a = B ");}

Else

{System. out. println ("! = B ");}

Output: a = B

Cause: = compare the content of two variables

See the following example:
Java code

The code is as follows: Copy 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 ");
// Output Character. MIN_VALUE to the console in the form of a number rather than a Character
System. out. println ("minimum value: Character. MIN_VALUE ="
+ (Int) Character. MIN_VALUE );
// Output Character. MAX_VALUE to the console in the form of a number rather than a Character
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.

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.