Operators
There are several operators available in Java and some of the important operators in Java are as follows:
Arithmetic Operators:
These operators are used for performing mathematical calculations such as Addition, subtraction, multiplication, division and remainder.
Arithmetic Operators |
Operation |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
Increment/Decrement Operators
Increment operator is represented as ++ and Decrement operator is represented as --. These operators work in two forms:
Prefix form: When used in Prefix form, it follows the rule Change-then-use. For example, ++x
Postfix form: When used in Postfix form, it follows the rule Use-then-Change. For example, x++
Example:
int S = 5,a = 10; S = S + ++a;
Value of S will be
16. |
int S = 5,a = 10; S = S + a++;
Value of S will be
15. |
Relational Operators
These operators are also called comparison operators. They compare the values of two variables. They are:
Relational Operators |
Description |
> |
Greater than |
< |
Less than |
== |
Equal to |
>= |
Greater than or equal to |
<= |
Less than or equal to |
!= |
Not Equal to |
Logical Operators
These operators are used to combine relational operators to make complex expressions for decision making.
Logical Operators |
Description |
&& |
AND |
|| |
OR |
! |
NOT |
Conditional Operator: Java offers a shortcut conditional operator (?:) that stores a value
depending on a condition. This condition is also known as Ternary Operator, i.e.,
it requires three operands:
expression1? expression2: expression3
int marks = 56;
String result
= marks>35? “Pass”:”Fail”;
Here, Answer will be Pass because test expression marks>35. Here, value stored within marks namely variable is 56 and it is clear that 56>35 and that is the reason due to which, the identifier result will have value “Pass” or else, result will have value “Fail”.
Practice 1:
Evaluate expression when value of X = 5
X+=X++ + ++X + --X +X
Solution:
X = 5+(5+7+6+6)
X = 5+(24)
X = 5+24
X = 29
Practice 2:
Evaluate expression when value of a = 7
a+=a++ + ++a + --a + a--
Solution:
a = 7+(7+9+8+8)
a = 7+(32)
a = 7+32
a = 39
Practice 3:
Evaluate the value of n if value of p = 5, q = 19
int n = (q-p)>(p-q)?(q-p):(p-q)
Solution:
int n = (q-p)>(p-q)?(q-p):(p-q)
int n = (19-5)>(5-19)?(19-5):(5-19)
int n = 14>-14?14:-14
int n = 14
Practice 4:
Assume value of basic = 1500, what will be the value of tax after the following statement is executed?
tax = basic>1200? 200:100;
Solution: 200
Answer is 200 as test expression is basic>1200. Here, value of basic is 1500 and it is clear that 1500>1200 due to which, the identifier tax will have value 200.
Practice 5:
Give the output of the following:
int x = 2, y = 4, z = 1;
int result = (++z)+y+(++x)+(z++);
Solution:
int result = (2)+4+(3)+(2)
int result = 2+4+3+2
int result = 6+5
int result = 11
Practice 6:
What is the value of x1 if x = 5?
x1 = ++x - x++ + --x
x1 = 6-6+6
x1 = 0+6
x1 = 6
What is the value of x1 if x = 5?
x1 = ++x - x++ + --x
x1 = 6-6+6
x1 = 0+6
x1 = 6
Practice 7:
Evaluate the following if the value of x = 7, y = 5
x+ = x++ +x+ ++y
x = x+(x++ +x+ ++y)
x = 7 + (7+8+6)
x = 7+21
x = 28
Evaluate the following if the value of x = 7, y = 5
x+ = x++ +x+ ++y
x = x+(x++ +x+ ++y)
x = 7 + (7+8+6)
x = 7+21
x = 28
Practice 8:
Give the output of the following:
int i = 10, m = 9;
String e = (m%i ==9)?"YES":"NO ";
System.out.println(e);
Answer is YES as test expression is 9%10==9. Here, values of i and m are 10 and 9 respectively and it is clear that remainder value is coming to 9 due to which, the identifier e will have value YES.
Give the output of the following:
int i = 10, m = 9;
String e = (m%i ==9)?"YES":"NO ";
System.out.println(e);
Answer is YES as test expression is 9%10==9. Here, values of i and m are 10 and 9 respectively and it is clear that remainder value is coming to 9 due to which, the identifier e will have value YES.
Practice 9:
Rewrite the following using ternary operator:
if(bill>10000)
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
Solution:
discount = (bill>10000)?(bill*10.0/100): (bill*5.0/100)
Rewrite the following using ternary operator:
if(bill>10000)
discount = bill * 10.0/100;
else
discount = bill * 5.0/100;
Solution:
discount = (bill>10000)?(bill*10.0/100): (bill*5.0/100)
Practice 10:
r = true;
else
r = false;
r = (n1>n2)?true;false;
No comments:
Post a Comment