Java programming data types and operators

Basic data type

Numeric:

Integer type (4 kinds):

Byte (1 byte): range (- 128 ~ 127);

short(2 bytes): range (- 32768 ~ 32767);

int(4 bytes);

long(8 bytes): if the data is defined as long and exceeds the range of int, add "L" (recommended) or "L" (the default is int. if it exceeds the range of int, the compilation will report an error, and if it does not exceed the range, it will not);

Floating point type (2):

Float (4 bytes): the first bit is the sign bit, the next 8 bits represent the exponent, and the next 23 bits represent the mantissa; If the specified floating-point type is float, add "F" or "F" (double by default, otherwise the compilation prompt may lose precision);

double(8 bytes): the first bit is the sign bit, the next 11 bits represent the exponent, and the next 52 bits represent the mantissa;

Form of floating point number:

  • a. Decimal number form: it must contain a decimal point (1.68, 168.0 and. 168), otherwise it is regarded as int type;
  • b. Scientific counting method: only floating-point values can be used, such as 1.68e2(168.0), 1.68E2 and 168e2(16800.0);

Special floating point number:

  • a. Positive infinity: divide a positive number by 0.0, and use the positive of Double or Float_ Infinity means that all positive infinities are mostly equal;
  • b. Negative infinity: divide a negative number by 0.0, and use the negative of Double or Float_ Infinity means that all negative infinities are mostly equal;
  • c. Non number: 0.0/0.0, represented by NaN of Double or Float, all non numbers are not equal;

Character type: char(2 bytes)

Boolean: boolean(1 byte)

Learning tutorials for basic data types:

Java300 set zero basic video tutorial for beginners_ Java 300 set zero basic tutorial_ Introduction to java video foundation consolidation tutorial_ Introduction to Java language_ Beep beep beep_ bilibili [highly recommended]
java data structure video tutorial_ Practical course of data structure and algorithm_ Beep beep beep_ bilibili
Basic knowledge of Java data structure and algorithm_ Java data structure and algorithm foundation to advanced_ Common data structure and algorithm problems in Java interview_ Sorting algorithm_ Recursion_ Half query_ Stack and queue_ Linked list_ Beep beep beep_ bilibili

Let's explain the basic data types in detail:

Basic data type

Integer variable int

public static void main1(String[] args) {
        int a = 10;
        System.out.println(a);
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        System.out.println(Integer.MAX_VALUE+1);
        System.out.println(Integer.MIN_VALUE-1);
    }

int represents an integer variable, which occupies 4 bytes (conversion: bit-8 - > byte-1024 - > kb-1024 - > mb-1024 - > gb-1024 - > TB) max in the code of the above example_ Value is the maximum value, MIN_VALUE is the minimum value.

long integer variable

public static void main2(String[] args) {
        long a = 10L;//10 is the default integer type by the system. Although short types can assign values to long types, assigning values to each other with corresponding types is more secure
        System.out.println(a);
        System.out.println(Long.MAX_VALUE);
        System.out.println(Long.MIN_VALUE);
    }

In general, the default input integer constant in Java is int, so 10 is defaulted as integer by the system. Although short types can assign values to long types, assigning values to each other with corresponding types is more secure

short integer variable

    public static void main7(String[] args) {
        short b = 12;
        System.out.println(b);
        System.out.println(Short.MIN_VALUE);
        System.out.println(Short.MAX_VALUE);
    }

Byte type byte

    public static void main6(String[] args) {
        byte b = 12;
        byte c = 21;
        System.out.println(b+" "+c);
        System.out.println(Byte.MIN_VALUE);
        System.out.println(Byte.MAX_VALUE);
    }

When the maximum value of byte type is added by one, it should return to the minimum value theoretically, but the output is 128. This phenomenon is the result of integer promotion. 1 defaults to integer. When the two are added, the byte type is promoted to integer by default, and the final output is also integer.

Floating Point Types

Double precision floating point type double

    public static void main3(String[] args) {
        double n = 9.5;
        System.out.println(n);
        System.out.println(Double.MIN_VALUE);
        System.out.println(Double.MAX_VALUE);

        int a=1;
        int b=2;
        System.out.println(a /b);

        double num = 1.1;
        System.out.println(num * num);

    }

The system defaults to the most commonly used floating-point type variable

