Operators In Java (ALL) In A Simple Guide

There are many operators in java programming language. You will learn what they are and how to use them on this page.

Let’s get started.

Shorthand Operators In Java

Shorthand allows you to write an arithmetic statement in shorter form.

You will agree that using an example is one of the best ways to explain programming.

So let’s start the explanation of shorthand in java from a related example:

public class Shorthand
               public static void main (Strings[] args)
                               int k = 3;
                               k = k + 5;

What you see in those three statements is that I declared an integer variable ‘k’.

After then, I assigned value 3 to it.

In the second statement, I equate ‘k’ to be ‘k + 5’, which is 3 + 5.

Lastly, the last statement prints out the final value of ‘k’.

Now, this is where shorthand operators in java come in.

Instead of repeating ‘k’ twice in the second statement, ‘k = k + 5’, you can just write:

k += 5;

And that is the same as: ‘k = k + 5’.

Shorthand allows you to write an arithmetic statement in shorter form.

‘+=’ in ‘k += 5’ is the shorthand in the statement.

A similar example will be:

k *= 5; // which is the same as k = k * 5;

And the two will give the same result.

k -= 5; // k = k – 5;

I hope you now understand the basics of shorthand operator in java.

But wait, you might ask this question…

How Is Shorthand Operator Different From Normal Operator In Java

Again, let’s see an example:

byte c = 3;
c = c* 2.5;

The program above will give you an error even though it looks perfect.

Maybe not so perfect but to some extent.

Why are we having an error?

Simple answer:

Because byte cannot store a real number.

Real numbers are the numbers with a decimal point.

The first line of the program declared ‘c’ as a byte data type and byte takes on integer numbers.

ALSO READ ABOUT  A Java Maximum And Minimum Program

This difference in data type is what leads to the error.

So what is the solution?

Use shorthand operator.

Try this similar statement:

c *= 2.5;

You might wonder why you got an answer this time because you didn’t specify another data type. This is one of the benefits of shorthand operators in java.

c = c* 2.5;

The statement above will calculate the right answer, 7.5, which is a real number.

Then it will try to store this real number into ‘c’ which can only accept integer number.

The error will occur as a result of that.


c *= 2.5;

will give the same result, 7.5 and it is a real number but because we used shorthand in that statement, the program will store the result as the same data type with the left-hand side part of the statement.

In that case, byte. And the result will be 7.

Let’s go to the next operators in java.

Binary Literal in Java

Check out the result of the following program:

public class Binary
               public static void main (Strings [] args)
                               int j = 7;

I equate ‘j’ to 7, and then I print out ‘j’.

You know that 7 will be the result, right?

What if I give the program a binary number that equals 7 and I want the output to be the decimal number?

Sound confusing? Just hold on a sec.

Try this:

int j = 111;

Surely the result will be 111.

But I can also make the value of ‘j’ to be binary by adding 0B or 0b just before 111 above.

So the statement will look like this:

int j = 0B111;

The answer will be 7 instead of 111 because I equate ‘j’ to be the binary literal of 111.

ALSO READ ABOUT  Variables In Java (Including If & Operators)

Here is a quick tip on how to convert binary into decimal…

Put the increment of 1, 2, 4, 8, 16.. at the top of the binary number (0s and 1s), then add up all the increment that have 1 as they binary digit.

Here’s an example:

4 2 1
1 1 1

You will add up 4 + 2 + 1 because you have all 1s as the binary number.

And that is how the program got 7.

Another example is this:

64 32 16 8 4 2 1
1 0 1 1 0 0 1

Now you will add all the numbers that have 1 as their binary digit.

64 + 16 + 8 + 1 = 89

So the binary number 1 0 1 1 0 0 1 will give decimal number 89.

Please review the above explanation again if you still don’t get the binary to decimal conversion.

It is very simple after you get it. Trust me.

Now let’s try the same conversion in java:

int a = 0B1011001;

The answer is 89. Simple huh?

It is the nature of binary numbers to be very long.

For example:

When you try to 10000000000 to decimal.

The answer is 1024 but these digits are many and someone can easily make mistake with while writing them.

So to make such digits easy to count java 8 provides the use of underscore (_) to separate the digits.

int a = 0B100_00_00_00_00;

The result will still remain the same. 1024.

Next is post and pre-increment in java.

Post And Pre Increment in Java

When the variable precedes the increment, like ‘a++’, it is called post-increment.

And when the increment precedes the variable, like ‘++a’, it is called pre-increment.

Take a look at the example below:

public class Increament
public static void main (String[] args)
	int b = 4;
	b =b + 1;

Since 4 + 1 = 5, the result of ‘b’ will be 5.

First I equate ‘b’ to be 4. And in the second statement, I made ‘b’ to be =’b + 1’.

The last statement prints out the value of ‘b’.

ALSO READ ABOUT  How To Swap The Values Of Two Variables In Java

You can write the second statement in a simpler way using shorthand like this:

b += 1; //same as b = b + 1;

‘b += 1’ tells the program to add an increment of 1 to ‘b’.

You can use ‘b++’ too to get the same result.

int b = 4;

Your answer will still be 5.

‘b++’ simply means 1 should be added to ‘b’.

Check out a little different program below.

int a = 4;
int b = a;

The result will be 4 because I’ve assigned 4 to ‘a’. And ‘a’ is assigned to ‘b’.

See this:

int a = 4;
Int b = a++;

You will think that the statement above will give 5 as the answer.

But no, it doesn’t work like that.

You will still get 4 because ‘a++’ is two parts.

The variable part ‘a’ and the increment part ‘++’.

When the variable precedes the increment, it is called post-increment.

And when the increment precedes the variable, like ‘++a’, it is called pre-increment.

So what made ‘int b = a++;’ to give the wrong answer is that the value of ‘a’ has been assigned to ‘b’ before the increment occur.

What happened is that ‘a ’ has been assigned to ‘b’ before the increment took place.

This is called a post-increment.

However, increment before the assignment which is called pre-increment will do the work here.

So instead of ‘a++’, you should use ‘++a’.

int a = 4;
int b = ++a;

And ‘b’ will be 5 because you have increased ‘a’ before assigning it to ‘b’.

Another example:

int m = 8;
m = ++m;

Our result will be 9.

Don’t make the mistake that ‘m’ will be 9 if you use ‘m = m++;’. No. In the second statement notice that at the right-hand side, ‘m’ is assigned to the left-hand side before the increment so the answer will still be 8.

Leave a Reply

Your email address will not be published.