2

While I'm executing this code, writing + is converting my integer into a String.

public class Test { public static void main(String...string){ int m=9; int k=10; String s=new String(m +"");//automatic type conversion from int to string String j=m+"" +k;////automatic type conversion from int to string System.out.println(s+j); String s1=String.valueOf(m); System.out.println(s1); } } 

I'm unable to understand what + is doing here, and how it's getting converted into a String. Does this have something to do with the right to left precedence of the = operator?

1
  • it is operator overloading concept...... Commented May 16, 2015 at 9:27

2 Answers 2

7

Does this have Got something to do with precedence with right to left of = operator ?

Answer: No

And it's has got nothing to do With Integer Type too. Why ? because Here is what JSL say

String conversion applies only to an operand of the binary + operator which is not a String when the other operand is a String.

In this single special case, the non-String operand to the + is converted to a String (§5.1.11) and evaluation of the + operator proceeds as specified in §15.18.1.

So even if you write any other type variable it will convert it Consider this snippet

public static void main(String...string){ double u=9.0; System.out.println(u+"hi"); } 

It gives me output

9.0hi 

Now Coming to How ?

For the code snippet that i posted Here is the part of compiled code of this

public static void main(java.lang.String...); flags: ACC_PUBLIC, ACC_STATIC, ACC_VARARGS Code: stack=5, locals=3, args_size=1 0: ldc2_w #16 // double 9.0d 3: dstore_1 4: getstatic #18 // Field java/lang/System.out:Ljav a/io/PrintStream; 7: new #24 // class java/lang/StringBuilder 10: dup 11: dload_1 12: invokestatic #26 // Method java/lang/String.valueOf :(D)Ljava/lang/String; 15: invokespecial #32 // Method java/lang/StringBuilder. "<init>":(Ljava/lang/String;)V 18: ldc #35 // String hi 20: invokevirtual #37 // Method java/lang/StringBuilder. append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 23: invokevirtual #41 // Method java/lang/StringBuilder. toString:()Ljava/lang/String; 26: invokevirtual #45 // Method java/io/PrintStream.prin tln:(Ljava/lang/String;)V 

So internally it invokes valueOf() method to convert double or non-string operand to String and than invokes append() to convert it into String totally . Hope this helps you :)

Sign up to request clarification or add additional context in comments.

Comments

0

I want to make the answer more simpler hence I am posting this.

'+' is the only operator in java that is overloaded for String type for string concatenation.

So if any one of the two operands is String.. the whole operation is overloaded for concatenation.

So string+(any other datatype) = string. So for any other operands use parentthesis along with string

So 5+5+"" =55

and (5+5)+"" = 10

NOTE: It has nothing to do with right to left precedence

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.