Home | | Sitemap ||Page number :6

OPERATORS

We widely use several operators in our daily language while working with values in Mathematics and even our day-today life.

Definition: Operators are certain symbols recognized by the compiler which when used with variables or constants (popularly also known as operands) do a pre-defined action.

Types of Operators are---

  1. Arithmetical Operators
  2. Increment Operators
  3. Bitwise Operators
  4. Logical Operators
  5. Relational Operators
  6. Ternary or Conditional Operator

Arithmetic Operators

The four operators ‘+’, ‘-’, ‘*’ and ‘/’ are known as arithmetic operators. They are used to perform simple arithmetic calculation in programming as in mathematics. Certain operators require only one operand (Example- ‘+, - ’) and are hence known as Unary operator. Other operators that are used with two operands are known as Binary Operator (‘*, / , +, -’)

Here is an example

class arith_opr { public static void main(String args[]) { float no1,no2; float sum,product,difference; float quotient;

no1=17; no2=02; sum=no1+no2; product=no1*no2; quotient=no1/no2; difference=no1-no2;

System.out.println("Two Numbers are "+no1+" "+no2); System.out.println("Sum is " +sum); System.out.println("Product is "+product); System.out.println("Quotient is "+quotient); System.out.println("Difference is "+difference); } }

fr

Increment and Decrement Operator

Like any other language such as C++ or C, Java too provides increment and decrement operator. The purpose of this operator is quite simple i.e. is to increment and decrement the value of operand by one.

Increment and Decrement operators can be in two forms.

  1. Prefix
  2. Postfix

Example ----

class inc_dcr { public static void main(String args[]) { int no=3; int no2=5;

no++; //Incrementing value by 1 ++no; //Incrementing value by 1

no2--; --no2;

System.out.println("Number 1-" +no); System.out.println("Number 2-" +no2); } }

f

Now we will study the differences between postfix and prefix in Java. This is the most important and really tricky part of the concept. Understanding this concept is really important. You should pay complete attention now.

The difference between prefix and postfix increment and decrement operator is seen only when we use them in an expression. Have a look at this program.

class pre_post { public static void main(String args[]) { int a,n,s; a=5; n=10;

s=++a*n; System.out.println("After first calculation s= "+s);

a=5; n=10; s=0; s=a++*n; System.out.println("After final calculation s= "+s); } }

j

There is a significant amount of difference between the two values of s. After observing this example you might have derived a conclusion. Let me explain in further…

Prefix Increment or Decrement Operator – The prefix Increment or Decrement operator works on the principle “First change then use.” The value of operand is first increased or decreased and then the value thus obtained is used in expression for calculation.

Postfix Increment or Decrement Operator – The postfix Increment or Decrement operator works on the principle “First use then change”. The value of operand is directly used in the expression and the value is changed later.

Some examples of this Operator ---

class ex_inc_dcr { public static void main(String args[]) { int a,b,c,d;

a=5; b=6; c=11; d=++a*b---c;

System.out.println(+d);

a=5; b=6; c=11; d=0; d=a++*b-c--;

System.out.println(+d); } }

As you are now familiar with the theory behind the difference in increment and decrement operator, Let us see the working –

(1).d=++a*b---c; a=5; b=6; c=11;

++a = 6; b--= 5(value 6 will be used in expression) c = 11;

After final calculation-

6*6-11= 36-11=25

(2). a=5; b=6; c=11; d=0;

d=a++*b-c--;

a++ =6( Value used in expression will be 5) b =6 c-- =10(value used in expression will be 11)

5*6-11 30-11=19

hh