Java - Data Types and Operators

content

1. Data type

1. Variables and Types

1. Data type size

2. Character data type

3. Boolean data type

4. String type variable

5. Scope of variables

6. Variable naming rules

2. Constants

1. Literal constants

2. Constants modified by the fanal keyword

3. Data type conversion

1. Automatic type conversion

2. Forced type conversion

3. Numerical improvement

2. Operators

1. Arithmetic operators

2. Relational operators

3. Logical Operators

1.&&: Logical AND

2.||: Logical OR

3.! : Logical NOT

4. Precautions

4. Bitwise operators

1.& bitwise AND

2.| Bitwise OR

3. ^ Bitwise XOR

4.~ Bitwise negation

5. Shift operation

6. Conditional operators

7. Operation priority

3. Keywords


 1. Data type

Java data types are divided into basic data types and reference types. There are eight basic data types, namely byte, short, int, long, float, double, char, and boolean. (ps: These eight data types are all variables) Reference types include String, interface, enumeration, etc.

1. Variables and Types

1. Data type size

First of all, we need to know that Java's character type uses the Unicode encoding scheme, and each Unicode code occupies 16 bits. The Unicode code character set includes Latin, English, and Chinese. All data types in Java have a range of values, so do not exceed the range of values ​​when storing. Let n be the bits occupied by the data type, then the value range of the data type is -2^(n-1)-2^(n-1)-1. Taking int as an example, int occupies 4 bytes, that is, 32 bits, and the value range of int is -2^31-2^31-1.

The following is the code to know the size of each data type

public static void main(String[] args) {//1个字节等于8个比特位
        System.out.println("byte:"+Byte.SIZE/8);
        System.out.println("char:"+Character.SIZE/8);
        System.out.println("short:"+Short.SIZE/8);
        System.out.println("int:"+Integer.SIZE/8);
        System.out.println("float:"+Float.SIZE/8);
        System.out.println("long:"+Long.SIZE/8);
        System.out.println("double:"+Double.SIZE/8);
    }

2. Character data type

Character literals are represented in Java in the form of single quotes + a single letter.

public static void main3(String[] args) {
        char ch = '王';//2个字节
        char ch2 = 67;//会解析成对印的字符,而不是数字67
        System.out.println(ch);
        System.out.println(ch2);
    }

Of course, an error may also occur when executing javac: unterminated character literal. At this time, you only need to add -encoding UTF-8 after javac.

3. Boolean data type

A variable of type boolean has only two values, true for true and false for false. It should be noted that the boolean type in Java cannot be interchanged with the int type, that is, there is no saying that 1 means true and 0 means false. In the regulations of the JVM, the size of the Boolean type is not clearly specified. Some say it is a bit, and some say it is a byte. Readers have different opinions. The specific usage is shown in the following code

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

4. String type variable

Java uses double quotes + several characters to represent string literals. As shown at the beginning, String is not a primitive data type, but a reference data type. You can judge whether it is a reference type by seeing whether it is an address stored.

    public static void main(String[] args) {
        String str = "你好啊";
        System.out.println(str);
    }

It should be noted that some specific characters in the string that are not easily represented directly need to be escaped.

escape character meaning
\n newline
\t Horizontal Tab (TAB)
\' apostrophe
\" Double quotes
\\ Backslash (/)

 Strings can also be used to concatenate two strings by "+", or concatenate strings and integers

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

5. Scope of variables

The scope of the variable only needs to find the corresponding curly braces, similar to the C language.

6. Variable naming rules

1. A variable name can only contain numbers, letters, and underscores (although $ can also be named, but it is not recommended)

2. Numbers cannot start

3. Variable names are strictly case-sensitive

4. Use small camel case for variable names and large camel case for class names

2. Constants

Constants have their values ​​determined at compile time, while variables don't know their values ​​until runtime. Constants cannot be modified while the program is running.

1. Literal constants

