ClassXI OPERATORS&EXPRESSIONS (Ch06)
ClassXI OPERATORS&EXPRESSIONS (Ch06)
Operators
Operators
Postfix: first use 48 then change to 49. Prefix: first change to 50 then use 50.
3 Ternary Operator: The Java Ternary operators require three operands. In Java, it is used as
conditional assignment operator (? :).
The general syntax of conditional operator is:
(condition) ? expression 1 : expression 2
Expression1 will assign to the variable if the condition is ‘true’ otherwise expression 2 will be
assigned.
e.g. Evaluate the value of n, if p=5 and q=19
int n =(q-p)>(p-q) ? (q-p) : (p-q) ;
sol. int n =(q-p)>(p-q) ? (q-p) : (p-q) ;
== Equal to x == y
!= Not equal x != y
3 Assignment Operator ( =): it is used to assign the value of an expression to the variable of
its left side. It has following syntax:
variable = expression
e.g. int a=10; // value 10 will assign to the variable a
int sum=a+b; // sum of a and b will assign to the variable sum
Shorthand assignment operators
a += b; equivalent to a=a+b;
a -= b; equivalent to a=a-b;
a *= b; equivalent to a=a*b;
a /= b; equivalent to a=a/b;
a %= b; equivalent to a=a%b;
4: Logical Operators: Logical operators are used to determine the logic between variables or
values:
&& Logical Returns true if both statements are true x < 5 && x < 10
and
! Logical Reverse the result, returns false if the !(x < 5 && x < 10)
not result is true
++ unary post-increment
2 -- not associative
unary post-decrement
++ unary pre-increment
-- unary pre-decrement
+ unary plus
2 - right to left
unary minus
! unary logical NOT
~ unary bitwise NOT
+ - addition,subtraction
5 + left to right
string concatenation
<< >>
6 >>> shift left to right
< <=
7 > >= relational not associative
instanceof
==
8 != equality left to right
= += -=
*= /= %=
15 &= ^= |= assignment right to left
<<= >>= >>>=
The “new” operator: The new operator is used to allocate memory (dynamically) for the
object.
e.g. Scanner sc = new Scanner(System.in);
The dot(.) operator: An object can access instance members(variables and methods) of the
class using dot(.) operator.
Syntax for dot operator is:
<object> . <instance member>;
e.g. sc.nextInt();
2 𝑙
(ii) T = √
𝑔
𝑎2 +𝑏2
(iii) f =
𝑎2 − 𝑏2
(ii) T =Math.sqrt(l/g)