[Super invincible and detailed Han Shunping's java notes] From beginner to proficient---five operators

1. Arithmetic operators

1 Introduction

Arithmetic operators operate on variables of numerical type.

 2. Demonstrate the use of arithmetic operators

public class ArithmeticOperator {
//编写一个 main 方法
public static void main(String[] args) {
// /使用
System.out.println(10 / 4); //从数学来看是 2.5, java 中 2
System.out.println(10.0 / 4); //java 是 2.5
// 注释快捷键 ctrl + /, 再次输入 ctrl + / 取消注释
double d = 10 / 4;//java 中 10 / 4 = 2, 2=>2.0
System.out.println(d);// 是 2.0
    }
}

% takes the modulo and the remainder
 is in the essence of %. Look at a formula!!!! a % b = a - a / b * b

public class ArithmeticOperator {
//编写一个 main 方法
public static void main(String[] args) {// % 取模 ,取余
// 在 % 的本质 看一个公式!!!! a % b = a - a / b * b
// -10 % 3 => -10 - (-10) / 3 * 3 = -10 + 9 = -1
// 10 % -3 = 10 - 10 / (-3) * (-3) = 10 - 9 = 1
// -10 % -3 = (-10) - (-10) / (-3) * (-3) = -10 + 9 = -1
System.out.println(10 % 3); //1
System.out.println(-10 % 3); // -1
System.out.println(10 % -3); //1
System.out.println(-10 % -3);//-1
    }
}
Use of ++
int i = 10;
i++;//自增 等价于 i = i + 1; => i = 11
++i;//自增 等价于 i = i + 1; => i = 12
System.out.println("i=" + i);//12
Use as an expression
Before ++ : ++i is incremented first and then assigned.
After ++ : i++ first assigns a value and then increments.
int j = 8;
//int k = ++j; //等价 j=j+1;k=j;
int k = j++; // 等价 k =j;j=j+1;
System.out.println("k=" + k + "j=" + j);//8 9

 6Self -increasing and self-decreasing classroom exercises

public class ArithmeticOperatorExercise01 {
//编写一个 main 方法
public static void main(String[] args) {
// int i = 1;//i->1
// i = i++; //规则使用临时变量: (1) temp=i;(2) i=i+1;(3)i=temp;
// System.out.println(i); // 1
// int i=1;
// i=++i; //规则使用临时变量: (1) i=i+1;(2) temp=i;(3)i=temp;
// System.out.println(i); //2
//
// 测试输出
int i1 = 10;
int i2 = 20;
int i = i1++;
System.out.print("i="+i);//10
System.out.println("i2="+i2);//20
i = --i2;
System.out.print("i="+i);//19
System.out.println("i2="+i2);//19
}
}
1) If there are still 59 days of vacation, ask: Total xx weeks and xx days
2) Define a variable to store the Fahrenheit temperature. The formula for converting Fahrenheit temperature to Celsius temperature is: 5/9*( Fahrenheit temperature -100). Request the Celsius temperature corresponding to Fahrenheit temperature. [234.5]
public class ArithmeticOperatorExercise02 {
//编写一个 main 方法
public static void main(String[] args) {
//1.需求:
//假如还有 59 天放假,问:合 xx 个星期零 xx 天
//2.思路分析
//(1) 使用 int 变量 days 保存 天数
//(2) 一个星期是 7 天 星期数 weeks: days / 7 零 xx 天 leftDays days % 7
//(3) 输出
//3.走代码
int days = 25911;
int weeks = days / 7;
int leftDays = days % 7;
System.out.println(days + "天 合" + weeks + "星期零" + leftDays + "天");
//1.需求
//定义一个变量保存华氏温度,华氏温度转换摄氏温度的公式为
//:5/9*(华氏温度-100),请求出华氏温度对应的摄氏温度
//
//2 思路分析
//(1) 先定义一个 double huaShi 变量保存 华氏温度
//(2) 根据给出的公式,进行计算即可 5/9*(华氏温度-100)
// 考虑数学公式和 java 语言的特性
//(3) 将得到的结果保存到 double sheShi
//3 走代码
double huaShi = 1234.6;
double sheShi = 5.0 / 9 * (huaShi - 100);
System.out.println("华氏温度" + huaShi
+ " 对应的摄氏温度=" + sheShi);
}
}

2. Relational operators

1 Introduction

  • The results of relational operators are all boolean , that is, they are either true or false.
  • Relational expressions are often used in the conditions of if structures or the conditions of loop structures.
2.

