#Why one works and the other doesnt
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @timber cedar! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
shouldnt they do the same
even when i just put one = it keeps saying
no statement
You used == instead of =
same thing
it says not a statement
actually
nvm i know why it happens
which makes me ask the next question
does x+=y
convert x
to whatever y is
?
depends on what type is x and y
if x is a integer
and y is a double
would x = y
x+=y
convert x to a double
I know the other way around wouldnt work because u cant convert a double to a integer
x is casted to double, then x+y, then cast to integer
ahh so u still lose data
ok ty
/run java ```java
int a = 5;
double b = 6.2;
a += b;
System.out.println(a);
Here is your java(15.0.2) output @west citrus
11
you're welcome
do these conversions happen anywhere else in java
will it always try to convert integers to doubles to do a operation and then convert back
not always
it won't let you do this int a = 5.2;
you will have to cast it explicitly
consider also, that there are some rules of math in java exists
like, if you do operations with integer and double, integer will be casted to double, and result will be in double
if short and integer -> short will be casted to integer etc
alexander the goat