Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

October 1, 2014 Categories: Core Java Basics. 1 Comment on Java operators

In this article you will learn about Java operators. You will look at what is an operator, types of operators in Java and an example for each of the Java operators.

Operator: An operator allows the programmer or the computer to perform an operation on the operands. An operand can be a literal, variable or an expression.

Operators can be divided along two dimensions: 1) number of operands on which the operator works and 2) type of operation the operator performs.

Based on number of operands, operators in Java can be divided into three types:

  • Unary operator (works on single operand)
  • Binary operator (works on two operands)
  • Ternary operator (works on three operands)

Based on the type of operation performed, operators are divided into five categories as shown below:

Java operators

 

Arithmetic Operators

Arithmetic operators are frequently used operators in the Java programs. They are used to perform basic mathematical operations like addition, subtraction, multiplication and division.

All the arithmetic operators and an example for each of them is provided below:

Arithmetic-operators

I think that the above table is self explanatory. The modulus (%) operator gives the remainder value of the division.

Let’s focus on the increment (++) and decrement (–) operators. They are both unary operators, means, they operate on a single operand. Based on whether the increment or decrement operator is placed before or after the operand, they are divided into two types: 1) Pre increment or Pre decrement and 2) Post increment or Post decrement. They will look like as shown below:

++a (Pre increment)
– – a (Pre decrement)
a++ (Post increment)
a – – (Post decrement)

Pre and post increment or decrement have different behaviors when used in assignment expressions. Their behavior is explained in the below example:

int a,b;
a = 10;

b = a++;   //a value will be assigned to b and then a will be incremented by 1. So, b value is 10

a = 10;

b = ++a;   //a value is incremented by 1 and then assigned to b. So, b value is 11

The above example only shown post and pre increment. Similar is the behavior for post and pre decrement.

The operators +=, -=, *=, /= and %= are known as shorthand assignment operators. All they do is save you from typing two more extra characters 😉

Consider the following example which shows you how to use the shorthand assignment operator:

int a = 10;
a += 2;   //Same as writing a = a + 2

a += 1;   // Same as writing a++

Similar is the behavior of other compound assignment operators. Just experiment with them 🙂

Arithmetic operators work only on numeric types and characters. The char is originally a sub type of int.

 

Bitwise Operators

Bitwise operators as the name implies works on bits of the value. These operators can be applied on integer types and character type.

Every integer value is represented as a combination of 0’s and 1’s (binary) inside the memory. For example the integer value 18 of type byte is represented in memory as shown below:

0 0 0 1 0 0 1 0

If you want to modify the value at bit-level then you can use the bit-wise operators. All the bit-wise operators and an example for each of them is provided below:

Bitwise-operators

Bitwise logical operators ~, &, | and ^ operate on two bits as per the table shown below:

bitwise logical operators

Read more about bitwise operators here

 

Relational Operators

Relational operators are used to compare two values. The result of comparing two values is always a boolean value true or false. The relational operators available in Java and an example for each operator is shown below:

Relational-operators

 

Relational operators are generally used in control statements which will be explained in another article. Unlike C and C++, true doesn’t refer any positive value other than zero and false doesn’t refer zero. So, writing while(1) to repeat a loop continuously doesn’t work in Java even though it works in C and C++.

 

Logical Operators

Logical operators are used to evaluate two boolean expressions or values and return the resultant boolean (truth) value. All the logical operators and an example for each logical operator is given below:

Logical-operators

 

The logical AND, OR, XOR and NOT work in similar fashion as the bitwise AND, OR, XOR and NOT except that the operands for logical operators are boolean values. The truth table for the logical operators is given below:

Logical-operators-table

 

The short-circuit AND (&&) and short-circuit OR (||) are special operators in Java. Special in the sense, in the case of short-circuit AND (&&), if the left operand evaluates to false, the right operand is not evaluated and is ignored. Similarly for short-circuit OR (||), if the left operand evaluates to true, the right operand is not evaluated and is ignored.

Below is an example which demonstrates the use of short-circuit AND (&&) to eliminate a runtime exception caused due to dividing a number by zero:

if(denom != 0 && num/denom > 10)
{

//code

}

In the above piece of code, if denom !=0 gives false, then the right side expression of && is not evaluated there by eliminating the chance of exception.

 

Assignment Operator

The assignment operator is represented by =. It is used for assigning a value to a variable as shown below:

int a = 10;

 

Conditional Operator

Java provides a special ternary operator that can be used as an replacement for if-then-else selection statement. The conditional operator is represented as ?:

The syntax for using the conditional operator is shown below:

expression 1 ? expression 2 : expression 3

Always expression 1 must evaluate to a boolean value. If the result of expression 1 is true, then expression 2 is evaluated or else if expression 1 is falseexpression 3 is evaluated and the resulting value will be returned. Consider the following example which demonstrates the use of conditional or ternary operator:

int a = 1, b = 1;
int result = (a==1 && b==1) ? 1 : 0;

The value stored in result variable will be 1 as the expression a==1&&b==1 evaluates to true.

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

Excellent website. Lots of helpful information here.
I’m sending it to a few buddies ans additionally sharing in delicious.
And of course, thank you to your effort!

Leave a Reply

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

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me