Object Oriented Programming with JAVA BCS306A
MODULE-I
Chapter 3 Operators
➢ Java provides a rich operator environment.
➢ Most of its operators can be divided into the following four groups: arithmetic, bitwise,
relational, and logical.
3.1 Arithmetic Operators
➢ Arithmetic operators are used in mathematical expressions in the same way that they are used
in algebra.
➢ The following table lists the arithmetic operators:
The Basic Arithmetic Operators
➢ The basic arithmetic operations—addition, subtraction, multiplication, and division— all
behave as you would expect for all numeric types.
➢ The minus operator also has a unary form that negates its single operand.
➢ Remember that when the division operator is applied to an integer type, there will be no
fractional component attached to the result. The following simple example program demonstrates
the arithmetic operators.
class BasicMath {
public static void main(String args[]) {
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3; Integer Arithmetic
a=2
int c = b / 4;
b=6
int d = c - a; c=1
int e = -d; d = -1
System.out.println("a = " + a); e=1
System.out.println("b = " + b); Floating Point Arithmetic
System.out.println("c = " + c); da = 2.0
System.out.println("d = " + d); db = 6.0
dc = 1.5
System.out.println("e = " + e);
dd = -0.5
de = 0.5
1
, Object Oriented Programming with JAVA BCS306A
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
The Modulus Operator(%)
➢ The Modulus Operator returns the remainder of a division operation.
➢ It can be applied to floating-point types as well as integer types.
class Modulus {
public static void main(String args[]) {
int x = 42; Output:
x mod 10 = 2
double y = 42.25;
y mod 10 = 2.25
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
Arithmetic Compound Assignment Operators
➢ Java provides special operators that can be used for all of the arithmetic, binary operators. Thus,
any statement of the form
var = var op expression;
➢ can be rewritten as
var op= expression;
➢ The compound assignment operators provide two benefits. First, they save you a bit of typing,
because they are “shorthand” for their equivalent long forms. Second, they are implemented
more efficiently by the Java run-time system than are their equivalent long forms.
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3; Output:
a=6
a += 5;
b=8
b *= 4; c=3
c += a * b;
c %= 6;
2
MODULE-I
Chapter 3 Operators
➢ Java provides a rich operator environment.
➢ Most of its operators can be divided into the following four groups: arithmetic, bitwise,
relational, and logical.
3.1 Arithmetic Operators
➢ Arithmetic operators are used in mathematical expressions in the same way that they are used
in algebra.
➢ The following table lists the arithmetic operators:
The Basic Arithmetic Operators
➢ The basic arithmetic operations—addition, subtraction, multiplication, and division— all
behave as you would expect for all numeric types.
➢ The minus operator also has a unary form that negates its single operand.
➢ Remember that when the division operator is applied to an integer type, there will be no
fractional component attached to the result. The following simple example program demonstrates
the arithmetic operators.
class BasicMath {
public static void main(String args[]) {
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3; Integer Arithmetic
a=2
int c = b / 4;
b=6
int d = c - a; c=1
int e = -d; d = -1
System.out.println("a = " + a); e=1
System.out.println("b = " + b); Floating Point Arithmetic
System.out.println("c = " + c); da = 2.0
System.out.println("d = " + d); db = 6.0
dc = 1.5
System.out.println("e = " + e);
dd = -0.5
de = 0.5
1
, Object Oriented Programming with JAVA BCS306A
// arithmetic using doubles
System.out.println("\nFloating Point Arithmetic");
double da = 1 + 1;
double db = da * 3;
double dc = db / 4;
double dd = dc - a;
double de = -dd;
System.out.println("da = " + da);
System.out.println("db = " + db);
System.out.println("dc = " + dc);
System.out.println("dd = " + dd);
System.out.println("de = " + de);
}
}
The Modulus Operator(%)
➢ The Modulus Operator returns the remainder of a division operation.
➢ It can be applied to floating-point types as well as integer types.
class Modulus {
public static void main(String args[]) {
int x = 42; Output:
x mod 10 = 2
double y = 42.25;
y mod 10 = 2.25
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);
}
}
Arithmetic Compound Assignment Operators
➢ Java provides special operators that can be used for all of the arithmetic, binary operators. Thus,
any statement of the form
var = var op expression;
➢ can be rewritten as
var op= expression;
➢ The compound assignment operators provide two benefits. First, they save you a bit of typing,
because they are “shorthand” for their equivalent long forms. Second, they are implemented
more efficiently by the Java run-time system than are their equivalent long forms.
class OpEquals {
public static void main(String args[]) {
int a = 1;
int b = 2;
int c = 3; Output:
a=6
a += 5;
b=8
b *= 4; c=3
c += a * b;
c %= 6;
2