Concept
Arithmetic
Operators
Arithmetic operators are used in
the mathematical expression. Following
are some arithmetic operators:
Operators |
Result |
Operators |
Result |
+ |
Addition |
- |
Subtraction |
* |
Multiplication |
/ |
Division |
% |
Modulus |
++ |
Increment |
+= |
Addition
Assignment |
-= |
Sub
Assignment |
*= |
Mul
Assignment |
/= |
Div
Assignment |
%= |
Mod
Assignment |
-- |
Decrement
|
The
Basic Arithmetic Operators
The basic arithmetic operators are
Addition, Subtraction, Multiplication,
and Division. All behave the same,
as you would expect for all numeric
types.
Example:
Class
MyMath {
public static void main (String
args[]) {
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;
System.out.println("a is:
" + a);
System.out.println("b is:
" + b);
System.out.println("c is:
" + c);
System.out.println("d is:
" + d);
System.out.println("e is:
" + e);
}
}
The
Modulus Operator
The modulus operator (%) returns
the remainder of a division operation.
It can be applied to floating point
type as well as integer types.
Examples:
Class MyModulus {
public static void main (String
args[]){
int x = 42;
double y = 42.3;
System.out.println("x
mode 10 = " + x % 10);
System.out.println("y mode
10 = " + y % 10);
}
}
Arithmetic
Assignment Operators
These operators combine an arithmetic
operation with an assignment. Statements
like the following are quite common
programming.
a
= a + 4
This
may also be written as:
a
+= 4;
Example:
class OpEquals {
public static void main (String
args[]){
int a = 2;
int b = 3;
int c = 4;
a += 5;
b *= 5;
c += a + b;
c %= 6;
System.out.println("a is =
" +a);
System.out.println("b is =
" +b);
System.out.println("c is =
" +c);
}}
Increment
and Decrement
The ++ and -- are java's increment
and decrement operators. The increment
operator increases its operand by
one; the decrement operator decreases
its operand by one;
x
= x + 1;
It
can also be written as by use of
increment operator.
x++;
Similarly
this statement:
x
= x - 1;
is
equivalent to: x--;
Example
class
IncDec {
public static void main(String args[]){
int a = 2;
int b = 3;
int c;
int d;
c = ++b;
d = a++;
System.out.println("a is = " + a);
System.out.println("b is = " + b);
System.out.println("c is = " + c);
System.out.println("d is = " + d);
}}
Relational
Operator
The
relational operator determine the
relationship that one operand has
to the other. Specially they determine
equality and ordering.
The
outcome of the operators is a Boolean
value. The relational operators
are normally used in the expression
that control the if statement and
the various loops statements.
int
a = 4;
int b = 5;
boolean c = a<b;
In
this case the result of a < b
(which is false) is stored in the
variable c.
or
if (done == 0)
if (done != 0)
Boolean
/ Logical Operators
The Boolean logical operators operate
only on Boolean operands. All of
the binary logical operators combine
two Boolean values to form a resultant
Boolean.
Operator |
Result |
Operator |
Result |
& |
Logical
AND |
| |
Logical
OR |
^ |
Logical
XOR |
|| |
Short
circuit OR |
&& |
Short
circuit AND |
! |
Logical
unary NOT |
&= |
AND
Assignment |
|= |
OR
Assignment |
^= |
XOR
Assignment |
== |
Equals
to |
!= |
Not
equals to |
|
|
Example:
Class
Boolean{
public static void main (String
args[]){
boolean a = true;
boolean b = false;
boolean c = a | b;
boolean d = a & b;
boolean e = a ^ b;
boolean f = (!a & b) | (a &
|b);
boolean g = !a;
System.out.println("a is: "
+ a);
System.out.println("b is: "
+ b);
System.out.println("a ! b is:
" + c);
System.out.println("a &
b is: " + d);
System.out.println("a ^ b is:
" + e);
System.out.println("(!a &
b) | (a |b) is: " + f);
System.out.println("!a is:
" + g);
}}
The
Assignment Operator
The assignment operator is the single
equal sign, =. The Assignment has
its general form as follows:
var
= expression;
int x, y, z;
x = y = z = 10;
// set x, y and z to 10;
Type
Casting
Type casting is a phenomenon in
which the value of one data type
is assigned to the variable of other
data type (implicitly) provided
these two types are compatible.
By compatible we mean that it is
possible to convert an int value
to long variable but it is not possible
to implicitly convert a long value
to byte type. However we have to
explicitly use cast to make the
later case possible. Lets see at
both these types.
Implicit
Type Casting
When one type of data is assigned
to the other type of variable, an
automatic type conversion occurs
with the following two conditions:
The
two types are compatible.
The
destination type is larger than
the source type.
When
these two conditions are met, an
automatic type conversion takes
place. For automatic conversion,
the numeric types (including int
and float) are compatible with each
other. However these are not compatible
with char or Boolean.
Example
class
impConversion{
public static void main (String
args[]){
byte b = 42;
char c = 'a';
short s = 1024;
int i = 5000;
float f = 5.67f;
double d = 0.12345;
double result = (f * b) + (i / c)
- (d * s);
System.out.println((f * b) + "
+ " + (i / c) + " - "
+ (d * s));
System.out.println ("The
Result is = " + result);
}}
Explicit
Type casting
When you want to assign a larger
type value to a smaller value, you
cannot do it without explicitly
casting it using cast operator.
For example you want to assign an
int value to a byte variable. This
conversion sometimes called narrowing
conversion, since you are explicitly
making the value narrower to fit
into the small size. To undergo
such a conversion we use cast.
target
variable = (target type) value
Note
that: some information may be
lost during that process. For example,
when floating point value is assigned
to an integer the fractional component
is lost.
Example
class
Expconversion{
public static void main (String
args[]){
byte b;
int i = 257;
double d = 323.567;
System.out.println ("Conversion
of int to byte");
b = (byte) i;
System.out.println ("i and
b " + i + " " + b);
System.out.println ("Conversion
of double to int");
i = (int) d;
System.out.println ("d and
i " + d + " " + i);
System.out.println ("Conversion
of double to byte");
b = (byte) d;
System.out.println ("d and
b " + d + " " + b);
}}