Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Robert Klemme Newsgroups: comp.lang.java.programmer Subject: Re: Type conversions with the empty string Date: Mon, 30 Jan 2017 10:08:30 +0100 Lines: 76 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: quoted-printable X-Trace: individual.net T+TE2XiqjmmqFM5O1S/KCgOrbr8FD0bqTXUN0pvRM9oH6RXb4= Cancel-Lock: sha1:i7Kds8G5PJSbY0tWfFslBaarRdk= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 In-Reply-To: Xref: csiph.com comp.lang.java.programmer:36224 On 30.01.2017 07:39, Joerg Meier wrote: > On Sun, 29 Jan 2017 17:57:55 +0100, Robert Klemme wrote: > >> On 28.01.2017 14:43, Stefan Ram wrote: >>> I'd say that > >>> "".valueOf( 123 ) > >>> is even more robust than > >>> String.valueOf( 123 ) > >>> because the former is still working when a file =C2=BBString.class=C2= =AB >>> is found early enough in the class path. >> What do you mean by that? Why would the second form be less robust? >> This does not make any sense to me as the same class is used. > > Check http://ideone.com/9xXYpi > > System.out.println("" + 123); > System.out.println(String.valueOf(123)); > =09 > ends up printing out: > > 123 > 15129 Your remark about class path mislead me. This is not about class path=20 but about namespaces. Come on. This is a constructed edge case for various reasons: - Although it is of course perfectly legal to implement a class called = "String" in any other package someone who does that would have to have=20 very good reasons to do it - especially since it makes any reference to=20 java.lang.String extremely tedious. - Even more so as it shadows methods in java.lang.String with methods=20 having the same name but incompatible semantics. - PrintStream.println(int) does exist, so would have been the much=20 more reasonable and concise choice which does not fall into this trap. https://docs.oracle.com/javase/8/docs/api/java/io/PrintStream.html#print-= int- Basically you are using a String constant as a shortcut for the full=20 qualified class name java.lang.String. Btw. that class could still have used java.lang.String.valueOf() and=20 avoided the String constant: class String { public static java.lang.String valueOf(int i) { return java.lang.String.valueOf(i * i); } } For me this edge case is so obscure that it does not warrant digressing=20 from what a reasonable programmer would do and to avoid the use of a=20 String constant here. This case does exhibit a interesting detail of the Java compiler in=20 Eclipse Mars 4.5.2: even though invocation of static methods on=20 instances is set to be flagged as warning it does /not/ bark on the line = with "" + 123. That is odd. Kind regards robert --=20 remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/