the book that im reading says
<< "this is the easy way to convert a numerical value to STRING"?Quote:
"" + <number>
assuiming that the variable number is an integer.
is it what they called explicit conversion?
Printable View
the book that im reading says
<< "this is the easy way to convert a numerical value to STRING"?Quote:
"" + <number>
assuiming that the variable number is an integer.
is it what they called explicit conversion?
No, that's an implicit conversion (technically, it's not a cast at all in this case, see the reason below). An explicit conversion is one you specifically tell Java to do:
Code :int a = 5; String b = (String) a; // explicit
Note: because of the funny implementation of Strings in Java, this cast here will fail (the compiler will complain).
Reason: Java normally doesn't allow operator overloading. However, the + has been over-ridden for Strings to mean concatenation. The String concatenation method is able to take any primitive data type and convert it to a string equivalent value. However, because String is not a primitive data type, you cannot explicitly tell Java you want to cast an int to a String and vise versa.
oh sorry thats what im trying to imply, i just misinterpret the difference between "implicit" and " explicit" conversion..
yah yah, thats what i want to ask.. "implicit" haha,, :) ,
i thought this was "explicit"
[CODE] "" + number[\CODE]
so this is the "implicit"
anyway, so thats how the java manipulate strings, and tnx for the knowledge about "concatenating", every time i concatenate a primitive type i thougt that it still have value (i mean its data type) as it is when you declared it..
like the code above, i thought that the variable "number" is still the data type as when you declared it , but when you concatenate it with strings. it will become string too(and that is "implicit") hehe, thought it was "explicit" ,
i just understood it reversely..
concatenation doesn't really apply to primitives... It means "adding to the end of".
So:
is the same asCode :"Hello" + " world"
Code :"Hello".concat(" world");