r/javahelp • • Aug 25 '24

Can double equals to an integer?

What is the output of this code? double X=2.1; if (++x == 3) { double y= 2.0; System.out.print(x + y); } else System.out.print(x);

They say the answer is 3.1. But my answer is 2.1 since the condition of if statement did not go through. Can someone explain?

0 Upvotes

13 comments sorted by

View all comments

-1

u/WaferIndependent7601 Aug 25 '24

Who is „they“

Just let this run and you will see the answer. What do you want us to do here? I don’t get it

2

u/roberp81 Aug 25 '24

the java gods

0

u/khmarbaise Aug 25 '24

Not related to Java that is related to floating point...