Dark horse programmer Java zero-based video tutorial notes-operators

1. Detailed explanation and comprehensive exercises of arithmetic operators

1. Operators and expressions

① Operators: Symbols that operate on literals or variables
② Expressions: Use operators to connect literals or variables, and expressions that conform to the Java syntax can be called expressions.
Expressions connected by different operators represent different types of expressions.
③ Example

int a = 10;
int b = 20;
int c = a + b;
+: is an operator, and is an arithmetic operator
a + b: is an expression, since + is an arithmetic operator, this expression is called an arithmetic expression Mode

2. Arithmetic operators

symbol effect
+ add
- reduce
* take
/ remove
% modulo, remainder

3. Sample

package com.itheima.arithmeticoperator;
public class ArithmeticoperatorDemo1{
    
    
    public static void main(String[] args){
    
    
        //+
        System.out.println(3 + 2);//5
        //-
        System.out.println(5 - 1);//4
        //*(shift + 数字8)
        System.out.println(7 * 9);//63
        
        //如果在计算的时候有小数参与
        //结论:
        //在代码中,如果有小数参与计算,结果有可能不精确的
        //为什么呢
        //暂时只要知道这个结论就可以了
        //具体的原因,我们到了JavaSE的时候,会再详细的讲解
        System.out.println(1.1 + 1.1);//2.2
        System.out.println(1.1 + 1.0);//2.1100000000000003
        System.out.println(1.1 - 1.01);
        System.out.println(1.1 * 1.01);
    }
}
package com.itheima.arithmeticoperator;
public class ArithmeticoperatorDemo2{
    
    
    //主入口
    //结论:
    //1.整数参与计算,结果只能得到整数
    //2.小数参与计算,结果可能是不精确的,如果我们需要精确计算,那么需要用到后面的知识点
    public static void main(String[] args){
    
    
        //除法
        System.out.println(10 / 2);//5
        System.out.println(10 / 3);//3
        System.out.println(10.0 / 3);//3.3333333333333335
  
        //取模,取余,实际上也是做除法运算,只不过得到的是余数而已
        System.out.println(10 % 2);//0
        System.out.println(10 % 3);//1

        //应用场景
        //1.可以用取模来判断,A是否可以被B整除
        //A % B   10 % 3
        //2.可以判断A是否为偶数
        //A % 2 如果结果为0,那么证明A是一个偶数;如果结果是1,那么证明A是一个奇数
        //三个玩家
        //把每一张牌都定义一个序号
        //拿着序号 %3 如果结果为1,就发给第一个玩家
        //如果结果为2,就发给得二个玩家
        //如果结果为0,就发给第三个玩家
    }
}

4. Practice

package com.itheima.test;
import java.util.Scanner;
public class test1{
    
    
    public static void main(String[] args){
    
    
        //键盘录入一个三位数,获取其中的个位,十位,百位
        //1.键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个三位数");
        int number = sc.nextInt();
        //2.获取个位,十位,百位
        //公式:
        //个位:数字%10
        //十位:数字/10%10
        //百位:数字/10/10%10
        //……
        int ge = number % 10;
        int shi = number / 10 % 10;
        int bai = number /100 % 10;
        System.out.println(ge);
        System.out.println(shi);
        System.out.println(bai);
    }
}

2. Implicit conversion and forced conversion

1. Three situations of "+" operation

① Number addition
② String addition
③ Character addition
[Note] When numbers are used for operations, the operation cannot be performed if the data type is different, and it needs to be converted into the same type before operation can be performed

2. Classification of type conversion

① Implicit conversion
② Mandatory conversion

3. Implicit conversion

① Definition: convert a value with a small value range into data with a large value range

int a = 10;
double b = a;

② Range of values: byte < short < int < long < float < double
③ When to convert:
the data types are different, so the calculation cannot be performed, and it needs to be converted to the same one before calculation can be performed
④ Conversion rules:
the value range is small, and When the value range is large, the operation is performed, and the small value will be promoted to a large value first, and then the operation will be performed.
When the three types of byte short char are operated, they will be directly promoted to int first, and then the operation will be performed

4. Casting

