Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #36231 > unrolled thread

Re: Type conversions with the empty string

Started byArne Vajhøj <arne@vajhoej.dk>
First post2017-01-30 21:27 -0500
Last post2017-01-30 21:27 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.java.programmer

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-01-30 21:27 -0500

#36231 — Re: Type conversions with the empty string

FromArne Vajhøj <arne@vajhoej.dk>
Date2017-01-30 21:27 -0500
SubjectRe: Type conversions with the empty string
Message-ID<o6oslq$1vpf$1@gioia.aioe.org>
On 1/28/2017 8:43 AM, Stefan Ram wrote:
>   There are actually two ways to convert »123« to a string
>   that both involve the empty string:
>
> ""+123
>
>   and
>
> "".valueOf( 123 )
>
>   , and both are frowned upon! But real problems in software
>   enginnering involve architecture or "logical" errors, seldom
>   small-scale wordings. What is the worst thing that could
>   happen when someone uses one of those? (That a beginner reading
>   the source might become confused?)
>
>   I'd say that
>
> "".valueOf( 123 )
>
>   is even more robust than
>
> String.valueOf( 123 )
>
>   because the former is still working when a file »String.class«
>   is found early enough in the class path.

"".valueOf( 123 ) is rather bad. Calling static methods on instances
is bad and may create warnings in many tools.

And one should not chose coding style to defend against mixing up
java.lang classes and user classes. Just avoid doing that.

""+123 is a lot more harmless. I would not use it. But given
how common the construct is with a non-empty string, then I can
see it with the empty string being that bad.

Arne

[toc] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web