Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator overloading in Java

Tags:

java

Acordding to my knowledge in java I know, that there is no operator overloading in the Java language. So, why this code prints 'true' twice ?

    String s1 = "abc";
    String s2 = "abc";
    System.out.println(s1==s2);

    Integer i1 = 1;
    Integer i2 = 1;
    System.out.println(i1==i2);
like image 337
Oz Molaim Avatar asked Mar 14 '11 15:03

Oz Molaim


People also ask

What is operator overloading in Java with example?

The overloading is done internally in java. We can take + (plus) for example: int a = 2 + 4; string = "hello" + "world"; Here, plus adds two integer numbers and concatenates two strings. So we can say that Java supports internal operator overloading but not user defined.

What is overloaded operator in Java?

Operator overloading is a technique by which operators used in a programming language are implemented in user-defined types with customized logic that is based on the types of arguments passed.

What is operator overloading with example?

This means C++ has the ability to provide the operators with a special meaning for a data type, this ability is known as operator overloading. For example, we can overload an operator '+' in a class like String so that we can concatenate two strings by just using +.

Is operator overloading is possible in Java?

Java doesn't allow user defined operator overloading because if you allow programmer to do operator overloading they will come up with multiple meanings for same operator which will make the learning curve of any developer hard and things more confusing and messing.


3 Answers

== for reference types compares the references; == for primitive types compares values. In case of your first example, the two object references turn out to be the same due to a concept known as string pool. Hence two true in the given case. Another code snippet you might want to try out:

String s1 = "abc";
String s2 = new String("abc");
System.out.println(s1 == s2);
System.out.println(s1.equals(s2));

As you must have already tried out; it prints out false and then true. The reason for this is that using the new keyword results in the creation of a completely new string even though a string object with the exact same contents already exists in the string pool. In this case, s1 now points to an interned string with the contents "abc" (or to a string in the string pool) whereas s2 now points to a completely new string object (again with the content "abc"). Hence the false in the first print statement.

In the second print statement, what we are doing is comparing the contents of the String object rather than its reference, which as it should prints true.

This is one of the most common mistakes made by beginners of the Java language; they use == for logical comparison when it actually results in a reference comparison. Read the link posted in one of the answers here for more details about string pooling. On a related note, String class "overrides" the equals method of the Object class to provide a logical comparison. Unless the class you write doesn't provide a logical implementation of the equals method, it doesn't matter whether you call equals or use the == operator; the result would be the same i.e. reference comparison.

For a more in-depth view on equality, read Brian's article; an excellent read.

like image 190
Sanjay T. Sharma Avatar answered Oct 06 '22 13:10

Sanjay T. Sharma


It's not entirely true that there is no operator-overloading in Java. There just isn't any custom operator overloading. For example there's some operator-overloading with + which adds as both addition and as String-concatenation. This is defined by the language and can't be modified by the developer.

Your example, however doesn't use operator overloading anywhere. == on reference types always does the same thing: return true when the left side and the right side refer to the exact same object.

In this case s1 and s2 reference the same object and i1 and i2 reference the same object as well.

  • s1 and s2 reference the same interned String, because string literals are guaranteed to be interned.
  • i1 and i2 reference the same cached Integer because auto-boxing will re-use a fixed pool of Integer objects for common numeric values.
like image 32
Joachim Sauer Avatar answered Oct 06 '22 14:10

Joachim Sauer


You don't get to overload operators, but that doesn't mean that it's not built into the JVM itself. The obvious counter example is the plus operator and the different behavior for String and numbers.

like image 27
duffymo Avatar answered Oct 06 '22 15:10

duffymo