Java Basic Data Type Operations

Keywords: Java encoding less

Java Basic Data Types and Their Operations

1. Integer operations

  1. Four Operations

    For integer types, Java only defines signed integers, so the bit of the highest bit represents the symbol bit (0 is positive, 1 is negative).The maximum range that each integer can represent is as follows:

  • byte: -128 ~ 127
  • short: -32768 ~ 32767
  • int: -2147483648 ~ 2147483647
  • long: -9223372036854775808 ~ 9223372036854775807

    Therefore, there is an overflow situation. To solve the overflow situation, only the specific number needs to be converted to binary to add or subtract. If the highest bit becomes 1, the number after overflow will become negative again.

    Note: It is not necessary to use byte and short for integer operations in order to save memory. The range is too small. Even if it is determined that within the byte type, subsequent conversion to int will make the code redundant and difficult to maintain.

    int i = 0
    while(i>0){//Not an infinite loop, because overflow becomes negative
        i += 1000;
    }
  1. Shift operation

    Integers always exist in binary form in computers

    //Move left, including truncating the corresponding bits to the left with 0 on the right
    int shitNum = 15;//00000000 00000000 00000000 00001111
    int shitNumLeft1 = shitNum << 1;//00000000 00000000 00000000 00011110
    System.out.println(shitNumLeft1);//Shift after binary conversion is equivalent to multiplying by an extra 2,30
    int shitNumLeft2 = shitNum << 2;//00000000 00000000 00000000 00111110
    System.out.println(shitNumLeft2);//Shift then converts binary by multiplying two more 2,60
    int shitNumLeft28 = shitNum << 28;//11110000 00000000 00000000 00000000
    System.out.println(shitNumLeft28);//Too many shifts can result in a negative number of -268435456, which means the left shift operation is risky to use as a multiplier of two
    
    //Move a negative number to the left, shift symbolically, which may change to positive, but also multiply by 2
    shitNum = -15;
    int shitNumNevigateLeft = shitNum << 1;
    System.out.println(shitNumNevigateLeft);

    Left shift is equal to multiply by 2. Symbol bits are within the truncation range, and positive and negative alternations may occur. If no loss of valid bits occurs, left shift by several bits is equal to multiply by several times of 2. If the number of bits moved is greater than 32, the remainder of the shift (shift 33 and shift 1 are the same) is calculated for 32 first, and there is no change after the shift.The original number, just get a new number.Note: Watch above to see that left shift cannot be used as multiplier 2 tool

     //A positive right removal is a removal of 2, which ends up with 0, a removal truncation, and a right 0 to add
     int shitNum = 15;//00000000 00000000 00000000 00001111
     int shitNumRight1 = shitNum >> 1;//00000000 00000000 00000000 0000111
     System.out.println(shitNumRight1);//Shift after binary conversion is equivalent to dividing by a 2,7
     int shitNumRight2 = shitNum >> 2;//00000000 00000000 00000000 00000011
     System.out.println(shitNumRight2);//When converting binary after shift, it is equivalent to dividing two 2,3
     int shitNumRight28 = shitNum >> 28;//00000000 00000000 00000000 00000000
     System.out.println(shitNumRight28);//Too many shifts may cause 0
    
    //Negative right shift also removes 2, but the highest bit stays the same, and the last one is -1, which is >> Right shift of unsigned bit for negative numbers
    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, negative numbers become positive once the signed shift, but the 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 removal is equivalent to removing 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 >> must be used as a division tool with high efficiency.If the number of bits moved is greater than 32, the remainder of 32 is first calculated on the shift remainder (shift 33 is the same as shift 1), and the original number is not changed after the shift, but a new number is obtained.

  2. Bit operation

    1. And operation, two bit s are 1
    2. Or operation, any bit is 1
    3. Non-Operational, 01 Interchange
    4. XOR operation, two bit s different 1, 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

Floating-point arithmetic can only perform four numeric operations compared to integer arithmetic operations, no shift operation, bitwise operation, floating-point representation is larger than integer, but floating-point arithmetic cannot be accurately represented inside the computer.That is, there is an error in the operation of floating-point numbers, so the difference between two floating-point numbers is usually less than a very small number.

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

Note: Integer dividing by 0 will cause an error, and floating-point dividing by 0 will return a special value

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, the remaining expressions will not be executed after determining the result

4. Character string

There are two different types of characters and strings in Java

4.1char

Basic data type, a char holds a Unicode character, either directly assigning the character to the char variable or escaping the character u+Unicode code code value assignment, assigning the character to the int type can see the corresponding Unicode encoding.

For an introduction to Unicode encoding, see

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

4.2String

By referencing a type variable, String is immutable because the source code modifies char[] with final, so assigning the same string variable twice creates a new string object that directly replaces the reference, and the original String object is recycled by the garbage collection mechanism.

String = "123 ";//Can contain spaces
String = " 123 \"";//String uses''to identify the beginning and end of a string, and \ if the string itself contains', there are other escape characters

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

A reference type variable can have a null value

Posted by andymelton on Wed, 04 Sep 2019 18:41:46 -0700