How does Increment operator work in Java?

How does Java overload operator "+" connect two strings? How does that invisible part work?

  • Answer:

    Vinay's example suffers from the fact that the compiler is going to optimize out the calls that it generates, so we can't see what it's really doing.  Here's a slightly different example that won't get optimized out and lets us see what's really happening. Here's the code: public class Test { public static String dummy (int x, int y) { return x + " + " + y + " = " + (x + y); } public static void main (String[] args) { System.out.println( dummy (1, 2) ); } } And here's the disassembly of the class file generated (I've only quoted the "dummy" method as that's where we need to look: public static java.lang.String dummy(int, int); Code: 0: new #16 // class java/lang/StringBuffer 3: dup 4: iload_0 5: invokestatic #18 // Method java/lang/String.valueOf:(I)Ljava/lang/String; 8: invokespecial #24 // Method java/lang/StringBuffer."<init>":(Ljava/lang/String;)V 11: ldc #27 // String + 13: invokevirtual #29 // Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer; 16: iload_1 17: invokevirtual #33 // Method java/lang/StringBuffer.append:(I)Ljava/lang/StringBuffer; 20: ldc #36 // String = 22: invokevirtual #29 // Method java/lang/StringBuffer.append:(Ljava/lang/String;)Ljava/lang/StringBuffer; 25: iload_0 26: iload_1 27: iadd 28: invokevirtual #33 // Method java/lang/StringBuffer.append:(I)Ljava/lang/StringBuffer; 31: invokevirtual #38 // Method java/lang/StringBuffer.toString:()Ljava/lang/String; 34: areturn Decompiled, this is roughly: return new StringBuffer(String.valueOf(x)) .append(" + ") .append(y) .append(" = ") .append(x+y) .toString(); The compiler, internally, uses the java.lang.StringBuffer class to implement string concatenation, and may use any of several different methods in that class along the way to do so.

Kelly Martin at Quora Visit the source

Was this solution helpful to you?

Other answers

An extension to Sagar's comment. String a = b +c; is transformed by the complier to the following statement : String a = new StringBuilder(b).append(c).toString(); As a side note, StringBuffer is a slight variation of StringBuilder in that, it is preferred in cases where synchronisation is not needed (ie) you don't want to share the buffer between threads.

Radhikaa Bhaskaran

The invisible part is that any Java reference type or primitive that appears where a String is expected will be converted or coerced into a String type by calling the toString() method of the class. The String representations are then concatenated using the StringBuilder class. Example: System.out.println( "1 + 2 =" + ( 1 + 2 ) ); The Compiler knows that System.out.println( String arg ) expects a String object. The first expression is converted to a String object by calling the String constructor: String literal = new String( "1 + 2 =" ); The second expression has a value equivalent to "int sum = 1+2;". It is converted to a String type using the wrapper class Integer that has a toString() method: String sum = new Integer(1+2).toString(); After this, both the string objects are concatenated using String builder and the original arguments to System.out.println() are replaced with the result: System.out.println( new StringBuilder( literal ).append( sum ).toString() );

Vinay Bharadwaj

In java Concept of  Operator overloading is removed which was in C++. Operator overloading is way of assigning many task to one operator like "+" can be used for addition, concatenation etc. But in java,methods are used for concatenation. String a="abc"; String b="pqr"; String c=a+b; String Buffer/StringBuilder class is used internally as follows: String c=new StringBuilder(a).append(b).toString(); So, Java is simple and easy to use difficult concepts like operator overloading and pointers are not there. For  more Details Refer following link: http://java.meritcampus.com/t/234/String-buffer-basics http://java.meritcampus.com/t/259/Stringbuilder http://java.meritcampus.com/t/231/Append---string-buffer

Manisha Mulchandani

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.