That is, determined such as 123, 123.0, true, 10.0L, etc.

2. Constants modified by the fanal keyword

final in Java has the same meaning as const in C language

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

 Compilation errors are displayed because constants cannot be modified while the program is running.

3. Data type conversion

1. Automatic type conversion

Automatic type conversion will be from low to high, such as int will be automatically converted to long, but cannot be converted to byte

2. Forced type conversion

So can't it be converted from high to low? In fact, it is not. For example, we can use coercion to convert long to int, but it may cause problems such as lack of precision.

public class TestDemo {
    public static void main(String[] args) {
        long x = 500;
        int y =(int) x;
        System.out.println(y);
    }
}

Note: If an attempt is made to convert a value from another type to another type beyond the representation range of the target type, the result will be truncated to a completely different value, such as (byte)500=-12

public class TestDemo {
    public static void main(String[] args) {
        long x = 500;
        byte y =(byte) x;
        System.out.println(y);
    }
}

3. Numerical improvement

When two data types of different sizes are calculated, the system will automatically promote the smaller range to the larger one.

public static void main(String[] args) {
        int a = 10;
        double b = 10.0;
        System.out.println(a+b);
    }

Finally, it will output 20.0 of type double, not 20 of type int 

2. Operators

1. Arithmetic operators

Arithmetic operators include: + - * / %, the usage is the same as that of the C language, and will not be repeated here.

2. Relational operators

Logical operators include: == != < > <= >=

The results of logical operators are all Boolean, that is, there are only two cases, true and false.

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);
    }

3. Logical Operators

There are three logical operators: &&, ||, !. The operands of logical operators (operands are often the result of relational operators) and return values ​​are both boolean

1.&&: Logical AND

expression1&&expression2

If one is false then all are false

Short circuit: if expression 1 is false, then expression 2 is not performed

2.||: Logical OR

expression1||expression2

All are true if one is true

Short circuit: if expression 1 is true, then expression 2 is not performed

3.! : Logical NOT

! expression

If the expression is true, the expression is false. The expression is true if the expression is false

4. Precautions

& and | also represent logical operations if the operand is a boolean. However, compared to && and ||, they do not support short-circuit evaluation, so it is not recommended to use them.

4. Bitwise operators

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

There are four main bitwise operators: & | ~ ^

Bit manipulation means operating on binary bits. Computers use binary to represent data (sequence composed of 01), and bitwise operations are performed in sequence according to each bit of the binary

1.& bitwise AND

Only when the corresponding binary bits are 1, the binary bit of the operation result is 1

2.| Bitwise OR

As long as one of the binary bits is 1, the binary bit of the operation result is 1

3. ^ Bitwise XOR

The same binary bit results in 0, and the different binary bit results in 1

4.~ Bitwise negation

 Turn to 1 if the bit is 0, turn to 0 if the bit is 1

5. Shift operation

Just understand, there are three shift operators: <<, >>, >>>

6. Conditional operators

expression1? Expression 2: Expression 3

When the value of expression1 is true, the value of the entire expression is the value of expression2; when the value of expression1 is false, the value of the entire expression is the value of expression3. The specific usage is shown in the following code

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

7. Operation priority

priority operator

1

()
2 ! + - ++ --
3 * / %
4 + -
5 < <=  > >=
6 == !=
7 ^
8 &&
9 ||
10 ? :
11 = += -= *= /= %=

3. Keywords

Keywords are some words with specific meanings in Java, which can also be called Java reserved words. Java keywords cannot be used as variable names, method names, class names, package names and parameters

At present, the keywords we have come into contact with are: public static final...

Interested friends can learn more through Java Keyword Daquan (detailed keyword classification) | Code Notes (mabiji.com)

Finally, thanks for reading! The author's level is limited, please correct me if I have any mistakes!

Guess you like

Origin blog.csdn.net/weixin_55752048/article/details/120769159