unterschied double.valueof(str) und double.parsedouble(str)

Status
Nicht offen für weitere Antworten.

dermoritz

Bekanntes Mitglied
gibt es irgendeinen unterschied zwischen den 2 methoden. java doc verweist von parsedouble auf valueof ("see also"). also mich interessiert ob sie identisch sind oder ob man eine bevorzugt benutzen sollte.


danke
 
Code:
    /**
     * Returns a <code>Double</code> object holding the
     * <code>double</code> value represented by the argument string
     * <code>s</code>.
     *
     * 

If <code>s</code> is <code>null</code>, then a
     * <code>NullPointerException</code> is thrown.
     *
     * 

Leading and trailing whitespace characters in <code>s</code>
     * are ignored.  Whitespace is removed as if by the {@link
     * String#trim} method; that is, both ASCII space and control
     * characters are removed. The rest of <code>s</code> should
     * constitute a [i]FloatValue[/i] as described by the lexical
     * syntax rules:
     *
 [..]
     * @param      s   the string to be parsed.
     * @return     a <code>Double</code> object holding the value
     *             represented by the <code>String</code> argument.
     * @exception  NumberFormatException  if the string does not contain a
     *               parsable number.
     */
    public static Double valueOf(String s) throws NumberFormatException {
	return new Double(FloatingDecimal.readJavaFormatString(s).doubleValue());
    }
    
    /**
     * Returns a new <code>double</code> initialized to the value
     * represented by the specified <code>String</code>, as performed
     * by the <code>valueOf</code> method of class
     * <code>Double</code>.
     *
     * @param      s   the string to be parsed.
     * @return the <code>double</code> value represented by the string
     *         argument.
     * @exception NumberFormatException if the string does not contain
     *            a parsable <code>double</code>.
     * @see        java.lang.Double#valueOf(String)
     * @since 1.2
     */
    public static double parseDouble(String s) throws NumberFormatException {
	return FloatingDecimal.readJavaFormatString(s).doubleValue();
    }
 
Die eine liefert ein double, die andere ein Double zurück. Siehe API-Dok. Sonst sind sie identisch. Siehe Quelltext.
 
aha, das komische ist in meinem fall hab ich eine variable vom typ Double. es funtionieren aber beide methoden:

Code:
Double time=null;
es funktioniert sowohl:
Code:
time=Double.valueOf(string);
als auch:
Code:
time=Double.parseDouble(string);

heißt das das in beiden fällen eh ein object erzeugt wird und es hier völlig egal ist?

Wofür braucht man eigentlich den primitiven typ double? ist Double nicht imer besser wegen der vielen methoden usw.?
 
Das nennt sich Autoboxing.

Und Double ist meistens schlechter als double: benötigt mehr Speicher und muss für jede Rechnung ausgebackt (in einen double umgewandelt) werden.

Beispiel: das hier
Code:
Double a, b, c;
a = b + c;
... würde vom Compiler in sowas übersetzt werden:
Code:
a = Double.valueOf( b.doubleValue() + c.doubleValue() );
Wären a, b und c "double"s, dann müssten da keine Methoden aufgerufen und Objekte erzeugt werden.
 
Status
Nicht offen für weitere Antworten.

Neue Themen


Zurück
Oben