Java Fundamentals 03 Java Operators

Like you who love programming!
Learn SpringBoot actual combat course https://edu.csdn.net/course/detail/31433
Learn SpringCloud introductory course https://edu.csdn.net/course/detail/31451


Preface

Before, everyone has mastered the variables and data types of Java. The saved data must be operated on. Let’s learn about the operators of Java.

Java operators

Common operators include

Assignment operator =,+=,-=,*=,/=,%=
Arithmetic Operator + , - , * , / , % , ++ , –
Relational operator > , < , >= , <= , == , !=
Logical Operators && ,|| , !,&, |
Ternary operator Condition? Value 1: Value 2

Assignment operator

Equal sign operator

Function: Assign the value on the right or the value of the expression to the variable on the
left, operate from right to left

int num = 1 + 2 + 3;

Note: The right side of = can be a value, variable or expression, and the left side of = must be a variable

num1 + num2 = 1 + 2 + 3; 错误

Compound assignment operator

Contains: +=, -=, *=, /=, %=, etc. The
function is to perform operations before assignment

int num1 = 2;
num1 += 3;
作用等价于 
num1 = num1 + 3;

Advantages: concise, higher performance

num1 += 3;		//只做一次运算
num1 = num1 + 3; 	//加法和赋值2次运算

Common interview questions: What is the result of the following code?

short s1 = 1;
short s2 = 1;
s1 += 1; //正确,复合运算是一次运算,1是在short范围内,看做short数值处理
s2 = s2 + 1;//出错,先执行加法运算,表达式的值会转换为int,int不能直接赋值给short

Note: The compound assignment operation has the hidden danger of numerical overflow. The compiler will not check the value range during the compound assignment operation.
Insert picture description here

Arithmetic Operator

Basic arithmetic operations

Contains: +,-, *, /,%
Note:

  1. The role of% is to find the remainder, 10% 3 = 1
  2. String + operation is to connect

Exercise

以下表达式的运行结果是:
10 + 20  - 30 * 4 / 5 % 6   = ?
"123" + 4 = ?  
1 + 3 - 5 * 2 + "100"  = ? 

Increment/decrement operator

Function: add one or subtract one to the value of the variable itself

int num = 10;
num++;   //num加一为11
num--;     //num减一为10

Prefix and suffix

 num++; // 后缀
 ++num; // 前缀

Note: If only ++ and – operations are performed, there is no difference between prefix and suffix.
If other operations are performed together:

  • Prefix: first increase or decrease, and then perform other operations
  • Suffix: perform other operations first, and then perform auto-increment or auto-decrement

Exercise 1: What are the values ​​of n1, n2, and n3?

int n1 = 10;
int n2= n1--;    		// 先赋值,后减一,n2 = 10     n1 = 9
int n3 = ++n2; 	       //先加一,后赋值,n2 = 11    n3 = 11

Exercise 2: Please infer the value of res1,a,b at this time

int a = 3; 
int b = 2; 
int res1 = (a++)*(--b) ; 

Exercise 3: Please infer the value of res1, a, b at this time

int a = 3; 
int b = 2; 
int res1 = (++a) + (b--) + (a++)+ (--b);

Relational operator

Contains:>, <, >=, <=, ==, !=
Function: used to calculate the relationship, the result returned after the operation is of boolean type
such as:

10 > 3  =  true
6 <= 100 = false

The difference between == and =

== 比较两个值是否相等
= 进行赋值

Exercise 1: What are the values ​​of b1, b2, and b3 below?

boolean b1 = 10 > 5;     
boolean b2 = 12 == 3;   
boolean b3 = b1 != b2;  

Logical Operators

Contains: &&, ||,!
Function: cooperate with relational operators to realize logical judgment, and must be operated with boolean expression

Logical and

Expression 1 && Expression 2
The expressions on both sides of && must be true at the same time, and the final result is true, otherwise it is false

true && true = true
true && false = false
false && false = false

Logical OR

Expression 1 || Expression 2
|| Only one of the two expressions needs to be true, and the result is true. If neither of them is true, the result is false

true || true = true
true || false = true
false || false = false

Logical negation

! Expression
negated

!true = false  
!false = true

priority: ! > &&> ||

Exercise 1: What are the values ​​of b1, b2, and b3 below?

boolean b1 = 10 > 5 && 12 == 3;   
boolean b2 = 12 == 3 || 4 > 3;  
boolean b3 = b1 != !b2;  

Exercise 2: Complete the form below

Operand 1 Operand 2 && || ! Operand 1
true true
false false
true false
false true

Exercise 3:
Completion conditions: the variable's single digit is 3 and can be divisible by 6 or the variable can be divisible by 100

int num = 1234;
boolean result = ?

Break function

During && and || operations, in order to improve efficiency, the compiler will determine whether the latter expression will execute
&& according to the result of the previous expression

表达式1 && 表达式2
表达式1为false,表达式2不会执行

||

表达式1 || 表达式2
表达式1为true,表达式2不会执行

& And | have no circuit break function, both before and after expressions are executed

Exercise 1: What is the final result of a, b, c, d?

int a = 3;
int b = 4;
boolean c = (a > b) && (--b < a);  
boolean d = (b > 0) || ( a++ > 0);   

Exercise 2: What is the final result of a, b, c, d?

int a = 3;
int b = 4;
boolean c = (a > b) & (--b < a);  
boolean d = (b > 0) | ( a++ > 0);   

Ternary operator

Contains three parts:

数据类型 变量名 = 条件 ? 值1 : 值2;

Function: It can be used for condition judgment, the condition is true, the return value is 1, the condition is false, the return value is 2.

Exercise 1: How to use the ternary operator to find the maximum value of n1 and n2.

int n1 = 10,n2 = 11;
int max = ?;

Exercise 2: Find the maximum value of three numbers?

int n1 = 10,n2 = 11,n3 =12;
int max = ?;

Operator precedence

Priority from high to low:

  • () Parentheses
  • ++, –,! Unary operator
  • *, /,% multiplication and division and remainder arithmetic operators
  • +,-addition and subtraction
  • >, <, >=, <=, ==,! = Relational operator
  • && logical operator and
  • || or
  • ?: Ternary operator
  • =, +=, -=, *=, /=, %= assignment operator

End

Leave the homework to check your learning situation:
1. Please infer the final value of res1, a, b and explain the calculation process.

int a = 3; 
int b = 2; 
int res1 = (a++)*(--b) + (--a)/(b++); 

2. Assuming that the variable value is a 4-digit integer, how to take out and output the tens, hundreds, and thousands of digits of the variable.
3. The rule for judging leap years is: the year can be divisible by 400, or the year can be divisible by 4 and not divisible by 100, define a year variable and output whether the year is a leap year
4. Define age variables and name variables, if the age is greater than 35, The name is Lao Wang, otherwise the name is Xiao Wang, which is realized by the ternary operator.
5. Enter a three-digit integer to determine whether the number is a daffodil number (the number is equal to the sum of the cubes of three digits) Number = cube of single digit + cube of tens digit + cube of hundreds digit, if yes The output is the number of daffodils otherwise the output is not.


If you need to learn other Java knowledge, poke here ultra-detailed knowledge of Java Summary

Guess you like

Origin blog.csdn.net/u013343114/article/details/112238345