Basic data type

The numerical model:

Integer types: Byte (1 byte) : ranges from -128 to 127. Short (2 bytes) : range (-32768 to 32767); Int (4 bytes); Long (8 bytes) : add "L" (recommended) or "L" (recommended) if the data type is defined as long beyond the int range. Floating point type (2 types) : 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. Add "F" or "F" if you specify float type (default: double, otherwise compilation prompt may lose accuracy); Double (8 bytes) : the first bit is the sign bit, the next 11 bits are the exponent, and the next 52 bits are the mantissa;Copy the code

Floating point form:

  • A. Decimal: Must contain a decimal point (1.68, 168.0, and.168), otherwise it is considered an int;
  • B. Scientific notation: Only floating point values can be used, such as 1.68e2(168.0), 1.68e2, and 168e2(16800.0).

Special floating point numbers:

  • A. Positive infinity: a positive number divided by 0.0, represented by POSITIVE_INFINITY of Double or Float, all POSITIVE_INFINITY are mostly equal;
  • B. Negative infinity: negative numbers divided by 0.0, represented by Double or Float NEGATIVE_INFINITY, all negative infinities are mostly equal;
  • C. Nonnumbers: 0.0/0.0, represented by NaN of Double or Float, all nonnumbers are not equal;

Char (2 bytes)

Boolean: Boolean (1 byte)

Basic data types can be matched with the tutorial:

Java300 sets zero basic for beginners video tutorial _Java300 sets zero basic tutorial _Java beginner beginner video foundation consolidation tutorial _Java language entry to proficient _bilibili _bilibili [highly recommended]

Java data structure video tutorial _ Data structure and algorithm practical course _ Bilibili _bilibili

Java basic introduction must learn knowledge data structure and algorithm _Java data structure and algorithm foundation to advanced _Java interview common data structure and algorithm problems _ sorting algorithm _ recursion _ folding query _ stack and queue _ linked list _bilibili _Bilibili

Here’s a closer look at the basic data types:

Basic data type

The integer variable is 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);
    }
Copy the code
Int is an integer variable that occupies 4 bytes (bit-8-> byt-1024 -> kB-1024 ->MB-1024->GB-1024->TB) in the preceding example, MAX_VALUE is the maximum value and MIN_VALUE is the minimum value.Copy the code

Long integer variable long

public static void main2(String[] args) { long a = 10L; //10 is an integer by default. Although short types can assign values to long types, it is safer to assign values to each other using corresponding types system.out.println (a); System.out.println(Long.MAX_VALUE); System.out.println(Long.MIN_VALUE); }Copy the code
Generally, the default input integer constant in Java is int, so 10 is the default integer type. Although short types can assign values to long types, it is safer to assign values to each other using corresponding typesCopy the code

Short

    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);
    }
Copy the code

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);
    }
Copy the code
When you add the maximum value of the byte type, in theory it should change back to the minimum value, but the output is 128. This phenomenon is the result of integer promotion. 1 defaults to integer, and when you add the two, the default byte type is promoted to integer, and the output is also an integer.Copy the code

Floating point type

Double 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); }Copy the code
The system defaults to the most commonly used floating point variableCopy the code

Single-precision floating point type float

Public static void main4(String[] args) {float f = 12.3f; System.out.println(f); In 12.3, the default type is double. If f is assigned directly, the accuracy may be lost. } // * * * * * * * * * * *Copy the code

The system forbids assigning a double – precision variable to a single – precision variable.

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

C will just warn and Java will forbid, which means C 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, which contains more characters than ASCII system.out.println (ch3); }Copy the code

Java uses the Unicode character set, which contains more characters, including even Chinese characters, with character encodings ranging from 0 to 65535, as well as ASCII codes, where ‘A’ is number 65 and ‘A’ is number 97.

When we compile Java, if the code contains Chinese characters, the compiler will fail, because the Windows system generally uses the GBK character set by default, while the Mac uses the UTF8 character set. Different mechanisms will lead to a mismatch.

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

javac -encoding UTF-8 dataType.java
Copy the code

Boolean type Boolean

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

Java specific variable type, no size, only right and wrong.

Reference types

Use the classic String type to illustrate:

String type

Common reference typesCopy the code
    public static void main(String[] args) {
        String s = "Hello";
        System.out.println(s);
    }
Copy the code
Escape characters When we use character representations, some characters are considered by the system to have special meanings, such as double quotation marks and single quotation marks. When we want to print or use these characters, we need to use escape characters: string concatenation uses + concatenation directlyCopy the code
        String a = "hello";
        String b = "world";
        String c = a + b;
        System.out.println(c);
Copy the code
Strings can be concatenated directly with numbersCopy the code
        String str = "result =";
        int i = 10;
        int j = 20;
        String result = str + i + j;
        System.out.println(result);
Copy the code
When one of the + expressions is a string, the basic data type is concatenated as a string. When there are no strings on either side of the + expression, the basic data type is computed as the original type.Copy the code

Constants:

Constants are values whose types and values cannot be changed while a program is running. There are two main forms of constants:

Literal constant

