0

I was just wondering, i've always used the first version but ran into instance of doing the second variation in our old codebase.

Is there any dangers related to second variation ? Are they interchangeable ?

It feels strange to me to convert into String by doing just variable + "";

Thanks!

Clomez
  • 1,410
  • 3
  • 21
  • 41

1 Answers1

1

In Java the strings have the special operator + which is concatenation. If the operands of the concatenation are not strings their respective .toString method is called, and if it is the value is null the "null" string is concatenated. Most modern JITs or even compilers might decide to use a stringbuilder. So the only danger here is that you create extra objects which get discarded. Additionally, this code might be less readible or more confusing.

Dr Phil
  • 833
  • 6
  • 18