CSE102 - Week3
CSE102 - Week3
CSE102 - Week3
PRINCIPLES OF PROGRAMMING
Lecture 3 - Operators
x = x + 1;
is the same as
x++;
And
x = x - 1;
is the same as
--x;
Both the increment and decrement
operators can either precede (prefix) or
follow (postfix) the operand.
For example,
x = x + 1;
can be written as
++x; // prefix form
or as
x++; // postfix form
In the foregoing example, there is no
difference whether the increment is applied
as a prefix or a postfix.
together here.
Operato Meaning
r
== Equal to
=! Not equal to
> Greater
& AND
| OR
XOR Exclusive OR
|| Short-circuit OR
! NOT
var op = expression;
+= -= *= /=
%= &= |= ^=
two benefits.
First, they are more compact than their
“longhand” equivalents.
Second, they are implemented more efficiently
type.
When these two conditions are met, a
program is invalid.
// *** This program will not compile. ***
class LtoD {
public static void main(String args[]) {
long L;
double D;
D = 100123285.0;
L = D; // Illegal!!!
System.out.println("L and D: " + L + " " + D);
}
}
There are no automatic conversions from the
numeric types to char or boolean. Also, char
and boolean are not compatible with each
other. However, an integer literal can be
assigned to char.
Casting Incompatible Types
Although the automatic type conversions are
helpful, they will not fulfill all programming
needs because they apply only to widening
conversions between compatible types.
For all other cases you must employ a cast.
(target-type) expression
Here, target-type specifies the desired type to
convert the specified expression to.
For example, if you want to convert the type of