#Why one works and the other doesnt

1 messages · Page 1 of 1 (latest)

timber cedar
#

Was curious to why doing x+=y works in this case and doing x = x + y doesnt, shouldnt they be the same

balmy garnetBOT
#

This post has been reserved for your question.

Hey @timber cedar! Please use /close or the Close Post button 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.

timber cedar
#

shouldnt they do the same

#

even when i just put one = it keeps saying

#

no statement

west citrus
#

sum == sum + something

#

you compare, not assign

timber cedar
#

wdym

#

even when I just put one equals

west citrus
#

You used == instead of =

timber cedar
#

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

#

?

west citrus
#

depends on what type is x and y

timber cedar
#

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

west citrus
#

x is casted to double, then x+y, then cast to integer

timber cedar
#

ok ty

west citrus
#

/run java ```java
int a = 5;
double b = 6.2;

    a += b;
    System.out.println(a);
tight forgeBOT
#

Here is your java(15.0.2) output @west citrus

11
west citrus
timber cedar
#

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

west citrus
#

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

timber cedar
#

oh interesting

#

I will give that a read

#

tysm alexander

#

helpful as always

#

!

gritty stream
#

alexander the goat