• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Why is "float f4 = 42e7" not considered a valid declaration of a float in Java?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Friends,

I was reading the Java Sun Certification Book and one of the sample questions in the first chapter is:

"
Which 3 are valid declarations of a float? (Choose 3)
A. float f1 = -343;
B. float f2 = 3.14;
C. float f3 = 0x12345;
D. float f4 = 42e7;
E. float f5 = 2001.0D;
F. float f6 = 2.81F;
"

The Book says the answer is A, C, and F. It is clear to me why F is one of the answers since the "F" suffix is attached to the end of the number. It is also clear why A and C are correct based on the explanation at the end of the chapter which says: "A and C are integer literals (32 bits), and integers can be legally assigned to floats (also 32 bits.)"

But what I don't understand is why D is not one of the correct answers, based upon the same logic which makes A and C correct. Specifically, isn't D (42e7) and integer literal as well, making it able to be legally assigned to a float. Since 42e7 represents (240,000,000) , isn't it an int and not a double by default as a literal?

Thank you for your help.

Nilay
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No 42e7 is a double.
 
Nilay Jhaveri
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain why its a double given that it is an int and in the solution they said ints can be assigned to floats?
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, it is not an int. When you express a number in scientific notation, it is considered a double unless you add a f or F.

Note what happens when you try to compile this.




[ June 11, 2006: Message edited by: Keith Lynn ]
 
Did you miss me? Did you miss this tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic