In Java, the arithmetic operators , -, *, /, and % represent addition, subtraction, multiplication, division, and modulo respectively.
There are three types in java: 1. Normal operation. 2. Use as positive and negative. 3. As a connector (when data of any data type is connected to a string, that number is the connector)
Example:
package com; public class liu { public static void main(String[] args) { //实例一:作为正常运算 int c = 1; int d = 2; int e = c + d; System.out.println(e) //最后结果为3; //实例二:作为连接符 //已知有两个变量a和b,值分别为1和2,在控制台上输出a + b = 3, 1 + 2 = 3 int a = 1; int b = 2; System.out.println(a + " + " + b + " = " + (a + b)); //控制台显示结果为:a + b = 3。 //注意此时的"+","="作为字符串,而(a + b)则进行运算。 } }
in java There are two types: 1. Normal operation. 2. Use as positive and negative.
Example:
package com; public class liu { public static void main(String[] args) { int a = 1; int b = 3; int c = b-a; System.out.println(c); //运算结果:2。 } }
Example:
package com; public class liu { public static void main(String[] args) { int i = 4; int j = 2; int a = i*j; System.out.println(a);//8 } }
When the two numbers involved in/operating are both integers , the result is an integer, otherwise it is a floating point number.
Example:
package com; public class liu { public static void main(String[] args) { //实例1:作为整数运算 int i = 4; int j = 2; int a = i / j; System.out.println(a);//2 //实例2:作为浮点运算 int i = 5; double j = 2; double a = i / j; System.out.println(a);//2.5 } }
The remainder operation of an integer (sometimes called modulo) is represented by %.
package com; public class liu { public static void main(String[] args) { int i = 2; int j = 3; System.out.println(i % j); //0 int a = 4; int b = 2; System.out.println(a % b); //2 } }
Used alone: No matter before or after, the result is the same. See Example 1 for details.
Participate in the operation: First: first add 1 to itself, and then participate in the operation, see Example 2 for details; Behind: first participate in the operation, and then add 1 to itself, see Example 3 for details.
Example:
package com; public class liu { public static void main(String[] args) { //实例1:单独运算。 int i = 1; i++; // i = i + 1 ++i; System.out.println(i);//结果都等于2。 //实例2:++在前。 int i = 1; int j = ++i; System.out.println(j);//2 System.out.println(i);//2 //实例3:++在后。 int i = 1; int j = i++; System.out.println(j);//1 System.out.println(i);//2 } }
Used alone: No matter whether – is in front or behind, the result is the same. See Example 1 for details.
Participate in the operation: – In the front: first decrement itself by 1, and then participate in the operation, see Example 2 for details; – In the back: first participate in the operation, and then decrement itself by 1, see Example 3 for details.
package com; public class liu { public static void main(String[] args) { //实例1:单独运算。 int i = 2; i--; // i = i - 1 --i; System.out.println(i);//结果都等于1。 //实例2:--在前。 int i = 1; int j = --i; System.out.println(j);//1 System.out.println(i);//1 //实例3:--在后。 int i = 1; int j = i--; System.out.println(j);//2 System.out.println(i);//1 } }
You can use binary operators in assignment, and the form is very simple: x = 4 is equivalent to x = x 4.
Common assignment operators are: =, =, -=, *=, /=, %=.
The following takes = as an example:
package com; public class liu { public static void main(String[] args) { int i = 1; i += 2; // i = i + 2 System.out.println(i); //输出结果3 byte b = 1; b += 2; // b = (byte)(b + 2) System.out.println(b); } }
The result of the relational operator must be boolean type data, which means that it is either true , or false
Common relational operators: >, <, >=, <=, !=, ==.
Example:
package com; public class liu { public static void main(String[] args) { int i = 3; int j = 2; System.out.println(i > j);//true System.out.println(i < j);//false System.out.println(i >= j);//true System.out.println(i <= j);//false System.out.println(i != j);//true System.out.println(i == j);//false // ==是比较两边的大小,=是赋值。 } }
The data types of the data on the left and right sides are all boolean type, and the results are all boolean type.
As long as one of the two sides is false, the result will be false
Example:
package com; public class Demo12 { public static void main(String[] args) { System.out.println(true & true); System.out.println(true & false); //false System.out.println(false & false); System.out.println(false & true); //false } }
As long as one of the two sides is true, the result is true.
Example:
package com; public class Demo12 { public static void main(String[] args) { System.out.println(true | true); System.out.println(true | false); System.out.println(false | false); //false System.out.println(false | true); } }
If both sides are the same, it will be false, and if both sides are different, it will be true.
Example:
package com; public class Demo12 { public static void main(String[] args) { System.out.println(true ^ true); //false System.out.println(true ^ false); System.out.println(false ^ false); //false System.out.println(false ^ true); } }
is directly the opposite.
Example:
package com; public class Demo12 { public static void main(String[] args) { System.out.println(!true); //false System.out.println(!false); //true System.out.println(!!true); //true System.out.println(!!!true); //false } }
As long as one of the two sides is false, the result will be false.
Example:
package com; public class Demo12 { public static void main(String[] args) { System.out.println(true && true); System.out.println(true && false); //false System.out.println(false && false); //false System.out.println(false && true); //false } }
What is the difference between & and &&?
&&: If the left side is false, the right side will no longer be executed, and the result will be false.
&: If the left side is false, the right side will still be executed, and the result will be false.
As long as one of the two sides is true, the result is true.
Example:
package com; public class Demo12 { public static void main(String[] args) { System.out.println(true || true); //true System.out.println(true || false); //true System.out.println(false || false); //false System.out.println(false || true); //true } }
What is the difference between | and ||?
||: If the left side is true, the right side will no longer be executed, and the result will be true.
|: If the left side is true, the right side will still be executed, and the result will be true.
The data on both sides are numbers, and the result is also a number.
Common bitwise operators: & (AND bit operation), | (OR bit operation), ^ (XOR bit operation), ~ (bitwise negation), >> (right shift operation), << (left shift operation), >>> (unsigned right shift).
Example:
package com; public class Demo01 { public static void main(String[] args) { System.out.println(3 & 2); //2 System.out.println(3 | 2); //3 System.out.println(3 ^ 2); //1 System.out.println(~3);//-4 System.out.println(3 >> 2);//0 System.out.println(3 << 2);//12 System.out.println(3 >>> 2);//0 System.out.println(-3 >> 2);//-1 System.out.println(-3 >>> 2); } }
>> What is the difference between >>>?
>>: If the data is a negative number, the leftmost sign bit is 1. After the right shift, 1 must be added to the left.
If the data is a positive number, the leftmost sign bit is 0. After the right shift, 0 must be added to the left.
>>>: Regardless of whether the data is positive or negative, after the right shift, 0 is added to the left.
Format: Conditional expression? Expression 1 : Expression 2; Equivalent to x > y ? x : y
Conditional expression The result of the expression must be of type boolean
Execution process:
If the conditional expression is true, expression 1 will be executed, and expression 2 will not be executed
If the condition If the expression is false, expression 2 will be executed and expression 1 will not be executed
Example:
package com; public class Demo02 { public static void main(String[] args) { //获取两个数的较大值 int i = 2; int j = 3; //第一种: int max = i > j ? i : j; System.out.println(max); //3 //第二种: System.out.println(i > j ? i : j); //表达式1和表达式2既然会得到一个结果,如果传递给一个变量去接收,该变量的数据类型应该和表达式1和表达式2的结果的数据类型匹配。 //浮点型: double d = 3 > 2 ? 1 : 2; System.out.println(d); //输出:1.0 //字符型: char c1 = 3 > 2 ? 97:98; System.out.println(c1); //输出:a } }
The above is the detailed content of Analysis of application examples of various operators in Java. For more information, please follow other related articles on the PHP Chinese website!