Java BASIC Syntax---java operators 0. Overview
The operators in Java are mainly divided into the following types:
- Arithmetic operators
- Assignment operators
- Relational operators
- logical operators
- Conditional operators
- Bitwise operators
- Other operators
1. Arithmetic operators
operator |
description |
+ |
addition, two numbers on both sides of the plus sign add |
- |
subtraction, minus the number to the left minus the number on the right |
* |
multiplication, multiply two numbers on both sides of the multiplication sign |
/ |
Division, Division sign the number on the left divided by the number on the right | /tr>
% |
modulo. The remainder of the left divided by the number on the right |
+ + |
increment, the value of the operand increased by 1 |
--< /td> |
decrement, operand value minus 1 |
public class Arithmetictest {public static void main (string[] args) {double number1 = 12.390; Double number2 = 74.340; int number3 = 4; System.out.println (number2 + "+" + number1 + "=" + (number2 + number1)); System.out.println (number2 + "-" + number1 + "=" + (number2-number1)); System.out.println (number1 + "*" + number3 + "=" + (Number1 * number3)); System.out.println (number2 + "/" + number1 + "=" + (number2/number1)); System.out.println (number1 + "%" + number3 + "=" + (float) (number1% number3)); int number4 = 5; int number5 = 5; int number = number4++; System.out.println ("number4++ =" + number + ", Number4 =" + Number4); Number = ++number5; System.out.println ("++NUMBER5 =" + number + ", Number5 =" + Number5); System.out.println ("(number++) =" + (number++));; System.out.println ("(number--) =" + (number--)); }}output:74.34 + 12.39 =86.73 74.34-12.39 = 61.95 12.39 * 4 = 49.56 74.34/12.39 = 6.0 12.39% 4 = 0.39 number4++ = 5, Number4 = 6 ++number5 = 6, Number5 = 6 (number++) = 6 (number--) = 7
2. Assignment operators
operator |
Description |
Example |
= |
A simple assignment operator that assigns the value of the right operand to the operand on the left |
c = A+b assigns the value of A+b to C |
+ = |
Add and assign operators, which add the left and right operands to the left operand |
c + = A is equivalent to C = C + A |
-= |
Minus and assignment operators, which subtract left and right operands to the left operand |
C-= A is equivalent to C = c-a |
*= |
Multiply and assign operators, which multiply the left and right operands by assigning the left operand the value of |
c * = A is equivalent to C = c * A |
/= |
In addition to the assignment operator, it assigns the left operand and the right operand to the left operand by dividing the number of |
c/= A is equivalent to C = c/a |
(%)= |
Modulo and assignment operators, which take the left operand and the right operand to the left operand after modulo |
c%= A is equivalent to C = c%a |
<<= |
Left Shift assignment operator |
c << = 2 equivalent to C = C << 2 |
>>= |
Right Shift assignment operator |
c >> = 2 equivalent to C = C >> 2 |
&= |
Bitwise-and-assignment operators |
C&= 2 is equivalent to C = c&2 |
^= |
Bitwise XOR or assignment operator |
c ^ = 2 equivalent to C = c ^ 2 |
|= |
Bitwise OR Assignment operators |
C | = 2 equivalent to C = C | 2 |
public class AssignmentTest { public static void main(String[] args) { int a = 100; int b = 200; int c = 4; System.out.println("a (a += b) = " + (a += b)); System.out.println("a (a -= 100) = " + (a -= 100)); System.out.println("a (a *= c) = " + (a *= c)); System.out.println("a (a /= 100) = " + (a /= 100)); System.out.println("a (a %= 100) = " + (a %= 100)); System.out.println("c ( c<<2 ) = " + ( c << 2 )); System.out.println("c ( c>>2 ) = " + ( c >> 2 )); }}Output: a (a += b) = 300 a (a -= 100) = 200 a (a *= c) = 800 a (a /= 100) = 8 a (a %= 100) = 8 c ( c<<2 ) = 16 c ( c>>2 ) = 1
3. Relational operators
operator |
description |
== |
Checks if the values of the two operands are equal, and if they are equal, the condition is true. |
! = |
Checks if the values of the two operands are equal, the condition is true if the values are not equal. |
; |
checks if the value of the left operand is greater than the right-operand value, if so the condition is true. |
< |
checks if the value of the left operand is less than the right-operand value, if so the condition is true. |
>= |
checks if the value of the left operand is greater than or equal to the value of the right operand, if so the condition is true. |
<= |
checks whether the value of the left operand is less than or equal to the value of the right operand, if so the condition is true. |
public class RelationalTest { public static void main(String[] args) { int a = 100; int b = 88; System.out.println("(a == b) = " + (a == b )); System.out.println("(a != b) = " + (a != b )); System.out.println(); System.out.println("(a <= b) = " + (a <= b )); System.out.println("(a >= b) = " + (a >= b )); System.out.println(); System.out.println("(a > b) = " + (a > b )); System.out.println("(a < b) = " + (a < b )); }}Output: (a == b) = false (a != b) = true (a <= b) = false (a >= b) = true (a > b) = true (a < b) = false
4. Logical operators
operator |
description |
&& |
is called a logical AND operator. The condition is true when and only if two operands are true. |
| | | The
is called a logical OR operator. If any one of the two operands is true, the condition is true. |
! |
is called a logical non-operator. The logical state used to reverse the operand. If the condition is true, the logical non-operator will get false. |
public class LogicalTest { public static void main(String[] args) { boolean a = true; boolean b = false; System.out.println("(a && b) = " + (a && b)); System.out.println("(a || b) = " + (a || b)); System.out.println("!(a && b) = " + !(a && b)); //短路逻辑运算符&&:先判断前一个条件,true则执行第二个判断操作,为false则不在执行 int c = 100; System.out.println((c > 100) && (c++ < 150)); System.out.println("c = " + c); System.out.println(); System.out.println((c++ < 150) && (c > 100)); System.out.println("c = " + c); }}Output: (a && b) = false (a || b) = true !(a && b) = true false c = 100 true c = 101
5. Conditional operators
Operator has 3 operands, you need to determine the value of a Boolean expression. The main decision of the operator is to decide which value should be assigned to the variable.
Eg:a = (b = = 1)? 20:30;
This statement: first to determine whether B is 1, true to set A to 20, false to set B to 30;
public class ConditionalTest { public static void main(String[] args) { int a; int b = 100; a = (b == 100) ? 66 : 88; System.out.println("a =" + a); System.out.println(); b = (a == 66) ? 888 : 88; System.out.println("b =" + b); }}Output:a =66b =888
6. Case studies
Determines whether the year entered is a leap years;
Leapyear: A year that can be divisible by 400 and divisible by 4 but not divisible by 100
public class LeapYearDemo { //LeapYear:能被400整除的年份和能被4整除但是不能被100整除的年份 public static void main(String[] args) { System.out.println("请输入要判断的年份: "); Scanner sc = new Scanner(System.in); int year = sc.nextInt(); if ((year % 400 == 0) || (year % 4 == 0 && year %100 != 0 )) { System.out.println(year + "是闰年!"); } else { System.out.println(year + "不是闰年!"); } }}Output: 请输入要判断的年份: 1997 1997不是闰年! 请输入要判断的年份: 2020 2020是闰年!
Java BASIC Syntax---java operators