Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #36224
| From | Robert Klemme <shortcutter@googlemail.com> |
|---|---|
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Type conversions with the empty string |
| Date | 2017-01-30 10:08 +0100 |
| Message-ID | <ef8e4gFgaq3U1@mid.individual.net> (permalink) |
| References | <emtpy-string-20170128143107@ram.dialup.fu-berlin.de> <ef6l8kF5p6bU1@mid.individual.net> <gkienx5o72gf.r7zyv6z3bfpc.dlg@40tude.net> |
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 »String.class«
>>> 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));
>
> ends up printing out:
>
> 123
> 15129
Your remark about class path mislead me. This is not about class path
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
very good reasons to do it - especially since it makes any reference to
java.lang.String extremely tedious.
- Even more so as it shadows methods in java.lang.String with methods
having the same name but incompatible semantics.
- PrintStream.println(int) does exist, so would have been the much
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
qualified class name java.lang.String.
Btw. that class could still have used java.lang.String.valueOf() and
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
from what a reasonable programmer would do and to avoid the use of a
String constant here.
This case does exhibit a interesting detail of the Java compiler in
Eclipse Mars 4.5.2: even though invocation of static methods on
instances is set to be flagged as warning it does /not/ bark on the line
with "" + 123. That is odd.
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Re: Type conversions with the empty string Robert Klemme <shortcutter@googlemail.com> - 2017-01-29 17:57 +0100
Re: Type conversions with the empty string Joerg Meier <joergmmeier@arcor.de> - 2017-01-30 07:39 +0100
Re: Type conversions with the empty string Robert Klemme <shortcutter@googlemail.com> - 2017-01-30 10:08 +0100
Re: Type conversions with the empty string Joerg Meier <joergmmeier@arcor.de> - 2017-02-07 09:26 +0100
Re: Type conversions with the empty string Andreas Leitgeb <avl@logic.at> - 2017-02-07 08:34 +0000
Re: Type conversions with the empty string lewbloch@gmail.com - 2017-02-12 11:20 -0800
Re: Type conversions with the empty string Joerg Meier <joergmmeier@arcor.de> - 2017-02-13 08:40 +0100
Re: Type conversions with the empty string Andreas Leitgeb <avl@logic.at> - 2017-02-13 17:38 +0000
Re: Type conversions with the empty string Andreas Leitgeb <avl@logic.at> - 2017-02-13 17:48 +0000
Re: Type conversions with the empty string Robert Klemme <shortcutter@googlemail.com> - 2017-02-16 10:53 +0100
Re: Type conversions with the empty string lewbloch@gmail.com - 2017-02-19 02:42 -0800
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-19 08:45 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-19 09:20 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-19 09:21 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-19 10:51 -0500
Re: Type conversions with the empty string Robert Klemme <shortcutter@googlemail.com> - 2017-02-19 19:55 +0100
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-19 15:55 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-19 23:31 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 08:39 -0500
Re: Type conversions with the empty string Eric Douglas <e.d.programmer@gmail.com> - 2017-02-20 06:10 -0800
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 09:54 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 10:25 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 11:02 -0500
Re: Type conversions with the empty string Eric Douglas <e.d.programmer@gmail.com> - 2017-02-20 08:45 -0800
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 14:46 -0500
Re: Type conversions with the empty string Eric Douglas <e.d.programmer@gmail.com> - 2017-02-20 12:23 -0800
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 16:31 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 11:58 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 14:46 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 15:45 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 15:54 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 20:31 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-19 09:13 -0500
Re: Type conversions with the empty string Martin Gregorie <martin@address-in-sig.invalid> - 2017-02-19 14:43 +0000
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-19 10:11 -0500
Re: Type conversions with the empty string Martin Gregorie <martin@address-in-sig.invalid> - 2017-02-19 15:20 +0000
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-19 10:53 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-19 23:24 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 08:43 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 10:16 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 11:03 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 12:00 -0500
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-20 14:49 -0500
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-20 15:47 -0500
Re: Type conversions with the empty string lewbloch@gmail.com - 2017-02-20 22:18 -0800
Re: Type conversions with the empty string Jerry Stuckle <jstucklex@attglobal.net> - 2017-02-21 08:23 -0500
Re: Type conversions with the empty string lewbloch@gmail.com - 2017-02-19 02:39 -0800
Re: Type conversions with the empty string Arne Vajhøj <arne@vajhoej.dk> - 2017-02-13 12:46 -0500
csiph-web