Author Topic:   Another "Exam" Question
Tony Alicea
sheriff
posted February 04, 2000 06:47 PM             
Which lines of code, if any, will cause compilation a error? Why?

(a) float f1 = 412;
(b) float f2 = 412.;
(c) float f3 = new Float(412.0f);
(d) double d = 3.14159f;

Paul Wheaton
sheriff
posted February 04, 2000 08:23 PM             
b) I dunno, I never thought someone would try that.

c) assigning an object to a primitive?

Rajiv Ahuja
greenhorn
posted February 04, 2000 09:14 PM             
O.k let me try
a) Correct 'cuz int gets converted to float
b) hmmm...thinking
c) ditto as Paul
d) float gets converted to double

Please let me know if my explanations are correct.

maha anna
bartender
posted February 04, 2000 09:41 PM             
float f = 412.; --> gives compile err as explicit cast needed to convert double to float. That means it accepts 412. right? To confirm that I changed the above code as float f= 412.f;
As I expected it compiles without any problem. When we print it 412.0 is displayed.

Tony! What on earth makes you to think like this so differently..
I hope you take this as a compliment!

Tony Alicea
sheriff
posted February 05, 2000 09:38 AM             
"Tony! What on earth makes you to think like this so differently.."

He he... My best friends think that is one characteristic of mine... Ok, what they really think is that I am crazy!

Oh yes, thank you.

Tony Alicea
sheriff
posted February 05, 2000 09:48 AM             
Paul:

"b) I dunno, I never thought someone would try that."

b) float f2 = 412.;"

For me it reminds me of my FORTRAN days: By just adding a decimal point the literal would be a floating point literal. The zero after the decimal point was not necessary (same with Java). It made an impression that by just adding a dot the whole number would be represented and used so differently. Impressionable junior programmer days, you know...

At any rate, many mock exams that I have taken use that trickery. Of course, I have no idea what Sun uses.

Finally, in today's applications we should almost always use double because memory is cheap but more importantly maybe because of the added precision of double versus float. The designers of Java were possibly thinking that when they decided that for a float you need to add an 'f' after the literal...

My $0.02

[This message has been edited by Tony Alicea (edited February 05, 2000).]

Tony Alicea
sheriff
posted February 05, 2000 10:43 AM             
Rajiv:

a) Correct 'cuz int gets converted to float
b) hmmm...thinking
c) ditto as Paul
d) float gets converted to double
Please let me know if my explanations are correct.

The only one you have a doubt on, is one of my favorites:

412. is the same as 412.0 which is a double. In the early FORTRAN days, by just adding a decimal point you converted a literal from "integer" to "real" (just like in C later and in Java now). I think that was the formal name: "real" (it's been a long time!). The term "double precision" was available but the 'real' variables had to be explicitly defined as double precision if that's what you wanted. It was not the default. And you'd better have a good reason esp. if using large arrays because of the memory shortage. You know, the one that gave us the Y2K scare

neetugupta
greenhorn
posted February 09, 2000 08:07 AM             
1. works fine as int can be assigned to float.
2. Error, as explicit cast is required to convert double to float.
3. Error, as Float can not be converted to primitive types.
4. works fine.

|