Welcome Guest   Login   Nov 23, 2017
Tutorials
C
HTML
Java
Chapter 4
Operators

Java provides rich operator environment. Most of its operators can be divided into the following groups: arithmetic, relational, logical.

Arithmetic Operators

Arithmetic operators are used in mathematical expression in the same way that they are used in algebra. The following table lists the arithmetic operators:
 
Operator Result
+ Addition
- Subtraction 
Multiplication
/ Division
% Modulus
++ Increment
+= Addition assignment
-= Subtraction assignment
*= Multiplication assignment
/=  Division assignment
%= Modulus assignment
-- Decrement

The operands of the arithmetic operators must be of numeric type. You cannot use them on boolean types, but you can use them on char types, since the char type in Java is, essentially, a subset of int.
 
class example1 {
public static void main(String args[]) {
System.out.println("Integer Arithmetic");
int a = 1 + 1;
int b = a * 3;
int c = b / 4;
int d = c - a;
int e = -d;

System.out.println("a = " + a);
System.out.println("b = " + b);
System.out.println("c = " + c);
System.out.println("d = " + d);
System.out.println("e = " + e);
 
 

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);

}
}


 

when you run this program you will see the following output

Integer Arithmetic

a = 2
b = 6
c = 1
d = -1
e = 1
 
 

Floating Point Arithmetic

da = 2
db = 6
dc = 1.5
dd = -0.5
de = 0.5
 

The Modulus Operator

The modulus operator, %, return the remainder of a division operation. It can be applied to floating-point types as well as integer types. The following program demonstrates.
 
class example2 {
public static void main(String args[]) {

int x = 42;
double y = 42.3;
System.out.println("x mod 10 = " + x % 10);
System.out.println("y mod 10 = " + y % 10);

}
}

when you run this program you will get following output:

x mod 10 = 2

y mod 10 = 2.299999999999997
 
 

Arithmetic Assignment Operators

Java provides special operators that can be used to combine an arithmetic operation with an assignment. As you probably know, statements like the following are quite common in programming.

a = a + 4;

In Java, you can write this statement as shown here

a += 4;

This version uses the += assignment operator. Both statements performs the same action: they increase the value of a by 4;here are some more examples.
 

a = a % 2; can be written as a %= 2;

b = b * 3; can be written as b *= 3;

c = c / 5; can be written as c /= 5;

d = d - 7; can be written as d -= 7;
 
 

Increment and decrement

The ++ and the – are Java’s increment and decrement operators. The increment operator increases its operand by one. The decrement operator decreases its operand by one. For example, this statement
 
 

x = x + 1; can be rewritten like this by use of increment operator. x++;
 
 

Similarly, this statement x = x - 1; is equivalent to x--;
 
 

These operators are unique in that they can appear both in postfix form, where they follow the operand as just shown, and prefix form, where they precede the operand.
 
class example3 {
public static void main(String args[]) {

int a = 23;
int b = 5;
System.out.println("a & b : " + a + " " + b);
a += 30;
b *= 5;
System.out.println("after arithmetic assignment a & b: "+a+" "+b);
a++;
b--;
System.out.println("after increment & decrement a & b: "+a+" "+b);
}
}

The output of this program follows

a & b : 23 5

after arithmetic assignment a & b : 53 25

after increment & decrement a & b : 54 24
 
 

Relational Operators

The relational operator determine the relationship that one operand has to the other. Specifically, they determine equality and ordering. The relational operators are shown here.
 
Operator Result
== Equal to
!=  Not equal to
> Greater than
Less than
>= Greater than or equal to
<= Less than or equal to

 

The outcome of these operation is a boolean value. The relational operators are most frequently used in the expressions that control the if statement and the various loop statements.

Any type in Java, including integers, floating-point numbers, characters, and Boolean can be compared using the equality test, ==, and the inequality test, !=. Notice that in Java equality is denoted with two equal signs, not one. ( single equal sign is the assignment operator.)

eg:

int a = 4;

int b = 1;

boolean c = a < b;
 
 

In this case, the result of a < b (which is false) is stored in c.

Boolean Logical operators

The boolean logical operators shown here operate only on boolean operands. All of the binary logical operators combine two boolean values to form a resultant boolean value.
 
Operator Result
&& AND
|| OR
! Logical unary NOT
== Equals to
!= Not Equals to
?: Ternary if-then-else

 
 
class example4 {

public static void main(String args[]) {

boolean b;
b = (2 > 3) && (3 < 2);
System.out.println("b = "+b);
b = false || true ;
System.out.println("b = "+b);

}
}

the output of the program is shown here.

b = false

b = true
 

The ? , : Operator

Java includes a special ternary (three-way) operator that can replace certain types of if-then-else statement. This operator is the ?. The ? has this general form.

expression1 ? expression2 : expression3

Here, expression1 can be any expression that evaluates to a boolean value. If expression1 is true, then expression2 is evaluated; otherwise, expression3 is evaluated. The result of the ? operation is that of the expression evalueated. Both expression2 and expression3 are required to return the same type, which can’t be void.

Eg: int i=20;

int j=30;

int max = ( i > j ) ? x : j;

this section checks for the condition, if i is greater than j then value of i is asigned to max else value of j is assigned to max.
 
Class example5 {
public static void main(String args[]) {
int i,k;
i = 10;
k = i < 0 ? -i : i; // get absolute value of I
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);

i = -10;
k = i < 0 ? -i : i; //get absolute value of I
System.out.print("Absolute value of ");
System.out.println(i + " is " + k);
}
}

The output generated by program is shown here.

Absolute value of 10 is 10

Absolute value of -10 is 10
 

Operator Precedence
 
Highest
     
()
[]
.
 
++
--
~
!
*
/
%
 
+
-
   
>
>=
<
<=
==
!=
   
&
     
^
     
&&
     
||
     
?:
     
=
op=
   
Lowest
     

Notice that the first row shows items that you may not normally think of as operators:

Parentheses, square brackets, and the dot operators, Parentheses are used to alter the precedence of an operation.
 

Using Parentheses

Parentheses raise the precedence of the operations that are inside them. This is often necessary to obtain the result you desire. For example the following expression :

a + b * 3;

This expression first multiplies b with 3 then adds it to a. However the result can differ by putting the parentheses around a + b.
 

( a + b ) * 3;

This expression first solves brackets i.e. first adds a to b then result is multiplied to 3.

My New Blog

My Award winning Whitepaper

Contradicting Light
My Personal Blog


Trace Mobile Location



My Book Reviews
 




Tech Jokes Worth Reading
 
Top
Home
www.deepjava.com 1999-2017 (C) Developed and maintained by D-Kay Consultancy