Day03 operator

Task 3: operator

1. Arithmetic operator

  • +Represents the addition operator
  • -Represents the subtraction operator
  • *Represents the multiplication operator
  • /Represents the division operator
  • %Represents the modulo / remainder operator
package com.lagou.Day03;

/**
 * Arithmetic operator
 */
public class Demo01 {
    public static void main(String[] args) {

        //1. Declare two variables of type int and initialize them
        //int ia = 6;ib = 2;// It means declaring two int variables IA and IB, which are not recommended
        int ia = 6;
        int ib = 2;

        //Implement arithmetic operators
        int ic = ia + ib;
        System.out.println(ic);//8
        System.out.println(ia + ib);//8

        //The whole of ia+ib is called the expression ia, and ib is called the operand
        System.out.println(ia + ib);//8
        System.out.println(ia - ib);//4
        System.out.println(ia * ib);//12
        System.out.println(ia / ib);//3
        System.out.println(ia % ib);//0
    }
}

2. Precautions for arithmetic operators

package com.lagou.Day03;

public class Demo02 {
    public static void main(String[] args) {
        //Note 1: when two integers are divided, only the integer part is retained and the decimal part is discarded
        System.out.println(5 / 2);//2

        //Note 2: if you want to keep the decimal part
        //Method 1: use forced type conversion to convert one of the operands to bit double type and then operate
        System.out.println((double)5 / 2);//2.5
        System.out.println(5/(double)2);//2.5
        System.out.println((double)5/(double)2);//2.5
        //System.out.println((double) (5/2));//2.0

        //Method 2: multiply one of the operands by 1.0 (recommended)
        System.out.println(5*1.0/2);//2.5

        //Note 3: 0 cannot be divisor
        //Exception in thread "main" java.lang.ArithmeticException can be compiled but not run
        System.out.println(5 / 0);

        System.out.println(5 / 0.0);//Infinity infinity
        System.out.println(0 / 0.0);//NaN not a Number
    }
}

3. Arithmetic operators realize time splitting

  • Prompt the user to enter the seconds of positive integer type, and output x hours X minutes x seconds after splitting the seconds.
  • For example, input 7199 and output 1 hour, 59 minutes and 59 seconds
package com.lagou.Day03;

import java.util.Scanner;

public class Test01 {
    public static void main(String[] args) {

        System.out.println("Please enter a positive integer");
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();

        int hour = x / 3600;//Split hours
        int min = x % 3600 /60;//Split minutes
        int sec = x % 60;

        System.out.println(x + "Second conversion bit" + hour + "hour" + min + "minute"+sec+"second");
    }
}

4. Concept and use of string linker

  • +You can connect strings and "connect" strings with other data types
package com.lagou.Day03;

/**
 * +It can be used as both string connector and addition operator
 * As long as one of the operands on both sides of + is of string type, the + is treated as a string connector,
 * Otherwise, it is treated as an addition operator
 */
public class Demo03 {
    public static void main(String[] args) {
        int hour = 1;
        int min = 1;
        int sec = 6;
        System.out.println(hour+min+sec);//Number: 8
        System.out.println(hour+min+sec+"");//String: 8
        System.out.println(hour+min+""+sec);//26;
        System.out.println(hour+""+min+sec);//116
        System.out.println(""+hour+min+sec);//116
        System.out.println(""+(hour+min+sec));//Count in parentheses first: 8
    }
}

5. Relation / comparison operator

>Indicates whether the is greater than the operator >=Indicates whether the is greater than or equal to operator
<Indicates whether it is less than the operator <=Indicates whether the is less than or equal to operator
==Indicates whether it is equal to the operator	!=Indicates whether it is not equal to the operator
package com.lagou.Day03;

/**
 * Relational operator
 */
public class Demo04 {
    public static void main(String[] args) {
        int a = 5;
        int b = 2;
        System.out.println(a > b);//true
        System.out.println(a >= b);//true
        System.out.println(a < b);//false
        System.out.println(a <= b);//false
        System.out.println(a == b);//false
        System.out.println(a != b);//true
    }
}

6. Relational operators realize negative number judgment

  • Prompt the user to enter an integer. Use the relational operator to judge whether the integer is negative. If so, print true. If not, print false.
package com.lagou.Day03;

import java.util.Scanner;

