1

Can someone explain why Java concatenates strings and integers in the following examples?

public class main {

    public static void main(String[] args) {
        System.out.println("test" + 5);
        System.out.println(5 + "5");
    }

}

And what is the difference from Python's implementation of the + operator.

In Python, this raises a TypeError:

"5" + 5
arshajii
  • 127,459
  • 24
  • 238
  • 287
rok
  • 9,403
  • 17
  • 70
  • 126

7 Answers7

6

What makes you think + should behave in the same way for two different languages? In Java, when you have something like

System.out.println(5 + "5");
55

string concatenation is performed, as you can see. This is outlined by the Java language's specification (see JLS §15.18.1). By contrast, Python doesn't coerce the types like Java (hence the TypeError); in order for string concatenation to occur, both operands of + must be strings, which is why you need

>>> "5" + str(5)
'55'

This is outlined in Python's specification (see Python Language Reference §5.6). There is no universal rule that states + must behave in a specific manner across languages: if I wanted to, I could create a language in which + performs subtraction!

As @Wooble points out, Perl's + behaves in yet another way:

$ perl -e 'print 5 + "5"; print "\n"'
10
Community
  • 1
  • 1
arshajii
  • 127,459
  • 24
  • 238
  • 287
  • It is in the [zen](http://www.python.org/dev/peps/pep-0020/) of python: `In the face of ambiguity, refuse the temptation to guess.` – RickyA Aug 21 '13 at 15:32
0

Simply put, in Java, the + operator is overloaded for Strings. Python obviously doesn't implement that override so you'll need to find out what the concatenation operator is for Python to make the latter case work.

StormeHawke
  • 5,987
  • 5
  • 45
  • 73
0

You get a TypeError because "5" is of type string and 5 is of type int. Like someone said, its just how it was designed. Im guessing in part to prevent confusion. Do you want "5" + 5 to equal 10 or 55?

If you want 55 you can do this by converting the int to a string

'5' + str(5)
von Mises
  • 266
  • 1
  • 6
  • 17
0

If you check docs

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method.

So ,it's java specific

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
0

Basically in Python you have to do the following:

"5" + str(5)

Because, although operator + is overridden for pairs of strings in both Python and Java, in Python for a string and an integer there is no appropriate override, and integers are not implicitly casted to strings when needed. Hence you need to explicitly cast 5 to a string, in order to have strings on both sides ot the concatenation.

damix911
  • 4,165
  • 1
  • 29
  • 44
0

The compiler java converts 5 + "5" into a StringBuilder internally and uses .append(int) to "add" the integer to the String.

F. Geraerts
  • 1,150
  • 17
  • 23
0

In java, if there is a string type in a + b, then + will do concatenation where as in python the + of two different types has not been defined by default. Thus, as suggested, in python, one needs to do str(Int) before concatenation.

MG.
  • 449
  • 6
  • 15