2

In a code base I'm working with I'm seeing this idiom being used.Can someone explain it for me?

new String("" + number) // `i` is an instance of Integer

For some context, this is approximately what the method looks like:

public String someMethod(String numberString) {
    Integer number = new Integer(numberString);
    // other stuff happens...
    return new String("" + number);
}
nicholaides
  • 19,211
  • 12
  • 66
  • 82
  • 6
    I'd say the author was inexperienced with Java. Don't know any reason you'd do that. – William Morrison Jul 07 '14 at 20:27
  • and also there is Integer.toString() :-) – Leo Jul 07 '14 at 20:27
  • Aside from chopping ridiculously long digits into `Integer`s, I can't think of any real use for this. And then why translate it back to a string? – aliteralmind Jul 07 '14 at 20:28
  • 1
    This is just crap. Once the number has been concatenated with a string it does not need to be handed to the constructor of another string. It's very poor programming and you should delete it. – Software Engineer Jul 07 '14 at 20:30
  • Actually, I'm commenting on the method you posted. The `"" + number`, which is equivalent to `new String("" + number)`, is indeed a shortcut to translating an int to a string. Should really be `(new Integer(number)).toString()` or `String.valueOf(number)` – aliteralmind Jul 07 '14 at 20:30
  • 1
    Using `new String(...)` may even avoid the intern pool, and so is less efficient in terms of memory than `"" + number`. Someone please correct me if I'm mistaken here. – William Morrison Jul 07 '14 at 20:30
  • Writer didn't know `String.valueOf(...)`, or it was too much typing for him/her – gpeche Jul 07 '14 at 20:30
  • @WilliamMorrison No magic intern() happen here. It just more verbose as well as slower. Code which doesn't do anything is the hardest to reason about because it doesn't have a reason to be there. – Peter Lawrey Jul 07 '14 at 20:36
  • @PeterLawrey `""+someNumber` doesn't use the intern pool behind the scenes then? – William Morrison Jul 07 '14 at 20:37
  • 1
    @WilliamMorrison only String literals do, unless you changed the code yourself ;) – Peter Lawrey Jul 07 '14 at 20:43
  • Kind of silly to use `new String(someString)` in about 95% of the cases where you see it. – Hot Licks Jul 07 '14 at 22:27
  • 1
    @WilliamMorrison - I don't think the compiler would cast `""+someNumber` into a String literal, but it's pretty silly to turn around and use that value for the argument to a String constructor regardless. – Hot Licks Jul 07 '14 at 22:29
  • @HotLicks thanks, peter corrected me already. I don't know why I ever thought this. – William Morrison Jul 08 '14 at 02:08

3 Answers3

4

It's most likely nothing but an inexperienced Java programmers attempt at converting a number to a String.

Whether converting numbers to Strings using "" + number is good practice is debateable. I for one find String.valueOf(number) to be more clear (although it's semantically equivalent).

It's completely unnecessary to wrap the result in new String(...) unless you (for some unimaginable reason) really need a new string, i.e. one that's referentially distinct from other strings.

Community
  • 1
  • 1
aioobe
  • 413,195
  • 112
  • 811
  • 826
3

This ideom is the easiest way of converting any primitive or Object type to a string.

It is quite easy: Typing "" + number is less work than typing String.valueOf(number) (or Integer.toString(number). The latter is the usual way of transforming an int into a String. Which one is clearer and to be preferred is a matter of taste. You will find advocates for both versions.

But note three things:

  1. Calling String.valueOf(number) is faster, because "" + number will become a string concatenation, it will be compiled to

    new StringBuilder().append("").append(number).toString()
    

    However, unless this statement is in a hot place in your code, the difference will not matter at all. Even in a hot place, the difference might still be negligible

  2. Simply calling number.toString() is also an option since number is a boxed integer. However, if number is null, this will trigger a null pointer exception so be careful!

  3. The last line of your example, i.e., new String("" + number); is simply crap! It wraps the result into a newly built string. This creates an unnecessary copy and costs precious extra keystrokes. It also generates unnecessary boilerplate noise in your code. This last line compiles to:

    new String(new StringBuilder().append("").append(number).toString())
    

    That is quite some work for transforming int to String. You first create a StringBuilder, then a built String and then a copy of this built String. This creates three objects instead of one.

gexicide
  • 38,535
  • 21
  • 92
  • 152
  • 3
    But why `new String(...)` instead of just `"" + number`? – aioobe Jul 07 '14 at 20:28
  • 3
    And `"" + number` doesn't express what you're trying to do nearly as clearly as `number.toString()` or `String.valueOf(number)`, IMO. – Jon Skeet Jul 07 '14 at 20:29
  • 1
    I think `""+number` is still clearer than `((Integer)number).toString();`. Anyways, it's basically `number to String conversion`. As for why they used the explicit constructor for the creation of the String rather than the result of the concatenation, I'm not sure myself. I don't think it should make a difference. – EpicPandaForce Jul 07 '14 at 20:30
  • +1 While `"" + i` is slower for the computer it is faster for the developer which is often worth much more, still clarity is the most important thing and which one you prefer is a matter of taste. – Peter Lawrey Jul 07 '14 at 20:33
  • @aioobe: Edited my answer. The `new String(...)` is crap. – gexicide Jul 07 '14 at 20:34
  • You forgot that the StringBuilder has a `char[]` and this has to be copied to another `char[]` for the String and possibly copied again, though most likely not. – Peter Lawrey Jul 07 '14 at 20:38
  • 3
    @Zhuinden `String.valueOf(number)` is more clear than `"" + number` – Marco Acierno Jul 07 '14 at 20:38
  • 1
    @Zhuinden: Why would you cast it to `Integer` first? No-one has suggested that. – Jon Skeet Jul 07 '14 at 20:39
  • Technically they didn't because the example above already created an Integer object so the casting was not necessary. But if you have a vanilla (primitive) `int` then you have to go all the way with the effort. – EpicPandaForce Jul 07 '14 at 20:42
  • 1
    @aioobe - You are correct in observing that the `new String()` op is totally wasted motion. – Hot Licks Jul 07 '14 at 22:30
0

This idiom is used to automatically convert the integer number to a string.

The String constructor expects an argument of type String. The expression new String(number) would not compile. Concatenting an empty string is a little trick to turn the Integer into a String.

You could of course use new String(number.toString()) or String.valueOf(number) instead.

Note: As Jon Skeet mentions in the comments there is no need to use new Sring() at all. You could just return number.toString(); or return String.valueOf(number)

Dennis Traub
  • 50,557
  • 7
  • 93
  • 108