java operators, expressions and statements

foreword

Simple distinction for java operators.

1. Steps to use

1. Unary operator

 2. Arithmetic operators

3. Relational operators

 

4. Self-increment and self-decrement operators

int a = 3, b = 4;
System.out.println("a = " + a);
System.out.println("\t a++ = " + (a++) + ",a = " + a);//先执行后自增
System.out.println("b = " + b);
System.out.println("\t ++b = " + (++b) + ",b = " + b);//先自增后执行
a = 3
	 a++ = 3,a = 4
b = 4
	 ++b = 5,b = 5

5. Logical operators

The short-circuit function of && simply means that the second condition will not be executed when the first condition is false.

The short-circuit function of || is that when the first condition is true, the following conditions will not be executed regardless of whether the compilation is wrong or not.

if (1 == 1 || 1/0 == 0){
    System.out.println("就算有错也可以进来");
}


结果  :就算有错也可以进来
if (1 == 1 | 1/0 == 0){
    System.out.println("就算有错也可以进来");
}


结果  : Exception in thread "main" java.lang.ArithmeticException: / by zero
	at com.jerei.test.Main.main(Main.java:43)  (出现报错,表示0不可以做分母)

6. Ternary operator

Format: variable = condition judgment? expression1: expression2

int max = 0;
int a = 20;
int b = 30;
max = a >= b ? a : b;
System.out.println("最大值为:" + max);


结果 : 最大值为:30

Summarize

The above is what I will talk about today. If you have any questions, please leave a message to consult.

Guess you like

Origin blog.csdn.net/wzw_wwl/article/details/124241743