Java Basic Data Types and Their Operations

Keywords: Java encoding less

Java Basic Data Types and Their Operations

1. Integer operations

  1. Four Operations

    For integer types, Java defines only signed integers, so the bit of the highest bit represents the symbol bit (0 represents a positive number, 1 represents a negative number). The maximum range of integers can be represented as follows:

  • byte: -128 ~ 127

  • short: -32768 ~ 32767

  • int: -2147483648 ~ 2147483647

  • long: -9223372036854775808 ~ 9223372036854775807

    Therefore, there are spillovers. To solve the spillovers, we only need to change the specific number into binary to add and subtract. If the highest bit is changed to 1, the number after spillovers will become negative again.

    Note: There is no need to use byte and short to perform integer operations in order to save memory. The scope is too small. Even if the byte type is determined, the later conversion from int to byte will make the code very redundant and difficult to maintain.

    int i = 0
    while(i>0){//It's not a dead cycle, because it overflows into negative numbers.
        i += 1000;
    }
    
  1. Shift operation

    Integers in computers always exist in binary form

    //Left shift, which includes truncating the corresponding digits to the left and adding 0 to the right
    int shitNum = 15;//00000000 00000000 00000000 00001111
    int shitNumLeft1 = shitNum << 1;//00000000 00000000 00000000 00011110
    System.out.println(shitNumLeft1);//After shifting, when converting binary, it is equivalent to multiplying an additional 2,30.
    int shitNumLeft2 = shitNum << 2;//00000000 00000000 00000000 00111110
    System.out.println(shitNumLeft2);//After shifting, when converting binary, it is equivalent to multiplying two more 2,60.
    int shitNumLeft28 = shitNum << 28;//11110000 00000000 00000000 00000000
    System.out.println(shitNumLeft28);//Too many shifts may result in negative numbers - 268435456, which means that the left shift operation is risky as a multiplier 2 tool.
    
    //Shift a negative number to the left, shift symbolically, and it may be positive, but it's also multiplied by 2.
    shitNum = -15;
    int shitNumNevigateLeft = shitNum << 1;
    System.out.println(shitNumNevigateLeft);
    

    Left shift is equal to multiplying 2. Sign bits in the truncated range may alternate positively and negatively. If there is no loss of valid digits, then left shift is equal to multiplying several times of 2. If the number of moving digits is greater than 32, the remainder of 32 is first calculated in the shift residue (shift 33 is consistent with shift 1), and there is no change after the shift. The original number, just get a new number. Note: Looking at the above, we can see that the left shift can not be used as multiplier 2 tool.

     //Positive right shift is to remove 2, and eventually it will be 0. The truncation that moves out will be complemented by zero on the right.
     int shitNum = 15;//00000000 00000000 00000000 00001111
     int shitNumRight1 = shitNum >> 1;//00000000 00000000 00000000 0000111
     System.out.println(shitNumRight1);//After shift, when converting binary, it is equal to dividing by 2,7.
     int shitNumRight2 = shitNum >> 2;//00000000 00000000 00000000 00000011
     System.out.println(shitNumRight2);//After shifting, when converting binary, it is equivalent to dividing two 2,3.
     int shitNumRight28 = shitNum >> 28;//00000000 00000000 00000000 00000000
     System.out.println(shitNumRight28);//Excessive displacement may cause 0
    
    //Negative number right shift is also divided by 2, but the highest bit is fixed, and finally - 1, that is to say, > For negative number, it is right shift without sign bit.
    int n = -536870912;
    int a = n >> 1;  // 11110000 0000000 0000000 00000000 <= -268435456
    int b = n >> 2;  // 10111000 0000000 0000000 00000000 <= -134217728
    int c = n >> 28; // 10000000 0000000 0000000 00000001 <= -2
    int d = n >> 29; // 10000000 0000000 0000000 00000000 <= -1
    
    //"> is the right shift of the signed bit, and the negative number becomes positive once the signed bit is shifted, but the essential absolute value may no longer be divided by 2.
    int n2 = -536870912;
    int a2 = n >>> 1;  // 01110000 0000000 0000000 00000000 <= 1879048192
    int b2 = n >>> 2;  // 00111000 0000000 0000000 00000000 <= 939524096
    int c2 = n >>> 29; // 00000000 0000000 0000000 00000111 <= 7
    int d2 = n >>> 31; // 00000000 0000000 0000000 00000001 <= 1
    

    Right-shift is equivalent to remove 2,> symbol bits are no longer moved within the range,>> symbol bits are within the range of movement, too many moving bits may be 0,** and right-shift operator species> can certainly be used as a division tool, and the efficiency is high. ** If the number of digits moved is greater than 32, the remainder of 32 is first calculated in the shift residue (shift 33 is the same as shift 1), and after the shift, the original number is not changed, but a new number is obtained.

  2. Bit operation

    1. With operation, two bit s are equal to 1
    2. Or operation, any bit is 1
    3. Non-Operational, 01 Interchange
    4. XOR operation, two bit s different 1, the same 0
     int i = 167776589; // 00001010 00000000 00010001 01001101
     int n = 167776512; // 00001010 00000000 00010001 00000000
     System.out.println(i & n); // 167776512
    

2. Floating Point Operations

Compared with integer operations, floating-point operations can only perform four numerical operations, not shift operations, bit operations, and the table demonstration range of floating-point operations is larger than that of shaping, but floating-point operations can not be accurately expressed in the computer. That is to say, there are errors in the operation of floating-point numbers, so the difference between two floating-point numbers is usually less than a very small number.

// Comparing whether x and y are equal, the absolute value of the difference is calculated first.
double r = Math.abs(x - y);
// Then determine whether the absolute value is small enough:
if (r < 0.00001) {
    // Equality can be considered
} else {
    // Can be considered unequal
}

Note: Errors will be reported when integer divides by 0, and special values will be returned when floating-point divides by 0.

double d1 = 0.0 / 0; // NaN
double d2 = 1.0 / 0; // Infinity
double d3 = -1.0 / 0; // -Infinity

3. Boolean operations

boolean always has only true and false values

Short-circuit operation & & & |, it is worth mentioning that ternary operators are also short-circuit operators, after determining the results, the remaining expressions will not be executed.

4. Character strings

In Java, characters and strings are two different types.

4.1char

The basic data type, a char saves a Unicode character, which can be assigned directly to the char variable, or can escape the character u+Unicode encoding value assignment. If the character is assigned to the int type, the corresponding Unicode encoding can be seen.

For an introduction to Unicode coding, see

char c = 'a';
char c = '\u0041'int unicodeVlaue = 'a';

4.2String

Referring to type variables, String is immutable, because the source code modifies char [], so the second assignment of the same string variable, in fact, creates a new string object to directly replace the reference, the original String object will be recycled by the garbage collection mechanism.

String = "123 ";//Can contain spaces
String = " 123 \"";//String uses "" to mark the beginning and end of a string. If the string itself contains ", use ", and there are other escape characters.

String message = "Output:";
int num = 9;
System.out.println(message + num);//+ For string splicing, it can be performed between multiple strings, or it can be used to splice strings with any other type to form new strings.

The value of a reference type variable can be null

Posted by echelon2010 on Wed, 04 Sep 2019 03:12:19 -0700