In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "summary of knowledge points of Java operators". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Java operator
One of the most basic uses of computers is to perform mathematical operations. As a computer language, Java also provides a rich set of operators to manipulate variables. We can divide the operators into the following groups:
Arithmetic operator
Relational operator
Bit operator
Logical operator
Assignment operator
Other operators
Arithmetic operator
Arithmetic operators are used in mathematical expressions, and they play the same role as in mathematics. The following table lists all the arithmetic operators.
The example in the table assumes that the value of integer variable An is 10 and that of variable B is 20:
Operator description example + addition-addition operator on both sides A + B equals 30-subtraction-left Operand minus right Operand A-B equals-10 * multiplication-multiplication operator A * B equals 200 / Division-left Operand divided by right Operand B / An equals 2% remainder-left Operand divided by right Operand B% An equals zero + Self-increment: the value of the Operand increases by 1B plus or + + B equals 21 (see below for details)-- minus: the value of the Operand decreases 1B-or-B equals 19 (see below for details) example
The following simple example program demonstrates the arithmetic operator. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
Example
Public class Test {public static void main (String [] args) {int a = 10; int b = 20; int c = 25; int d = 25; System.out.println ("a + b =" + (a + b)); System.out.println ("a-b =" + (a-b)); System.out.println ("a * b =" + (a * b)); System.out.println ("b / a =" + (b / a) System.out.println ("b% a =" + (b% a)); System.out.println ("c% a =" + (c% a)); System.out.println ("ahem + =" + (aqu +)); System.out.println ("aMub-=" (Amura -)); / / View the different System.out.println ("dash + =" (dumby +)) of dumped + and + + d; System.out.println ("+ + d =" + (+ + d));}}
Run the instance »
The compilation and running results of the above examples are as follows:
A + b = 30a-b =-10a * b = 200b / a = 2b% a = 0c% a = 5aqv + = 10aMub-= 11dq + = 25cm d = 27 self-increasing and self-subtracting operator
1. The self-increasing (+) self-subtracting (- -) operator is a special arithmetic operator, which requires two operands to operate in the arithmetic operator, while the self-increasing and self-subtracting operator is an Operand.
Example
Public class selfAddMinus {public static void main (String [] args) {int a = 3 int / define a variable; int b = + + a System.out.println / self-increment int c = 3; int d =-- c; System.out.println ("the value after self-increment is equal to" + b); System.out.println ("the value after self-subtraction is equal to" + d);}}
The running result is:
The value after self-increment operation is equal to 4, and the value after self-subtraction operation is equal to 2.
Parsing:
Int b = + + a; the process of splitting operation is as follows: b=a=4, and the final result is baked 4
Int d =-- c; the process of split operation is as follows: cymbal color 1cm 2; d=c=2, the final result is dumb2m cymbi2.
2. Prefix self-increment and self-subtraction (+ + a-mai-mai-mura): the operation of self-increment or self-subtraction is carried out first, and then the expression operation is performed.
3. Suffix increment and self-subtraction: perform expression operation first, and then perform self-increment or self-subtraction operation example:
Example
Public class selfAddMinus {public static void main (String [] args) {int a = 5 / define a variable; int b = 5; int x = 2 variables; int y = 2 variables; System.out.println ("after operator prefix operation a =" + a + ", x =" + x "); System.out.println (" operator suffix operation after b = "+ b +", y = "+ y);}}
The running result is:
After the prefix operation of the increasing operator, after the operation of the prefix of the increasing operator, after the operation of the prefix of the increasing operator, after the operation of the prefix of the increasing operator, after the operation of the prefix of the increasing operator, after the operation of the prefix of the increasing operator, after the operation of the suffix of the increasing operator, after the operation of the suffix of the increasing operator
The following table shows the relational operators supported by Java
The value of the instance integer variable An in the table is 10, and the value of variable B is 20:
The operator description example = = checks whether the values of two operands are equal, and if they are equal, the condition is true. (a = = B) is false. ! = check whether the values of the two operands are equal, and if the values are not equal, the condition is true. (a! = B) is true. > check whether the value of the left Operand is greater than that of the right Operand. If so, the condition is true. (a > B) is false. = B) false. B)); System.out.println ("a
< b = " + (a < b) ); System.out.println("b >= a = "+ (b > = a)); System.out.println (" b > 2 = "+ c); c = a > 2; / * 15 = 0000 1111 * / System.out.println (" a > 2 = "+ c);}}
The compilation and running results of the above examples are as follows:
A & b = 12a | b = 61a ^ b = 491a =-61a > 2 = 15a > > 2 = 15 logical operator
The following table lists the basic operations of logical operators, assuming that Boolean variable An is true and variable B is false
Operators describe examples & & called logic and operators. The condition is true if and only if both operands are true. (a & B) is false. | | called logic or operator. If either of the two operands is true, the condition is true. (a | | B) is true.! Is called a logical non-operator. Used to reverse the logical state of operands. If the condition is true, the logical non-operator will get false.! (a & B) is true. Example
The following simple example program demonstrates the logical operator. Copy and paste the following Java program and save it as a Test.java file, then compile and run the program:
Example
Public class Test {public static void main (String [] args) {boolean a = true; boolean b = false; System.out.println ("astatableb =" + (aquifolib)); System.out.println ("a | | b =" + (a | | b)); System.out.println ("! (astatableb) =" +! (astatableb));}}
The compilation and running results of the above examples are as follows:
A & b = falsea | | b = true! (a & b) = true short circuit logic operator
When using the and logic operator, the result is true when both operands are true, but when the first operation is false, the result must be false, and the second operation is not judged.
Example
Public class LuoJi {public static void main (String [] args) {int a = 5 / define a variable; boolean b = (a > = 2; System.out.println ("c > > = 2 =" + c); c & = a; System.out.println ("c & = a =" + c); c ^ = a; System.out.println ("c ^ = a =" + c); c | = a; System.out.println ("c | = a =" + c);}}
The compilation and running results of the above examples are as follows:
C = a + b = 30c + = a = 40c-= a = 30c * = a = 300c / = a = 1c% = a = 5c = 2 = 5c > > = 2 = 1c & = a = 0c ^ = a = 10c | = a = 10 conditional operator (?)
Conditional operators are also known as ternary operators. The operator has three operands and needs to determine the value of the Boolean expression. The main purpose of this operator is to determine which value should be assigned to the variable.
Variable x = (expression)? Value if true: value if false instance Test.java file code:
Public class Test {public static void main (String [] args) {int a, b; a = 10; / if an equals 1, set b to 20, otherwise 30 b = (a = 1)? 20: 30; System.out.println ("Value of b is:" + b); / / if an equals 10, set b to 20, otherwise 30 b = (a = 10)? 20: 30 System.out.println ("Value of b is:" + b);}}
The compilation and running results of the above examples are as follows:
Value of b is: 30Value of b is: 20instanceof operator
This operator is used to manipulate an object instance to check whether the object is of a specific type (class type or interface type).
The instanceof operator is used in the following format:
(Object reference variable) instanceof (class/interface type)
If the object referred to by the variable on the left side of the operator is an object of the class or interface (class/interface) on the right side of the operator, the result is true.
Here is an example:
String name = "James"; boolean result = name instanceof String; / / because name is of type String, it returns true
If the object being compared is compatible with the right type, the operator still returns true.
Look at the following example:
Class Vehicle {} public class Car extends Vehicle {public static void main (String [] args) {Vehicle a = new Car (); boolean result = an instanceof Car; System.out.println (result);}}
The compilation and running results of the above examples are as follows:
TrueJava operator precedence
When multiple operators appear in an expression, who comes first? This involves the priority of operators. In a multi-operator expression, different operator precedence can lead to a big difference in the final result.
For example, (1: 3) + (3: 2) * 2, if this expression is calculated by the plus sign first, the answer is 18, and if the multiplication sign is first, the answer is 14.
For example, x = 7 + 3 * 2; here x gets 13, not 20, because the multiplication operator has a higher priority than the addition operator, so calculate 3 * 2 to get 6, and then add 7.
The operator with the highest priority in the following table is at the top of the table and the lowest priority is at the bottom of the table.
Category operator associativity suffix () []. (dot operator) left to right unary + + -! ~ from right to left multiplicative * /% left to right additive +-left to right shift > =
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.