//演示关系运算符的使用
//
public class RelationalOperator {
//编写一个 main 方法
public static void main(String[] args) {
int a = 9; //老韩提示: 开发中,不可以使用 a, b
int b = 8;
System.out.println(a > b); //T
System.out.println(a >= b); //T
System.out.println(a <= b); //F
System.out.println(a < b);//F
System.out.println(a == b); //F
System.out.println(a != b); //T
boolean flag = a > b; //T
System.out.println("flag=" + flag);
}
}

3.Details

  • The results of relational operators are all boolean , that is, they are either true or false .
  • Expressions composed of relational operators are called relational expressions. a > b
  • The comparison operator "==" cannot be mistakenly written as "="

3. Logical operators

1. Introduction
Used to connect multiple conditions (multiple relational expressions), the final result is also a boolean value.
2. List of logical operators
Study in two groups
1) Short circuit with && , short circuit or || , negation !
2) Logical AND & , logical OR | , ^ logical XOR

Explain the logical operation rules:
  1. a&b: & is called logical AND: rule: when a and b are true at the same time, the result is true, otherwise it is false
  2. a&&b: && is called short-circuit AND: rule: when a and b are true at the same time, the result is true, otherwise it is false
  3. a|b: | Called logical OR, rule: when one of a and b is true, the result is true, otherwise it is false
  4. a||b : || is called short-circuit OR, rule: when one of a and b is true, the result is true, otherwise it is false
  5. !a: Called negation, or not operation. When a is true, the result is false; when a is false , the result is true
  6. a^b: called logical exclusive OR. When a and b are different, the result is true, otherwise it is false.

1. Basic rules of && and &

name grammar Features
Short circuit and &&
Condition 1&& Condition 2
If both conditions are true , the result is true, otherwise false
logical AND &
Condition 1 & Condition 2
If both conditions are true , the result is true, otherwise false
1.1. The difference between the use of && and &
  • && short-circuit AND: If the first condition is false , the second condition will not be judged, and the final result is false , which is highly efficient.
  • & Logical AND: Regardless of whether the first condition is false , the second condition must be judged
/**
* 演示逻辑运算符的使用
*/
    public class LogicOperator01 {
    //编写一个 main 方法
        public static void main(String[] args) {
        //&&短路与 和 & 案例演示
            int age = 50;
            if(age > 20 && age < 90) {
                System.out.println("ok100");
}
//&逻辑与使用
            if(age > 20 & age < 90) {
                System.out.println("ok200");
}
//区别
        int a = 4;
        int b = 9;
//对于&&短路与而言,如果第一个条件为 false ,后面的条件不再判断
//对于&逻辑与而言,如果第一个条件为 false ,后面的条件仍然会判断
            if(a < 1 & ++b < 50) {
                System.out.println("ok300");
}
                System.out.println("a=" + a + " b=" + b);// 4 10
}
}

2. Basic rules for || and |
name grammar Features
Short circuit or ||
Condition 1|| Condition 2
As long as one of the two conditions is true, the result is true, otherwise it is false.
| logical or
Condition 1| Condition 2
As long as one condition is true, the result is true, otherwise it is false

2.1 The difference between .|| and |

  •  || Short-circuit or: If the first condition is true , the second condition will not be judged, and the final result is true , which is highly efficient.
  •  | Logical OR: No matter whether the first condition is true or not , the second condition must be judged, which is inefficient
  •  During development, we basically use ||

3. Rebellion

name grammar Features
! Not (negation)
! Condition
If the condition itself is true, the result is false , otherwise it is true
public class InverseOperator {
//编写一个 main 方法
    public static void main(String[] args) {
    //! 操作是取反 T->F , F -> T
        System.out.println(60 > 20); //T
        System.out.println(!(60 > 20)); //F
    }
}

4. Logical XOR

a^b: called logical exclusive OR. When a and b are different, the result is true, otherwise it is false.
^ Logical XOR, System.out.println( (4 < 1) ^ (6 > 3) ); // T
public class InverseOperator {
//编写一个 main 方法
    public static void main(String[] args) {
        //a^b: 叫逻辑异或,当 a 和 b 不同时,则结果为 true, 否则为 false
        boolean b = (10 > 1) ^ ( 3 > 5);
        System.out.println("b=" + b);//T
    }
}

(Supplementary) if statement:

The basic format of the if statement is as follows:

if (条件) {
    // 条件为真时执行的代码块
}

A condition can be any expression that returns a Boolean value (true or false). If the condition is true, the code block in the if statement will be executed. If the condition is false, the code block will be skipped.

