Results 1 to 8 of 8

Thread: One of those Java gotchas....

  1. #1
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts

    One of those Java gotchas....

    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.

  2. #2
    Senior Member Kezzer's Avatar
    Join Date
    Sep 2003
    Posts
    4,863
    Thanks
    12
    Thanked
    5 times in 5 posts
    TIAS

    But yeh, you have to call it as a float if it's a simple decimal point number for some reason. Bit random though, can't seem to find anything

    EDIT: What course are you doing btw? 'cause all my programming lies in java too
    Last edited by Kezzer; 15-05-2005 at 11:22 PM.

  3. #3
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    This is the SCJP whatever...its the Sun Microsystems Certified Java Programmers course. I just recently graduated with a BS in CS (hehe) and so I decided to get this certification done along with the developers certification...(you gotta pass the Programmers test first to get to take the Developers test) before I get to going to grad school. Hoping to get a job and so on working in java in the industry before committing to some more studying in a more specialized field. Am probably gonna get into Java 3d graphics for medical field related work and specialize in that for me masters...if I get into a good school that is hehe.

    This certification I am told is very highly regarded in the industry apparently...sort of like a feather in the cap so I am keen on getting these 2 tests out of the way...they are tough but I got time to work on it. Just another way of trying to separate myself from the rest of the graduates I suppose.

    Anyway I just dont think that the value of 3.14 is a double...but then again what do I know? I didnt write the book...so i gotta trust the authors I guess cause I looked it up on the web but to no avail.

    Cheers

  4. #4
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Quote Originally Posted by suryad
    ...
    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'm guessing the - in "F." is a typo.

    The book has it right - A, C and F are float, the other three are double.
    To err is human. To really foul things up ... you need a computer.

  5. #5
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    No yamangman no typo..its perfectly legal in Java...and could you explain your reasoning as to why answer B is a double? That is what seems kind of messed up to me...thanks.

  6. #6
    Large Member
    Join Date
    Apr 2004
    Posts
    3,720
    Thanks
    47
    Thanked
    99 times in 64 posts
    Hehhe, very amusing. "float f6 - 2.81F;" is an illegal variable declaration in java. You can't come up with a float called f6 and take 2.81F away from it. What value would it hold?

    If you want to know the difference between float and double, i suggest you look up (i.e. search Google for) the 'IEEE 754' standard.
    To err is human. To really foul things up ... you need a computer.

  7. #7
    Senior Member
    Join Date
    Feb 2005
    Location
    Folsom, CA
    Posts
    221
    Thanks
    0
    Thanked
    0 times in 0 posts
    Sorry my bad the '-' sign is an '=' sign...my apologies

  8. #8
    Sam
    Sam is offline
    Registered+
    Join Date
    Oct 2003
    Posts
    22
    Thanks
    0
    Thanked
    0 times in 0 posts
    Anything with a decimal point or exponential form (42e7 etc) defaults to being a double so BDE isn't legal code.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Best book to learn JAVA 2?
    By Taz in forum Software
    Replies: 16
    Last Post: 27-08-2005, 08:54 PM
  2. Java Linux
    By Raz316 in forum Software
    Replies: 8
    Last Post: 06-04-2005, 09:35 AM
  3. Potential HUGE source of free Java games / apps
    By PriestJPN in forum Smartphones and Tablets
    Replies: 4
    Last Post: 03-11-2004, 01:59 AM
  4. Java Problems
    By Applecrusher in forum Software
    Replies: 4
    Last Post: 27-04-2004, 01:12 PM
  5. Java
    By Jonny M in forum Software
    Replies: 8
    Last Post: 24-12-2003, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •