Java beginners learn online

Some notes on operators

1. Pay attention to the difference between assignment operator '=' and relational operator '= ='

  • Assignment operators are used to assign values to variables
  • Relational operators are used to judge the relationship between two expressions or both sides of '= ='

give an example:

		int a = 10;
		a = a + 10;
		System.out.println("a = " + a);

2. Pay attention to self increasing and self decreasing operators in calculation

Self increment:
a + +: Post + + first brings the value of the variable into the expression for operation, and then performs the self increment operation.
+ + a: the pre + + first performs the autoincrement operation, and then brings the autoincrement result into the expression for operation.
Self subtraction:
a --: post – first bring the value of the variable into the expression for operation, and then perform self subtraction.
-- a: front – first perform self subtraction operation, and then bring the result of self increment into the expression for operation.

Note: whether it is a pre operation or a post operation, the auto increment / Auto decrement operation will be executed immediately after the reference is completed.

give an example:

		int a = 10;
		a++; // a = a + 1;
		System.out.println("a = " + a);
		++a; // a = a + 1;
		System.out.println("a = " + a);
		System.out.println();
		a--; // a = a - 1;
		System.out.println("a = " + a);
		--a; // a = a - 1;
		System.out.println("a = " + a);
		System.out.println();
		a = 10;
		int b = 10;
		int result = 0;
		result = a++ + b + a;
		System.out.println("result = " + result);
		System.out.println("a = " + a);

		a = 10;
		b = 10;
		result = 0;
		result = ++a + b + a;
		System.out.println("result = " + result);
		System.out.println("a = " + a);

3. Relational operators

The result of relational operators is always a Boolean value.

True: true

False: false

‚Äč < >= <= == !=.

You can only operate on numbers.

give an example:

int a = 10, b = 20;
		boolean result;
		
		result = a > b;
		System.out.println(result);
		
		result = a < b;
		System.out.println(result);
		
		result = a <= b;
		System.out.println(result);
		
		result = a >= b;
		System.out.println(result);
		
		result = a == b;
		System.out.println(result);
		
		result = a != b;
		System.out.println(result);

4. Boolean operator

And:
& Logic in

- Boolean variables and integer type variables can be operated

- the expression on the right will execute regardless of whether the result can be obtained directly on the left

& &: short circuit to

- only Boolean variables can be manipulated

- when the result can be obtained directly on the left, the expression on the right will not be associated with the execution

- Boolean operations are recommended to be short circuited to

- if two Boolean participate in the operation, when both conditions are true, the final result is true, otherwise it is false.

Or:

|: logical or

- Boolean variables and integer type variables can be operated

- the expression on the right will execute regardless of whether the result can be obtained directly on the left

|: short circuit or

- only Boolean variables can be manipulated

- when the result can be obtained directly on the left, the expression on the right will not be associated with the execution

- short circuit or short circuit is recommended for Boolean operations

If two Boolean participate in the operation, when both conditions are false, the final result is false, otherwise it is true.

Non:!

- not true is false, not false is true.

- when & & | appears in our expression at the same time, calculate the & & operation first and then the | operation. The priority of the & & operator is higher than that of the | operator

give an example:

boolean b1 = true;
		boolean b2 = true;
		boolean b3 = true;

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

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

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

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

5. Bitwise operator

Bitwise operators are mainly used to calculate integers, mainly in binary

Binary: 0: false 1: positive

&: the same 1 is 1, the same 0 is 0, and the difference is 0

|: the same 1 is 1, the same 0 is 0, and the difference is 1

~: reverse by bit

^: bitwise operation, 0 for the same, 1 for the different

< <: shift left operation: the highest displacement is out of the range, the lowest displacement is 0, and the shift left is equivalent to * 2

> >: shift right operation: the lowest bit moves out of the range, and the highest bit complements 0 with a positive number and 1 with a negative number. Shift left is equivalent to / 2

> > >: shift right operation: the lowest bit moves out of the range, and the highest bit complements 0. Shift left is equivalent to / 2

give an example:

int a = 10; // 0000 1010
		int b = 7; 	// 0000 0111
					// 0000 1111
		int result = a | b;
		System.out.println(result);
		// -117 -11
		byte p = 88; // 0x37
		// 0011 0111
		//~1100 1000 - complement calculation original code (take method + 1) - > 0011 1000 -- > 0x38 -- - 56	
		
		a = 10; // 0000 1010
		b = 5;	// 0000 0101
				// 0000 1111
		result = a ^ b;
		System.out.println(result);

		a = 10;
		b = 20;
		System.out.println("a = " + a + " b = " + b);
		a = a ^ b;
		b = a ^ b;
		a = a ^ b;
		System.out.println("a = " + a + " b = " + b);
		
		//Another way to complete the exchange without intermediate variables
		a = 10;
		b = 20;
		a = a + b;
		b = a - b;
		a = a - b;
		System.out.println("a = " + a + " b = " + b);
		
		// Use intermediate variables to complete the exchange of two numbers
		a = 10;
		b = 20;
		int c = a;
		a = b;
		b = c;
		System.out.println("a = " + a + " b = " + b);
	
		//Displacement operation
		byte b = -15;
		System.out.println(b>>>1);

6. Extended assignment operator

	    //Left operation < = > right operation
		a += 10; // a = a + 10;
		a -= 10; // a = a - 10;
		a *= 10; // a = a * 10;
		a /= 10; // a = a / 10;
		a %= 10; // a = a % 10;

		// Ternary operator: condition (boolean type or Boolean expression)? Result 1 (true): result 2 (false);
		int b = 30;
		int c = 20;

		int reuslt = b < c ? b : c;
		System.out.println(reuslt);

Tags: Java

Posted by Imperialdata on Sat, 07 May 2022 00:12:41 +0300