① If a value with a large value range is assigned to a variable with a small value range, direct assignment is not allowed. If you must do this, you need to add mandatory conversion
② Format: target data type variable name = (target data type) the data to be converted

5. Sample

package com.itheima.arithmeticoperator;
public class ArithmeticoperatorDemo3{
    
    
    public static void main(String[] args){
    
    
        byte b1 = 100;
        byte b2 = 100;
        //现在我们要强转的是谁
        //b1 + b2计算之后的结果
        //byte(b1) + b2 强转的是b1,并不是最终的结果
        byte result = byte(b1 + b2);
        System.out.println(result);//结果发生错误,因为要转换的数据过大
    }
}

3. The addition operation of strings and characters

1. String "+" operation

① When a string appears in the "+" operation, this "+" is a string connection character, not an arithmetic operator. The data before and after will be spliced ​​and a new string will be generated.
For example: "123"+123="123123"
② When performing "+" operations, execute them one by one from left to right
For example: 1+99+"Dark Horse of the Year"="Dark Horse of 200 Years"
③Practice

package com.itheima.test;
import java.util.Scanner;
public class test1{
    
    
    public static void main(String[] args){
    
    
        //键盘录入一个三位数,获取其中的个位,十位,百位
        //1.键盘录入
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个三位数");
        int number = sc.nextInt();
        //2.获取个位,十位,百位
        //公式:
        //个位:数字%10
        //十位:数字/10%10
        //百位:数字/10/10%10
        //……
        int ge = number % 10;
        int shi = number / 10 % 10;
        int bai = number /100 % 10;
        System.out.println("个位是:"+ge);
        System.out.println("十位是:"+shi);
        System.out.println("百位是:"+bai);
    }
}

2. Character "+" operation

When character + character/number, the character will be queried to the corresponding number through the ASCII code table and then calculated

package com.itheima.arithmeticoperator;
public class ArithmeticoperatorDemo4{
    
    
    public static void main(String[] args){
    
    
        char c = 'a';
        int result = c + 0;
        System.out.println(result);
    }
}

4. Self-increment and self-decrement operators

1. Basic Usage

symbol effect illustrate
++ add Increment the value of the variable by 1
- - reduce Decrement the value of the variable by 1

Note: ++ and - - can be placed before or after the variable

2. Two usages


① Use ++ and - alone , whether it is placed before or after the variable, the result of writing a line alone is the same

package com.itheima.arithmeticoperator;
public class ArithmeticoperatorDemo5{
    
    
    public static void main(String[] args){
    
    
        //++和--
        int a = 10;
        //表示把变量a里面的值+1
        a++;
        System.out.println(a);//11
        //表示把变量a里面的值+1
        ++a;
        System.out.println(a);//12
        //表示把变量a里面的值-1
        a--;
        System.out.println(a);//11
        //表示把变量a里面的值-1
        --a;
        System.out.println(a);//10      
    }
}

② Participate in the calculation,
use first and then add

int a = 10;
int b = a++;

add before use

int a = 10;
int b = ++a;

3. Sample

package com.itheima.arithmeticoperator;
public class ArithmeticoperatorDemo6{
    
    
    public static void main(String[] args){
    
    
         int x = 10;
         //后++:先用后加
         //先把x变量中的值拿出来用,赋值给y,然后再进行自增
         //赋值给y的值是自增前的
         int y = x++;// x = 11 y = 10
         //先++:先加后用
         //先把x进行自增,然后把自增后的结果赋值给左边的变量
         //先把x自增,变成12,然后再把自增后的12赋值给z
         int z = ++x;// x =12 z = 12
         System.out.println("x:"+x);//12
         System.out.println("y:"+y);//10
         System.out.println("z:"+z);//12
    }
}

Five, assignment operator and relational operator

1. Assignment operator

① classification

symbol effect illustrate
= assignment int a=10, assign 10 to variable a
+= post-add assignment a+=b, give the value of a+b to a
-= assignment after subtraction a-=b, give the value of ab to a
*= assignment after multiplication a*=b, give the value of a*b to a
/= assignment after division a/=b, give the quotient of a÷b to a
%= assignment after remainder a%=b, give the remainder of a÷b to a

② Sample

