Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-31-2007, 04:55 AM
Member
 
Join Date: Jul 2007
Posts: 40
baltimore is on a distinguished road
Help with valid declarations of a float
Hi, Ok please can someone explain if the value 3.14 is a double or a float...I am looking at this Java Sun Certification book and it says that 3.14 is a float and not a double.

The question is as follows:
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;
I chose the answers to be A, B and F. The book says A, C and F. Can someone explain this to me? I mean I am leaning towards the I am wrong but the book is right kind of deals but something tells me I am right and the book wrong.
Thanks.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-31-2007, 11:28 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,022
hardwired is on a distinguished road
If a number has a decimal it is a double. To make such a number a float you can do
Code:
float f = (float)3.14; // cast // or float c = 3.14f; // or 3.14F
And any number of lesser precision can be assigned to a reference of higher precision.
Code:
float f = 25; double d = 15f; long b = 88; // int
To see what java says:
Code:
class FloatTest { public static void main(String[] args) { check(-343); check(3.14); check(0x12345); check(42e7); check(2001.0D); check(2.81F); } private static void check(Number n) { System.out.println(n + " = " + n.getClass().getName()); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
net.sf.jasperreports.engine.JRException: Report design not valid simon XML 2 01-21-2008 09:15 AM
Float to String mew New To Java 4 12-29-2007 06:08 PM
Valid declarations heat84 New To Java 1 12-22-2007 03:29 AM
MySite/Valid.java:56: cannot resolve symbol Albert Enterprise JavaBeans 1 07-05-2007 06:49 AM
org.apache.commons.mail.EmailException: Cannot find valid hostname Ed Advanced Java 2 07-02-2007 09:03 PM


All times are GMT +3. The time now is 12:11 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org