Java operator

Daily nuts 2020-11-06 22:48:57
java operator

Catalog ：

1. Concept

2. Arithmetic operator

3.  Assignment operator

4.  Comparison operator

5.  Logical operators

6.  Ternary operator

Concept

Operator ： A symbol for a specific operation, such as ：+ - * /

expression ： Expressions connected by operators are called expressions

stay Java Divided into ： Arithmetic operator 、 Assignment operator 、 Comparison operator 、 Logical operators 、 Ternary operator Basic usage

public static void main(String[] args) {
//  Attribute operations can be performed between constants
System.out.println(20 + 30);

//  Variables can also be operated between mathematics
int a = 20;
int b = 30;
System.out.println(a + b);
//  Variables and constants can be mixed
System.out.println(a * 10);

//  division
// 1  Are integers. , The result is also an integer , Just look at the business , There is no remainder
int x = 10;
int y = 3;
System.out.println(x / y); // 3

//  modulus （ Take the remainder ） Only the division modulus of the corresponding integer is meaningful
int result2 = x % y;
System.out.println(result2); // 1

//  If the operation has different data types , The result is a wide range of data types , First raise the data type , Re operation
double result = x / y;
System.out.println(result); // 3.0

}

+ Three uses of

public static void main(String[] args) {
//  usage 1： For numbers , Namely + Law
int x = 30;
int y = 40;
int result = x + y;
System.out.println(result);

//  usage 2： For characters char In terms of type ,char Will be prompted to be int. And then calculate
char c = 'A';
int result2 = c + 1;
System.out.println(result2);

//  usage 3： Corresponding string String(),+  Represents a string join operation
String s1 = "Hello ";
String s2 = "Word";
String result3 = s1 + s2;
System.out.println(result3);

//  Add any data type to a string , The results are all strings
int z = 300;
String s3 = " individual ";
System.out.println(z + s3);

//  priority
String s4 = s3 + z + x;
System.out.println(s4);

//  Change priorities
System.out.println(s3 + (x + y));

}

Auto increment and auto decrement operator ++、--

meaning ： Let's make a variable +1 perhaps -1

Format ：++ Variable name , Variable name ++

public static void main(String[] args) {
//  Use alone
int a = 100;
a++; //  after ++
System.out.println(a); // 101
++a; //  front ++
System.out.println(a); // 102

//  A mixture of
// 1  If it's before ++, Variables are calculated first , Reuse
int num1 = 100;
int num2 = 100;
num1 = num1 + ++num2;
System.out.println(num1); // 201
System.out.println(num2); // 101
// 2  If it's post ++, Variable first uses , Calculate again
int b = 100;
int z = 100;
b = b + z++;
System.out.println(b); // 200
System.out.println(z); // 101
}

Note the arithmetic operator ：

1. Both constant and variable can be operated between mathematics

2. Constants cannot use the increment and decrement operator , Because constants themselves are immutable

2. Java In the use of the assignment operator operation , You don't get a decimal

Assignment operator Basic assignment operators （=）

meaning ： Put the amount to the right of the symbol , Assign a value to the variable on the left

Sign assignment operator

public static void main(String[] args) {
// =
int a = 100; //  Put the... On the right 100 Give it to the left a Storage

// +=  amount to b = b + b;
int b = 100;
b += b;
System.out.println(b); // 200

// -=
int num1 = 100;
num1 -= 100;
System.out.println(num1); // 0

// /=
int num2 = 10;
num2 /= 3;
System.out.println(num2); // 3

// %=
int num3 =10;
num3 %= 3;
System.out.println(num3); // 1

//  Symbolic operators include cast
byte num4 = 30;
num4 += 5;
System.out.println(num4); // 35
}

Note the assignment operator ：

1.  Constants cannot be used for assignments , It cannot be written to the left of the assignment operator

2. The compound assignment operator implies cast

int num4 = 30;
num4 += 5;
// The following transformation will occur
num4 = byte + int;
num4 = int + int;
num4 = (byte)int;

Comparison operator public static void main(String[] args) {
//  Constant comparison
System.out.println(1< 2);

//  Variable comparison
int a = 10;
int b = 20;
System.out.println(a < b);

// >=  Greater than or greater than = All for true
System.out.println(a > 11); // false
System.out.println(a >= 10); // true

// ==  be equal to
System.out.println(a == b); // false

// !=  It's not equal to
System.out.println(a != b); // true
}

Note the assignment operator

1. The result must be boolen value

2. You can't judge more than once in a row 10 < a < 20（ Wrong writing ）, You need to use logical operators

Logical operators

 && || ！ And （ also ） or （ perhaps ） Not （ Take the opposite ） Is full of true It's just true There is one true by true,     Is full of false by false If true Take instead false
public static void main(String[] args) {
int a = 10 ;
int b = 20;
int c = 30;

// &&  And
boolean flag = a > b && b > c;
System.out.println(flag);

// ||  or
boolean flag1 = a > b || b > c;
System.out.println(flag1);

// !  Not
boolean flag2 = !(a > b || b > c);
System.out.println(flag2);

//  Logic short circuit
System.out.println(a > b && ++a >b);
System.out.println(a);  //a = 10, It's not implemented && Later code
}

Note the logic operator

1. It must be boolean value

2. && 、|| It has the effect of short circuit

3. && 、|| There should be... On both sides boolean value ,！ Just have to have one boolean value

4. You can write multiple conditions

Logic short circuit ： If the code on the left can already judge the final result , Then the code on the right will no longer execute

Ternary operator

Unary operator ： An operator that can operate with only one data , Such as ： Take the opposite !、 Self increasing ++、 Self reduction --

Binary operator ： An operator that requires two operators to operate , Such as ： Add +、 assignment =

Ternary operator ： An operator that requires three data to operate on

Ternary operator format ： data type Variable name = conditional ？ expression A : expression B

technological process ： public static void main(String[] args) {
int a = 10;
int b = 20;
int result = a > b ? a : b;
System.out.println(result);    // 20
}

Note the ternary operator

1. The expression on the right must be guaranteed A And expressions B All match the data type on the left

2. Ternary operator results must be used

//  Error model
double d = 2.5;
int result2 = a > b ? a : d;    // Type mismatch
a > b ? a : d; // not used