• 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
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

OCAJP 8 study guide Chapter 1 Review questions 16 Have trouble understanding the answer D

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



A. int amount = 9L;
B. int amount = 0b101;
C. int amount = 0xE;
D. double amount = 0xE;
E. double amount = 1_2_.0_0;
F. int amount = 1_2_;
G. None of the above.


The right answer is BCD. I don't understand why D is right. I think binary, hexadecimal, octal are all for integers....... Is float amount = 0x18Ef right then? What about short amount = 0xF? byte amount = 0xE? long amount = 0xEL? double amount = 0xE.12F?(F doesn't mean float here, this is a confusing point also) float amount = 0x18E。134f?

Any help will be appreciated!
 
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Gem Wang,

First of all, a warm welcome to CodeRanch!

Gem Wang wrote:I think binary, hexadecimal, octal are all for integers.


You are correct! Integer literals can be expressed by one of these number systems: binary, decimal, octal, and hexadecimal. And that's exactly what happens in option D You have a variable amount (of type double) and you have the value 0xE. Every literal representing an integral number is by default considered to be an int (if the literal ends with the letter L or l, it is a long). So this value is an int literal and thus allowed to be expressed in hexadecimal. When this value is assigned to the variable amount, the value is widened from int to double.

Gem Wang wrote:Is float amount = 0x18Ef right then? What about short amount = 0xF? byte amount = 0xE? long amount = 0xEL? double amount = 0xE.12F?(F doesn't mean float here, this is a confusing point also) float amount = 0x18E。134f?


You can easily verify yourself if these statements are allowed or not. You could create a small code snippet similar with this oneWhen executed this code snippet will print "14 237 237,000000 239,000000 14". line1 gives a compiler error, because you try to express a floating-point literal in hexadecimal which is not allowed.

Hope it helps!
Kind regards,
Roel

PS. Always use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information. Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers. Jeanne already added the code tags for you. See how much easier the code is to read?
 
Gem Wang
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Roel,

Thank you so much for your explanation. You have been really helpful!! And I will definitely put code tag next time. Good to know!
I appreciate the help from you and Jeanne!

Thanks!
Gem
 
Roel De Nijs
Sheriff
Posts: 11606
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Gem Wang wrote:And I will definitely put code tag next time. Good to know!


Awesome Thanks!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic