Operators and basic data type conversion in Java

Today, I learned all kinds of operators and data type conversion in Java. Now I'll share what I've learned. I hope you can guide and supplement.

1, Arithmetic operator

operatormeaning
+Sum
-subtract
*product
/Quotient (divisible or quotient)
%Remainder (modulus)
++Self plus one
--Self minus one

           ++: Unary operator, the operation of adding 1 by itself. If it is placed in the front, add it by itself first, and then participate in the operation
                 If you put it later, first participate in the operation, and then add it yourself
           --: Unary operator, the operation of self subtracting 1. If it is placed in the front, self subtraction first, and then participate in the operation
                 If you put it later, first participate in the operation, and then subtract from it

public class OperatorDemo{
    public static void main(String[] args){
        int a = 1;
        int b = 2;
        System.out.println(a + b);
        System.out.println(a - b);
        System.out.println(a * b);
        System.out.println(a / b);
        System.out.println(a % b);
        System.out.println(a++);
        System.out.println(--b);
}
}

2, Assignment operator

One equals sign = is assignment, and two equals signs = = are relational operators

3, Extension operator

Extended assignment operators: + =, - =, * =/=

//Extension operator
		int c = 10;
		//Operation of c plus 2
		//The results of the two writing methods are the same, so it is recommended that everyone use the extension operator
		//c = c + 2;
		c += 2;

4, Relational operator

Relational operators: >, <, > =, < =, = ==

The first few are more than, less than, greater than or equal to, less than or equal to, which are commonly used in mathematics. There is no need to say more

"= =" indicates whether the value on the left is equal to the value on the right

"! =" indicates that the output result is the same as! The latter result is the opposite

//Relational operator
		System.out.println(4 > 5);
		System.out.println(4 < 5);
		System.out.println(4 >= 5);
		System.out.println(4 <= 5);
		System.out.println(4 == 5);
		System.out.println(4 != 5);
		

5, Logical operator

Logical operators: & &, &, ||

/*
		Logical operators:
			&&: Short circuit and are executed from left to right. If the first expression returns false, the following expressions will not be executed
				&: Logical and are executed from left to right. No matter whether the first expression returns true or false, the following expressions will be executed
			||: Short circuit or, execute from left to right. If the first expression returns true, the following expressions will not be executed
				|: Logical or is executed from left to right. No matter whether the first expression returns true or false, the following expressions will be executed
		*/
		
		System.out.println( 2 > 3 && 5 > 4);
		int i = 1;
		//System.out.println( 2 > 3 && i++ > 1);
		//System.out.println(i);//1
		System.out.println( 2 > 3 & i++ > 1);
		System.out.println(i);//2
		System.out.println( 2 > 3 || 5 > 4);
		System.out.println(!false);
		System.out.println(!true);
		System.out.println(!(2 > 3));

6, Bitwise operator

Bitwise operators: &, |, ^, ~, > >, <, > > (understand!!!)

>>2. Signed right shift. For positive numbers, a few bits to the right is the division of several bits
            <<: A signed left shift, for a positive number, a few bits to the left is a multiple of 2
            >>>: unsigned right shift

I don't particularly understand these bitwise operators, and I don't understand what Lao Yu said today, so I won't explain this, just for understanding.

7, Conditional operator

Conditional operator?:

Conditional operator: also known as ternary operator. When the first expression returns true, the value of "conditional operator" is taken? If the first expression returns false, the following value will be taken

        System.out.println(2 < 3 ? 2 : 3);\\2
		System.out.println(false ? false : true ? false : true);\\false

  8, Conversion of basic data types

Conversion between basic data types:
1. Automatic conversion (implicit conversion)
      Assignment operator = when the data range on the left is large and the data range on the right is small, and generally the same data type, automatic conversion will occur
  2. Force conversion
       Assignment operator = when the data range on the left is small and the data range on the right is large, and the data type is generally the same, the cast must be used
Syntax: (datatype)
Casting may result in loss of data precision

        byte bb = 10;
		//bb = (byte)(bb + 2);
		bb += 2;
		System.out.println(bb);

  Well, that's the end of this class. See you next time! bye-bye! 🤗

Keywords: Java Back-end

Added by TowerOfPower on Wed, 17 Nov 2021 17:44:58 +0200