Basic Java operators and basic java Operators
Java Operators
Operator: a special symbol used to calculate, assign values, and compare data.
Operators in Java are classified into the following categories:
- Arithmetic Operator: +-*/++ --
Sample Code:
1 public class TestAir {2 public static void main (String [] args) {3 int I = 12; 4 int j = 5; 5 // addition/subtraction operation 6 int k = I + j; 7 System. out. println (k); 8 k = I-j; 9 System. out. println (k); 10 // multiplication and division operation 11 k = I * k; 12 System. out. println (k); 13 double dou; 14 dou = I * 5.0; 15 System. out. println (dou); 16 // represents 17 double d = I/j for a certain number; 18 System. out. println (d); 19 d = I/5.0; 20 System. out. println (d); 21 // % indicates that the remainder of a number is 22 k = I % j; 23 System. out. println (k); 24 25 // ++, -- operator 26 System. out. println (I ++); // ++ after the variable, + 1 is performed only after the variable is used. 27 System. out. println (++ I); // ++ before the variable, + 1 is performed before the variable is used. 28 // ++ and -- operator 29 System. out. println (I --); // -- after the variable is used, the-1 operation is performed 30 System. out. println (-- I); // -- before the variable, the-1 operation is performed before the variable is used. 31} 32}
- Value assignment operator
Note that the assignment operator has the lowest priority in Java, that is, the assignment operator is always executed in the formula where other operators exist.
The sample code is as follows:
1 public class TestAssign { 2 public static void main(String[] args) { 3 //+=、-=、*=、/= 4 int a=10; 5 int b=2; 6 a+=b;//a=a+b; 7 System.out.println(a); 8 a-=b;//a=a-b; 9 System.out.println(a);10 a*=b;//a=a*b;11 System.out.println(a);12 a/=b;//a=a/b;13 System.out.println(a);14 }15 }
- Comparison operator (relational operator)
The comparison operator is used to determine the size of two data types, for example, greater than, equal to, or not equal. The comparison result is a Boolean value (true or false ).
The following table lists common comparison operators in Java:
The sample code is as follows:
1 public class TestCompare {2 public static void main (String [] args) {3 int a = 16; 4 double B = 9.5; 5 String str1 = "hello "; 6 String str2 = "imooc"; 7 System. out. println ("a equals B:" + (a = B); 8 System. out. println ("a greater than B:" + (a> B); 9 System. out. println ("a less than or equal to B:" + (a <= B); 10 System. out. println ("str1 equals str2:" + (str1 = str2); 11} 12}
Note:
1.>, <, >=, <= only the operands on both sides of the left and right are numerical values.
2, = ,! = The operands on both sides can be numeric or reference type
Logical operators are mainly used for logical operations. Common logical operators in Java are shown in the following table:
We can understand the logical operators from the perspective of "voting:
1. And: requires everyone to vote for a certain topic.
2. Or: only one person is required to vote for a certain topic.
3. Non: a user who originally voted for the vote can invalidate the vote by using non-operators.
4. Exclusive or: only one person who votes to agree can pass a certain topic.
When using logical operators, we encounter a "Short Circuit" phenomenon.
For example, in (a> B) & (a <c), if the running result of the expression on the left is determined to be false, the system considers it unnecessary to execute the expression on the right.
Similarly, in (a> B) | (a <c), if you can determine that the running result of the Left expression is true, the system will also think that there is no need to execute the expression on the right!
Is it also a conditional operator boolean expression? Expression a: When expression B boolean expression returns true, expression B is executed when expression a and boolean expression returns false.
The sample code is as follows:
1 public class TestCondition {2 public static void main (String [] args) {3 int score = 68; 4 String mark = (score >=60 )? "Pass !! ":" Sorry, fail !! "; 5 System. out. println (" How are exam scores: "+ mark); 6} 7}