package com.itheima.assigningoperator;
public class AssigningoperatorDemo1{
    
    
    public static void main(){
    
    
        //+=
        //规则:将左边和右边进行相加,然后再把结果赋值给左边
        int a = 10;
        int b = 20;
        //把a+b,再把结果赋值给左边的变量a
        a += b;
        //等同于 a = (int)(a + b);
        System.out.println(a);//30
        System.out.println(b);//20

        //细节:
        //+=,-=,*=,/=,%= 底层都隐藏了一个强制类型转换
        short s = 1;
        //把左边和右边进行相加,得到结果2,再赋值给左边的变量
        s += 1;
        //等同于:s = (short)(s + 1);
        System.out.println(s);//2
    }
}

2. Relational operators

① classification

symbol illustrate
== a==b, judge whether the values ​​of a and b are equal, true if true, false if not true
!= a!=b, judge whether the values ​​of a and b are not equal, true if true, false if not true
> a>b, judge whether a is greater than b, true if true, false if not true
>= a>=b, judge whether a is greater than or equal to b, true if true, false if false
< a>b, judge whether a is less than b, true if true, false if not true
<= a<=b, judge whether a is less than or equal to b, true if true, false if false

Note: The results of relational operators are all boolean types, either true or false
② Sample

package com.itheima.assigningoperator;
public class AssigningoperatorDemo2{
    
    
    public static void main(){
    
    
        //1.== 判断左右两边是否相等
        int a = 10;
        int b = 10;
        int c = 20;
        System.out.println(a == b);//true
        System.out.println(a == c);//false
    }
}

3. Practice

package com.itheima.test;
import java.util.Scanner;
public class Test2{
    
    
    public static void main(String[] args){
    
    
        /*
        需求:
        您和您的约会对象正试图在餐厅获得一张桌子。
        键盘录入两个整数,表示你和你约会对象衣服的时髦度。(手动录入0~10之间的整数,不能录入其他)
        如果你的时髦度大于你对象的时髦度,相亲就成功,输出true,否则输出false。
        */
        
        //1.键盘录入两个整数表示衣服的时髦度
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入我们自己的衣服时髦度");
        int myFashion = sc.nextInt();
        System.out.println("请输入相亲对象的衣服时髦度");
        int girlFashion = sc.nextInt();

        //2.把我衣服的时髦度跟女孩的时髦度进行对比就可以了
        boolean result = myFashion > girlFashion;

        //3. 打印结果
        System.out.println(result);
    }
}

Six, four logical operators

1. Classification

symbol effect illustrate
& logical AND And, both sides are true, the result is true
| logical or Or, if both sides are false, the result is false
^ XOR Same as false, different as true
! logical NOT reconciliation

2. Sample

package com.itheima.logicoperator;
public class LogicoperatorDemo1{
    
    
    public static void main(String[] args){
    
    
        //1.& 并且
        //两边都为真,结果才是真
        System.out.println(true & true);//true
        System.out.println(false & true);//false
        System.out.println(true & false);//false
        System.out.println(false & false);//false

        //2.| 或者 
        //两边都为假,结果才是假
        System.out.println(true | true);//true
        System.out.println(false | true);//true
        System.out.println(true | false);//true
        System.out.println(false | false);//false        
    }
}
package com.itheima.logicoperator;
public class LogicoperatorDemo2{
    
    
    public static void main(String[] args){
    
    
        // ^ 异或 
        //相同为false,不同为true
        System.out.println(true ^ true);//false
        System.out.println(false ^ true);//true
        System.out.println(true ^ false);//true
        System.out.println(false ^ false);//false

        // ! 逻辑非 取反 
        //提示:
        //取反的感叹号不要写多次,要么不写,要么只写一次
        System.out.println(!false);//true
        System.out.println(!true);//false   
    }
}

Seven, short-circuit logical operators

1. Classification

symbol effect illustrate
&& short circuit with The result is the same as &, but has a short-circuit effect
|| short circuit or The result is the same as |, but with a short-circuit effect

Note: & |, whether the left side is true or false, the right side must execute
&& ||, if the left side can determine the result of the entire expression, the right side will not be executed

2. Sample

