Showing posts with label operators. Show all posts
Showing posts with label operators. Show all posts

Monday, March 23, 2009

Assignment Operator In JAVA

The assignment operator in java is the most frequently used operator. This operator is used to assign the value on its right to the variable on the left. The general format for the assignment operator is given below.

variable_name = expression;

where,
expression is an arthmetic expression built using constants, variables and binary operators.
variable_name is the name of the variable to which the expression should be assigned.

For Example

a = 3;
x = 1*3;
num - x+8;
num1 = (4*a)+(21-a)

Note
It is also possible to assgn single value to multiple variables as shown below.

i = j = k = 10;
which will assgn the value 10 to the variables i, j and k.

Binary Operators In JAVA

As the name itself indicates, binary operators are operators that operate on two operand. The following table gives lists the different binary operator available in java.

Operator

Operation

Example

+

Addition

2+3 is 5

-

Subtraction

8-2 is 6

*

Multiplication

2*5 is 10

/

Division

8/8 is 1

%

Modulo

15%2 is 1

Example Program


class binaryoperatorexample

{

public static void main(String args[])

{

int a=10,b=5,c,d,e,f,g;

c=a+b;

d=a-b;

e=a*b;

f=a/b;

g=a%b;

System.out.println("Addition:"+c);

System.out.println("Subtraction:"+d);

System.out.println("Multiplication:"+e);

System.out.println("Division:"+f);

System.out.println("Modulo:"+g);

}

}

Please make sure to include all the necessary header files before compiling the program. The output of the program will be as below.


Addition: 15

Subtraction: 5

Multiplication: 50

Division: 2

Modulo: 0