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


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

Re: Type conversions with the empty string

Path csiph.com!aioe.org!.POSTED!not-for-mail
From Arne Vajhøj <arne@vajhoej.dk>
Newsgroups comp.lang.java.programmer
Subject Re: Type conversions with the empty string
Date Mon, 30 Jan 2017 21:27:06 -0500
Organization Aioe.org NNTP Server
Lines 40
Message-ID <o6oslq$1vpf$1@gioia.aioe.org> (permalink)
References <emtpy-string-20170128143107@ram.dialup.fu-berlin.de>
NNTP-Posting-Host PBuS014lhzVyHjLbXmo/ZQ.user.gioia.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.java.programmer:36231

Show key headers only | View raw


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

Back to comp.lang.java.programmer | Previous | Next | Find similar | Unroll thread


Thread

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

csiph-web