public class Test02 {
    public static void main(String[] args) {
        System.out.println("Please enter an integer");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        System.out.println(num<0);
    }
}

7. Concept and use of self increasing and decreasing operator

  • ++Indicates the self increasing operator, which is used to increase the value of the current variable by 1
  • – represents the self subtraction operator, which is used to reduce the value of the current variable by 1
package com.lagou.Day03;

public class Demo05 {
    public static void main(String[] args) {
        int a = 10;
        System.out.println(a);//10

        //It means adding 1 to the value of variable a itself and overwriting the original value of the variable
        a++;
        System.out.println(a);//11

        ++a;
        System.out.println(a);//12
    }
}

8. Difference between front and back

package com.lagou.Day03;

/**
 * a++This whole is called an expression, where a is called an operand / variable.
 * That is, a + + and a have different meanings, so the memory space occupied should be different
 */
public class Demo06 {
    public static void main(String[] args) {
        int a = 10;

        //Print expression results here
        //The latter + + means that the value of variable a is taken as the result of the whole expression, and then the value of variable a itself is added by 1
        System.out.println(a++);//10

        //The result of the variable is printed here
        //First + + add 1 to the value of a variable itself, and then take the value of the variable as the result of the whole expression
        System.out.println(a);//11
        System.out.println(++a);//12
        System.out.println(a);//12
    }
}

9. Written test point of self increasing and decreasing operator

package com.lagou.Day03;

public class Test04 {
    public static void main(String[] args) {
        int a = 12;

        int b = a++;
        System.out.println(b);//12
        System.out.println(a);//13

        int c = ++a;
        System.out.println(c);//14
        System.out.println(a);//14
      
        System.out.println(a++ + ++a);//30
        System.out.println(a);//16  
    }
}

##10. Logical operator

  • &&Represents logic and operator, which is equivalent to "and". Both true and false are true and false.
  • ||Represents a logical or operator, equivalent to "or". One true is true and the same false is false.
  • ! Represents a logical non operator, which is equivalent to "Negation". True is false and false is true.
  • The operands of logical operators are boolean expressions.
package com.lagou.Day03;

public class Demo07 {
    public static void main(String[] args) {

        boolean b1 = true;
        boolean b2 = false;
        System.out.println(b1 && b2);//false
        System.out.println(b1 || b2);//true
        System.out.println(!b1);//false
    }
}

11. Short circuit characteristics of logical operators

  • For logic and operators, if the first expression is false, the result is false. At this time, skip the second expression;
  • For logic or operators, if the first expression is true, the result is true. At this time, skip the second expression;
package com.lagou.Day03;

public class Test04 {
    public static void main(String[] args) {
        int a = 3;
        int b = 5;
        boolean c = (++a == 3) && (++b == 5);
        System.out.println(c);//false
        System.out.println(a);//4
        System.out.println(b);//5
    }
}

12. Logical operators judge three digits

package com.lagou.Day03;

import java.util.Scanner;

/**
 * Programming uses logical operators to judge three digits
 */
public class Demo09 {
    public static void main(String[] args) {

        //1. Prompt the user to enter a positive integer and use variable records
        System.out.println("Please enter a positive integer");
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        //2. Use logical operators to judge whether it is three digits and print
        System.out.println(num>=100 && num<=999);
    }
}

13. Conditional / ternary operator

  • Conditional expression? Expression 1: expression 2

14. Look up the maximum value with the ternary operator

15. Assignment operator

  • =Represents the assignment operator, which is used to assign the data on the right of = to the variable on the left of = and overwrite the original value of the variable.
  • The assignment expression itself also has a value, and its own value is the assigned value
  • +=,-=,*=,/=,...
package com.lagou.Day03;

public class Demo11 {
    public static void main(String[] args) {
        //1. Declare a variable of type int and initialize it
        int a = 3;
        System.out.println(a);//3
        System.out.println("--------------");

        //2. Use of simple assignment operator
        //Indicates that data 5 is assigned to variable a and overwrites the original value of variable a
        a = 5;
        System.out.println(a);//5
        //The following code prints the result of the expression
        System.out.println(a=5);//5
        int b = a = 6;
        System.out.println(a);//6
        System.out.println(b);//6
        int c;
        c = b = a = 8;
        System.out.println(a);//8
        System.out.println(b);//8
        System.out.println(c);//8
        System.out.println("--------------");

        //3. Use of compound assignment operator
        a = a + 2;
        System.out.println(a);//10
    }
}

