See the Tutorial List

Java Operators

Java provides a rich set of operators environment. Java operators can be divided into following categories:

  • Arithmetic operators
  • Relation operators
  • Logical operators
  • Bitwise operators
  • Assignment operators
  • Conditional operators
  • Misc operators

Arithmetic operators

Arithmetic operators are used in mathematical expression in the same way that are used in algebra.

Operator Description
+ adds two operands
- subtract second operands from first
* multiply two operand
/ divide numerator by denumerator
% remainder of division
++ Increment operator increases integer value by one
-- Decrement operator decreases integer value by one

Example:

    
class Arithmetic_operators1{
public static void main(String as[])
    {
        int a, b, c;
        a=10;
        b=2;
        c=a+b;
        System.out.println("Addtion: "+c);
        c=a-b;
        System.out.println("Substraction: "+c);
        c=a*b;
        System.out.println("Multiplication: "+c);
        c=a/b;
        System.out.println("Division: "+c);
        b=3;
        c=a%b;
        System.out.println("Remainder: "+c);
        a=++a;
        System.out.println("Increment Operator: "+a);
        a=--a;
        System.out.println("decrement Operator: "+a);       

    }
}
    

airthmatic operator Example


Relation operators

The following table shows all relation operators supported by Java.

Operator Description
== Check if two operand are equal
!= Check if two operand are not equal.
> Check if operand on the left is greater than operand on the right
< Check operand on the left is smaller than right operand
>= check left operand is greater than or equal to right operand
<= Check if operand on left is smaller than or equal to right operand

Example:

    
class Relational_operators1{
public static void main(String as[])
    {
        int a, b;
        a=40;
        b=30;
        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("b >= a = " + (b >= a) );
        System.out.println("b <= a = " + (b <= a) );    
    }
}
    

relation operator example


Logical operators

Java supports following 3 logical operator. Suppose a=1 and b=0;

Operator Description Example
&& Logical AND (a && b) is false
|| Logical OR (a || b) is true
! Logical NOT (!a) is false

Example:

    
class Logical_operators1{
public static void main(String as[])
    {
        boolean a = true;
        boolean b = false;
        System.out.println("a && b = " + (a&&b));
        System.out.println("a || b = " + (a||b) );
System.out.println("!(a && b) = " + !(a && b));
    }
}
    

Logical Operator Example


Bitwise operators

Java defines several bitwise operators that can be applied to the integer types long, int, short, char and byte

Operator Description
& Bitwise AND
| Bitwise OR
^ Bitwise exclusive OR
<< left shift
>> right shift

Now lets see truth table for bitwise &, | and ^

a b a & b a | b a ^ b
0 0000
0 1011
1 0011
1 1110

The bitwise shift operators shifts the bit value. The left operand specifies the value to be shifted and the right operand specifies the number of positions that the bits in the value are to be shifted. Both operands have the same precedence.

Example:
a = 0001000
b = 2
a << b = 0100000
a >> b = 0000010 
    
class Bitwise_operators1{
public static void main(String as[])
    {
        int a = 50; 
        int b = 25; 
        int c = 0;

        c = a & b;        
        System.out.println("a & b = " + c );

        c = a | b;        
        System.out.println("a | b = " + c );

        c = a ^ b;        
        System.out.println("a ^ b = " + c );

        c = ~a;           
        System.out.println("~a = " + c );

        c = a << 2;      
        System.out.println("a << 2 = " + c );

        c = a >> 2;       
        System.out.println("a >>2  = " + c );

        c = a >>> 2;     
        System.out.println("a >>> 2 = " + c );
    }
}
    

bitwise operator example


Assignment Operators

Assignment operator supported by Java are as follows:

Operator DescriptionExample
=assigns values from right side operands to left side operanda = b
+=adds right operand to the left operand and assign the result to lefta+=b is same as a=a+b
-=subtracts right operand from the left operand and assign the result to left operanda-=b is same as a=a-b
*=mutiply left operand with the right operand and assign the result to left operanda*=b is same as a=a*b
/=divides left operand with the right operand and assign the result to left operanda/=b is same as a=a/b
%=calculate modulus using two operands and assign the result to left operanda%=b is same as a=a%b

