OCA Java SE 8 Operators/Statements - Java Operators in Details








Unary Operators

A unary operator requires exactly one operand.

The following table lists the unary operators in Java.

Unary operatorDescription
+Indicates a number is positive
-Indicates a literal number is negative or negates an expression
++Increments a value by 1
--Decrements a value by 1
!Inverts a Boolean's logical value

Logical Complement

The logical complement operator, !, flips the value of a boolean expression.

For example, if the value is true, it will be converted to false, and vice versa.

To illustrate this, compare the outputs of the following statements:

public class Main{
   public static void main(String[] argv){
        boolean x = false; 
        System.out.println(x);  // false 
        x = !x; 
        System.out.println(x);  // true 
   }
}

The code above generates the following result.





Negation Operators

The negation operator, -, reverses the sign of a numeric expression, as shown in these statements:

public class Main{
   public static void main(String[] argv){
/*from www  .j av  a  2s  .  co  m*/
        double x = 1.21; 
        System.out.println(x);  // 1.21 
        x = -x; 
        System.out.println(x);  // -1.21 
        x = -x; 
        System.out.println(x);  // 1.21 
   }
}

We cannot apply a negation operator, -, to a boolean expression, nor can we apply a logical complement operator, !, to a numeric expression.

For example, none of the following lines of code will compile:

    int x = !5;  // DOES NOT COMPILE 
    boolean y = -true;  // DOES NOT COMPILE 
    boolean z = !0;  // DOES NOT COMPILE 

Unlike some other programming languages, in Java 1 and true are not related in any way, just as 0 and false are not related.

The code above generates the following result.





Increment and Decrement Operators

Increment and decrement operators, ++ and --, respectively, can be applied to numeric operands and have the higher order or precedence, as compared to binary operators.

They often get applied first to an expression.

The order applied to their operand makes a difference in how an expression is processed.

If the operator is placed before the operand(pre-increment/decrement operator), the operator is applied first and the value return is the new value of the expression.

If the operator is placed after the operand(post-increment/decrement operator), the original value of the expression is returned, with operator applied after the value is returned.

The following code snippet illustrates this distinction:

public class Main{
   public static void main(String[] argv){
//  ww w  . java2s  .c  o m
        int counter = 0; 
        System.out.println(counter);  // Outputs 0 
        System.out.println(++counter);  // Outputs 1 
        System.out.println(counter); // Outputs 1 
        System.out.println(counter--);  // Outputs 1 
        System.out.println(counter);  // Outputs 0 
   }
}

The code above generates the following result.

Assignment Operators

An assignment operator is a binary operator that assigns the variable on the left-hand side of the operator with the result on the right-hand side of the equation.

The simplest assignment operator is the = assignment:

int x = 1; 

This statement assigns x the value of 1.

Java will automatically promote from smaller to larger data types during the assignment.

Java will throw a compiler exception if it detects you are trying to convert from larger to smaller data types.

The following statement does not compile because you are trying to assign a double value 1.0 to an integer value. Even though the value is a mathematic integer, you're instructing the compiler to treat it as a double.

int x = 1.0;  // DOES NOT COMPILE 

The following statement does not compile because the literal value 9921222 is outside the range of short.

short y = 9921222;  // DOES NOT COMPILE 

The following statement does not compile since the f is added to the end of the number and it instructs the compiler to treat the number as floating-point value.

int z = 9f;  // DOES NOT COMPILE 

The following statement does not compile since Java interprets the literal as an int and notices that the value is larger than int allows. The literal would need a postfix L to be considered a long.

long t = 992303453453453453;  // DOES NOT COMPILE 

Casting Primitive Values

Casting primitives is required when converting from a larger numerical data type to a smaller numerical data type, or converting from a floating-point number to an integral value.

int x = (int)1.0; 
short y = (short)(921222;  
int z = (int)9l; 
long t = 991231231231231231L; 

Overflow and Underflow

Overflow happens when a number is too large to fit within the data type, so the Java "round" the value to the next lowest value.

Underflow happens when the number is too low to fit in the data type.

The following code does not compile. short values are automatically promoted to int when applying any arithmetic operator, with the resulting value being of type int.

short x = 10; 
short y = 3; 
short z = x * y;  // DOES NOT COMPILE 

To make it compile, cast the result of the multiplication:

short x = 10; 
short y = 3; 
short z = (short)(x * y);