Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #35906 > unrolled thread
| Started by | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| First post | 2016-11-20 08:37 -0500 |
| Last post | 2016-11-28 05:34 -0800 |
| Articles | 8 — 5 participants |
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.
Re: "Double.toString" Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-11-20 08:37 -0500
Re: "Double.toString" Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-11-20 22:49 +0100
Re: "Double.toString" Eric Sosman <esosman@comcast-dot-net.invalid> - 2016-11-21 08:25 -0500
Re: "Double.toString" lewbloch@gmail.com - 2016-11-22 15:10 -0800
Re: "Double.toString" Lothar Kimmeringer <news200709@kimmeringer.de> - 2016-11-24 14:07 +0100
Re: "Double.toString" Arne Vajhøj <arne@vajhoej.dk> - 2016-11-24 11:12 -0500
Re: "Double.toString" lewbloch@gmail.com - 2016-11-27 08:16 -0800
Re: "Double.toString" Eric Douglas <e.d.programmer@gmail.com> - 2016-11-28 05:34 -0800
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-11-20 08:37 -0500 |
| Subject | Re: "Double.toString" |
| Message-ID | <o0s8u2$rj6$1@dont-email.me> |
On 11/19/2016 10:53 AM, Stefan Ram wrote:
> When a course participant used the static method
> »Double.toString«, I said it won't work this way,
> because there is no such static method.
>
> Turned out, a static »toString« method sits there
> fat and happy on the documentation page of the class
> »java.lang.Double«. And it does not even seem to be
> new (no »since 1.8« or some such).
>
> How could I have missed this method all those years?
> Or did I know it once and my memory failed on me?
>
> Have you been aware of this static toString method?
> Is it a "well-known", prominent method to you?
I wouldn't say "prominent," simply because I don't use it much:
Whenever I want Java's string version of a double, I usually want it
in combination with another string. So I write
System.out.println("\u03c0 \u2248 " + Math.PI);
... and let Java fill in the toString() method call. However, I've
always been aware there's a method call involved, hence I've always
been aware of the existence of the called method. (I confess to not
paying much attention to where the method call is made -- it's inside
StringBuilder somewhere, not in my code -- but the fact that the method
exists has always been plain to me.)
FWIW, the method is listed in the oldest Java book I own: O'Reilly
"Java in a Nutshell, Second Edition" (1997, "updated for Java 1.1").
--
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
[toc] | [next] | [standalone]
| From | Lothar Kimmeringer <news200709@kimmeringer.de> |
|---|---|
| Date | 2016-11-20 22:49 +0100 |
| Message-ID | <1u072gaozj278$.dlg@kimmeringer.de> |
| In reply to | #35906 |
Eric Sosman wrote:
> I wouldn't say "prominent," simply because I don't use it much:
> Whenever I want Java's string version of a double, I usually want it
> in combination with another string. So I write
>
> System.out.println("\u03c0 \u2248 " + Math.PI);
>
> ... and let Java fill in the toString() method call.
The resulting code being created by the compiler is quite
more complicated with this rather than doing
Double.toString(Math.PI). Your example isn't that bad, but
the most common way to create a String from a double
myString = "" + myDoubleValue
results in something like this:
myString = new StringBuilder()
.append("")
.append(String.valueOf(myDoubleValue)
.toString();
The number of objects being created is way higher than when
simply calling Double.toString(myDoubleValue). I read about
that in an article once and used the toString()-methods
ever since (makes code more readable IMHO as well).
Cheers, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2016-11-21 08:25 -0500 |
| Message-ID | <o0usjr$vp6$1@dont-email.me> |
| In reply to | #35910 |
On 11/20/2016 4:49 PM, Lothar Kimmeringer wrote:
> Eric Sosman wrote:
>
>> I wouldn't say "prominent," simply because I don't use it much:
>> Whenever I want Java's string version of a double, I usually want it
>> in combination with another string. So I write
>>
>> System.out.println("\u03c0 \u2248 " + Math.PI);
>>
>> ... and let Java fill in the toString() method call.
>
> The resulting code being created by the compiler is quite
> more complicated with this rather than doing
> Double.toString(Math.PI). Your example isn't that bad, but
> the most common way to create a String from a double
> myString = "" + myDoubleValue
> results in something like this:
> myString = new StringBuilder()
> .append("")
> .append(String.valueOf(myDoubleValue)
> .toString();
Almost: The second append() call is just
.append(myDoubleValue)
... and StringBuilder handles the double->String conversion internally.
> The number of objects being created is way higher than when
> simply calling Double.toString(myDoubleValue).
I suppose so. But my point is that I seldom want that particular
string (Java's "warts and all" representation), and that when I do --
in a quick-and-dirty log message, say -- I usually want it along with
a text more substantial than "". So I write
throw new IllegalArgumentException("bad value: " + doubleArg);
instead of
throw new IllegalArgumentException("bad value: "
+ Double.toString(doubleArg));
... and it turns out that the first version is not only easier to type,
but generates three fewer bytes of bytecode! (Wow!!!)
> I read about
> that in an article once and used the toString()-methods
> ever since (makes code more readable IMHO as well).
If you find it easier to read, don't let me change your habits!
But ease of reading, like beauty, is in the eye of the beholder -- and
my eye seems not to be like yours.
--
esosman@comcast-dot-net.invalid
"Nobody ever went broke underestimating the intelligence of the
American public." -- HLM (paraphrased)
[toc] | [prev] | [next] | [standalone]
| From | lewbloch@gmail.com |
|---|---|
| Date | 2016-11-22 15:10 -0800 |
| Message-ID | <9a71ae21-aa0e-4ee6-930f-b7b871a8d7cc@googlegroups.com> |
| In reply to | #35910 |
On Sunday, November 20, 2016 at 1:49:48 PM UTC-8, Lothar Kimmeringer wrote:
> Eric Sosman wrote:
>
> > I wouldn't say "prominent," simply because I don't use it much:
> > Whenever I want Java's string version of a double, I usually want it
> > in combination with another string. So I write
> >
> > System.out.println("\u03c0 \u2248 " + Math.PI);
> >
> > ... and let Java fill in the toString() method call.
>
> The resulting code being created by the compiler is quite
> more complicated with this rather than doing
> Double.toString(Math.PI). Your example isn't that bad, but
> the most common way to create a String from a double
> myString = "" + myDoubleValue
> results in something like this:
> myString = new StringBuilder()
> .append("")
> .append(String.valueOf(myDoubleValue)
> .toString();
>
> The number of objects being created is way higher than when
> simply calling Double.toString(myDoubleValue). I read about
> that in an article once and used the toString()-methods
> ever since (makes code more readable IMHO as well).
The number of objects created is very unlikely to present an issue. The `StringBuilder` will be GCed painlessly, and the HotSpot is likely to optimize away a whole lot of that anyway. Let's not panic about the negligible putative differences in performance and focus on clarity and maintainability of the source.
--
Lew
[toc] | [prev] | [next] | [standalone]
| From | Lothar Kimmeringer <news200709@kimmeringer.de> |
|---|---|
| Date | 2016-11-24 14:07 +0100 |
| Message-ID | <1gcla4rjhg12u.dlg@kimmeringer.de> |
| In reply to | #35934 |
lewbloch@gmail.com wrote:
> The number of objects created is very unlikely to present
> an issue. The `StringBuilder` will be GCed painlessly, and
> the HotSpot is likely to optimize away a whole lot of that
> anyway. Let's not panic about the negligible putative
> differences in performance and focus on clarity and
> maintainability of the source.
IMHO
myString = "" + myDouble
is less readable than
myString = Double.toString(myDouble);
But that's IMHO, YMMV
Cheers, Lothar
--
Lothar Kimmeringer E-Mail: spamfang@kimmeringer.de
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)
Always remember: The answer is forty-two, there can only be wrong
questions!
[toc] | [prev] | [next] | [standalone]
| From | Arne Vajhøj <arne@vajhoej.dk> |
|---|---|
| Date | 2016-11-24 11:12 -0500 |
| Message-ID | <o173hl$jm9$1@gioia.aioe.org> |
| In reply to | #35958 |
On 11/24/2016 8:07 AM, Lothar Kimmeringer wrote: > lewbloch@gmail.com wrote: >> The number of objects created is very unlikely to present >> an issue. The `StringBuilder` will be GCed painlessly, and >> the HotSpot is likely to optimize away a whole lot of that >> anyway. Let's not panic about the negligible putative >> differences in performance and focus on clarity and >> maintainability of the source. > > IMHO > myString = "" + myDouble > is less readable than > myString = Double.toString(myDouble); > But that's IMHO, YMMV I think the concatenation and implicit conversion is very readable (similar constructs are frequently used). But it do bother my aesthetic feelings (I don't like implicit conversion). Arne
[toc] | [prev] | [next] | [standalone]
| From | lewbloch@gmail.com |
|---|---|
| Date | 2016-11-27 08:16 -0800 |
| Message-ID | <d30ebc27-6d36-47f5-b06b-0ab6b1ec27c7@googlegroups.com> |
| In reply to | #35958 |
That is not a decision you based on speculation about performance or GC, nor in contradistinction from a `StringBuilder` idiom, though, is it? So it doesn't pertain to the decision criteria under discussion here, does it? -- Lew
[toc] | [prev] | [next] | [standalone]
| From | Eric Douglas <e.d.programmer@gmail.com> |
|---|---|
| Date | 2016-11-28 05:34 -0800 |
| Message-ID | <d81eb79f-bee8-46b6-912a-fe59fc72b1b8@googlegroups.com> |
| In reply to | #35958 |
On Thursday, November 24, 2016 at 8:07:09 AM UTC-5, Lothar Kimmeringer > > IMHO > myString = "" + myDouble > is less readable than > myString = Double.toString(myDouble); > But that's IMHO, YMMV On simple assignment I'd prefer the myString = Double.toString(myDouble). On method calls, particularly multiple concatenation I favor the + myObject.callMethod(myDouble1 + " / " + myDouble2)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web