java basic

Last update:2018-05-08

This is the second section of part 1.

Operator is a kind of signs which has certain function,in order to notify Java to do certain operations.

There are several common operators down below that you can see in Java:

Arithmetic operators

Arithmetic operators mainly are use to do basic arithmetic operations,such as addition,subtraction, mutiplication and division etc.

Common arithmetic operators in Java:

Arithmetic operators Definition Example
+ Addition 5 + 12 = 17
- Subtraction 25 - 10 = 15
* Multiplication 3 * 8 = 24
/ Division 24 / 6 = 4
% Modulus 24 % 7 = 3
++ Increment int i = 5;i++
decrement int i = 5;i–

Notice:

  1. There are some differences between modulo operation and remainder operation,% means remainder in C/C++ and Java,but modulo in Python.
  2. ++/– can be on the left side or the right side of the number,but the output is different

Ex.1

1
2
3
4
5
//Left side
int a = 5;
int b = ++a;//Variable a wil execute increment first,and then assign the value to variable b
System.out.println("a:" + a);
System.out println("b:" + b);

Output

1
2
a:6
b:6

Ex.2

1
2
3
4
5
//Right side
int a = 5;
int b = ++a;//Variable a's value will be assign to variable b fisrt,and then execute increment
System.out.println("a:" + a);
System.out println("b:" + b);

Output

1
2
a:6
b:5

Both increment operator and decrement operator can only use to operate variables,you can’t directly on values or constants.

Ex

1
2
5++;//Wrong
8--;//Wrong

Assignment operators

Assignment operators is a kind of notation that use to assign value for variables and constants

Common assignment operators in Java:

Operator Definition Example
= Assignment c = a + b,assign the sum of a & b to c
+= Addition a += b,assign the sum of a & b to a
-= Subtraction a -= b,assign the margin of a - b to a
*= Multiplication a *= b,assign the product of a & b to a
/= Division a /= b,assign the quotient of a & b to a
%= Modulus a %= b,assign the remainder of a divide by b to a

Comparison operators

Comparsion operators is use to compare two values,and output a boolean value(true or false)

Common comparsion operators in Java:

Operator Definition Example Output
> Greater than a = 5;b = 2;a > b true
< Less than a = 5;b = 2;a < b false
>= Greater or equal a = 5;a >= 3 true
<= Less or equal a = 5;b = 2;b <= a true
== Equal to a = 5;b = 2;a == b false
!= Not equal to b = 2;b != 2 true

Notice:

  1. , < , >= , <= can only support numeric type on both sides of the operators

  2. == , != can support numeric type and reference type on both sides

Logical operators

It’s mainly use for logical operations.

Common logical operators in Java:

Logical operator Definition Example Output
&& Logical and a && b if a and b both are true,then return true
|| Logical or a || b if one of a and b is true,then return true
! Logical not !a if a is false,then return true
^ Exclusive or a ^ b if a and b only have one of them is true,then return true

Conditional operator

Conditional operator is also called Ternary Operator

  • syntax: Boolean expression ? expression 1 :expression 2

If the value of boolean expression is true,then return the value of expression 1;If it’s false,then return the value of expression 2.

Ex

1
2
3
4
int result = (8 > 5) ? 1 : 0;
System.out.println(result);
//output
//1

The priority level of operators

Priority Operators
1 ()
2 var++ , var–
3 +(positive) , -(negative) , ++var , –var
4 (type)(type convert)
5 !(not)
6 * , / , %
7 +(plus) , -(minus)
8 < , <= , > , >=
9 == , !=
10 ^
11 &&
12 ||
13 ?:
14 = , += , -= , *= , /= , %=

Level 1 has the highest priority and level 11 has the lowest.

In real life,we usually use brackets to assist priority management.