MISC OPERATORS

There are few other operators supported by Java Language.

Conditional Operator ( ? : ):

Conditional operator is also known as the ternary operator. This operator consists of three operands and is used to evaluate boolean expressions. The goal of the operator is to decide which value should be assigned to the variable. The operator is written as :

Following is the example:

1

2

3

4

5

6

7

8

9

10

11

12

public class Test

{

    public static void main(String args[])

    {

        int a, b;

        a = 10;

        b = (a == 1) ? 20 : 30;

        System.out.println("Value of b is : " + b);

        b = (a == 10) ? 20 : 30;

        System.out.println("Value of b is : " + b);

    }

}

This would produce following result:

Instance Of Operator:

This operator is used only for object reference variables. The operator checks whether the object is of a particular type(class type or interface type). instanceOf operator is wriiten as:

( Object reference variable ) instanceOf (class/interface type)

If the object referred by the variable on the left side of the operator passes the IS-A check for the class/interface type on the right side then the result will be true. Following is the example:

String name = = 'James';
boolean result = name instanceOf String; 
// This will return true since name is type of String

This operator will still return true if the object being compared is the assignment compatible with the type on the right. Following is one more example:

1

2

3

4

5

6

7

8

9

10

11

12

13

class Vehicle

{

 

}

class Car extends Vehicle

{

    public static void main(String args[])

    {

        Vehicle a = new Car();

        boolean result = a instanceof Car;

        System.out.println(result);

    }

}