programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Tim Cooke
• Devaka Cooray
• Ron McLeod
• Jeanne Boyarsky
Sheriffs:
• Liutauras Vilda
• paul wheaton
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Piet Souris
• Carey Brown
• Tim Holloway
Bartenders:
• Martijn Verburg
• Frits Walraven
• Himai Minh

# assignments: too easy?

Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:
Howdy ranchers,

the following fragment is not from a mock exam but easily could be:

What will be the output?

Bu.

Greenhorn
Posts: 6
• Number of slices to send:
Optional 'thank-you' note:
0.0

1 / 2 -> (int / int = 0.)
0 * 0.5 = 0.
[ July 18, 2007: Message edited by: Thitipong Suparurkrat ]

Burkhard Hassel
Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:
But there's a double in the expression. Shouldn't they all become double then?

Bu.

Ranch Hand
Posts: 112
• Number of slices to send:
Optional 'thank-you' note:
I think it will execute like this. Correct me if I am wrong. I have not tried running it .

double d = 1 / 2 * 0.5 ;

First of all , 2*0.5 will be executed. That will result in 1.0 .

Then 1/1.0 will execute. 1 is int and 1.0 is double hence int will be broadened to double and answer will be 1.0

Prints : 1.0

Vivian Josh
Ranch Hand
Posts: 112
• Number of slices to send:
Optional 'thank-you' note:
Now, I tried running in eclipse .

Its giving 0.0

Why's that ? Is it first executing 1/2 as int and then multiplying 0 and 0.5 ?

Ranch Hand
Posts: 424
• Number of slices to send:
Optional 'thank-you' note:

output: 0.25 (predicted output)

So I agree with Thitipong 1/2 int/int got excuted first result in 0 then got multiplied by 0.5

Ranch Hand
Posts: 377
• Number of slices to send:
Optional 'thank-you' note:
Hi,

evaluation order is always from left to right.

Burkhard Hassel
Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:
Manfred posted

evaluation order is always from left to right.

System.err.println(1 / 2 * 0.5);
System.out.println(0.5 * 1 / 2);

prints
0.0
0.25

Bu.

Ranch Hand
Posts: 169
• Number of slices to send:
Optional 'thank-you' note:
when an expression involves operators of equal precedence then the evaluation always starts from left to right unless overriden by using parenthesis.
Hence in 1/2*.5 '/' is evaluated and since both operands are int, hence integer division takes place.Hence O/P is 0.

In 0.5 * 1 / 2---> '*' gets evalutaed and since one of the operand after multiplication is floating point hence 2 also gets promoted to 2.0
Hence O/P is .5

Ranch Hand
Posts: 621
• Number of slices to send:
Optional 'thank-you' note:
well!!!
The above question is confusing me
please anyone can explain how integer division result in 0

as it is explained above int/int :>result in 0.......how this is possible?

what i think is if execution start from left the answer should be

1/2*0.5

first step

1/2........results in :>0.5......as this value is getting assigned to a double.......it will be double ie 0.5

later in second step..........0.5*0.5 results in 1.0...........
[ July 19, 2007: Message edited by: dhwani mathur ]

Burkhard Hassel
Ranch Hand
Posts: 1274
• Number of slices to send:
Optional 'thank-you' note:
Howdy ranchers,

dhwani mathur posted
T

he above question is confusing me
please anyone can explain how integer division result in 0
(...)
first step

1/2........results in :>0.5

There it is. The expression 1 / 2 * 0.5 is not evaluated all in once, but pairwise from left to right.
First comes 1/2.

The compiler finds two integer literals, therefore the result is also an integer (and not a double!)
1/2 ---> 0.5 , but only the integer part of it will reside in memory, and that is 0 without a dot and without the halve (not 0.5!).

Next step (the one with the X)

Here the compiler finds an integer zero in memory and a double literal from the code.
Therefore this pair is converted to double:
0.0 * 0.5

Hence 0.0

BTW, when dividing integers, you always loose the part right of the decimal point:
System.out.println (3/2);
results in 1

Bu.

 Why does your bag say "bombs"? The reason I ask is that my bag says "tiny ads" and it has stuff like this: the value of filler advertising in 2021 https://coderanch.com/t/730886/filler-advertising