Fehler zur compilezeit

hi
dieser java-source-code-fragment

float f = 10.3;
int i = 0;
float ff = f + i;

erzeugt einen fehler zur compilezeit
ich möchte gerne wissen warum für mich sieht alles richtig aus
bitte
raissa

Hallo

float f = 10.3;

Kommazahlen sind für den Compiler immer doubles. Du musst die Zahl erst in ein float parsen:
float f = (float) 10.3;
Oder die Zahl vorher als float definieren:
float f = 10.3f; // bzw.
float f = 10.3F;
Grüße, Cymatoxa

Hallo

float f = 10.3;

Kommazahlen sind für den Compiler immer doubles. Du musst die
Zahl erst in ein float parsen:

was bedeutet parsen ?
aber wenn float f = 10.3 steht bedeutet es nicht dass f schon von typ float ist ?
danke für deine antwort
raissa

Hallo

float f = 10.3;

Kommazahlen sind für den Compiler immer doubles. Du musst die
Zahl erst in ein float parsen:

was bedeutet parsen ?
aber wenn float f = 10.3 steht bedeutet es nicht dass f schon
von typ float ist ?

Parsen = Typumwandlung
Beispiel bei primitiven Datentypen:
double d = 49.6;
int i = (int) d; // ohne das (int) käme es zu einem Fehler
// i ist 49 (es wird in diesem Fall immer abgerundet)
short s = (short) d; // genau das Gleiche
char c = (char) d // selbst das funktioniert
// c ist jetzt ‚a‘, weil der charcode für ‚a‘ 49 ist
Eine wesentlich bessere Erklärung:
–> http://de.wikipedia.org/wiki/Typumwandlung

aber wenn float f = 10.3 steht bedeutet es nicht dass f schon
von typ float ist ?

Dein f ist zwar vom Typ float, die 10.3 aber nicht. Dashalb kann f nichts damit anfangen und es kommt zum Feler.

Grüße, Keks

Typumwandlung = cast
Hallo,

Parsen = Typumwandlung

wobei das normalerweise als cast bezeichnet wird.

Gruß
Heavy