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
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Marcus Q1

Greenhorn
Posts: 25
• Number of slices to send:
Optional 'thank-you' note:
Hi,
Here is a question from Marcus
Which of the following are legal statements?
1) float f=1/3;
2) int i=1/3;
3) float f=1.01;
4) double d=999d;
I compiled & got it correct too.But i have a small doubt.When u say float f=1/3,why does'nt it consider 1/3 which 0.3333 as double.We know that the default type for decimal type is double.Why does'nt it give an error for No.2 option too?I have the same doubt here.Can anyone please make it clear for me?
Thanks

Ranch Hand
Posts: 103
• Number of slices to send:
Optional 'thank-you' note:
rubna,
I think it is considering 1/3 as integer division in both 1. and 2. The result integer 0 is assigned to float in case 1 and to int in case 2.
Savithri

Ranch Hand
Posts: 44
• Number of slices to send:
Optional 'thank-you' note:
In case of 1, f = 1/3 is converted to 1.0f/3.0f
Thanks
Raj

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

Originally posted by Raj Devaraj:
In case of 1, f = 1/3 is converted to 1.0f/3.0f
Thanks
Raj

i am afraid it is not true. try this:
<code>
float ff = 1/3;
System.out.println(ff);
float f=1.0f/3.0f;
System.out.println(f);
</code>

Greenhorn
Posts: 11
• Number of slices to send:
Optional 'thank-you' note:
I believe Savithri is right.
In float f = 1/3; The right hand side is first caculated.
The result integer 0 is thenprompted as 0f and assigned
to the left hand side.

Ranch Hand
Posts: 93
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I think Savuthiri is right . I executed this in JBuilder 2 and works out.
Michael: I too hope that Devaraj's statement is not true.
Thanks.
Nirmal

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.