Discussion on the Size, Reference and Conversion of Java Basic Data Types

Practice every day


foreword

Follow the tutor Xiaobaibai to learn Java, check in the various mobs you meet on the Java road, the homepage of the tutor blog:

 The blog of Xiaobaibai who loves programming_CSDN blog-[JAVA basic + advanced], SSM+Boot+mybatis plus, blogger in the field of WeChat applet


1. Commonly used basic types of data

       The first is the integer type and the character type. The variable definition method is [type variable name = initial value]. It should be noted that the assignment of the character type data must use single quotes, not double quotes.

        Then there are floats and booleans, which have the following lengths:

       It can be seen that the memory size of the type to which each variable belongs increases in turn: byte  < short  < int  < float  < double

       There are also types: unsigned int : 0~2^32-1; long : -2^63 ~ 2^63-1, that is, -9223372036854774808 ~ 9223372036854774807. 8 bytes. Long. Add L at the end. (You can also not add L), etc., but we rarely use it, so I won’t give an example

2. Mandatory conversion between types

        By converting the memory size, we can know the actual stored decimal value of this type. As shown in the figure above, when the defined variable is lower than or exceeds the value range of the type, the editor will report an exception. Just now we know that the length of the floating-point type is larger than that of the integer type, and the integer type int is larger than the short integer type short, so there are two cases when we force the conversion type:

       This is the first case. Types with large lengths are merged with types with small lengths , and the values ​​of byte and short types are assigned to int type variable d3, which can receive all of them and retain the original value size. (But I don't know what the character type b2 passed over)

 

        This is the second case, using a data type with a small length to receive a data type with a large length , the syntax format here is

[Type variable name = (the type to be converted) initial value], as long as the mandatory conversion is added, the editor will convert it for you, but the result may not be satisfactory. For details of the calculation in the computer memory during the conversion, please refer to my tutor. Type conversion blog, link is:

 [JAVA Basics] Commonly used operators for type conversion process control - Programmer Sought

3. The specificity of the reference type

      The String type is a string type. It is different from the char type in that it can store multiple characters, and chat can only store a single character, so its variables must be wrapped in double quotes. In java, the array type is also a reference type, and the definition and The method of use is as shown in the figure:

        It is worth mentioning that although both strings and arrays are reference types, strings have the characteristics of value types, which means that changes in the values ​​of other variables will not affect their own variables. But when the array type points to the same storage unit, the change of one variable will cause the change of other variables. We can clearly understand it through the following two figures.

      It can be seen that the String type s3 is equal to the value of s2, but changing the value of s2 will not affect s3, because both s2 and s3 point to a storage unit. But when the value of s2 changes, the system will open up a space for s2 to store the new value, and point to the address of this space. At this moment, if the value of s3 also changes, the system will also open up a space for s3 to store the new value, and the previous space for storing the value will be discarded, waiting for the system's recycling mechanism to release resources.

     Although the value of an array type variable is also placed in the heap, when multiple variables refer to the same storage unit and one of them changes, the system will not create a new space to store the new value, but directly overwrite the original value. I am not too clear about the specific details, if there is something wrong, please point out and correct it.


Summarize


For example: the above are the differences and introductions of some basic types of Java, check in every day, learn Java, and encourage everyone

Guess you like

Origin blog.csdn.net/qq_51294997/article/details/130719816