![]() |
![]() |
Java if statement | |
Java if-else statement | |
Java nested if-else | |
Java else-if ladder | |
Java Switch case statement | |
Java Nested Switch Case | |
Java Ternary/Conditional operator |
Java while loop | |
Java do-while loop | |
Java for loop | |
Java Enhanced loop/for each loop | |
Java Labelled for loop | |
Java Nesting of loops | |
Java Break and Continue |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions | |
An operator is a symbol
that indicated operations to be performed on variables. Operators are used in programs to manipulate data and variables.
Operators are classified into following categories
Arithmetic operators are used to perform on Mathematical
expressions and the computations.
Following list shows the different Arithmetic operators
Operator | Meaning of operator |
---|---|
+ | Addition or unary plus |
- | Subtraction or unary minus |
* | Multiplication |
/ | Division |
% | Modulus or remainder |
We are getting values from user throw commandline argument
when an expression contains both integer values then expression is called as Integer expression
.
Ex- 12 + 30
when both the operands in the expression are real, then the expression is called Real arithmetic
.
Ex- 12.3 + 66.32
when both the operands are of the different types then expression is called as Mixed mode arithmetic
.
Ex- 3.14 + 55
The logical operators are the operators which are used to form multiple
conditions by combining two or more relations.
Logical expression gives true
or false
value, based on expression.
Logical AND is used when we want to check for both conditions to be same. It returns true when both the conditions are same otherwise returns false.
Logical OR is used when we want to check for only one condition to be true. It returns true when anyone condition becomes true .
It negates the result of expression from non zero to zero. It makes the result as true.
Operator | Meaning of operator | Example | Result |
---|---|---|---|
&& | Logical AND | ((10 < 20) && (10 > 50)) | false |
|| | Logical OR | ((30 < 20) || (30 > 5)) | true |
! | Logical NOT | (! (50 > 100)) | true |
Truth table of Logical Operators (true = T , false = F)
Operand 1 | Operand 2 | && | || |
---|---|---|---|
T | T | T | T |
T | F | F | T |
F | T | F | T |
F | F | F | F |
Comparing the above numbers by logical operators using if-else statement.
class ArithOperations { public static void main(String args[]) { int first, second; first = Integer.parseInt(arg[0]); second = Integer.parseInt(arg[1]); System.out.println("Addition : "+ (first + second)); System.out.println("Subtraction : "+ (first - second)); System.out.println("Multiplication : "+ (first * second)); System.out.println("Division : "+ (first / second)); } }
When the comparison between two quantities is performed depending on their relation
, certain decisions are made.
Ex- If we compare salaries of two persons for finding who have more salary then relational operators are used.
The value of relational expression is either true
or false
. Java supports 6 relational operators as below
Operator | Operator Meaning | Example | Result |
---|---|---|---|
< | Less than | 20 < 10 | true |
> | Greater than | 20 > 10 | true |
== | Equals to | 33 == 44 | false |
<= | Less than or equals to | 50 <= 90 | true |
=> | Greater than or equals to | 68 => 90 | false |
!= | Not equals to | 50 != 90 | true |
Assignment operators are used to assign the value of an expression to a variable. Assignment operator ‘ = ‘ is used in the program for assignment.
NOTE : = and = = are different operators, we are using single = for assignment. |
variable = value;
Assume B = 5
Simple Assignment | Shorthand Assignment | Result |
---|---|---|
B = B + 1 | B += 1 | 6 |
B = B - 1 | B -= 1 | 5 |
B = B * (1+1) | B += (1+1) | 7 |
B = B / (1+1) | B /= (1+1) | 5 % 2 |
B = B % 5 | B %= 5 | 5 % 5 |
The increment (double ++
) adds one or increments
the value by 1
. And the decrement (double --
) subtracts or decrements
the value by 1
Increment and Decrement has following forms
Operator | Meaning | What it does? |
---|---|---|
C++ | Post increment | First prints the value then increments C by 1 |
++C | Pre increment | First increments the value by 1 then prints C |
C-- | Post increment | First Prints the value then decrements C by 1 |
--C | Pre increment | First decrements the C by 1 then prints value |
The conditional operator (?
) is a ternary operator in java, which is used to construct conditional expression.
We will see in more detail in Decision making chapter.