Is conversion to String using ("" + <int value>) bad practice?

Viewed 5184

Is conversion to String in Java using

"" + <int value>

bad practice? Does it have any drawbacks compared to String.valueOf(...)?

Code example:

int i = 25;
return "" + i;

vs:

int i = 25;
return String.valueOf(i);

Update: (from comment)

And what about Integer.toString(int i) compared to String.valueOf(...)?

8 Answers

I wonder what is best for static final variables contributing to compile-time constants:

public static final int VIEW_TYPE_LABEL_FIELD = 1;
public static final int VIEW_TYPE_HEADER_FIELD = ;

...

List <String[]> listViewInfo = new ArrayList<>();

listViewInfo.add(new String[]{"Label/Field view", String.valueOf(VIEW_TYPE_LABEL_FIELD)});
listViewInfo.add(new String[]{"Header/Field view", "" + VIEW_TYPE_LABEL_FIELD});

The compiler can potentially replace the String expressions with a constant. Is one or the other more recognizable as a compile-time constant? Maybe easier for the ("" + ..) construct?

Related