|
Operator symbol | Example | Meaning |
---|---|---|
< | a < b | Returns true if a < b, otherwise returns false |
<= | a <= b | Returns true if a ≤ b, otherwise returns false |
> | a > b | Returns true if a > b, otherwise returns false |
>= | a >= b | Returns true if a ≥ b, otherwise returns false |
== | a == b | Returns true if a is equal to b, otherwise returns false |
!= | a != b | Returns true if a is not equal to b, otherwise returns false |
|
|
import java.util.Scanner; public class Divisible { public static void main(String[] args) { int a, b; Scanner in = new Scanner(System.in); // Construct Scanner object System.out.print("Enter a: "); a = in.nextInt(); // Read in number into a System.out.print("Enter b: "); b = in.nextInt(); // Read in number into b if ( (a % b) == 0 ) System.out.println(a + " is divisible by " + b); } } |
Explanation:
|
How to run the program:
|
|
|
Priority level | Operator(s) | Description | Associativity |
---|---|---|---|
1 | ( ) | Brackets | |
2 | (int) − | Casting, negation | Right to left |
3 | ++, -- | Increment, decrement | |
4 | * / % | Multiple, divide, remainder | Left to right |
5 | + - | Add, subtract | Left to right |
6 |
< <= > >=
== != |
Compare operators | |
7 | = += -= ... | Assignment operators | Right to left |
Reference: click here
boolean a; Statement: a = 3 > 1; Operators in statement: = > Executed as follows: a = 3 > 1; // > has higher priority than = a = true; |
boolean a; Statement: a = 3 + 4 <= 5 - 2; Operators in statement: = + <= - Executed as follows: a = 3 + 4 <= 5 - 2; // + and - has highest priority a = 7 <= 3; // <= has higher priority than = a = false; |
Example:
if ( a = 0 ) { ... } else { ... } |
The = symbol represents the assignment operator
You must use == to compare equality (no spaces between the == characters)