Operators in java with example programs

Operators are one of the basic and important concept in java. Operator is a symbol that performs an action. An operator act on some variables called operands. Now, you have a doubt called what is the difference between operand and operator. To clear all your queries continue with this article.

a + b

        In above, example a,b are the operands and + is the operator. If an operator acts on a single variable is called “unary operator”. If an operator acts with two variables is called “binary operator”. If an operator works with three variables is called “ternary operator”. This is one type of classification.

Arithmetic Operator:
        These operators are used to perform fundamental arithmetic operation like addition, subtraction,etc. There are 5 arithmetic operators are there in java. Since these operators work on two operands at a time. These are called binary operators.

        Addition operator (+) also used to add two String, see below Strings.

String s1 = wel;
String s2 = come;
String s3 = s1+s2; //here + is adding s1 and s2.

        Now, we get s3 value is welcome. This is called “String Concatenation”.

Unary Operators:
        The name indicates, unary operator act only one operand. There are 3 kinds of unary operators.
  • Unary minus operator(-)
  • Increment operator(++)
  • Decrement operator(--)
Unary minus operator:
        This operator is used to negate a value. Negation means converts a negative value into positive and vice-verse.

int a = 5;
a = -(5);

        In this code snippet, the unary minus operator is used to negate a value. First, a value is 5 only. It became -5 when apply unary minus on a.

Increment operator:
        This operator increases the value of variable 1.

int a = 1;
a++;

        Here, the value of a incremented by 1. when applies the increment operator on a either before/ after. The above operation is equals to this operation.

a = a+1;

        Here, main thing is pre-increment at left hand side.

int a = 1;
System.out.println(a); //Display the value of a as 1
System.out.println(++a); // Increment the a value 1 and display it as 2.
System.out.println(a); // Display the value of x, which is already incremented 2

        Another one is post-incrementation at right hand side.

System.out.println(a); //Display the value of a as 1
System.out.println(a++); //Display the value of a as 1, then increment the value
System.out.println(a); //Display the value of a incremented as 2.

Decrement operator:
        This operator decrease the value of variable 1.

 int a = 1;
 a--;

        In the above parameter a value is decremented by 1. When applies the decrements operator on corresponding parameters either before/after, the value of the parameter is equals to the below operation.

 a = a-1;

        Pre-decrementation and post-decrementation are same as above operation. But in increment operator place use the decrements operator.

Assignment Operator:
        Assignment operator is used to store some value into variables. It is used in 3 ways.
  1. It is used to store a value into variable,
int a = 1; //value 1 is stored into variable a.
  1. It is used to store a variable value into another variable.
int b = a; // variable a value saved into variable b.
  1. It is used to store a value of an expression into variable.
int c = a+b-1; //The expression a+b-1 value saved into variable c.

Relational Operators:
        Relational operators are used for the purpose of comparing. Which one is bigger/lower or whether two quantities are equal or not. Relational operators are 6 types. That are,
  1. > Greater than operator
  2. >= Greater than or equals operator
  3. < Less than operator
  4. <= Less than or equals operator
  5. == Equals to operator
  6. != Not -Equals to operator
Logical Operators:
        Logical Operator are used to construct the compound condition. Compound condition is a combination of several conditions. Logical operators are 3 types.
  1. && and operator
  2. || or operator
  3. ! not operator
For Ex,

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

        In the above one any of one condition is satisfies, the output will display as 'yes'. 

if(a< b && b > c) 
  System.out.println("yes");

        In the above there are 2 conditions a>b , b<c. These two are combined by &&. If both conditions are true, then display 'yes'.

if(!(str1.equals(str2))) 
    System.out.println(Not equals);

        In the above condition str1, str2 are two String objects and equals() is the method to compare both the strings. The ! Operator tells the str1 and str2 are not equals then only 'Not equals' shown.

Boolean Operators:
        Boolean operators act on boolean variables and produce boolean type result. These are 3 types.
  1. &boolean and boolean
  2. |boolean or boolean
  3. !boolean not boolean
        Boolean & operator returns true if the both variables are true. Boolean | operator sends true if any variable is true. Boolean ! Operator converts true to false and vice-versa.

Note: Boolean variables are returns only true or false.

Ternary Operator or Conditional Operator:(?:)
        Ternary operator is called ternary because it acts on 3 variables. The other name of this operator is conditional operator, since it represents a conditional statement. Two symbols are used for this operator ? and :

Syntax:

variable = condition ? expression1 : expression2 ;

         In the previous, if 'condition' is true expression1 value stored into variable. If 'condition'  is false expression2 value stored into variable.

Ex:

max = (a>b) ? a : b;

        Here, condition(a>b) is true a value saved into max, else b value saved into max.

Member Operator:(.)
        Member operator also called as a dot operator since it's symbol is a (.). This operator tells about member of a package or a class. It is used in three ways.
  • package contains classes. We ca use . Operator to refer the class or a package.
Syntax:

packagename.classname;

Ex:

java.io.BufferedReader;
  • Each class contains variables and methods. To refer to the variables of a class, we can use this operator.
Syntax:

 classname.variablename;
           or
 objectname.variablename;

Ex:

System.out //out is a static variable in System Class
emp.id; // id is the variable of Employee class, emp is the object of Employee
  • Class also contains methods. Using . operator we can refer to the methods of a class.
Syntax:

classname.methodname;
         or
objectname.methodname;

Ex:

Math.sqrt(); // sqrt() is a method in math class.
br.read(); // read() is method in BufferedReader class. br is object of BufferedReader.

instaceof Operator:
        instanceOf operator is used to test weather an object is belongs to particular class or not.

Note:
        The word instance means object. This operator can also be used to check if an object belongs to an interface or not.

Syntax:

boolean variable = object instaceofclass;
               or
boolean variable = object instaceofinterface;

Ex:

boolean x = emp instaceof Employee;

new Operator:
        new operator is often used to create objects to classes. That objects are created at 'heap memory' by JVM, dynamically(at runtime)

Syntax:

classname object = new classname();

Ex:

Employee emp = new Employee();

Cast Operator:
         Cast operator is used to convert one data type into another data type. This operator can be used by writing datatype inside the simple braces.

Ex:

double a = 11.05;
int b = a;  //error because variable a and b datatypes are different

        If you want to store a value into b. First, convert the datatype of a into datatype of b. It means double datatype should be converted into int datatype. This is called cast operator.

 int b = (int) a;

        In the above statement, (int)is called the cast operator. Cast operator is generally used before a variable or before a method.

Example of bit-wise operators:
        The below example explains you to the use of various bit-wise operators.


public class Bits{

      public static void main(String [] args){
         byte x,y;
         x = 10;
         y = 20;
         System.out.println("~x="+(~x));
         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>>>y= "+(x>>>y));
   }

}

Comments

Post a Comment

Popular posts from this blog

how to count the page views by using JSP

Exception in thread "main" java.lang.NoClassDefFoundError: javax/transaction/SystemException

Multithreading in java with example