Introduction
Java Supports a rich set of operators. An operator is a symbol that tells the computer to perform certain mathamatical or logical manipulataion. Operators are use in programes to manipulate data and variables. Usually it is part of mathmatical or logical expressions. There are some categories of java operators as below:
- Arithmatic Opertaors
- Relational Operators
- Logical Operators
- Assignments Operators
- Increment and Decrement Operators.
- Conditional Operators
- Bitwise Operators
- Special Operators.
1. Arithmatic Operators:
Arithmatic operators are use to construct mathemaical expressions an in algebra. Java supports all the basic arithmatic operators.
Arithmatic operators are use as below:
a-b
a-b=10
-14%3=-2
Example code is below:
public class ArithmaticOperations {
public static void main(String[] args) {
// TODO Auto-generated method stub
float a=20.5f, b=4.4f;
System.out.println(" a = "+a);
System.out.println(" b = "+b);
System.out.println(" a+b = "+(a+b));
System.out.println(" a-b = "+(a-b));
System.out.println(" a*b = "+(a*b));
System.out.println(" a/b = "+(a/b));
System.out.println(" a%b = "+(a%b));
}
}
Output:
a = 20.5
b = 4.4
a+b = 24.9
a-b = 16.1
a*b = 90.200005
a/b = 4.659091
a%b = 2.8999996
2. Relational Operators:
We compare two quantities, and depending on there relation, take certain decision. For example, If we compare age of two persons, or comparing price of two items and so on we use relational operators. An expression such as
a < b or x < 18
public class RelatinalOperators {
public static void main(String[] args) {
float a=15.05f, b=15.0f , c=15.0f;
System.out.println(" a = "+a);
System.out.println(" b = "+b);
System.out.println(" c = "+c);
System.out.println(" a < b = "+(a<b));
System.out.println(" a > b = "+(a>b));
System.out.println(" a == c = "+(a==b));
System.out.println(" a <= c = "+(a<=b));
System.out.println(" a >= c = "+(a>=b));
System.out.println(" b != c = "+(b!=c));
}
}
Output:
a = 15.05
b = 15.0
c = 15.0
a < b = false
a > b = true
a == c = false
a <= c = false
a >= c = true
b != c = false
3. Logical Operators:
Java has three logical operators:
The logical operators
&& and
|| are use when we wnat to form compound conditions by combining two or more relations. For example,
a > b && a || b
4. Assignment Operators:
Assignment operator are use to assign the value of an expression to a variable. For example,
v op = exp;
Where v is variable, exp is an expression and op is a java binary operator.
5. Increment and Decrement Operators:
Java has two very useful operators generally not found in other languages. These are increment and decrement operators.
++ and -
while operator ++ ands 1 and operator - subtarcts 1. Both are unary operators. For example,
m++ OR ++m
a-- OR --a;
a= m++ OR a=++m
b= a-- OR b=--a
The increment and decrement operators extensively use in for and while loops.
m=5;
y=++m;
So in above example the y having 6 value because m value will be increment first by 1 and then assign to y. so the y and m value is 6
m=5;
y=m++;
So in above example the y haveing the 5 value because it is assign first and then increment the value of m by one.
So the m value will be 6 and the y value will be 5.
6. Conditional Operators:
If there is any ternary operator avalabel then it is calld conditional operators. The charactor pair with ? : is a ternary operators availabe in java.
exp1 ? exp2 : exp 3
Here the exp1, exp,2 and exp3 are the expression. In normal if....else function is as follows:
if(a>b)
x=a;
else
x=b;
In above example if first statement, a>b is true then the a value will assign to x, if first condition, a>b is false b value will be assign to x. We can do the same thing into conditional operators like,
x=(a>b) ? a : b;
7. Bitwise Operators:
Java supports some of the special operators known as Betwise Operators for manipulation of data at values of bit level. These operators are use for testing the bits, shifting to right or left according to conditions or requirements. These operators are not applied on float or double.
Java supports special operators such as instanceof operators and member selection operators ( . ).
Instanceof is an object reference operators and return true if the object on the left hand side is an instance of the class given in right hand side. For example,
person instanceof student
is true if the person belogs to the class student, otherwise return false.
The dot operator is use to access the instance variables and methods of class objects. For example,
person.age;
person.salary();
Comments
Post a Comment