Java Miscellaneous 1 - Data Types and Variables

data

Before we talk about variables, let's talk about what data is? Variables and data are inseparable.

Data is actually binary in the computer, 0101010101. These binaries are actually inconvenient to operate and manage. Who can directly identify binary strings?

no. Therefore, in order to facilitate management and operation, the concepts of data types and variables are introduced in almost all high-level languages.

type of data

Knowing what data is, then the data type is easy to explain. The data type is actually used to classify the data, which is easy to understand and operate.

The data is binary inside the computer. It is difficult for binary programmers to understand and operate directly. Use int/long for integers, float/double for decimals, and String for strings. Using these data types and classes, programmers understand and It is more convenient to operate

After classifying the data through the data type definition, the computer can standardize the allocation of different sizes of memory space for different data types.

The role of data types

  1. Type checking improves security
  2. Reasonable memory allocation

For example, there are eight basic data types in java

  • Integer type: byte/short/int/long
  • Decimal type: float/double
  • Character type: char
  • Boolean type: boolean

Just like everything in the world is composed of 108 basic elements in the periodic table, the basic data type is equivalent to the basic elements in chemistry, and the object is equivalent to everything in the world, String is composed of char, and the date type is long.

floating point number
Introduction to floating point types

There is no need to cast the long type to the float type, that is to say, compared with the float type, the long type is a small type, and the storage range is smaller. However, the float only occupies 4 bytes, while the long occupies 8 bytes. The storage space of the long type is larger than that of the float type.

Floating point numbers use the IEEE (Institute of Electrical and Electronics Engineers) format. Floating point types are represented using a sign bit, exponent, and significand (mantissa).

In java, float and double are structured as follows:

type|sign bit|exponent field|significance field

-|-|-

float|1bit|8bit|23bit

double|1bit|11bit|52bit

precision of floating point numbers
  1. The stored decimal value may be an ambiguous value

    Why do you say that? see the code below

    ~java
    public static void main(String[] args) {
    double d1 = 0.1;
    double d2 = 0.2;
    System.out.println(d1+d2 == 0.3);
    System.out.println(d1+d2);
    }
    ~

    The result of running the above code is false 0.30000000000000004

    That is, 0.1 plus 0.2 of the double type is not exactly 0.3. It should be noted that such a result is not due to an operation error, but that binary cannot accurately represent 0.3 in decimal. Just like decimal 3/10 cannot be represented exactly by decimals, many decimals cannot be represented exactly by floating point.

    The decimal of the decimal is converted into binary, and the decimal of the decimal must be continuously multiplied by 2 until the final result is an integer, which is the final binary value, but it is possible that the final result is an infinite value, which cannot be represented by a floating-point type.

    But for integers , within the valid range of floating-point numbers, they are all exact. Conversion algorithm: The algorithm for converting decimal integers into binary is to continuously find the remainder of 2, and there will be no infinite value.

  2. Significance and precision of floating-point numbers

    The number of significant digits that can be represented by a floating-point type is limited, so even if an integer exceeds the number of significant digits, it can only store approximate values, that is, data beyond the least significant digits will be lost, resulting in loss of precision.
    The binary significant bits of the float type are 24 bits, corresponding to 7 to 8 decimal digits; the double type has 53 binary digits, corresponding to 10 to 11 decimal digits.

    The range that double and float types can represent is wider than that of int and long types. However, it cannot represent integers perfectly, and the loss of precision of floating-point types can cause some problems.

    public static void main(String[] args) {
       int a = 3000000;
       int b = 30000000;
       float f1 = a;
       float f2 = b;
       System.out.println("3000000==3000001 "+(f1==f1+1));
       System.out.println("30000000==30000001 "+(f2==f2+1));
       System.out.println("3000000的有效二进制位数:"+ Integer.toBinaryString(a).length());
       System.out.println("30000000的有效二进制位数:"+ Integer.toBinaryString(b).length());
    }

    operation result:

    3000000 == 3000001 false
    30000000 == 30000001 true
    Significant binary digits of 3000000: 22
    Significant binary digits of 30000000: 25

    The above example is a good example of the consequences of the loss of precision: 30000000==30000001the comparison is actually true. The reason for this result is that the effective binary digits of 30000000 are 25 bits, which exceeds the 24 significant digits that float can represent, and the last digit is discarded, so the 1 just added is also discarded. , so the floating-point representation before and after the addition of 30000000 is the same.

    If the floating point type in the above example uses double, there is no loss of precision, because the precision of double is 52 bits.

  3. How to fix floating point precision loss problem

    JDK provides us with two high-precision large number operation classes: BigInteger and BigDecimal.

