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


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

Re: "Double.toString"

From Eric Sosman <esosman@comcast-dot-net.invalid>
Newsgroups comp.lang.java.programmer
Subject Re: "Double.toString"
Date 2016-11-21 08:25 -0500
Organization A noiseless patient Spider
Message-ID <o0usjr$vp6$1@dont-email.me> (permalink)
References <Double-toString-20161119164723@ram.dialup.fu-berlin.de> <o0s8u2$rj6$1@dont-email.me> <1u072gaozj278$.dlg@kimmeringer.de>

Show all headers | View raw


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)

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


Thread

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

csiph-web