2.2 the operator

2.2.1 Arithmetic operators

symbol instructions The sample
+ Mathematical addition operation int i = 5+5;
Mathematical subtraction int i =1-6;
A negative number int i = -96;
* Mathematical multiplication operation int i = 7*8;
/ Mathematical division operations int i = 80/5;
% Mathematical mod operation int i = 5%2;
++ Mathematical increment operation x++/++x
Mathematical subtraction x–/–x
package day_05_25;
/ * * + + : said on the difference between x++ and + + x * 1 * x++ : ascribed values in operation - > x++ involved in operations, first in itself since the add * + + x: first operation in assignment - > + + x for the first, in the operation * /
public class Demo_04 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 int i = 10;
		 int j = 30; 
		 int c = ++j +(i++);
		 System.out.println(i);/ / 11
		 System.out.println(c);/ / 41
		 int a = 1;
		 int b = 10; 
		 int g = (--a)+(a++)+(b--)+(b--)+(--b)+(a+b)+(++a)+(b--)-(a++);
		               //0 0 10 9 7 8 2 7 2
		 System.out.println(a);/ / 3
		 System.out.println(b);/ / 6
		 System.out.println(g);/ / 41}}Copy the code

2.2.2 Assignment operators

symbol instructions The sample
= Math assignment int i = 2;
+ = x += 2 x= x+2;
– = x -=2 x=x-2;
* = x*=2 x=x*2;
/ = x/=2 x=x/2;
% = x%=2 x=x%2;

2.2.3 Relational operators

symbol instructions The sample The results of
= = Equivalent to 1 = = 2 false
! = Not equal to the 1! = 2 true
> Is greater than 1 > 2 false
> = Greater than or equal to 1 > = 0 true
< Less than 1 < 0 false
< = Less than or equal to 1 < = 1 true
package day_05_25;

public class Demo_06 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a = 10 ;
		int b =20;
		System.out.println(a==b);// The result is falseSystem.out.println(a! =b);// The result is true
		System.out.println(a>b);// The result is false
		System.out.println(a>=b);// The result is false
		System.out.println(a<b);// The result is true
		System.out.println(a<=b); // The result is true}}Copy the code

2.2.4 Logical operators

symbol instructions explain The results of
& T1 and T2 False is fasle fasle
| or True if true exists true
! (common) non ! True is false! False to true
^ Exclusive or False if they are the same; true if they are different
&& (often used) Short circuit and If T1 is false, T2 does not need to be determined. T1 is true, and T2 needs to be determined
|| Short circuit or T1 is true, T2 does not need to be determined. T1 is false,T2 needs to be determined
package day_05_25;

public class Demo_07 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int a=4;
		int b=5;
		int c=6;
		// If it is false, it is false
		System.out.println("-------- logic and -------");
		System.out.println((a>b)&(a>c));//fasle&false
		System.out.println((a>b)&(a<c));//fasle&true
		System.out.println((a<b)&(a<c));//true&true
		System.out.println((a<b)&(a>c));//true&false
		// or/if there is truth, then it is true
		System.out.println("-------- logic or -----------");
		System.out.println((a>b)|(a>c));//fasle|false
		System.out.println((a>b)|(a<c));//fasle|true
		System.out.println((a<b)|(a<c));//true|true
		System.out.println((a<b)|(a>c));//true|false
		System.out.println("-- -- -- -- -- - or -- -- -- -- -- -- --");
		System.out.println((a>b)^(a>c));//fasle^false
		System.out.println((a>b)^(a<c));//fasle^true
		System.out.println((a<b)^(a<c));//true^true
		System.out.println((a<b)^(a>c));//true^false
		System.out.println("------ logic not -------"); System.out.println(! (a<b));//fasleSystem.out.println(!!! (a>b));//true
		System.out.println("-------- phrase and ----------");
		System.out.println((a>b)&&(a>c));//fasle
		System.out.println((a>b)&&(a<c));//fasle
		System.out.println((a<b)&&(a<c));//true&&true
		System.out.println((a<b)&&(a>c));//true&false
		System.out.println("-------- short circuit or -----------");
		System.out.println((a>b)||(a>c));//fasle||false
		System.out.println((a>b)||(a<c));//fasle||true
		System.out.println((a<b)||(a<c));//true
		System.out.println((a<b)||(a>c));//true }}Copy the code

2.2.5 Ternary operators

Returns a result when the data is true or false

Format: Relational expressions? Expression 1: expression 2;

Component T (condition)? Result 1: Result 2;

Execution flow: Returns true or false depending on the evaluation of the expression

If true, the expression 1 is taken as the result

If false, take expression 2 as the result

package day_05_25;

public class Demo_08 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
      int x = 3;
      int y =4;
      inti = (x>y)? x:y; System.out.println(i);// Return y,4}}Copy the code