Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

This question already has an answer here:

String is a special case in Java. It's a class, which I can examine in the source code, but it also has its own infix operator +, which seems to be syntactic sugar for StringBuilder.

For example,

"Hello " + yourName;

could become

new StringBuilder().append("Hello ").append(yourName).toString();

There are no user-defined operators in Java, so where is + specified for String?

Could the same mechanism be used to make additional operators, such as for vectors?

share|improve this question

marked as duplicate by dan04, Daniel Pryden, The Guy with The Hat, hichris123, nhahtdh 2 hours ago

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
No. It could not. –  Boris the Spider 18 hours ago
5  
Where is + specified for int? –  gronostaj 18 hours ago
    
@gronostaj - + is recognised by the compiler as well. int i= 2+3 will be replaced by int i=5. In case the value of int cannot be determined at compile time, then there are instructions like iadd which are used in place of + –  TheLostMind 17 hours ago
7  
@TheLostMind I know. OP asked "where is + specified for String?", so I've tried to make him think why would the type matter. –  gronostaj 16 hours ago

5 Answers 5

up vote 40 down vote accepted

+ is implemented in java compilers. The compiler replaces String + String with either compile time constants or StringBuilder code. Note that this applies to primitives too. i.e, int i=1+2 could get directly replaced to int i=3 during compilation itself.

share|improve this answer
2  
Do you have a link to the compiler source where this happens? –  sdgfsdh 21 hours ago
2  
@sdgfsdh - I could show you byte code . Would that suffice?. You will be able to see that + is NOT going as part of byte code –  TheLostMind 21 hours ago
2  
@aishu - No. JVM usually doesn't come into picture here. Compiler does this stuff –  TheLostMind 21 hours ago
1  
Or with StringBuffer :) It varies as per compiler implementation. Upvote from my end. –  sᴜʀᴇsʜ ᴀᴛᴛᴀ 21 hours ago
6  
As you asked for a link to compiler source, here's the Jikes compiler implementing + for Strings –  Stuart Caie 12 hours ago

You can check with specification. The compiler have the implementation of it, not the Java source code.

Java Language Specification- 15.18.1. String Concatenation Operator +

An implementation may choose to perform conversion and concatenation in one step to avoid creating and then discarding an intermediate String object. To increase the performance of repeated string concatenation, a Java compiler may use the StringBuffer class or a similar technique to reduce the number of intermediate String objects that are created by evaluation of an expression.

It shows the evidence that the implementation is depends on the compiler.

share|improve this answer
1  
Yes. java compiler may use.. But most compilers do. :) –  TheLostMind 21 hours ago

Java compilers have the implementation for +. The Javadocs says:

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. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

You can try to check this:

public static void main(String[] args) {
    String s1 = "s1";
    String s2 = "s2";
    String s3 = s1 + s2;
    String s4 = new StringBuilder(s1).append(s2).toString();
}

The above code generates the same bytecode for + and when using StringBuilder:

L0
LINENUMBER 23 L0
LDC "s1"
ASTORE 1

L1
LINENUMBER 24 L1
LDC "s2"
ASTORE 2

// s3 = s1 + s2

L2
LINENUMBER 25 L2

NEW java/lang/StringBuilder
DUP
ALOAD 1
INVOKESTATIC java/lang/String.valueOf(Ljava/lang/Object;)Ljava/lang/String;
INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
ALOAD 2
INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;

ASTORE 3

// s4 = new StringBuilder(s1).append(s2).toString()

L3
LINENUMBER 26 L3

NEW java/lang/StringBuilder
DUP
ALOAD 1
INVOKESPECIAL java/lang/StringBuilder.<init>(Ljava/lang/String;)V
ALOAD 2
INVOKEVIRTUAL java/lang/StringBuilder.append(Ljava/lang/String;)Ljava/lang/StringBuilder;
INVOKEVIRTUAL java/lang/StringBuilder.toString()Ljava/lang/String;

ASTORE 4
L4
LINENUMBER 27 L4
RETURN
share|improve this answer

While currently most of Java compilers using StringBuilder chain, it's not specified that it should be always in this way. In particular there's a proposal to change this drastically in Java-9 replacing with single invokedynamic call and introduce new metafactory which will generate an appropriate MethodHandle in runtime to perform concatenation.

share|improve this answer
    
I wonder why Java doesn't static concatenation methods which take two, three, or four String instances, and also one that takes or else a string[], and in any case returns a new string containing the concatenation of all the strings in question? Such functions could be in every case more efficient than code which actually uses StringBuilder. The only way I can see StringBuilder as being even remotely reasonable would be if most versions of the JIT peep-hole optimize certain usage patterns to invoke internal functions of the indicated form. –  supercat 11 hours ago
    
@supercat, are you looking for String.join("", String...)? Note that StringBuilder can accept not only strings, but primitive values like int, double, boolean and so on. And during String concatenation they are not boxed, nor separate strings are created: their values are concatenated directly into the buffer. How would you imagine to solve this by static methods? The number of possible combinations is too big. –  Tagir Valeev 7 hours ago

As for the language, no, it's not extensible. This is specific behavior, no other class extends the + operator.

As for where this is done, search for string_add (not a real JVM op) in the following files:


As to why, Java was supposed to be simple, or at least simpler than C++, and a basic thing like string manipulation was kind of mandatory. However, operator overloading would add complexity.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.