Java learning road-data types

Java learning road-data types

One, data type classification

img

Two, basic data types

type of data Size (unit: bit/bit) Packaging category Ranges
byte 1 byte = 8 bit java.lang.Byte -128 ~ +127
short 2 byte = 16 bit java.langShort -32768 ~ +32767
int 4 byte = 32 bit java.lang.Integer -2^31 ~ +2^31-1
long 8 byte = 64 bit java.lang.Long -2^63 ~ +2^63-1
float 4 byte = 32 bit java.lang.Float -3.4 * 10^38 ~ +3.4 * 10^38
double 8 byte = 64 bit java.lang.Double -1.7 * 10^308 ~ +1.7 * 10^308
char 2 byte = 16 bit java.lang.Character 0 ~ 65535
boolean 4 byte = 32 bit (boolean variables in the JVM specification are treated as int) java.lang.Boolean Only two values ​​are "true" and "false"
## 三、引用数据类型

1. Class

A class is a collection of entities with some common characteristics. It is an abstract data type. It is an abstraction of entities with the same characteristics.

**Note:** String is actually a class, so string is not a basic data type!

achieve:

class Demo {
    
    }

2. Interface

An interface is a specification, a description of some functions provided by a certain thing to the outside.

achieve:

interface Demo {
    
    }

3. Array

An array is a collection used to store multiple data of the same type

achieve:

int[] array = {
    
    0,1,2};

Four, type conversion

The eight basic data types, except Boolean, can be converted between other types.

1. Automatic type conversion

In Java programs, data types with small capacity can be automatically converted to data types with large capacity.

Conversion sequence:

byte
short
int
char
long
float
double

When multiple data types are mixed to participate in the calculation, the Java program will first convert all variables to the largest data type before performing operations.

E.g:

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        byte a = 10;
        char b = 'A';  // Ascii码值为65
        int c = 100;

        System.out.println(a + b + c);  // 175
    }
}

In the above example, three types of data are added. Since among the three types, the int type is the type with the largest capacity, the compiler will first convert the three types of data into int type data, and then perform the addition operation. Therefore, the output value is 175.

2. Forced type conversion

The operation of converting a large-capacity data type to a small-capacity data type is called coercion.

If you want to perform forced type conversion, you need to strengthen the type conversion symbol program to compile and pass.

Converting from a large-capacity number to a small-capacity data type, the converted value may change, so use it with caution!

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        double a = 10.1;
        int b;

        b = (int)a;
        System.out.println(b);  // 10
    }
}

Convert double data to int data, because the int type has no decimals, so the decimal part is discarded during the conversion process. All output results are 10.

Five, operator

1. Arithmetic Operators

Operator description
+ Addition-Add the values ​​on both sides of the operator
- Subtraction-the left operand subtracts the right operand
* Multiplication-Multiply the values ​​on both sides of the operator
/ Division-divide the left operand by the right operand
Remainder-the remainder of dividing the left operand by the right operand
++ Increment: The value of the operand increases by 1
Decrement: The value of the operand is decreased by 1

Note: There are certain differences between i++ and ++i:

  • ++i returns the value after adding one operation, i++ returns the value before adding one operation
  • If i is a built-in numeric type, the execution efficiency of the two is the same; if i is some custom class, the efficiency of i is greater than that of i++

2. Relational operators

Operator description
== Check if the values ​​of the two operands are equal, if equal then the condition becomes true
!= Check if the values ​​of the two operands are equal, if the values ​​are not equal then the condition becomes true
> Check if the value of the left operand is greater than the value of the right operand, if so, then the condition is true
< Check if the value of the left operand is less than the value of the right operand, if so, then the condition is true
>= Check whether the value of the left operand is greater than or equal to the value of the right operand, if so, then the condition is true
<= Check if the value of the left operand is less than or equal to the value of the right operand, if so, then the condition is true

3. Bitwise operators

Operator description
If the corresponding bits are all 1, the result is 1, otherwise it is 0
| If the corresponding bits are all 0, the result is 0, otherwise it is 1
^ If the corresponding bit value is the same, the result is 0, otherwise it is 1
The bitwise negation operator flips each bit of the operand, that is, 0 becomes 1 and 1 becomes 0
<< Bitwise left shift operator. The left operand is shifted to the left by the number of bits specified by the right operand
>> Right bitwise shift operator. The left operand is shifted to the right by the number of bits specified by the right operand
>>> Bitwise shift right zero padding operator. The value of the left operand is shifted to the right by the number of bits specified by the right operand, and the space obtained by the shift is filled with zeros

4. Logical operators

Operator description
&& It is called the logical AND operator. The condition is true if and only if both operands are true
| | Called the logical OR operator. If any two operands are true, the condition is true
Called the logical negation operator. Used to reverse the logic state of the operand. If the condition is true, the logical negation operator will get false

5. Assignment Operator

Operator description
= Simple assignment operator, assign the value of the right operand to the left operand
+ = Addition and assignment operator, it adds the left operand and the right operand to the left operand
- = Subtraction and assignment operator, it subtracts the left operand and the right operand and assigns to the left operand
* = Multiplication and assignment operator, it multiplies the left operand and the right operand and assigns to the left operand
/ = Division and assignment operator, it divides the left operand and the right operand and assigns to the left operand
(%)= Modulus and assignment operator, it takes the left operand and the right operand and assigns the value to the left operand
<< = Left shift assignment operator
>> = Right shift assignment operator
&= Bitwise AND assignment operator
^ = Bitwise XOR assignment operator
| = Bitwise or assignment operator

6. Conditional Operator

expression:

variable x = (expression) ? value if true : value if false

example:

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        int a = 10, b;
        // 如果 a 等于 1 成立,则设置 b 为 20,否则为 30
        b = (a > 1) ? 20 : 30;
        System.out.println( "Value of b is : " +  b );  
        // Value of b is : 20
    }
}

7. instanceof operator

expression:

( Object reference variable ) instanceof  (class/interface type)

example:

public class Demo {
    
    
    public static void main(String[] args) {
    
    
        String name = "James";
        // 由于 name 是 String 类型,所以返回真
        System.out.println(name instanceof String);
        // true
    }
}

8. Operator precedence

category Operator Relevance
suffix (), [],. (Dot operator) Left to right
Unification expr++、expr– From left to right
Unification ++expr、–expr、+、-、~、! Right to left
Multiplicative *、/、% Left to right
Additive +、- Left to right
Shift >>、>>> 、<< Left to right
relationship >、>=、<、<= Left to right
equal ==、!= Left to right
Bitwise and Left to right
Bitwise XOR ^ Left to right
Bitwise or | Left to right
Logical and && Left to right
Logical OR | | Left to right
condition ?: Right to left
Assignment =、+=、-=、*=、/=、%=、>>=、<<=、&=、^=、|= Right to left
comma Left to right

Guess you like

Origin blog.csdn.net/qq_43580193/article/details/109999308