Skip to Content
bytegarden

Operators and Expressions

Decresing order of precedence

Operator TypesExamplesAssociativity
Unary++, --, + (unary plus), - (unary minus), !, ~Right-to-left
Arithmetic*, /, %, +, -Left-to-right
Shift<<, >>, >>>Left-to-right
Relational<, <=, >, >=, ==, !=Left-to-right
Bitwise&, |, ^Left-to-right
Logical&&, ||, !Left-to-right
Conditional (Ternary)? :Right-to-left
Assignment=, +=, -=, *=, /=, %=Right-to-left
  • instanceof Operator: Used to test if an object is an instance of a class (same precedence as of arithmetic)
  • String Concatenation Operator: Used to join strings (same precedence as of relational)

Increment/Decrement Operators

  • Pre (prefix): ++x or --x & Post (postfix): x++ or x--
  • Cannot perform on boolean type variables

Bitwise Operators

OperatorSymbolDescription
AND&Both bits must be 1 for result to be 1
OR|If any bit is 1, result is 1
XOR^Result is 1 if exactly one bit is 1
NOT~Inverts all bits
LEFT SHIFT<<Shifts bits left
RIGHT SHIFT>>Shifts bits right
UNSIGNED RIGHT SHIFT>>>Shifts bits right (Java-specific)
a
b
a & b
00000000
00000000
00000000
00001000
8

AND. A result bit is 1 only where both a and b have a 1.

int result = a & b; // 8

Unsigned right shift on a negative number:

Bitwise.java
public class Bitwise {
    public static void main(String[] args) {
        int x=-0b1010;
        int y;
        y=x>>>1;

        System.out.println("x = "+x);
        System.out.println("y = "+y);

        System.out.println(String.format("Binary form of x= %s", Integer.toBinaryString(x)));
        System.out.println(String.format("Binary form of y= %32s", Integer.toBinaryString(y)));
    }
}
Runs in your browser with Java 21. Ctrl/⌘ + Enter to run.Built with CheerpJ

Bit Manipulation Techniques

  • Merging (Using OR operation)
    1. Used to set (or marked) specific bits to 1
    2. Preserves existing 1s while adding new ones
  • Masking (Using AND operation)
    1. Used to check if specific bits are set (or marked)
    2. Can check one or multiple bits at once

Swapping two numbers with XOR, without a third variable:

Swap.java
// Swap two numbers without using third variable

public class Swap {
    public static void main(String[] args) {
        int a=10, b=15;
        System.out.println(a+ " " +b);
        a = a^b;
        b = a^b;
        a = a^b;
        System.out.println("After Swapping:");
        System.out.println(a+ " " +b);
    }
}
Runs in your browser with Java 21. Ctrl/⌘ + Enter to run.Built with CheerpJ
Merge.java
// program to merge two numbers without using a third variable

public class Merge {

    /*
    public static void main(String[] args) 
    {
      int a=10, b=15;
      
      a=a^b;
      b=a^b;
      a=a^b;
      
      System.out.println(a+ " "+b);
    }
    */
    
    /*
    public static void main(String[] args) 
    {
      byte a=9, b=12;
      
      byte c;
      
      c=(byte)(a<<4);
      c=(byte)(c|b);
      
      System.out.println((c&0b11110000)>>4);
      System.out.println((c&0b00001111)>>4);
      
    }
    */
    
    
    public static void main(String[] args) 
    {
      
      
      byte c;
      
      c=(byte)(9<<4);
      c=(byte)(c|12);
      
      System.out.println((c&0b11110000)>>4);
      System.out.println((c&0b00001111)>>4);
      
    }
}
Runs in your browser with Java 21. Ctrl/⌘ + Enter to run.Built with CheerpJ

Type Conversion

Rule

Conversion from smaller data type to larger data type will be implicit. Whereas from a larger data type to a smaller one will be explicit cast.

Techniques

The terms “upcasting” and “downcasting” are also commonly used in OOP for class inheritance relationships, though the concept is similar

  1. Widening (UPCASTING)

    • Process of storing smaller data type into larger data type without data loss
    • Happens automatically (implicit conversion)
    • Source data type ≤ Destination data type
    • Types must be compatible
    // Valid Widening Examples
    byte -> short -> int -> long -> float -> double
    int num = 10;
    double d = num;    // Automatically widened
    
    char -> int -> long -> float -> double
    char ch = 'A';
    int n = ch;        // Automatically widened
  2. Narrowing (DOWNCASTING)

    • Process of storing larger data type into smaller data type which lead to data loss
    • Must be done explicitly
    • Source data type > Destination data type
    // Narrowing Examples (Explicit Conversion Required)
    double -> float -> long -> int -> short -> byte
    double d = 100.04;
    int n = (int)d;    // Explicit narrowing required
    
    // Float to Integer Example
    float f = 10.5f;
    int x = (int)f;    // Requires explicit cast
  • Source is in right hand side, destination is on left hand side
byteshortintlongfloatdoublecharboolean
byte✓✓✓✓✓✓✗✗
short✗✓✓✓✓✓✗✗
int✗✗✓✓✓✓✗✗
long✗✗✗✓✓✓✗✗
float✗✗✗✗✓✓✗✗
double✗✗✗✗✗✓✗✗
char✗✗✗✗✗✗✓✗
boolean✗✗✗✗✗✗✗✓