javastringparseint

Is converting "Integer to String" by appending it to double quotes like 'return ("" + 2)' costly operation? If yes, what is alternative?


I have integer status response from a REST API. I need to return string, so I use it like

return "" + statusCode

Is it fine way to do that or is it costly? Or should I use String.valueOf() or is there another alternative?


Solution

  • No need - if you decompile the class file, you'll see that the compiler does this for you anyway. I think this makes the code very readable, however you might want to ask yourself why are you doing this anyway... if you are confident that returning a number as a string from a method is correct, this is a good way to do it, IMO.