8

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() + "" ?

MicNeo
  • 716
  • 3
  • 12
  • 20

7 Answers7

15

No, it works like String.valueOf( a ) + "", which in turn behaves like new 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.)

biziclop
  • 48,926
  • 12
  • 77
  • 104
  • Thanks, that is what I asking for :) Do you can give some link to documentation about this? – MicNeo Feb 06 '12 at 11:27
  • 1
    Here: http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.18.1 In 15.18.1.2 it says similar to this answer. – Harry Joy Feb 06 '12 at 11:30
  • 2
    BTW: `String.valueOf(a)` just calls `Integer.toString(a)` – Peter Lawrey Feb 06 '12 at 11:39
  • 1
    Here is proof of the comment above in Java 7: http://hg.openjdk.java.net/jdk7u/jdk7u6/jdk/file/8c2c5d63a17e/src/share/classes/java/lang/String.java#l2948 – Steve Chambers Oct 29 '19 at 21:53
6

a is cast to String

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 turns null into "null" and other non-String types into String by calling toString().)

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)

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

Doesn't this: a+"" works like: a.toString() + "" ?

True. it doesn't.

+ is overloaded internally. and Java doesn't support method calls on primitives.

Azodious
  • 13,752
  • 1
  • 36
  • 71
1

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 an Integer, a double to a Double, 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.

Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28
0

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 for int. You can call toString() 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.

shift66
  • 11,760
  • 13
  • 50
  • 83
  • Ok, I know. But how this: a + "" works? Is implicty used something like: String.valueof(a)? – MicNeo Feb 06 '12 at 11:26
0

Because int a - is not an object, it is primitive type. So it doesn't have any methods. You should use boxing:

Integer objectA = new Integer(a);
objectA.toString();
kornero
  • 1,109
  • 8
  • 11
  • Unless you are not stuck with an outdated version of Java, don't use `new Integer(a)` where a is an int. You should rely on autoboxing or use `Integer.valueOf(a)` since Java internally caches instantiations for primitives like Integer and thus avoids creating unnecessary instances. – Axel Feb 06 '12 at 12:12
0

toString() is a method in Object class, any class that inherits from it will have that method, like Integer or String. But int is a primitive type not an Object, so the method does not exist.

mohdajami
  • 9,604
  • 3
  • 32
  • 53