You are here:

Java/how string+int is resolved

Advertisement


Question
sir;
when we are writing

System.out.println("as"+x); x is an integer type of variable, then how ii is resolved.
1>x is converted to wrapper type and calls toString method and then added with  string
2>string class has already operator overloading which can take any primitive value  and return a string

which one is true?or is there any other mechanism

(if + operator is overloaded , then where is its definition, in java docs i dont find any operator overloading methods )..

Answer
The + cannot be overloaded by user code. The definition for what happens is listed in the JLS, not the java docs.

Effectively, the code is translated too:
StringBuilder s = new StringBuilder("as");
s.append(Integer.toString(x));
System.out.println(s.toString());


Although each compiler can do it differently as long as the result is a concatenated string. E.g. some compilers might use String.valueOf(x) instead.

Java

All Answers


Answers by Expert:


Ask Experts

Volunteer


Artemus Harper

Expertise

I have a Masters in computer science. I can answer questions on core J2SE, swing and graphics. Please no questions about JSP or J2ME.

Experience

I have experience in Core Java, good background in Java swing/gui, some experience with JNI, Java reflection. Some experience in bio-informatics. Basics in c++ and c#

Organizations
Washington State University

Education/Credentials
MS in Computer Science from Washington State University and a BS in Mathematics and Computer Science from Central Washington University.

©2016 About.com. All rights reserved.