Blogger templates

Your Ad Here

Video

Blogroll

Blogger news

Java Operators


Operators are used to manipulate primitive data types. Java operators can be classified as unary, binary, or ternary—meaning taking one, two, or three arguments, respectively. A unary operator may appear before (prefix) its argument or after (postfix) its argument. A binary or ternary operator appears between its arguments.

Java operators fall into eight different categories: assignment, arithmetic, relational, logical, bitwise, compound assignment, conditional, and type.


Assignment Operators            =
Arithmetic Operators            -      +     *     /     %     ++    --
Relational Operators            >      <     >=    <=    ==    !=
Logical Operators               &&     ||    &     |     !     ^
Bit wise Operator               &      |     ^     >>    >>>
Compound Assignment Operators   +=     -=    *=    /=    %=    <<=   >>=     >>>= 
Conditional Operator            ?:
 


Assignment operators

The java assignment operator statement has the following syntax:
<variable> = <expression>
If the value already exists in the variable it is overwritten by the assignment operator (=).

public class Demo {
    public Demo() {
     //Assigning Primitive Values
        int j, k;
        j = 10; // j gets the value 10.
        j = 5; // j gets the value 5. Previous value is overwritten.
        k = j; // k gets the value 5.
        System.out.println("j is : " + j);
        System.out.println("k is : " + k);
     // Showing how to assig References
        Integer i1 = new Integer("1");
        Integer i2 = new Integer("2");
        System.out.println("i1 is : " + i1);
        System.out.println("i2 is : " + i2);
        i1 = i2;
       System.out.println("i1 is : " + i1);
       System.out.println("i2 is : " + i2);
     // Multiple Assignments
       k = 10;j = 10; System.out.println("j is : " + j);
       System.out.println("k is : " + k);
   }
   public static void main(String args[]) {
        new Demo();
   }
} 

Arithmetic operators

Java provides eight Arithmetic operators. They are for addition, subtraction, multiplication, division, modulo (or remainder), increment (or add 1), decrement (or subtract 1), and negation.
The binary operator + is overloaded in the sense that the operation performed is determined by the type of the operands. When one of the operands is a String object, the other operand is implicitly converted to its string representation and string concatenation is performed.

For example :
String message = 100 + “Hello”; 
Output : 100 Hello


public class Demo {

     public Demo() {
          int x, y = 10, z = 5;
          x = y + z;
          System.out.println("+ operator resulted in " + x);
          x = y - z;
          System.out.println("- operator resulted in " + x);
          x = y * z;
          System.out.println("* operator resulted in " + x);
          x = y / z;
          System.out.println("/ operator resulted in " + x);
          x = y % z;
          System.out.println("% operator resulted in " + x);
          x = y++;
          System.out.println("Postfix ++ operator resulted in " + x);
          x = ++z;
          System.out.println("Prefix ++ operator resulted in " + x);
          x = -y;
          System.out.println("Unary operator resulted in " + x);
  // Some examples of special Cases
           int tooBig = Integer.MAX_VALUE + 1; // -2147483648 which is
  // Integer.MIN_VALUE.
           int tooSmall = Integer.MIN_VALUE - 1; // 2147483647 which is
  // Integer.MAX_VALUE.
           System.out.println("tooBig becomes " + tooBig);
           System.out.println("tooSmall becomes " + tooSmall);
           System.out.println(4.0 / 0.0); // Prints: Infinity
           System.out.println(-4.0 / 0.0); // Prints: -Infinity
           System.out.println(0.0 / 0.0); // Prints: NaN
           double d1 = 12 / 8; 
// result: 1 by integer division. d1 gets the value  1.0.
           double d2 = 12.0F / 8; // result: 1.5
           System.out.println("d1 is " + d1);
           System.out.println("d2 iss " + d2);
      }
      public static void main(String args[]) {
           new Demo();
      }
} 
 
Relational operators
Relational operators in Java are used to compare 2 or more objects. Java provides six relational operators:
greater than (>), less than (<), greater than or equal (>=), less than or equal (<=), equal (==), and not equal (!=).

Logical operators

Logical operators return a true or false value based on the state of the Variables. There are six logical, or boolean, operators. They are AND, conditional AND, OR, conditional OR, exclusive OR, and NOT.

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[]) {
         new Demo();
    }
}

Bitwise operators

Java provides Bit wise operators to manipulate the contents of variables at the bit level. These variables must be of numeric data type ( char, short, int, or long). Java provides seven bitwise operators. They are AND, OR, Exclusive-OR, Complement, Left-shift, Signed Right-shift, and Unsigned Right-shift.


public class BitwiseOperatorsDemo {

     public BitwiseOperatorsDemo() {
         int x = 0xFAEF; //1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1
         int y = 0xF8E9; //1 1 1 1 1 0 0 0 1 1 1 0 1 0 0 1
         int z;
         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));
         System.out.println("x << y : " + (x << y));
         System.out.println("x >> y : " + (x >> y));
         System.out.println("x >>> y : " + (x >>> y));
  //There is no unsigned left shift operator
    }
    public static void main(String args[]) {
          new BitwiseOperatorsDemo();
    }
}
 


Output
3,0,3

Conditional operators

The Conditional operator is the only ternary (operator takes three arguments) operator in Java. The operator evaluates the first argument and, if true, evaluates the second argument. If the first argument evaluates to false, then the third argument is evaluated. The conditional operator is the expression equivalent of the if-else statement. The conditional expression can be nested and the conditional operator associates from right to left: (a?b?c?d:e:f:g) evaluates as (a?(b?(c?d:e):f):g)

public class Demo {

    public Demo() {
        int x = 10, y = 12, z = 0;
        z = x > y ? x : y;
        System.out.println("z : " + z);
    }
    public static void main(String args[]) {
         new Demo();
    }
}


Type conversion allows a value to be changed from one primitive data type to another. Conversion can occur explicitly, as specified in
the program, or implicitly, by Java itself. Java allows both type widening and type narrowing conversions.
In java Conversions can occur by the following ways:
  • Using a cast operator (explicit promotion)
  • Using an arithmetic operator is used with arguments of different data types (arithmetic promotion)
  • A value of one type is assigned to a variable of a different type (assignment promotion)

Operator Precedence

The order in which operators are applied is known as precedence. Operators with a higher precedence are applied before operators with a lower precedence. The operator precedence order of Java is shown below. Operators at the top of the table are applied before operators lower down in the table. If two operators have the same precedence, they are applied in the order they appear in a statement.
That is, from left to right. You can use parentheses to override the default precedence.


Next : Control statements

 

Most Reading

Stats