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 Green Exam 3, Q 1

Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
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 could not understand y opt) 1,2 are also correct in addition to option 4.
What I understand is / operator has more precedence then assigment so first 1/3 should be evaluated. Then it will become "0.33" that should be treeted as double by default. And double value can not be assigned to float or int. So only option 4 should be correct...
Any explanation???
Rehan

Ranch Hand
Posts: 219
• Number of slices to send:
Optional 'thank-you' note:
I beleive a double can be assigned to a float or an int...it just loses precision. In the int case, anything after the decimal point is dropped, so it ends up benig 0. And in the float case, there is a chance of losing precision in larger numbers, but in this case it keeps the same value.
Correct me if I am wrong...

rehan hamid
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:

Originally posted by ryan burgdorfer:
I beleive a double can be assigned to a float or an int...it just loses precision. In the int case, anything after the decimal point is dropped, so it ends up benig 0. And in the float case, there is a chance of losing precision in larger numbers, but in this case it keeps the same value.
Correct me if I am wrong...

I dont think that you can assign more broden data types to less broden data types unless type casted.

Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
Hi rehan.You are right in stating that a floating point value by default is interpreted as a double in Java.Let's go through the code..

The expression

cannot be evaluated at compile time.
Division will be performed at runtime,and 0.333...3 will be obtained,which will be a double value.But this value is being stored in a variable of type float,and this results in loss of precision.Now float f will have value 0.0.
Same is the case for int
But the expression

can evaluated at compile time ,since 1.01 is a compile time constant.Here the compiler evaluates 1.01 to a double value,and as such throws an Exception.
I hope that helps.
------------------
Udayan Naik
Sun Certified Java 2 Programmer

Ranch Hand
Posts: 119
• Number of slices to send:
Optional 'thank-you' note:
Hi,
I believe the reason both 1 and 2 are correct is that / always returns an int. Hence since the size of float is the same as int there's no problem with (1).
cheerios,
Yoo-Jin.

Udayan Naik
Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
Yeah,actually both the operands are integers,so integer division is done,result of which is an int.This int can now be stored in a float as well as an int.

------------------
Udayan Naik
Sun Certified Java 2 Programmer
[This message has been edited by Udayan Naik (edited March 03, 2001).]

Greenhorn
Posts: 14
• Number of slices to send:
Optional 'thank-you' note:
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 could not understand y opt) 1,2 are also correct in addition to option 4.
hi friends!!
option 1,2 and 4 r correct .A double cant b assigned to an int as an int of 32 bits cant store double of 64 bits or u have to cast it.
in the first option 1/3 1 is an int and 3 is also taken as an int so the operation involves int/int which returns an int and since an int can b stored in a float,therefore it is correct.
second option is also correct as it also returns an int and the result is stored in an int(ans will b 0).
Third option is false coz 1.01 is double by default and u cant store double in float(1.01 is taken as 1.01d).it will run fine if u rite 1.01f.
fourth is no problem,it will run fine without riting a d suffix.
hope u got that!!

rehan hamid
Greenhorn
Posts: 16
• Number of slices to send:
Optional 'thank-you' note:
THANKS ALL OF YOU!!

Yoo-Jin Lee
Ranch Hand
Posts: 119
• Number of slices to send:
Optional 'thank-you' note:
Puneet,
I don't believe that you can use d to indicate a double just f or l for float and long, respectively.
Just a note.
Yoo-Jin.

Ranch Hand
Posts: 38
• Number of slices to send:
Optional 'thank-you' note:
a note to Yoo-Jin Lee: You can use a "d" to designate a double. It is understood to be there if you do not put it. Therefore, it is a valid assignment.

 Consider Paul's rocket mass heater.