|
Operators and Assignments
|
Operators
Operands are used to indicate some operation on
operands. Depending on the number of operands,
operators can be broadly classified as
-
Unary
Operators
The unary operators operate on only one
operand. Java unary operators are +, -,
++, --, ~, ! and cast operators.
-
Binary
Operators
The binary operators operate on two operands.
Java binary operators can be further
classified as...
-
Arithmetic operators
These operators are related to arithmetic
operations. *, /, %, + and
- are the arithmetic
operators in java.
-
Relational Operators
These operators perform comparison
operation. <, >, <=
and >= does numeric
comparison. instanceof operator
does the (Object's) type
comparison.
The type of a relational expression is
always boolean.
-
Equality Operators
The == (equal to) and the
!= (not equal to) operators
are analogous to the relational
operators. These operators also perform
comparison operation. However, they have
lower precedence over relational
operators.
The type of an equality expression is
always boolean.
-
Bitwise and Logical
Operators
The bitwise operators and logical
operators include the AND operator
&, exclusive OR operator
^, and inclusive OR operator
|.
-
Shift Operators
The shift operators include left shift
<<, signed right shift
>>, and unsigned right
shift >>>; Please
note that there is no such thing as
unsigned left shift.
-
Conditional Operators .&&
(Conditional-And Operator) and ||
(Conditional-Or Operator)
Each operand must be of type boolean. The
operands may be expressions.
-
Assignment Operator
= is simple assignment
operator. *= /= %= += -= <<=
>>= >>>= &= ^=
|= are compound assignment
operators.
-
Ternary Operator
-
The conditional operator
?:
This operator has three-operand
expressions.
? appears between the first
and second expressions, and
: appears between the second
and third expressions. The first
expression must be of type boolean.
The boolean value of first expression
decides which of two other expressions
should be evaluated.
More in ebook
|
|
Operators
and operand types
-
Bitwise Operators
~ is a unary bitwise inversion
operator. It works on integral data types
(byte, short,
char, int, long). It does not work on
boolean
data type.
More in ebook
! is a unary bitwise inversion
operator. It does inversion for boolean data
type.
Bit wise binary operators & (AND),
| (OR) and ^ (XOR) works
on all integral data types.
They also work on boolean data type.
More in ebook
-
Short
circuit
&& (AND) and
|| (OR)
&& and || are also known as
Conditional-And and Conditional-OR
operations. These operators are introduced
for optimization. They work only on
boolean
operands.
In case of &&,
(expr1) && (expr2) //expr2 is never evaluated if expr1 is false.
In case of ||,
(expr1) || (expr2) //expr2 is never evaluated if expr1 is true.
More in ebook
-
Ternary operator ?:
The Conditional Operator ? : is used in
expression as ...
a = x ? b :
c;
The above expression is equivalent to
if(x)
a = b;
else
a = c;
Please note that expression b and c must result into
compatible data types to that of a. Expression x must be of
boolean type.
More in ebook
-
String
concatenation operator +.
+ operator works on string operands. If
atleast one operand expression is of type
java.lang.String, then string conversion is
performed on the other operand to produce a
string at run time. The result is a reference
to a newly created String object that is the
concatenation of the two string
operands.
For example,
System.out.println(2 + " is a company");
In above example, primitive 2 is converted to string and
then after concatenation string "2 is a company" is printed on
console.
More in ebook
-
Operator
+= (compound
addition)
Any compound assignment expression of the
form expr1 op=
expr2 is equivalent to expr1 = (Type)((expr1) op
(expr2)).
'Type' is the type of expr1.
Thus, compound operators have casting implied
in it. This type of casting is mostly the
narrowing conversion.
For example, the following code is correct
because b += 3
is same as b = (byte)
(b+3);
byte b = 2;
b += 3; // Valid
byte b1 = 2;
b1 = b1 + 3; // Invalid, will throw compile time exception.
In expression (b1 + 3), b1 is promoted from
byte to
int . So
the result of (b1 + 3) is int. Since
int value
cannot be stored into byte variable,
exception will be thrown.
More in ebook
-
Assignment Operator
Assignment operation returns value.
For example,
int a = 2;
int b = 3;
int c;
c = a = 2;
In above example c is assigned a value 2, because the
assignment (a = 2) returns 2, which is then assigned to c.
This is more important in case of
boolean
types.
For example
if(a=b) { // do something;
}
(Please note that the above expression has = and not ==
operator)
Expression (a = b) will be invalid if a and b
are of integral types. Because then
assignment a=b will return value of that
type. However, if a and b are of type
boolean
then assignment a=b will return
boolean and
therefore, if(a=b) will be
valid.
More in ebook
|
|
|
Operators & Order-of-evaluation
- Java expressions are evaluated from left
to right as per the
Operator precedence
. While evaluating, the operators with higher
precedence are evaluated before the
operators of lower precedence.
More in ebook
|
|
equals() method and identity
test(==).
More in ebook
|
|