Operators in Java

In my previous article i explained, how to declare and initialize variables. In this article, i will explain how to perform different operations on them. Java operators are special symbols used to perform specific operations on one, two or three operands and returns the result.

Operator Precedence

Operator Precedence

Above table explores the precedence of operators, which operators have the highest precedence. In above table operators are listed in precedence order. The top most operator have the highest precedence then the lower one. Operators with highest precedence are evaluated first then the operators of the lower precedence. Operators from the same row have equal precedence, when operators of same precedence appears in one statement then a rule must handle it that which one is evaluated first. All binary operators except assignment operator are evaluated from left to right where as assignment operators are evaluated from right to left.
Assignment Operators : One of the most common operators used in java is assignment “=” operator. In java, assignment operator is also used to assign object reference to another object.

Arithmetic Operators : Java provides arithmetic operators to perform addition, multiplication, subtraction and division. Only the operator you might not be familiar is “%” used to compute reminder of two operands by dividing one operand with another one.

Unary Operators :The unary operators require only one operand; they perform various operations such as incrementing/decrementing a value by one, negating an expression, or inverting the value of a boolean.

The increment/decrement operators can be applied before (prefix) or after (postfix) the operand. The code result++; and ++result; will both end in result being incremented by one. The only difference is that the prefix version (++result) evaluates to the incremented value, whereas the postfix version (result++) evaluates to the original value. If you are just performing a simple increment/decrement, it doesn’t really matter which version you choose. But if you use this operator in part of a larger expression, the one that you choose may make a significant difference.

Equality and Relational Operators : 

The equality and relational operators determine if one operator is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use “==”, not “=”, when testing if two primitive values are equal.

Conditional Operators

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit “short-circuiting” behavior, which means that the second operand is evaluated only if needed.

Another conditional operator is ?:, which can be thought of as shorthand for an if-then-else statement. This operator is also known as the ternary operator because it uses three operands. In the following example, this operator should be read as: “If someCondition is TRue, assign the true to condtion. Otherwise, assign the false to condition.”

Comparison Operator instanceOf
The instanceof operator compares an object to a specified type. You can use it to test if an object is an instance of a class, an instance of a subclass, or an instance of a class that implements a particular interface.

The following program, defines a parent class (named A), a simple interface (named X), and a child class (named B) that inherits from the parent and implements the interface.

Comments

  1. By Danial

    Reply

  2. By Raj

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.