You can also add an else clause after the if statement, as shown below:

if (条件) {
    // 条件为真时执行的代码块
} else {
    // 条件为假时执行的代码块
}

In this case, if the condition is true, the code block in the if statement will be executed; if the condition is false, the code block in the else statement will be executed.

4. Assignment operator

1 Introduction
The assignment operator assigns the value after a certain operation to the specified variable.
2 Classification of assignment operators
  •  Basic assignment operator =
        int a = 10;
  •  compound assignment operator
        +=, -= , *= , /= , %=, etc. , focus on one += , the other uses are the same.
a += b; [ 等了 a = a + b; ]
a -= b; [ 等了 a = a - b; ]
3 Characteristics of assignment operators
  •  The order of operations is from right to left : i nt num = a + b + c;
  •  The left side of the assignment operator can only be variables , and the right side can be variables, expressions, and constant values : int num = 20; int num2= 78 * 34 - 10; int num3 = a;
  • The compound assignment operator is equivalent to the following effect : a+=3; equivalent to a=a+3; and so on.
  •  Compound assignment operators perform type conversions. : byte b = 2; b+=3; b++;
    //演示赋值运算符的使用
public class AssignOperator {
    //编写一个 main 方法
    public static void main(String[] args) {
        int n1 = 10;
        n1 += 4;// n1 = n1 + 4;
    System.out.println(n1); // 14
        n1 /= 3;// n1 = n1 / 3;//4
    System.out.println(n1); // 4
        //复合赋值运算符会进行类型转换
        byte b = 3;
        b += 2; // 等价 b = (byte)(b + 2);
        b++; // b = (byte)(b+1);
    }
}

5. Ternary operator

1Basic grammar
Conditional expression ? expression1 : expression2 ;
Operation rules:
1. If the conditional expression is true , the result after the operation is expression 1 ;
2. If the conditional expression is false , the result after the operation is expression 2 ;
Formula : [ Master Yideng: Master Yizhen ]
//三元运算符使用
    public class TernaryOperator {
//编写一个 main 方法
    public static void main(String[] args) {
        int a = 10;
        int b = 99;
// 解读
// 1. a > b 为 false
// 2. 返回 b--, 先返回 b 的值,然后在 b-1
// 3. 返回的结果是 99
        int result = a > b ? a++ : b--;
            System.out.println("result=" + result);
            System.out.println("a=" + a);
            System.out.println("b=" + b);
    }
}
2Usage detailsTernaryOperatorDetail.java _
1) Expression 1 and Expression 2 must be of a type that can be assigned to the receiving variable ( or can be automatically converted )
2) The ternary operator can be converted into an if--else statement
//三元运算符细节
    public class TernaryOperatorDetail {
//编写一个 main 方法
    public static void main(String[] args) {
//表达式 1 和表达式 2 要为可以赋给接收变量的类型
//(或可以自动转换/或者强制转换)
    int a = 3;
    int b = 8;
    int c = a > b ? (int)1.1 : (int)3.4;//可以的
        double d = a > b ? a : b + 3;//可以的,满足 int -> double
    }
}
public class TernaryOperatorExercise {
//编写一个 main 方法
public static void main(String[] args) {
//案例:实现三个数的最大值
int n1 = 553;
int n2 = 33;
int n3 = 123;
//思路
//1. 先得到 n1 和 n2 中最大数 , 保存到 max1
//2. 然后再 求出 max1 和 n3 中的最大数,保存到 max2
int max1 = n1 > n2 ? n1 : n2;
int max2 = max1 > n3 ? max1 : n3;
System.out.println("最大数=" + max2);
//使用一条语句实现, 推荐使用上面方法
//老师提示: 后面我们可以使用更好方法,比如排序
// int max = (n1 > n2 ? n1 : n2) > n3 ?
// (n1 > n2 ? n1 : n2) : n3;
// System.out.println("最大数=" + max);
//
int abcclass = 10;
int n = 40;
int N = 50;
System.out.println("n=" + n);//40
System.out.println("N=" + N);//50
//? abc 和 aBc 是两个不同变量
int abc = 100;
int aBc = 200;
//int a b = 300;
//int a-b=10;
int goto1 = 10;
}
}

6. 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 in the previous line always take precedence over those in the next line.
2) Only unary operators and assignment operators operate from right to left.
3) Don’t memorize the list . If you use it more, you will become familiar with it.

 

Guess you like

Origin blog.csdn.net/qq_45206556/article/details/131789918