Single precision floating point type float

    public static void main4(String[] args) {
        float f = 12.3f;
        System.out.println(f);//An error will be reported. In 12.3, the system defaults to double type. If f is directly assigned, precision may be lost.
        //It reflects the security of Java and ensures the accuracy and safety of data. C language will only warn and Java will prohibit the passage
        
    }

The system prohibits a double precision variable from being assigned to a single precision variable, which will directly report an error and cannot run.

It embodies the security of Java and ensures the accuracy and security of data

The C language will only warn and Java will prohibit the passage, indicating that the C language is a weak language and Java is a strong language.

Character variable char

    public static void main5(String[] args) {
        char ch = 'a';
        System.out.println(ch);

        char ch2 = 'high';
        System.out.println(ch2);

        char ch3 = 97;//Unicode character set code that contains more characters than ASCII
        System.out.println(ch3);
    }

Java uses the Unicode character set, which contains more characters and even Chinese characters. The character code is 0-65535, including ASCII code, of which 'a' is No. 65 and 'a' is No. 97.

When we compile Java, if the code contains Chinese characters, the compilation fails because the GBK character set is generally used by default in the Windows system and the UTF8 character set is used in the Mac. Different mechanisms will lead to mismatches.

As long as we declare to use UTF-8 character set when compiling

javac -encoding UTF-8 dataType.java

boolean type

    public static void main(String[] args) {
        boolean n = true;
        System.out.println(n);
    }

Java's unique variable type has no specified size, only right and wrong.

reference type

Use the classic common type String type to explain:

String type

Common reference types

    public static void main(String[] args) {
        String s = "Hello";
        System.out.println(s);
    }

Escape character

We are using character expressions. Some characters are regarded by the system as characters with special purposes, such as double quotation marks and single quotation marks. When we want to output or use these characters, we need to use escape characters:

String splicing

Direct use + splicing

        String a = "hello";
        String b = "world";
        String c = a + b;
        System.out.println(c);

Strings can be spliced directly with numbers

        String str = "result =";
        int i = 10;
        int j = 20;
        String result = str + i + j;
        System.out.println(result);

When one of the + expressions is a string, the basic data type is spliced according to the string. When there is no string on both sides of +, it is calculated according to the original type.

Constant:

Constant refers to the value whose type and value cannot be changed during program operation, mainly in the following two forms:

literal constant

final keyword modifier constant

final int a = 10;
a = 20;//Compilation error: unable to assign value to final variable

Constants cannot be modified while the program is running.
Constants can only be initialized once.

operator

Arithmetic operators:

ยท + - * / %

Division operation

 public static void main(String[] args) {
        System.out.println(5/2);//2
        System.out.println(5.0/2);//2.5
        System.out.println((float)5/2);//2.5
        System.out.println((float)(5/2));//2.0
    }

be careful:

int / int result or int.

When the precision of one of the / is greater, the type promotion occurs, as shown above: float / int = float

The fourth is the strong conversion of 5 / 2 itself, not a single party, so 5 / 2 is still calculated in the form of int / int.

Remainder operation

public static void main(String[] args) {
        System.out.println(10%3);//1
        System.out.println(-10%3);//-1
        System.out.println(10%-3);//1
        System.out.println(-10%-3);//-1
    }

The plus or minus of the remainder depends on the plus or minus of the divisor.

%You can modulo not only int, but also double

System.out.println(11.5 % 2.0); 
// Operation results
1.5

Whether it is remainder or division, 0 cannot be used as divisor, and runtime exception.

Incremental assignment operator + = - = * = / =%=

Auto increment / Auto decrement operator + + –

The above two are the same as C language.

Relational operator

== != < > <= >=

public static void main(String[] args) {
        int a = 10; 
        int b = 20; 
        System.out.println(a == b); 
        System.out.println(a != b); 
        System.out.println(a < b); 
        System.out.println(a > b); 
        System.out.println(a <= b); 
        System.out.println(a >= b); 
    }

The outputs of relational operators are of boolean type

Logical operator

There are three main logical operators:

&&  ||  !

Note: the operands (operands are often the result of relational operators) and return values of logical operators are Boolean v

Logic and&&

Rule: if both operands are true, the result is true, otherwise the result is false

Logical or||

