- Java Tutorial
- Your first Java Program
- Java and JVM Internals
- Java Keywords and Best Practices
- Java Variables
- Data Types and Literals
- Operators in Java
- Operators in Java Continued
- Operator Precedence and Operators Example
- Java Strings and Command Line Arguments
- Control Statements in Java
- Java looping statements
- Java arrays
- Java Classes and Objects
- Java garbage collection and access modifiers
- Java class methods
- Java class inheritance
- Java abstract classes and abstract methods
- Java final classes, final methods and final data members
- Access superclass constructor and superclass members
- Method Overriding and Run Time Polymorphism
- Multilevel Inheritance and Referencing
- Java interfaces
- Java Interface Variables and Default Methods
- Java Interface References and Extending Interfaces
- Java Exception Handling
- Multiple catch blocks and nested try blocks
- Methods in Throwable, Java’s Built-in Exceptions, and Custom Exception
- Java throw, finally and throws keywords
- Java I/O – built in I/O classes

This lesson is continuation of previous lesson Operators in Java. In this lesson we will discuss Relational, Bitwise and Logical Operators.

These operators give the relationship between two variables such as greater than, less than, equals and not equals etc. Below table provides a list of relational operators, where X is 45 and Y is 5.

Operator Name | Symbol | Description | Example |
---|---|---|---|

Equal to | == | Compares for equality, if equal returns true else returns false |
X == Y gives false |

Not Equal to | != | Compares for inequality, if not equal returns true else returns false |
X != Y gives true |

Greater than | > | if first value is greater than second value then return true else false |
X > Y gives true |

Less than | < | if first value is less than second value then return true else false |
X < Y gives false |

Greater than or equal to | >= | if first value is greater than or equal to second value then return true else false |
X >= Y gives true |

Less than or equal to | <= | if first value is less than or equal to second value then return true else false |
X <= Y gives false |

/* This is a simple Java program about Relational Operators. Call this file KH_RelationalOperators.java. */ public class KH_RelationalOperators { // A Java program begins with a call to main(). public static void main(String args[]) { int x = 45; int y = 5; System.out.println("Equal to x == y : " + (x == y)); System.out.println("Not Equal to x != y : " + (x != y)); System.out.println("Greater than x > y : " + (x > y)); System.out.println("Less than x < y : " + (x < y)); System.out.println("Greater than or equal to x >= y : " + (x >= y)); System.out.println("Less than or equal to x <= y : " + (x <= y)); } }

Download the code Run the code

Equal to x == y : false Not Equal to x != y : true Greater than x > y : true Less than x < y : false Greater than or equal to x >= y : true Less than or equal to x <= y : false

These operators are used to compare multiple conditions. And gives the final result based on logical operator used.

In the below table X is *true* and Y is *false*.

Operator Name | Symbol | Description | Example |
---|---|---|---|

Logical and | && | If both operands are true then returns true else returns false |
X && Y give false |

Logical or | || | If either operand is true then returns true else returns false |
X || Y give true |

Logical not | ! | Changes true to false and vice versa |
!X give false |

/* This is a simple Java program about Logical Operators. Call this file KH_LogicalOperators.java. */ public class KH_LogicalOperators { // A Java program begins with a call to main(). public static void main(String args[]) { boolean x = true; boolean y = false; System.out.println("Logical And x && y = " + (x && y)); System.out.println("Logical Or x || y = " + (x || y)); System.out.println("Logical Not !x = " + (!x)); } }

Download the code Run the code

Logical And x && y = false Logical Or x || y = true Logical Not !x = false

These are the only operators in Java which perform operations on each bit of operand. As we already know every items in computer stores in form of bits either 0 or 1. These operators works on these bits.

Below table shows all bitwise operators. Where X is 45 which is 00101101 and Y is 3 which is 00000011

Operator Name | Symbol | Description | Example |
---|---|---|---|

Bitwise and | & | Returns bit value 1 if both corresponding bits in operands are 1 else return 0 | X & Y give 00000001 which is 1
X = 00101101 Y = 00000011 ------------ X&Y=00000001 ------------ |

Bitwise or | | | Returns bit value 1 if either one corresponding bit in operands is 1 else return 0 | X | Y give 00101111 which is 47
X = 00101101 Y = 00000011 ------------ X|Y=00101111 ------------ |

Bitwise XOR | ^ | Returns bit value 1 if both corresponding bits in operands are not same else return 0 | X ^ Y give 00101110 which is 46
X = 00101101 Y = 00000011 ------------ X^Y=00101110 ------------ |

bitwise compliment | ~ | Changes 1’s to 0 and 0’s to 1 | ~X give 11010010 which is -46
X = 00101101 ------------ ~X= 11010010 ------------ |

left shift | >> | Shits each bit to left by specified number and fills empty left most bits with 0 | X < 2 give 10110100 which is 180
X = 00101101 -------------- X<2= 101101 -------------- X<2= 10110100 -------------- |

right shift/td> | >> | Shits each bit to right by specified number and fills empty right most bits with 0 | X > 2 give 00001011 which is 11
X = 00101101 -------------- X>2= 001011 -------------- X>2= 00001011 -------------- |

/* This is a simple Java program about Bitwise Operators. Call this file KH_BitwiseOperators.java. */ public class KH_BitwiseOperators { // A Java program begins with a call to main(). public static void main(String args[]) { int x = 45; int y = 3; System.out.println("Bitwise And x && y = " + (x & y)); System.out.println("Bitwise Or x || y = " + (x | y)); System.out.println("Bitwise XOR x ^ y = " + (x ^ y)); System.out.println("Bitwise compliment ~x = " + (~x)); System.out.println("Bitwise left shift x << 2 = " + (x << 2)); System.out.println("Bitwise right shift x >> 2 = " + (x >> 2)); } }

Download the code Run the code

Bitwise And x && y = 1 Bitwise Or x || y = 47 Bitwise XOR x ^ y = 46 Bitwise compliment ~x = -46 Bitwise left shift x << 2 = 180 Bitwise right shift x >> 2 = 11

We already used assignment operator ‘=’ in our previous examples, Java provides different shorthand operators with combination of assignment and other operators. Below table lists those operators.

Operator Name | Symbol | Description | Example |
---|---|---|---|

Assignment | = | Assigns right side value to left left side variable. | C = A + B, result A + B assign into C |

Shorthand Addition | += | Combination of assignment and addition. | C += A => C = C + A |

Shorthand Subtraction | -= | Combination of assignment and subtraction. | C -= A => C = C – A |

Shorthand Multiplication | -= | Combination of assignment and multiplication. | C *= A => C = C * A |

Shorthand Division | -= | Combination of assignment and division. | C /= A => C = C / A |

Shorthand Modulus | -= | Combination of assignment and modulus. | C %= A => C = C % A |

Shorthand Left shift | -= | Combination of assignment and left shift. | C <<= A => C = C << A |

Shorthand Right Shift | -= | Combination of assignment and right shift. | C >>= A => C = C >> A |

Shorthand Bitwise AND | -= | Combination of assignment and bitwise AND. | C &= A => C = C & A |

Shorthand Bitwise OR | -= | Combination of assignment and bitwise OR. | C |= A => C = C | A |

Shorthand Bitwise XOR | -= | Combination of assignment and bitwise XOR. | C ^= A => C = C ^ A |

In the next lesson we will discuss precedence of operators, reading input from user and one practical example which uses what we learnt till now.