Operators
Various operators that are used in Java are enlisted in following Pics.
Arithmetic Operators
The arithmetic operators are used to perform basic arithmetic operations. The operands used for these operators must be of numeric type. The Boolean type operands can not be used with arithmetic operators.
Java Program
//Program for demonstrating arithmetic operators
class ArithOperDemo
{
public static void main(String[] args)
{
System.out.println("\n Performing arithmetic operations ");
int a 10,b=20,c;
System.out.println("a = "+a);
System.out.println("b= "+b);
c=a+b;
System.out.println("\n Addition of two numbers is "+c);
c=a-b;
System.out.println("\n Subtraction of two numbers is "+c);
c=a*b;
System.out.println("\n Multiplication of two numbers is "+c);
c=a/b;
System.out.println("\n Division of two numbers is "+c);
}
}
Output
Performing arithmetic operations
a= 10
b= 20
Addition of two numbers is 30
Subtraction of two numbers is -10
Multiplication of two numbers is 200
Division of two numbers is 0
Relational Operators
Output
The relational operators typically used to denote some condition. These operators establish the relation among the operators. The <><>= are the relational operators. The result of these operators is a Boolean value.
Java Program
//Program for demonstrating relational operators
import java.io.*;
import java.lang.*;
import java.util.*;
public class RelOper
{
public static void main(String args[])
{
int a,b,c;
a=10;
b=20;
if(a>b)
{
System.out.println("a is largest");
}
else
{
System.out.println("b is largest");
}
}
}
Logical Operators
The logical operators are used to combine two operators. These two operands are Boolean operators.
Java Program
//Program for demonstrating logical operators
import java.io.*;
import java.lang.*;
public class LogicalOper
{
public static void main(String args[])throws IOException
{
boolean oper1,oper2;
oper1=true;
oper2=false;
boolean ans1,ans2;
ans1=oper1&oper2;
ans2=oper1|oper2;
System.out.println("The oper1 is: "+oper1);
System.out.println("The oper2 is: "+oper2);
System.out.println("The oper1&oper2 is: "+ans1);
System.out.println("The oper1 | oper2 is: "+ans2);
}
}
Output
The oper1 is: true
The oper2 is: false
The oper1&oper2 is: false
The oper1 oper2 is: true
Special Operators
• There are two special operators used in Java-instanceof and dot operators.
• For determining whether the object belongs to particular class or not- an instanceof operator is used. For example-
• Ganga instanceof River if the object Ganga is an object of class River then it returns true otherwise false.
• The dot operator is used to access the instance variable and methods of class objects.
For example -
Customer.name //for accessing the name of the customer
Customer.ID //for accessing the ID of the customer
Conditional Operator
The conditional operator is "?" The syntax of conditional operator is
Condition?expression1:expression2
Where expression1 denotes the true condition and expression2 denotes false condition.
For example:
a>b?true:false
This means that if a is greater than b then the expression will return the value true otherwise it will return false.