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() + ""
?
We can convert int to String in java using String. valueOf() and Integer. toString() methods.
No. It is converted to a String. (You can't cast a primitive type to a String, either implicitly or explicitly.)
As was mentioned before, (String) myInt is a typecast. In Java, we can either cast within primitives or upwards in the object-hierarchy.
Converting int to string in C# is used to convert non-decimal numbers to string character. This can be done by using int to string conversion, int to string with Int32. ToString(), int to string with string concatenation, int to string with StringBuilder, int to string with Convert.
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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With