Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Mock Exam Errata and the fly likes Rules Roundup Q45 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Rules Roundup Q45" Watch "Rules Roundup Q45" New topic

Rules Roundup Q45

Jeff L.

Joined: Sep 02, 2003
Posts: 8
I assume the questions are the same, but to clarify, I am using the beta of the second version.
The question asks:
45) Integer literals are always what size?
The answer given is:
Integer literals are always treated as 32-bit ints.
However, this is disproven by the following code: attempting to assign an integer literal to a byte works just fine, while attempting to assign the same integer causes a problem. I think the correct response is, "Integer literals are always treated as the smallest integer-type that can hold them without an overflow."
byte b = 5; //This works just fine!
int xx = 5;
b = xx; //This causes an error!
Jeff L.

Joined: Sep 02, 2003
Posts: 8
I found this thread after I posted.
Note that I searched for Integer literal before posting, but I restricted my search to the mock exam, and missed the discussion elsewhere.
This proves that my surmise about byte is wrong, but int is a little misleading. Actually, it treats Integer literals as final ints rather than unadorned ints. (Ok, I'm splitting hairs so at least I'm not completely wrong... )
Anyway, I thought I'd share the results.
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
Your display name does not comply with our naming policy so I'll have to ask you to change it.

Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
I agree. Here's the link:
subject: Rules Roundup Q45
It's not a secret anymore!