Why when i use this:
int a = 1;
methodWithParamString(a + "");
a is cast to String, bu i can't use toString() on integer?
int a = 1;
methodWithParamString(a.toString());
Doesn't this: a+""
works like: a.toString() + ""
?
Why when i use this:
int a = 1;
methodWithParamString(a + "");
a is cast to String, bu i can't use toString() on integer?
int a = 1;
methodWithParamString(a.toString());
Doesn't this: a+""
works like: a.toString() + ""
?
No. It is converted to a String. (You can't cast a primitive type to a String, either implicitly or explicitly.)
The details of this conversion are specified in the JLS - 15.18.1.1. The specification states that for a primitive type, the conversion is done "as if" you created an instance of appropriate wrapper type and then called
toString()
on it. This leaves the Java compiler the option of using other approaches provided that the end result is the same. (For reference types, the conversion turnsnull
into"null"
and other non-String types into String by callingtoString()
.)The JLS states that the concatenation is then performed "as if" by a call to
String.concat(...)
, noting that the JLS explicitly permits optimization of sequences of concatenations. (JLS 15.18.1.2)No, it works like
String.valueOf( a ) + ""
, which in turn behaves likenew StringBuilder( String.valueOf( a ) ).append( "" ).toString()
.The important thing to know is that it's all just done by the compiler, in other words it's syntactic sugar. This is why adding strings together in a loop isn't a good idea for example. (Although modern VMs might have some mechanism to reduce the performance overhead.)
The way this is accomplished in Java is actually a bit simpler than the other answers seem to suggest.
Using the string concatenation operator with a String and an object results in a call to that object's
toString()
method, then performs string concatenation.Remember that whenever a primitive is passed to a context where an Object would be valid, Java performs "autoboxing", converting an
int
to anInteger
, adouble
to aDouble
, etc.So you can think of it as autoboxing followed by toString() followed by string concatenation.
At the same time, you should realize that the JLS string concatenation spec allows JVM implementations to optimize away the autoboxing, provided the result is the same, so your JVM may instead use a StringBuilder, or use any other valid JVM bytecode to create the resulting string, so the performance of the string concatenation operator is often better than performing boxing yourself would be.
No.
int
is a primitive type so it's not an object and can't have any methods.Read this I think it will be helpful. There are wrapper classes for primitives for example
Integer
forint
. You can calltoString()
method for them.Actually
String
is a special class in java. And you can use + (and not only) operator for Strings and primitives. I think you'll find full answer to your question here.toString()
is a method inObject
class, any class that inherits from it will have that method, likeInteger
orString
. Butint
is a primitive type not an Object, so the method does not exist.True. it doesn't.
+
is overloaded internally. and Java doesn't support method calls on primitives.