Char type

char is used to represent a character, which can be a Chinese character or an English character. Enclose constant characters in single quotes when assigning.

We know that when dealing with characters inside java, Unicode is used. char is essentially an unsigned positive integer that occupies two bytes fixedly. This positive integer corresponds to the Unicode number and is used to represent the character corresponding to that Unicode number. Due to the fixed occupation of two bytes, char can only represent characters whose Unicode numbers are within 65536, but cannot represent characters beyond the range. For those beyond this range, two chars are used.

assignment of char
//Ascii码
char c = 'A'
//中文字符
char c = '哈'
// 十进制 Unicode编号
char c = 39532
// 16进制 Unicode编号
char c = 0x9a6c
//Unicode字符形式
char c = '\u9a6c'
char operation

Since char is essentially an integer, some operations that can be performed on integers can be performed, and they will be regarded as int when performing operations. However, since char occupies two bytes, the operation result cannot be directly assigned to the char type, and a forced type is required. Conversion, which is similar to byte, short participating in integer operations.

The comparison of char types is the comparison of their Unicode numbers.

The addition and subtraction of char is based on its Unicode number. Generally, it is meaningless to add and subtract characters, but Ascii code characters are meaningful. For example, for case conversion, the number of uppercase AZ is 65-90, and the number of lowercase az is 97-122, the difference is exactly 32, so the conversion from uppercase to lowercase only needs to add 32, while the conversion from lowercase to uppercase only needs to subtract 32. Another application of addition and subtraction is encryption and decryption. Encryption and decryption can be done by performing some reversible mathematical operation on characters.

variable

An area of ​​memory that holds data.

In order to operate the data, we need to put the data into the memory. There is no way to directly operate the data in the disk. It needs to be read into the memory before it can be directly operated. The memory in the computer is actually a continuous space with address numbers. After we put the data into a certain location in the memory, in order to find and operate the data conveniently, we need to give the location a name. This process is done by variable declarations and assignments.

In real life, Silicon Valley Plaza has its own address number, No. 86, Wenhua Road (memory address number), but it also has its own name (variable name)

In other words, the so-called variable, you can think of it as a container that can be used to carry something, what does it mean? The value of your basic type, the reference of the object type can be put into the variable, this process is called the assignment of the variable. The reason why it is called a variable is because it represents a location in memory, and the value stored in this location can be changed.

Although the value of the variable can be changed, the name is unchanged. The name should represent the meaning of the memory location in the programmer's mind, and the meaning should remain unchanged. For example, the variable int second represents the number of seconds in the clock. It can be assigned different values ​​at different times, but it represents the number of seconds in the clock. The reason why it should be said is because it is not necessary. If you insist on naming a variable age but giving it the value of height, the computer can't do anything about it.

A variable is to give a name to the data, which is convenient for finding different data. Its value can be changed, but its meaning cannot be changed.

The operation of variables is divided into two parts, the declaration of the variable and the reference of the variable.

Variable declaration, must have the variable type and variable name

By declaring variables, each variable is given a data type and a meaningful name

After declaring a variable, a location is allocated in memory, but the content of this location is unknown. Assignment is to set the content of this location to a certain value.

Assignment to a variable, using the assignment operator =

The assignment form is very simple. You can directly assign the familiar numeric constant form to the variable, and the value of the corresponding memory space changes from unknown to definite constant. But the constant cannot exceed the representation range of the corresponding type

At the same time, we can further visualize the explanatory variables through some small cases in life:

A variable is like a cup that can hold something.

The cups have different sizes and types, corresponding to int short in java, etc. Their sizes are different, and the data that can be placed are also different.

Classification of variables

variable

What needs to be explained is the object type. In java, there is actually no object variable, because the data is not placed in the variable, but the reference is placed. That is, the reference (address) of the object is placed in the cup of the variable of the object type, and the reference is like a remote control .

The whole logic is to put a reference in the variable, and the reference points to the object data.


I can't guarantee that every place is right, but I can guarantee that every sentence, every line of code has been scrutinized and considered. I hope that behind every article is my attitude of pursuing a purely technical life.

Always believe that good things are about to happen.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480367&siteId=291194637