The final keyword modifies constants

final int a = 10; a = 20; // Compile error: unable to assign value to final variableCopy the code

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

The operator

Arithmetic operators:Copy the code
· + - * / %
Copy the code
divisionCopy the code
public static void main(String[] args) { System.out.println(5/2); / / 2 System. Out. Println (5.0/2); / / System. 2.5 out. Println ((float) 5/2); / / System. 2.5 out. Println ((float) (5/2)); / / 2.0}Copy the code

Note:

Int/int is still an int.

Type promotion occurs when one side of/is more precise, as in: float/int = float

The fourth is a strong turn of 5/2 itself, not a single side, so 5/2 is still calculated as int/int.

Take over operations

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}Copy the code

The mod is positive or negative depending on the dividend.

% can be modulo not only to int, but also to double

System. The out. Println (2.0 11.5%); // The result is 1.5Copy the code

In both mod and division, 0 cannot be used as a divisor, the runtime exception.

The incremental assignment operator += -= *= /= %=

The increment/decrement operator ++ —

The above two are the same as the C language in use.

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); }Copy the code

The output of a relational operator is a Boolean output

Logical operator

There are three main logical operators:

&&  ||  !
Copy the code

Note: Both the operands of logical operators (which are often the results of relational operators) and the return value are Booleanv

Logic and &&

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

Logic or | |

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

The logical not!

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

Logical operators can only be used on Boolean data.

Short circuit is evaluated

&& and | | follow the rules of the short circuit is evaluated.

System.out.println(10 > 20 && 10 / 0 == 0); / / print false System. Out.println (10 < 20 | | 10/0 = = 0); / / print trueCopy the code

As we all know, calculating 10/0 causes the program to throw an exception. But the above code works fine, indicating that 10/0 is not really evaluated.

Conclusion:

  • For &&, if the left-hand expression values false, then the entire expression must be false without evaluating the right-hand expression.
  • For | | if left expression evaluates to true, the expression of the value of the whole must be true, don’t need to calculate the right expression.

An operator

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

There are four main bitwise operators:

& | ~ ^

Bit operation means to operate by bits of a binary. In computers, data is represented in binary (a sequence of 01), and bitwise operations are performed according to each bit of the binary.

  • Bitwise and &: The result is 1 if both binary bits are 1, otherwise 0
  • Bitwise or | : if two bits are zero, the result is 0, otherwise the result is 1.
  • Invert by bit ~: If the bit is 0, it is converted to 1; if the bit is 1, it is converted to 0
  • X or ^ by bit: The result is 0 if the binary bits of two digits are the same, and 1 if they are different.

\

Shift operator

There are three shift operators:

<< >> >>>
Copy the code

It’s all done in bits

  • Move to the left <<: do not leave the leftmost bit, add 0 to the right most.
  • Right shift >>: the rightmost bit is not, the leftmost complement sign bit (positive complement 0, negative complement 1)

Note: Bit operations are faster than basic operations.

  • Unsigned right shift >>>: the right most bit is not, the left most fill in 0.

Note:

  • If you move 1 bit to the left, it is the same as the original number * 2. If you move N bits to the left, it is the same as the original number * 2 to the N.
  • If you shift one bit to the right, you are equal to the number over 2. If you shift N bits to the right, you are equal to the number over 2 to the power N.
  • Since computers are more efficient at calculating shifts than multiplication and division, when a code is exactly multiplied and divided by 2 to the N, it can be replaced by a shift operation.
  • It makes no sense to move negative places or shift the number of bits over most of the time

Conditional operator

There is only one conditional operator:

Expression 1? Expression 2: Expression 3

  • When the value of expression 1 is true, the entire expression is the value of expression 2;
  • When expression 1 has a value of false, the entire expression has the value of expression 3.

A simplified form of conditional statement

Operator precedence

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

The result is 7, which means you calculated 2*3 first and then 1+

Here’s another example:

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

In this case, it is obvious to calculate 10 < 20 and 20 < 30 first, and then calculate &&. Otherwise, operations like 20 && 20 are syntactically incorrect (&& can only have Boolean operands).

Operators have precedence. We don’t have to memorize the exact rules. Add parentheses to potentially ambiguous code


Here’s an exercise to test yourself after learning about data types:

The data type

1. What are the basic data types and how many bits does each have?

2. Define a variable with each of the 8 basic types?

Constants in Java

The operator

  • Arithmetic operator: +(add)- (subtract) *(multiply) /(divide) %(take the remainder)– (arithmetic operator tests respectively for example)
  • Logical operators: & (and) | (or)! (a) ^ (exclusive or) && (short circuit and) | | (short circuit or) – (column table test respectively, when I was in class column of table)
  • Comparison operator: ==! = > >= < <=
  • Single operator (one operand) : ++ — (tests the effect of placing ++ and — before and after variables, respectively)
  • Bitwise operators: >>(signed right shift) <<(signed left shift) >>> (unsigned right shift)
  • Ternary operators: expressions? Value 1: Value 2 (self example test, important)

Finally: Java learning knowledge in the update, like the partner remember to give a praise yo ~~