Learning not only to learn new things, but also to return from time to time leackage check ...
This article refers to the old horse said programming series and so on, recommended that everyone concerned about the old Horse programming series of articles
Body
Basic knowledge
Data types are primarily designed to classify data, facilitate understanding and manipulation, and in Java, there are the following basic data types:
- Integer type: There are four types of integer byte/short/int/long, the memory space occupies 1/2/4/8 bytes respectively
- Decimal type: There are two types of float/double, with memory space occupying 4/8 bytes respectively
- Character type: char, which represents a single character, memory space is occupied by 2 bytes
- True or False type: Boolean, True or False
An object is a complex data type that is a combination of basic data types, arrays, and other objects.
Memory can be directly understood as a very long array, the CPU can be directly located in the memory of any one location
The origin of a variable is due to the data type placed in memory, in order to easily find and manipulate the data, give the position of a name, so called variable, because it is only the memory of the location, the location of the value can be changed
Memory representation
A basic type variable, there will only be a block of memory space in memory. But the array has two blocks, one for storing the contents of the array itself, and the other for storing the contents.
In a further explanation, we often say that the basic data type holds the value itself, while the reference type holds the location of the value
Arithmetic rules
You should pay attention to the scope of the result and use the appropriate data type. Two positive numbers can be represented by an int, but the result of multiplying may be super, and the result will be confusing.
int // 2147483647 is the maximum value that int can represent
The result of a is-2. Why is-2 we do not explain, in order to avoid this situation, our result type should use a long, but only to long is not enough, because the operation is the default by the int type, you need to represent at least one data as long, that is, after adding L or L, the following will appear the desired result:
long a = 2147483647*2l;
In addition, it is important to note that the integer division is not rounded, but rather directly to the decimal digits:
double d = 10/4;
The result is 2 instead of 2.5, if you want to operate on a decimal, you need to represent at least one number as a decimal, or you can use a forced type conversion, which is preceded by a number (double), which means that the number is treated as a double type, as in any of the following forms:
double d = 10/4.0double d = 10/(double) 4;
When doing decimal operations, there are often strange values, such as executing
This is because the bottom of the computer for floating-point (fractional) representation is not accurate, in the final analysis, because of the reason of 2 binary
Self-increment/decrement is to do one's own plus and minus one operation, but each has two forms, one is placed after the variable, such as a++, a--, and the other is placed before the variable, such as ++a,--a
1 intA = 0;2System.out.println (a++);//03 intb = 0;4System.out.println (++B);//15 intc = 0;6System.out.println (c--);//07 intd = 0;8System.out.println (--d);//-1
There are several logical operators
- With (&): Two is true is true, as long as there is a false or false
- or (|) : As long as there is a true is true, all is False is False
- Non-(!) : True turns false for a variable, false turns True
- XOR (^): two identical to False, two different true
- Short circuit with (&&): and & similar, the difference is a&b, if a is false also need to judge B is false, and a&&b, if A is false, then the direct is false
- Short Circuit or (| |) : Similar to |, the difference is a| b, if A is true also needs to be in the judgment B is true, and a| | B, if A is true, then direct is true
Add
Complement
Binary uses the highest bit to represent the sign bit, 1 for negative numbers, and 0 for positive numbers. But a negative number means that the highest bit is not simply changed to 1, for example:
- byte A =-1, if you just change the highest bit to 1, the binary should be 10000001, but in fact, it should be 11111111.
- Byte a=-127, if you just change the highest bit to 1, the binary should be 11111111, but actually it should be 10000001.
This notation is called the complement notation, and the expression that conforms to our intuition is called the original code notation, and the complement representation is based on the original code representation and then adds 1. The inverse is to change 0 to 1 and 1 to 0. Why should negative numbers be used in the form of a complement? This is because the complement is guaranteed to be correct when calculating the addition and subtraction!
Displacement operation
Left: the operator is <<, move left, the right side of the lower 0, the high is discarded, the binary is considered an integer, left 1 is the equivalent of multiplying by 2.
Unsigned Right Shift: operator is >>>, move right, discard on right, 0 on left.
Signed right Shift: the operator is >>, to the right, the right to discard, the left to fill what depends on what the original top, the original is 1 to fill 1, the original is 0 to fill 0, the binary as an integer, right shift 1 is equivalent to dividing by 2.
Some decimal calculations why is the correct
System.out.println (0.1f+0.1f); 0.2system.out.println (0.1f*0.1f); 0.010000001
According to the above, the first line should not be equal to 0.2 is correct, but why is equal to 0.2? In fact, this is only the Java language to create the illusion, the results are not accurate, but because the results and 0.2 close enough, in the output, Java chose the output 0.2 of this seemingly very concise number, rather than a middle has a lot of 0 decimal. When the error is small enough, the result looks accurate, but inaccuracy is the norm. If you really need a higher precision, one method is to convert the decimal to an integer to operate, and then convert the operation to a decimal number, the other way is generally using the decimal data type, this does not have a uniform specification, in Java is BigDecimal
New---Java (i)