Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does + work with Strings in Java?

Java can't do operator overloading, but + works okay for String and Integer and some other classes. How is this possible?

update:
Why does this work?

Integer i = 4;
Integer p = 5;

System.out.println(i*p); // prints 20
like image 765
VextoR Avatar asked Mar 28 '11 19:03

VextoR


People also ask

How does works with string Java?

Strings in Java are Objects that are backed internally by a char array. Since arrays are immutable(cannot grow), Strings are immutable as well. Whenever a change to a String is made, an entirely new String is created.

Why does == not work with strings Java?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

Why does == work on strings in Java?

In String, the == operator is used to comparing the reference of the given strings, depending on if they are referring to the same objects. When you compare two strings using == operator, it will return true if the string variables are pointing toward the same java object. Otherwise, it will return false .

Why do we use string in Java?

Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. The Java platform provides the String class to create and manipulate strings.


1 Answers

+ is not an example of operator overloading. + is built into the language as a concatentation operator and an arithmetic-addition operator.

What this means is that a person writing a program with Java cannot overload operators, but as far as the grammar of the Java language is concerned, + is defined as a concatenation and an addition operator.

EDIT

It works for other classes such as Integer and Double because of autoboxing.

If you take a look at the bytecode of a Java program that performs string concatenation, you'll see that it creates StringBuilder and uses the append() method. The Java compiler sees the + operator and realizes that the operands are strings and not primitive types (like int).

If you look at the bytecode of a program that does integer addition, you will see that it uses the iadd instruction to perform integer addition. This is because the compiler realizes that the operands to the + operation are integers.

As far as doing something like Integer i = 4, the bytecode will show that you're actually doing Integer i = Integer.valueOf(4). This is called autoboxing. Later on, when you do something like i + p, where both i and p are of type Integer, the generated bytecode will show that you're doing i.intValue() + p.intValue(), where the return types of both methods are int (the actual bytecode instruction again, is iadd).

This is why + works Integer even though they are not actual primitive types.

like image 197
Vivin Paliath Avatar answered Sep 26 '22 06:09

Vivin Paliath