Java + operator and Operator overloading

In this article, we will discuss:
 
– What is Operator overloading ?

– Does Java support operator overloading ?

– Using + as addition operator

– Tricky scenarios of using + as addition operator

– Using + as String concatenation operator

– Tricky scenarios of using + as concatenation operator

 

Java + operator and Operator overloading

 
An operator is said to be overloaded if it can be used to perform more than one functions.
 
The + operator is overloaded in Java.
 
However, Java does not support user-defined operator overloading.

 
The + operator can be used to as an arithmetic addition operator to add numbers. It can also be used to concatenate strings.
 
+ operator overloading in java

 

+ as Addition Operator

 
The + operator can be used as an addition operator to add two numbers.
 
Here is an example :
 

  int b1 =1;
  int b2=2;
    
  int b3 = b1 +b2; 

 
The result obviously is 3.
 
 

Tricky scenarios of using + as addition operator

 
We can use the + operator to add other number types like double, float, long, byte etc.
 
 
What do you think will be the output of below program ?
 

  byte b1 =1;
  byte b2=2;
  
  byte b3 = b1 +b2;
  System.out.println(b3);

 

This program results in a compilation error. But, why ??

 
The error says, the result b1+b2 is an int and can not be converted to a byte.
 

 
To understand this, lets go through :
 

 
Rules for using + operator for addition :

 

If either operand is of type double, the other operand is converted to double and the result is also of type double.

else, if either operand is of type float, the other operand is converted to float and the result is also of type float.

else, if either operand is of type long, the other operand is converted to long and the result is also of type long.
else, both operands are converted to int and the sum is of type int.
 

 
 
Using these rules, the result of b3 = b1 + b2 is an int. So, the code needs to be rewritten as follows :
 

  byte b1 =1;
  byte b2=2;
  
  byte b3 = (byte) (b1 +b2); // Compilation error since the result is an int
  
  byte b4 = 1 + 2;

 
 

+ operator with compile-time constants

 
Could you predict the output of this statement:
 

    byte b4 = 1 + 2;
    System.out.println(b4);

 
No, it does not result in a compilation error.

It prints 3 !!

 
The reason is, the operands 1 and 2 are compile time constants. Therefore, the compiler computes the sum as compile time and replaces 1+2 as 3.
 
So, the expression becomes :
 
byte b4 = 3;
 
 

+ as String Concatenation Operator

 
The + operator can be used to concatenate strings.
 

    String str1 = "Hello";
    String str2 = "World";
    
    String str3 = str1 + str2;
    
    System.out.println(str3);

 
The above program prints :
 
HelloWorld
 
 

Tricky scenarios of using + as String concatenation operator

 

+ operator on mix of strings and numbers

 
The + operator performs addition if both operands are numbers. If one operand is String it performs concatenation.
 

    int str1 = 1;
    String str2 = "World";
    
    String str3 = str1 + str2;
    
    System.out.println(str3);

 
This code prints,
 
1World
 
 
Let’s look at another program.
 
What will be the result of this :
 
    int one = 1;
    String three= "3";
    
    System.out.println(one + 2 + three);

 

This program prints 33, not 123 or 6. Why ??

 

Rules to remember while using + operator on strings and numbers

 

1. If both operands are numeric, + means numeric addition.

2. If either operand is a String, + means concatenation.

3. The expression is evaluated left to right.

 
Since the expression is evaluated from left to right, 1 + 2 is calculated first and then concatenated with 3. So, result is 33.
 

Here is an interesting puzzle on this concept :
 
Java quiz on String concatenation

 

+ operator on String and null

 
What will be the output of following program :
 

    String str1 = "Hello";
    String str2 = null;
    
    System.out.println(str1 + str2);

 
No, it does not print Hello.
 

It prints Hellonull. But, why ??

 
String str2 is null, the compiler replaces it will string “null”.
 
Hence, the result of this concatenation is Stringnull.
 
 

Summary

 
1) + operator is overloaded in java. It can be used as Addition operator for numbers and Concatenation operator for strings.
 
2)

Rules for using + operator for addition :

 

If either operand is of type double, the other operand is converted to double and the result is also of type double.

else, if either operand is of type float, the other operand is converted to float and the result is also of type float.

else, if either operand is of type long, the other operand is converted to long and the result is also of type long.

else, both operands are converted to int and the sum is of type int.
 

3)

Rules for using + operator on strings and numbers :

 

– If both operands are numeric, + means numeric addition.

– If either operand is a String, + means concatenation.

– The expression is evaluated left to right.
 
4) Java does not support user-defined operator overloading.
 
 

You may also like the following posts :

 
4 Different ways to concatenate Strings in java
 
Method Overloading in java
 
Method Overriding and Runtime polymorphism in java

 

Books and References

 
Beginning Java 8 Fundamentals: Language Syntax, Arrays, Data Types, Objects, and Regular Expressions

Beginning Java 8 Language Features: Lambda Expressions, Inner Classes, Threads, I/O, Collections, and Streams

 
 

© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post

One comment

  1. […] You may also like the following posts :   String join() method in Java8   Java + operator and Operator overloading […]

Leave a Reply.. code can be added in <code> </code> tags

%d bloggers like this: