Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Marcus Green Exam 3, Q 1

 
rehan hamid
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ryan burgdorfer
Ranch Hand
Posts: 219
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Udayan Naik
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

My mistake.Sorry for misleading.
------------------
Udayan Naik
Sun Certified Java 2 Programmer
[This message has been edited by Udayan Naik (edited March 03, 2001).]
 
puneet pruthi
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS ALL OF YOU!!
 
Yoo-Jin Lee
Ranch Hand
Posts: 119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
tony hutcheson
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic