Operators in Java
Operators are the symbol that is used to perform operations . There are many types of operator available in java. Operators are as follows
1.Assignment Operators :
Assignment operators are used to assign a value in
in variable .If a value exists in variable then new value will be overridden in
variable . Here is an example .
public class Demo
{
public Demo(){
int x;
x=10; // assigned value in x
System.out.println("before override the value of x is : " + x); // print 10
x=20;
System.out.println("after override the value of x is : " + x); // print 20
}
public static void main(String args[]) {
Demo demo=new Demo();
}
}
2.Arithmetic Operators :
Arithmetic Operators are used for Arithmetic calculation . Arithmetic operators are +,-,*,/,%,++,--
public class Demo
{
public Demo()
{
int x, y = 50, z = 10;
x = y + z;
System.out.println("result of + operator is : " + x);
x = y - z;
System.out.println("result of - operator is : " + x);
x = y * z;
System.out.println("result of * operator is : " + x);
x = y / z;
System.out.println(result of "/ operator is : " + x);
x = y % z;
System.out.println(result of "% operator is : " + x);
x = y++;
System.out.println("result of Post ++ operator is : " + x);
x = ++z;
System.out.println("result of Pre ++ operator is : " + x);
x = -y;
System.out.println("result of Unary operator is : " + x);
}
public static void main(String args[]) {
Demo demo=new Demo();
}
}
3.Relational Operators :
Relational Operators are used to compare between two or more objects in java. It returns the boolean value.Here is an example
public class Demo
{
public Demo( )
{
int x = 50, y = 10;
System.out.println("x > y : "+(x > y)); // true
System.out.println("x < y : "+(x < y)); // false
System.out.println("x >= y : "+(x >= y)); // true
System.out.println("x <= y : "+(x <= y)); // false
System.out.println("x == y : "+(x == y)); // false
System.out.println("x != y : "+(x != y)); // true
}
public static void main(String args[])
{
Demo demo=new Demo();
}
}
4.Logical Operators :
Logical Operators return always boolean value(true or false) based on the state of variables . Here is an example
public class Demo
{
public Demo( )
{
boolean x = true;
boolean y = false;
System.out.println("x & y : " + (x & y));
System.out.println("x && y : " + (x && y));
System.out.println("x | y : " + (x | y));
System.out.println("x || y: " + (x || y));
System.out.println("x ^ y : " + (x ^ y));
System.out.println("!x : " + (!x));
}
public static void main(String args[])
{
Demo demo=new Demo();
}
}
5.Bit wise Operator : Java provides seven bit wise operators . Operators are
a)AND
b)OR
c)Exclusive-OR
d)Complement
e)Left-shift
f)Signed Right-shift
g)Unsigned Right-shift
6.Conditional Operator :
Ternary operator is called as conditional operator. It takes three arguments.
Syntax is
public class Demo
{
public Demo( )
{
int x = 100, y = 120, z = 0;
z = x > y ? x : y;
System.out.println("z : " + z); // print 120
}
public static void main(String args[])
{
Demo demo=new Demo();
}
}
7. Instanceof Operator :
instanceof Operator is used to check the instance is of a particular type(class or interface) or not . It returns boolean value .
Syntax is
public class Demo {
public static void main(String args[]) {
int x = 10;
// following will return true if 'x' is type of int
boolean result = x instanceof float;
System.out.println( result ); // print false
boolean result1 = x instanceof int;
System.out.println( result1 ); // print true
}
}
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
1.Assignment Operators :
Assignment operators are used to assign a value in
in variable .If a value exists in variable then new value will be overridden in
variable . Here is an example .
public class Demo
{
public Demo(){
int x;
x=10; // assigned value in x
System.out.println("before override the value of x is : " + x); // print 10
x=20;
System.out.println("after override the value of x is : " + x); // print 20
}
public static void main(String args[]) {
Demo demo=new Demo();
}
}
2.Arithmetic Operators :
Arithmetic Operators are used for Arithmetic calculation . Arithmetic operators are +,-,*,/,%,++,--
public class Demo
{
public Demo()
{
int x, y = 50, z = 10;
x = y + z;
System.out.println("result of + operator is : " + x);
x = y - z;
System.out.println("result of - operator is : " + x);
x = y * z;
System.out.println("result of * operator is : " + x);
x = y / z;
System.out.println(result of "/ operator is : " + x);
x = y % z;
System.out.println(result of "% operator is : " + x);
x = y++;
System.out.println("result of Post ++ operator is : " + x);
x = ++z;
System.out.println("result of Pre ++ operator is : " + x);
x = -y;
System.out.println("result of Unary operator is : " + x);
}
public static void main(String args[]) {
Demo demo=new Demo();
}
}
3.Relational Operators :
Relational Operators are used to compare between two or more objects in java. It returns the boolean value.Here is an example
public class Demo
{
public Demo( )
{
int x = 50, y = 10;
System.out.println("x > y : "+(x > y)); // true
System.out.println("x < y : "+(x < y)); // false
System.out.println("x >= y : "+(x >= y)); // true
System.out.println("x <= y : "+(x <= y)); // false
System.out.println("x == y : "+(x == y)); // false
System.out.println("x != y : "+(x != y)); // true
}
public static void main(String args[])
{
Demo demo=new Demo();
}
}
4.Logical Operators :
Logical Operators return always boolean value(true or false) based on the state of variables . Here is an example
public class Demo
{
public Demo( )
{
boolean x = true;
boolean y = false;
System.out.println("x & y : " + (x & y));
System.out.println("x && y : " + (x && y));
System.out.println("x | y : " + (x | y));
System.out.println("x || y: " + (x || y));
System.out.println("x ^ y : " + (x ^ y));
System.out.println("!x : " + (!x));
}
public static void main(String args[])
{
Demo demo=new Demo();
}
}
5.Bit wise Operator : Java provides seven bit wise operators . Operators are
a)AND
b)OR
c)Exclusive-OR
d)Complement
e)Left-shift
f)Signed Right-shift
g)Unsigned Right-shift
6.Conditional Operator :
Ternary operator is called as conditional operator. It takes three arguments.
Syntax is
result=condition?true result:false result ;
Here is an examplepublic class Demo
{
public Demo( )
{
int x = 100, y = 120, z = 0;
z = x > y ? x : y;
System.out.println("z : " + z); // print 120
}
public static void main(String args[])
{
Demo demo=new Demo();
}
}
7. Instanceof Operator :
instanceof Operator is used to check the instance is of a particular type(class or interface) or not . It returns boolean value .
Syntax is
(object reference variable) instanceof (class or interface)
Here is an examplepublic class Demo {
public static void main(String args[]) {
int x = 10;
// following will return true if 'x' is type of int
boolean result = x instanceof float;
System.out.println( result ); // print false
boolean result1 = x instanceof int;
System.out.println( result1 ); // print true
}
}
Find us :
Facebook : @apnaandroid
Google+ : Apna Java
YouTube : Android & Java Tutorial
Comments
Post a Comment