Guyz in this post we are going to discuss Java Operator. So let's see what are all the types of java operator is there. Operators are symbols, which perform an operation on various data items known as operands. For example, in a+b, a and b are operands and + is an operator. Note that to perform the operation, operators and operands are combined together forming an expression. For example, to perform an addition operation on operands a and b, the addition (+) operator is combined with the operands a and b forming an expression.
Depending on the function performed, the java operators can be classified into various categories. These include:
1.Arithmetic operators
2.Relational operators
3.logical operators
4.Assignment operators
5.Increment and decrement operators
6.Bitwise operators
7.conditional operators
8.Special operators
Arithmetic operators:
Arithmetic operator are used to performing arithmetical calculation.they can work on any built-in data type of Java except on boolean type.Java provides various arithmetic operators that are,+(addition or unary plus),-(subtraction or unary minus),*(multiplication),/(division) and %(modulus).
The different arithmetic operators that were that were used in java programming are:
S.NO
|
OPERATOR
|
EXAMPLE
|
MEANING
|
1
|
+
|
a+b
|
addition or unary plus
|
2
|
-
|
a-b
|
subtraction or unary minus
|
3
|
*
|
a*b
|
multiplication
|
4
|
/
|
a/b
|
division
|
5
|
%
|
a%b
|
modulo division
|
Sample pogram:
import java.io.*;
class arithdemo
{
public static void main(string args[])
{
int a=10,b=5;
system.out.pritnln("The addition value is:"+(a+b));
system.out.println("The sudtract value is:"+(a-b));
system.out.println("The multiple value is:"+(a*b));
system.out.println("The division value is:"+(a/b));
system.out.println("The modulo value is:"+(a%b));
}
}
The progam displays the following output:
The adition value is :15
Thesubstraction value is:5
the multiplication value is:50
the division value is:2
the modulo value is:0
Relational operators:
Relational operator is used to finding out the relationship between the two operands.the various relational operator provided by java are less than'<',less than or equal to '<=',greater then'>',greater than or equal to'>=", equal to'=' and not equal to'!=' operator.The return value of the boolean type, that is, either true or false. for example, consider two value a and b having values 20 and 30 respectively.in this case, the expression a<b returns true whereas the expression a>b return false.
operator operation example
< less than a<b
> greater than a>b
<= less than equal to a<=b
>= greater than equal to a>=b
= equal to a=b
!= not equal to a!=b
Note: The operator = and != are also known as equality operator as they are used for checking the equality of the operands.
sample program:
import java.io.*;
class relationaldemo
{
public staic void main(String args[])
{
int a=10,b=5;
system.out.println("The a<b value is:"+(a<b));
system.out.println("The a>b value is:"+(a>b));
system.out.println("The a<=b value is:"+(a<=b));
system.out.println("The a>=b value is:"+(a>=b));
system.out.println("The a==b value is:"+(a==b));
system.out.println("The a!=b value is:"+(a!=b));
}
}
Logical operators:
A logical operator is used to finding out the relationship between two or more relationship expression. The logical operator used in java programming is:Operator meaning Description
&& logical AND it returns true only if all the expression evaluate to true, otherwise it returns false.
|| logical OR It returns true if any one or all the expression evaluate to true and return false only if all the expression evaluates to false.
! logical NOT It returns true if the expression on which it is operating is false and vice versa.
The logical operator && and || are used when we want to form compound conditions by combining two or more relations.
The logical operator return results indicate in the following table:
X Y X&&Y X||Y
T T T T
T F F T
F T F T
F T F F
Some example of usage of logical expression are:
if(salary>5000 && da>450)
if(salary>5000 || da>450)
if(age>55 && salary<1000)
Sample program:
import java.io.*;
import java.util.*;
import java.lang.*;
class logical{
public static void main(String args[])
{
int x,y,z;
Scanner s= new scanner(system.in);
x=s.nextInt();
y=s.nextInt();
z=s.nextInt();
system.out.println("x value:"+x);
system.out.println("y value:"+y)
system.out.println("z value:"+z)
system.out.println("x>y and x>z:"+((x>y)&&(x>z)));
system.out.println("x not equal to z:"+(x!=z));
}
}
output of this program is:
x value:5
y value:4
z value:3
x>y and x>z:true
x not equal to z:true
Assignment operators:
Assignment operators are used to assigning the value of an expression to a variable. The general form is
v op=exp;
Where,
v is variable, op is the binary operator and exp is an expression. This form is equivalent to the statement
v=v op(exp);
For Example,The expression x=x+6 can be written as x+=6.In this expression,x is incremented by 6 and then the result is assign to x.The various compound assignment operator used in java are '+=' , '-=' ,'*=' , '/=' and '%=' .
The table is given below lists the assignment operator with example operator description.
Operator meaning Expression
+= add assign x+=a , x=x+a
-= sub assign x-=a , x=x-a
*= multiple assign x*=a , x=x*a
/= division x/=a , x=x/a
%= modulo x%=a , x=x%a
Sample program
import java .io.*;
class assignmentdemo
{
public static void main(String args[])
{
int a=2,b=3,c=6;
system.out.println("the assignment value is");
a+=2;
b-=2;
c*=2;
system.out.println("The value of a:"+a);
system.out.println("The value of b:"+b);
system.out.println("the value of c:"+c);
}
}
The output of this program is
The value of a:4
The value of b:1
The value of c:12
Increment and decrement operators:
Java has two very useful operators.They are increment (++)and decrement(--) operator.The increment operator(++) add 1 to tge opersatpr value contaoined in the variable.The decrement operator(--) substract from the value contained in the variable.Increment variable has two type:1.pre increment(++ varaible) 2.post increment(variable++).Decrement has two types: 1.pre decrement(--variable) 2.post decrement(variable--).Expression process Example
a++ add 1 to a variable after use int a=10,b;
b=a++;
then a=11,b=10
++a add 1 to a variable before use int a=10,b;
b=++a;
then a=11,b=11
a-- substract 1 from a variable after use int a=10,b;
b=a--;
then a=9,b=10
--a substract 1 from a variable before use int a=10,b;
b=--a;
then a=9,b=9
Sample program:
import java.io.*;
class inc&decdemo
{
public static void main(String args[])
{
int x=1;
int y=3;
int u;
int z;
u=++y;
z=x--;
system.out.println(x);
system.out.println(y);
system.out.println(u);
system.out.println(z);
}
}
The output for this program
1
3
4
1
Bitwise operators:
Bitwise operators are used to performing bit by bit operation.This operator can be applied to all the primitive data type such as long, int, short, char and byte.Various bitwise operators are bitwise AND(&), Bitwise OR(!),Bitwise exclusive OR(^),one's complement(~),shift left(<<),shift right(>>),shift right with zero fill(>>>).Bitwise AND(&):
The bitwise operations are carried out between two-bit patterns.Example,
let x=0101 y=1011 x&y=0001
Bitwise OR(|):
The bitwise operation is carried out between bit patterns.Example,
let x=0100 y=1011 x|y=1111
Bitwise Exclusive OR(^):
let x=0010 y=1010 x^y=1000
Conditional operator:
The conditional operator is also called as ternary operator.it is used to construct conditional expressions. Conditional operator has three operands. The general format is:
expression1? expression2: expression3
Expression1 is evaluated first. If the expression1 is true expression2 is evaluated if the expression is false,expression3 is evaluated.
Example:
(x==5)?8:9;
In this example, if the value of x is equal to 5, then the expression returns 8, otherwise, the expression returns 9.
Sample program:
import java.io.*;
class conditionaldemo
{
public static void main(String args[])
{
int a=2,b=1,c=(a>b)?a:b;
if(c==a)
system.out.println("A is big");
else if(c==b)
system.out.println("b is big");
}
}
Special operator:
Java supports two types of special operators.
1.instanceof operators
2.Member selection(.) operator
instanceof operator:
instanceof operator is used to finding out whether the object belongs to a particular class or not.'instanceof' operator also important in case of casting object at runtime.instanceof operator returns a boolean value, if an object reference is of the specified type then it returns true otherwise false. The general format is:
objectname instanceof classname
emp instanceof employee
In the above example, it is true if the object person belongs to the class employee, otherwise, it is false.
sample Program:
import java.io.*;
public class test
{
public static void main(String args[])
{
test t=new test();
system.out.println( t instanceof test);
}
}
The output is true.
Member selection(.) operator:
Member selection operator is otherwise called as dot operator. It is used to access the variable and the method of the class object. Dot operator links the name of the object with the name of the variable or method which needs to be accessed.
Example
Employee.empid; //accessing an instance variable of the class.
employee.net_amount; //accessing method of the class.
No comments:
Post a Comment