Is concatenating with an empty string to do a stri

2019-01-11 12:30发布

Let's say I have two char variables, and later on I want to concatenate them into a string. This is how I would do it:

char c1, c2;
// ...

String s = "" + c1 + c2;

I've seen people who say that the "" + "trick" is "ugly", etc, and that you should use String.valueOf or Character.toString instead. I prefer this construct because:

  • I prefer using language feature instead of API call if possible
    • In general, isn't the language usually more stable than the API?
    • If language feature only hides API call, then even stronger reason to prefer it!
      • More abstract! Hiding is good!
  • I like that the c1 and c2 are visually on the same level
    • String.valueOf(c1) + c2 suggests something is special about c1
  • It's shorter.

Is there really a good argument why String.valueOf or Character.toString is preferrable to "" +?


Trivia: in java.lang.AssertionError, the following line appears 7 times, each with a different type:

    this("" + detailMessage);

9条回答
手持菜刀,她持情操
2楼-- · 2019-01-11 12:41

In my opinion, "" + x is very readable, short, and precise to the point. I'd prefer it to longer constructs like String.valueOf. The behavior is well defined and it's so commonly in use that I find it hard to call it a hack at all.

The only thing I'd be slightly worried about is performance - and am very positive that it does not matter usually (even though I did not measure or look at the binary). There is also a fair chance that this kind of concat is optimized away, since it should be easy to detect it (this is just a guess though).

查看更多
甜甜的少女心
3楼-- · 2019-01-11 12:45

What about

new String(new char[] {a, b})

and if you do it alot you could create a class "Strings" with:

public static String valueOf(char... chars) {
    return new String(chars);
}

Your line would then read

String s = Strings.valueOf(a, b);

Nice and short.

Edited

A better name might be:

String s = Chars.asString(a, b);
查看更多
闹够了就滚
4楼-- · 2019-01-11 12:46

I think that in "" + var the + is actually overloaded to make the conversion:

The Java language provides special support for the string concatenation operator ( + ), and for conversion of other objects to strings. String concatenation is implemented through the StringBuilder(or StringBuffer) class and its append method. String conversions are implemented through the method toString, defined by Object and inherited by all classes in Java. For additional information on string concatenation and conversion

So no difference and no problem from a technical point of view.

Form a readability point of view - it's a matter of personal preference or agreed coding style within the team.

查看更多
贼婆χ
5楼-- · 2019-01-11 12:50

The "" + var style has one very big issue in my opinion. It just uses the toString method of the var. So if you change the type of var to something else, you will not get an exception. A nice example I just encountered was that the type was changed from int to Optional<Integer>. The code still compiles fine, but you get a completely different result.

查看更多
Lonely孤独者°
6楼-- · 2019-01-11 12:51

The problem with that construct is that it usually doesn't express the intent.

It represents concatenation of a String with another value, but concatenation is not usually the goal of this line.

In the specific case that you demonstrated, concatenation is actually the goal, so this code does express the intent.

In the more common use of this approach (String s = "" + intValue;), the concatentation is merely a tolerated side-effect, while the conversion of intValue is the actual goal. And a simple String.valueOf(intValue) expresses that intent much clearer.

查看更多
戒情不戒烟
7楼-- · 2019-01-11 12:52

Unless your app needs every ounce of performance, write the code that's quicker to write and easier to read. "" + is a slower-to-execute syntax, but it certainly seems easier to read every time I've used it.

查看更多
登录 后发表回答