Example:

    
class Assignment_operators1{
public static void main(String as[])
{
    int a = 30;
    int b = 10;
    int c = 0;

      c = a + b;
System.out.println("c = a + b = " + c );

      c += a ;
System.out.println("c += a  = " + c );

      c -= a ;
System.out.println("c -= a = " + c );

      c *= a ;
System.out.println("c *= a = " + c );

      a = 20;
      c = 25;
      c /= a ;
System.out.println("c /= a = " + c );

      a = 20;
      c = 25;
      c %= a ;
System.out.println("c %= a  = " + c );

      c <<= 2 ;
System.out.println("c <<= 2 = " + c );

      c >>= 2 ;
System.out.println("c >>= 2 = " + c );

      c >>= 2 ;
System.out.println("c >>= 2 = " + c );

      c &= a ;
System.out.println("c &= a  = " + c );

      c ^= a ;
System.out.println("c ^= a   = " + c );

      c |= a ;
System.out.println("c |= a   = " + c );
    }
}
    

assignment operator example


Misc. operator

There are few other operator supported by java language.


Conditional operator

It is also known as ternary operator and used to evaluate Boolean expression,

epr1 ? expr2 : expr3

Example:

    
class Conditional_operators1{
public static void main(String as[])
{
int a, b;
      a = 20;
      b = (a == 1) ? 30: 40;
System.out.println( "Value of b is : " +  b );

      b = (a == 20) ? 30: 40;
System.out.println( "Value of b is : " + b );
}
}
    

If epr1Condition is true? Then value expr2 : Otherwise value expr3

conditonal operator example


instanceOf operator

This operator is used for object reference variables. The operator checks whether the object is of particular type (class type or interface type)

Example:

    
class instanceof_operators1{
public static void main(String as[])
{
      String a = "Studytonight";
boolean b = a instanceof String;
System.out.println( b );
}
}
    

instance of operator example

Naming convention


In Java, the naming convention is not the rule but it is suggested to by the java communities such as Netscape and Sun Microsystems to follow. Following the Naming convention does not generate confusion or erroneous code. All the classes, interfaces, packages, methods and fields of Java programming language have been given specific naming convention. Java uses Camel Case notation for naming methods, variables etc. and TitleCase notation for classes and interfaces.

!) Class

The name of the class should start using uppercase letter and it should be a noun for example colour, Button, System, Thread, etc.

Example:

    
Public class Student
{
-------code-------
}
    

2) Interface

The name of an Interface should start with an uppercase letter and it should be an adjective, for example, ActionListener, Runnable etc.

Example:

    
interface Student  
{  
    ------code----- 
}  
    

3) Method

The name of a Method should start with a lowercase letter and it should be a verb, for example, print(), println(). The name of a method can have lowercase as well as an upper case but the name should always start with lowercase and followed by uppercase for example, actionPerformed().

Example:

    
class Student  
{  
-----method----  
void draw()  
{  
------code------  
}  
}  
    

4) Variable

The name of a Variable start with a lowercase letter for example id, name, roll etc. it should not start with special characters like &, $, _ etc. The name of a variable can have lowercase as well as an upper case but the name should always start with lowercase and followed by uppercase, for example, firstName, lastName.

Example:

    
class Student  
{  
int name, s_Id;  
}  
    

5) Package

The name of a Packagestart with a lowercase letter, for example, studytonight, java etc. the name of a package can also contain multiple words but it should be separated by dots (.) for example java.util.

Example:


package com.studytonight; //package  
class Student  
{  
----code----
}  

6) Constant

The name of a Constantstart with anuppercase letter, for example, RED, GREEN. the name of a Constant can also contain multiple words but it should be separated by an underscore(_) for example MIN_PRIORITY.

Example:


class Student  
{  
//constant  
        static final int MAX_AGE = 21;  
----code----
}