Discussion:
Q) Parsing Command-Line Arguments
(too old to reply)
Proud Japanese
2011-08-09 12:28:36 UTC
Permalink
Hello,

I see that there are two ways to parse command line arguments. For
example,

double d1 = Double.valueOf(args[0]).doubleValue() ;
double d2 = Double.parseDouble(args[0]);

Is there a difference between these two options? Personally I think
that the second option is simpler.

Which one is more preferable?

Do both of these options [i.e. Type.valueOf(args[i]).TypeValue() &
Type.parseType(args[i])] work with all of Java's eight basic data
types [i.e.character, boolean, byte, short, integer, long, float &
double]

Regards,
Hayato
markspace
2011-08-09 14:50:50 UTC
Permalink
Post by Proud Japanese
double d1 = Double.valueOf(args[0]).doubleValue() ;
double d2 = Double.parseDouble(args[0]);
Which one is more preferable?
The first one actually returns a Double, not a double. It's equivalent to:

double temp = Double.parseDouble( string );
Double retVal = new Double( temp );

Since you then assign it to a double, you must then extract the double
value again:

double d1 = retVal.doubleValue();

That's three statements in place of one. I'd prefer the second here.
Roedy Green
2011-08-09 16:52:36 UTC
Permalink
On Tue, 9 Aug 2011 05:28:36 -0700 (PDT), Proud Japanese
Post by Proud Japanese
I see that there are two ways to parse command line arguments. For
example,
double d1 = Double.valueOf(args[0]).doubleValue() ;
double d2 = Double.parseDouble(args[0]);
Is there a difference between these two options? Personally I think
that the second option is simpler.
Which one is more preferable?
Those are conversion techniques, nothing to do with parsing the
command line. The library code that calls main does the parse.

See http://mindprod.com/applet/conversion.html

It will show you how to convert any of the basic types into any of the
others. It shows multiple ways sometimes, with notes on the
advantages of each. In general, the one I consider generally best is
at the top.
--
Roedy Green Canadian Mind Products
http://mindprod.com
Most of computer code is for telling the computer
what do if some very particular thing goes wrong.
Loading...