# Ternary Operators

There was a question about the resultant values wrt datatypes in Ternary Operators in the JavaRanch forum.

Reference thread from JavaRanch ->

http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=33&t=023287

The original question is pasted below:

int i=5; System.out.println(((i>5)?9.9:9)); can anyone clear my doubt why the answer is 9.0 ?

One of the answers given in the thread is :

The condition expression returns the value according to the first one that is in this case 9.9. If you change it to some other data type then the return will also be according to it. So the return is in the format of the first one.

As per the JLS (Java Language Specification) on Conditional Operators (available at http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293),

When an operator applies **binary numeric promotion** to a pair of operands, each of which must denote a value of a numeric type, the following rules apply, in order, using widening conversion (§5.1.2) to convert operands as necessary: * If either operand is of type double, the other is converted to double. * Otherwise, if either operand is of type float, the other is converted to float. * Otherwise, if either operand is of type long, the other is converted to long. * Otherwise, both operands are converted to type int.

Thats why the value is of type floating which is **9.0**. The link to the **Binary Numeric Promotion** mentioned above is at :

http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#170983

# Shift Operators

There are three shift operators! Left Shift Operator («) and Signed Right Shift Operator (») and Unsigned Right Shift Operator (»>).

In general, an expression **V op S** involving a shift operator where

* V is the actual value to be shifted

* op is the shifting operator of any kind

* S is the number of digits to be shifted

As per JLS, each individual operand undergoes a Unary Numeric Conversion individually before the shifting is performed. The Unary numeric conversion ensures that the operand is *convertible to a numeric type* otherwise it generates a *compile-time error*.

**Left Shift Operator («)**

Left Shift Operator

**Signed Right Shift Operator (»)**

Signed Right Shift Operator

**Unsigned Right Shift Operator(»>)**

Unsigned or zero filled Right Shift Operator.