Java operators, identifiers, and bases

Articles and codes have been archived in [Github warehouse: https://github.com/timerring/java-tutorial] or the public account [AIShareLab] can also be obtained by replying to java .

Operator introduction

  1. arithmetic operator
  2. assignment operator
  3. relational operator
  4. Logical Operators
  5. bitwise operator
  6. ternary operator

arithmetic operator

Relational Operators (Comparison Operators)

The results of relational operators are all boolean, that is, either true or false

Logical Operators

It is used to connect multiple conditions (multiple relational expressions), and the final result is also a boolean value.

  1. short-circuit and &&, short-circuit or ||, negate!
  2. Logical AND &, Logical Or |, ^Logical XOR

assignment operator

Classification of Assignment Operators

Basic assignment operator =

Compound assignment operators
+= , -= , *= , /= , %= , etc., focus on explaining one += , other uses are the same

Assignment operator characteristics

Compound assignment operators perform type conversion

byte b = 2; b+=3; b++;

ternary operator

basic grammar

条件表达式? 表达式1: 表达式2;

Operation rules:

  1. If the conditional expression is true, the result of the operation is expression 1;
  2. If the conditional expression is false, the result of the operation is expression 2;

usage details

  1. Expression1 and Expression2 must be of a type that can be assigned to the receiving variable (or can be automatically converted)
  2. The ternary operator can be turned into an if–else statement

operator precedence

  1. Operators have different priorities. The so-called priority is the order of operations in expression operations. As shown in the table on the right, operators on the previous row always take precedence over the next row.
  2. Only unary operators and assignment operators operate from right to left.

Naming Rules and Specifications for Identifiers

identifier concept

The sequence of characters used by Java to name various variables, methods, and classes is called an identifier

Identifier Naming Rules

  1. Consists of 26 uppercase and lowercase English letters, 0-9, or $

  2. Numbers cannot begin with.

  3. Keywords and reserved words cannot be used, but keywords and reserved words can be included.

  4. Strictly case-sensitive in Java, unlimited length

  5. Identifiers cannot contain spaces. int ab = 90;

Identifier naming convention

  1. Package name: All letters are lowercase when composed of multiple words: aaa.bbb.ccc //for example com.hsp.crm
  2. Class name, interface name: When composed of multiple words, the first letter of all words is capitalized: XxxYyyZzz [big hump]
    For example: TankShotGame
  3. Variable name, method name: When composed of multiple words, the first letter of the first word is lowercase, and the first letter of each word is capitalized at the beginning of the second word: xxxYyyZzz [small hump, referred to as hump method] For example:
    tankShotGame
  4. Constant names: All letters are uppercase. When there are multiple words, each word is connected with an underscore: XXX_YYY_ZZZ
    For example: define an income tax rate TAX_RATE
  5. When we learn about classes, packages, interfaces, etc. later, our naming conventions must be followed in this way, see the documentation for more details.

keywords

Keyword Definition and Characteristics

Definition: a special meaning given by the Java language and used as a string for special purposes

Features: All letters in the keyword are lowercase

reserved word

Java reserved words: Not used by existing Java versions, but may be used as keywords in future versions. When naming identifiers yourself, avoid using these reserved words byValue, cast, future, generic, inner, operator, outer, rest, var, goto, const

keyboard input statement

In programming, if you need to receive the data input by the user, you can use the keyboard input statement to obtain it. Input.java requires a scanner (object), which is Scanner

  1. The package that imports this class, java.util.*
  2. Create an object of this class (declare variables)
  3. call the function inside
import java.util.Scanner;//表示把java.util下的Scanner类导入 
public class Input {
    
     
    //编写一个main方法
    public static void main(String[] args) {
    
    
        //演示接受用户的输入
        //Scanner类 表示 简单文本扫描器,在java.util 包
        //1. 引入/导入 Scanner类所在的包
        //2. 创建 Scanner 对象 , new 创建一个对象,体会myScanner 就是 Scanner类的对象
        Scanner myScanner = new Scanner(System.in);
        //3. 接收用户输入了, 使用 相关的方法
        System.out.println("请输入名字");

        //当程序执行到 next 方法时,会等待用户输入~~~
        String name = myScanner.next(); //接收用户输入字符串
        System.out.println("请输入年龄");
        int age = myScanner.nextInt(); //接收用户输入int
        System.out.println("请输入薪水");
        double sal = myScanner.nextDouble(); //接收用户输入double
        System.out.println("人的信息如下:");
        System.out.println("名字=" + name 
            + " 年龄=" + age + " 薪水=" + sal);

    }
}

hexadecimal

For integers, there are four representations:
Binary: 0, 1, full 2 ​​into 1, beginning with 0b or 0B.
Decimal system: 0-9, enter 1 when 10 is completed.
Octal: 0-7, full 8 into 1. It starts with the number 0.
Hexadecimal: 0-9 and A(10)-F(15), full 16 into 1. It starts with 0x or 0X. AF here is case insensitive.

//演示四种进制
//
public class BinaryTest {
    
     

    //编写一个main方法
    public static void main(String[] args) {
    
    

        //n1 二进制 10
        int n1 = 0b1010;
        //n2 10进制 1010
        int n2 = 1010;
        //n3 8进制 520
        int n3 = 01010;
        //n4 16进制 65793
        int n4 = 0X10101;
        System.out.println("n1=" + n1);
        System.out.println("n2=" + n2);
        System.out.println("n3=" + n3);
        System.out.println("n4=" + n4);
        System.out.println(0x23A); // 570
    }
}

binary to octal conversion

Rules: Starting from the low bit, convert the binary number into the corresponding octal number for every three-digit group.

Case: Please convert 0b11010101 to octal

0b 11(3)010(2)101(5) => 0325

binary to hexadecimal

Rules: Starting from the low bit, convert the binary number into a corresponding hexadecimal number in groups of four.

Case: Please convert 0b11010101 into hexadecimal
0b1101(D)0101(5) = 0xD5

Convert octal to binary

Rules: Convert every 1 digit of an octal number into a corresponding 3-digit binary number.
Case: Please convert 0237 into binary
02(010)3(011)7(111) = 0b10011111

hexadecimal to binary

Rules: Convert each 1-digit hexadecimal number into a corresponding 4-digit binary number.
Case: Please convert 0x23B into binary
0x2(0010)3(0011)B(1011) = 0b001000111011

Original code, inverse code, complement code

The explanation of the original code, inverse code, and complement code on the Internet is too complicated, so I will simplify a few words here: (back down)

For signed ones:

The highest bit in binary is the sign bit: 0 for a positive number and 1 for a negative number

The original code, inverse code and complement code of positive numbers are the same (three codes in one)

Negative number's inverse code = its original code sign bit remains unchanged, and other bits are reversed (0->1,1->0)

The complement of a negative number = its complement + 1 , the complement of a negative number = the complement of a negative number - 1

The inverse code of 0, the complement code is 0

Java does not have unsigned numbers, in other words, all numbers in java are signed

When computing in a computer, it is calculated in the way of two's complement

When we look at the result of the operation, we need to look at its original code (emphasis)

bitwise operator

There are 7 bitwise operations in java (&, |, ^, ~, >>, << and >>>)

  1. Arithmetic right shift >>: Low bit overflows, the sign bit remains unchanged, and the high bit of the overflow is filled with the sign bit
  2. Arithmetic left shift<<: the sign bit remains unchanged, and the lower bit is filled with 0
  3. >>> Logical right shift is also called unsigned right shift, the operation rule is: low bit overflow, high bit fill 0
  4. Special Note: There is no <<< symbol

Guess you like

Origin blog.csdn.net/m0_52316372/article/details/130122007