16. Test point 1 of assignment operator

package com.lagou.Day03;

public class Test05 {
    public static void main(String[] args) {
        byte b1 = 10;
        System.out.println(b1);//10

        /**
         * Error: incompatible type: conversion from int to byte may be lost
         * byte + int The addition result is of type int
         */
        //b1 = b1 + 2;
        /**
         * Error: incompatible type: conversion from int to byte may be lost
         * byte + byte The addition result is of type int
         */
        //b1 = b1 + (byte)2;
        b1 += 2;
    }
}

17. Test point 2 of assignment operator

package com.lagou.Day03;

public class Test06 {
    public static void main(String[] args) {
        /**
         * a == 2;-Indicates whether the value of variable a is equal to 2
         * 2 == a;-It means to judge whether 2 is equal to the value of variable a. in terms of the result, it is equivalent. This method is recommended.
         * a = 2; -It means that 2 is assigned to variable a and overwrites the original value of variable a
         * 2 = a; -Compilation error
         */
    }
}

18. Shift operator

<<Shift left operator, which is used to move the binary bits of data to the left, supplemented by 0 on the right>>Shift right operator is used to move the binary bits of data to the right, and the left is supplemented by sign bits>>>Represents a logical shift right operator, which is used to move the binary bits of data to the right, supplemented by 0 on the left

19. Use of shift operator

package com.lagou.Day03;

/**
 * Programming the use of shift operator
 */
public class Demo13 {
    public static void main(String[] args) {
        //1. Declare a byte type variable and initialize it
        byte b1 = 13;
        //2. Print the value of the variable
        System.out.println("b1 = "+b1);//b1=13

        System.out.println("--------");
        //3. Use of shift operator
        /**
         * 13 The binary bit of is: 0000 1101 = > the result of shifting 1 bit left is: 000 11010 = > the result of converting decimal integer: 26
         */
        //byte b2 = b1 << 1;// Error: incompatible type: conversion from int to byte may result in loss of auto promotion bit int type, i.e. 32 bits
        byte b2 = (byte)(b1 << 1);
        System.out.println("b2 = " + b2);//26 shift left by 1 bit, which is equivalent to the value of the current integer * 2
        System.out.println(b1 << 2);//52
        System.out.println("----------");

        System.out.println(b1 >> 1);//6
        System.out.println(b1 >> 2);//3

        System.out.println("----------");
        System.out.println(b1 >>> 2);//3
    }
}

20. Bitwise operator

  • &Indicates the bitwise and operator, which performs and operation according to binary bits. The same truth is true and false is false (the same 1 is 1 and one 0 is 0)
  • |Indicates the bitwise OR operator, which performs or operation according to binary bits. If there is true, it is true, and if there is the same false, it is false
  • - indicates the bitwise negation operator. Negation is carried out according to binary bits. 1 is 0 and 0 is 1
  • ^Indicates the bitwise XOR operator, which performs XOR operation according to binary bits. The same is 0, but the difference is 1

21. Use of bitwise operators

package com.lagou.Day03;

/**
 * Bitwise Operators 
 */
public class Demo14 {
    public static void main(String[] args) {
        //1. Declare two byte type variables and initialize them
        byte b1 = 11;
        byte b2 = 13;

        //Bitwise implementation of operator 2
        /**
         * b1 Binary: 0000 1011
         * b2 Binary: 0000 1101
         */
        System.out.println(b1 & b2);//The same 1 is 1, a 0 is 0 0000 1001 = > to decimal is: 9
        System.out.println(b1 | b2);//One 1 is 1, the same 0 is 0 0000 1111 = > converted to decimal: 15
        System.out.println(b1 ^ b2);//The same is 0, the difference is 1 0000 0110 = > to decimal: 6  
        System.out.println(~b1);//1 is 0, 0 is 1 1111 0100 = > - 12
        //Binary 1111 0100 is converted to decimal, first minus 1:1111 0011, reverse 0000 by bit, 1100 is converted to decimal: 12 result: - 12
    }
}

22. Operator priority

  • () has a high priority
  • =Very low priority
  • If the priority cannot be confirmed, use () to ensure it

Keywords: Java JavaSE

Added by mobile target on Thu, 10 Feb 2022 01:23:32 +0200