Reputation: 33
What does this snippet of code mean?
int value;
if (value > 0)
String input = "" + value;
Upvotes: 1
Views: 936
Reputation: 15418
String input = "" + value;
value
is an integer type. adding it to empty string-""
just making it a string. Suppose value = 3
, then ""+value
will be "3"
Edit: Forgot to mention about String.valueOf(val)
function, another static utility method to convert almost all primitive type to String
.
Upvotes: 2
Reputation: 2346
Compiler know how to add an integer with some string value. So in the code rather calling directly the integer to string conversion method. The coder generates an constant string ""(having no value in it) and then call concatenation operator(+) overloaded method to add and cast the integer into string value.
Upvotes: 0
Reputation: 17051
It is converting value
to a string. "" + value
is very similar to value.toString()
. The ""
means the compiler is looking for a string after the +
, so when it sees value
in that space, it automatically calls value.toString()
to produce the string result.
Upvotes: 10