Rule: if both operands are false, the result is false, otherwise the result is true

Logical non!

Rule: the operand is true and the result is false; The operand is false and the result is true (this is a unary operator with only one operand)

Logical operators can only act on data of boolean type.

Short-circuit evaluation

&&And | observe the rules of short circuit evaluation

System.out.println(10 > 20 && 10 / 0 == 0); // Print false 
System.out.println(10 < 20 || 10 / 0 == 0); // Print true 

We all know that calculating 10 / 0 will cause the program to throw an exception But the above code works normally, indicating that 10 / 0 is not really evaluated

Conclusion:

  • For & &, if the left expression value is false, the overall value of the expression must be false, and there is no need to evaluate the right expression
  • For |, if the left expression value is true, the overall value of the expression must be true, and the right expression does not need to be evaluated

Bitwise Operators

The smallest unit of data operation in Java is not bytes, but binary bits

There are four bitwise operators:

& | ~ ^

Bit operations represent operations on binary bits In computers, binary is used to represent data (sequence composed of 01). Bitwise operation is to calculate in turn according to each bit of binary bit

  • Bitwise AND &: if both binary bits are 1, the result is 1, otherwise the result is 0
  • Bitwise or |: if both binary bits are 0, the result is 0, otherwise the result is 1
  • Reverse by bit ~: if this bit is 0, it turns to 1; if this bit is 1, it turns to 0
  • Bitwise exclusive or ^: if the binary bits of two numbers are the same, the result is 0, and if they are different, the result is 1

Shift Operators

There are three shift operators:

<< >> >>>

They all operate according to binary bits

  • Move left < <: don't use the leftmost position, and fill 0 on the rightmost position
  • Shift right > >: the rightmost bit is not needed, and the leftmost sign bit is filled (positive number is filled with 0, negative number is filled with 1)

Note: the speed of bit operation is greater than that of basic operation.

  • Unsigned shift right > > >: the rightmost bit is not needed, and the leftmost bit is filled with 0

be careful:

  • Shift left by 1 bit, equivalent to the original number * 2 Shift N bits to the left, which is equivalent to the nth power of the original number * 2
  • Shift 1 bit to the right, equivalent to the original number / 2 Shift N bits to the right, which is equivalent to the nth power of the original number / 2
  • Because the shift efficiency of computer calculation is higher than that of multiplication and division, when a code exactly multiplies and divides to the nth power of 2, it can be replaced by shift operation
  • Moving negative digits or shifting too many digits is meaningless

Conditional operator

There is only one conditional operator:

Expression 1? Expression 2: expression 3

  • When the value of {expression 1} is true, the value of the whole expression is the value of {expression 2};
  • When the value of expression 1 is false, the value of the whole expression is the value of expression 3

Simplified writing of conditional judgment statements

Operator precedence

System.out.println(1 + 2 * 3); 

The result is 7, indicating that 2 * 3 is calculated first, and then 1+

Another example:

System.out.println(10 < 20 && 20 < 30); 

At this time, it is obvious to calculate 10 < 20 and 20 < 30 first, and then calculate & & Otherwise, operations like 20 & & 20 are syntactically incorrect (& & operands can only be boolean)

There is priority between operators We don't have to remember the specific rules Just put parentheses in the code that may be ambiguous

After learning data types, you can give yourself an exercise to test yourself:

data type

1. What are the basic data types and how many bits do they occupy?

2. Define a variable with 8 basic types respectively?

3. Constants in Java

operator

  • Arithmetic operators: + (add) - (subtract) * (multiply) / (divide)% (take remainder) -- (arithmetic operators are tested with examples respectively)
  • Logical operators: & (and) | (or)! (non) ^ (XOR) & & (short circuit and) | (short circuit or) -- (list cells are tested separately, and the table I listed in class)
  • Comparison operator: = =! = > > = <<=
  • Single operator (one operand): + + -- (test the effect of placing + + and -- before and after variables respectively)
  • Bitwise operators: > > (signed shift right) < < (signed shift left) > > (unsigned shift right) (just understand)
  • Ternary operator: expression? Value 1: value 2 (test with your own example, important)

Finally: in the update of Java learning knowledge, remember to praise your favorite partners~~

Keywords: Java

Added by moehome on Tue, 11 Jan 2022 00:16:22 +0200