In my coding various places i need to change integer values to string values. To convert the cast integer to string i use concatenation with “” to integer.
i found an another way that is using String.parseInt(..); method.
my question is i do not know which is optimized method to do casting in java and how it is optimized?. is there any other way to cast except my code?
my sample code:
int total = mark1 + mark2;
String str_total = ""+total; // currently doing.
…….
String str_total = String.parseInt(total); // i am planning to do.
You can also use –
OR
Use Integer rahter than int in your code and then use toString() on Integer like
In your code –
String str_total = “” + total;
Actually you are creating 2 new string objects first for
""and secondstr_totalbut in my code only
one new string objectwill be created.Implementation of valueOf in String class is as follows –
here toString will create a new String object