package com.itheima.logicoperator;
public class LogicoperatorDemo3{
    
    
    public static void main(String[] args){
    
    
        //1.&&
        //运行结果跟单个&是一样的
        //表示两边都为真,结果才是真
        System.out.println(true && true);//true
        System.out.println(false && true);//false
        System.out.println(true && false);//false
        System.out.println(false && false);//false
        
        //2.||
        //运行结果跟单个 | 是一样的
        //表示两边都为假,结果才是假
        System.out.println(true || true);//true
        System.out.println(false || true);//true
        System.out.println(true || false);//true
        System.out.println(false || false);//false   
        
        //3.短路逻辑运算符具有短路效果
        //简单理解:当左边的表达式能确定最终的结果,那么右边就不会参与运行了
        int a = 10;
        int b = 10;
        boolean result = ++a < 5 && ++b <5;
        System.out.println(result);//false
        System.out.println(a);//11
        System.out.println(b);//10
    }
}

3. Practice

package com.itheima.test;
public class Test3{
    
    
    public static void main(String[] args){
    
    
        /*
        数字6是一个真正伟大的数字,键盘录入两个整数
        如果其中一个为6,最终结果输出true
        如果它们的和为6的倍数,最终结果输出true
        其他情况都是false
        */

        //分析:
        //1.键盘录入两个整数
        // 变量a 变量b
        
        //2.a == 6 || b == 6 || (a + b) % 6 == 0
        //如果满足其中一个,那么就可以输出true

        //键盘录入两个整数
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个整数");
        int number1 = sc.nextInt();
        System.out.println("请输入第二个整数");
        int number2 = sc.nextInt();

        //可以短路逻辑运算符去连接三个判断
        boolean result = number1 == 6 || number2 == 6 || (number1 + number2) % 6 == 0;
        System.out.println(result);
    }
}

Eight, ternary operator

1. Format

Relational expression? expression1 : expression2;

int max = a > b ? a : b;

2. Calculation rules

① First calculate the value of the relational expression
② If the value is true, the value of expression 1 is the result of the operation
③ If the value is false, the value of expression 2 is the result of the operation

3. Sample

package com.itheima.ternaryoperator;
public class TernaryoperatorDemo1{
    
    
    public static void main(String[] args){
    
    
        //需求:使用三元运算符,获取两个数的较大值

        //分析:
        //1.定义两个变量记录两个整数
        int number1 = 10;
        int number2 = 20;
        
        //2.使用三元运算符获取两个整数的较大值
        //格式:关系表达式 ? 表达式1 : 表达式2;
        //整个三元运算符的结果必须要被使用
        int max = number1 > number2 ? number1 : number2;
        System.out.println(max);
        System.out.println(number1 > number2 ? number1 : number2);
    }
}

4. Practice

package com.itheima.test;
import java.util.Scanner;
public class Test4{
    
    
    public static void main(String[] args){
    
    
        /*
        需求:动物园里有两只老虎,体重分别通过键盘录入获得
        请用程序实现判断两只老虎的体重是否相同
        */

        //分析:
        //1.键盘录入两只老虎的体重
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一只老虎的体重");
        int weight1 = sc.nextInt();
        System.out.println("请输入第二只老虎的体重");
        int weight2 = sc.nextInt();   
        //2.比较
        //true false
        //相同 不同
        //System.out.println(weight1 == weight2);

        String result = weight1 == weight2 ? "相同" : "不同";
        System.out.println(result);     
    }
}
package com.itheima.test;
import java.util.Scanner;
public class Test5{
    
    
    public static void main(String[] args){
    
    
        /*
        需求:一座寺庙里住着三个和尚,已知他们的身高分别为150cm、200cm、165cm
        请用程序实现获取这三个和尚的最高身高
        */

        //1.定义三个变量记录三个和尚的身高
        int height1 = 150;
        int height2 = 210;
        int height3 = 165;
        //2.拿着第一个和尚和第二个和尚进行比较
        //再拿着结果和第三个和尚进行比较即可
        int temp = height1 > height2 ? height1 : height2;
        int max = temp > height3 ? temp : height;
        //ctrl + alt + L 自动格式化代码
        System.out.println(max);
    }
}

5. Operator precedence

⭐Remember ()

Guess you like

Origin blog.csdn.net/m0_68111267/article/details/130358604