[HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
BSilviu
Posts: 7
Joined: Wed Feb 01, 2023 9:00 am
Contact:

[HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Post by BSilviu »

Hi guys,

Stupid question maybe, but I'm feeling a little bit puzzled:

final int i = 10;
byte b = i; compiles fine because i is a compile time constant.

But, for instance, if I write something like:

final long k = 10;
int j = k; it doesn't compile anymore.

What am I missing? I expect, since k is a compile time constant, to compile just fine...

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Post by admin »

Yes, ideally, since k is a compile time constant and its value fits an int, this assignment should be allowed. However, the Java Language specification allows implicit narrowing only for byte, char, short, and int.
As per section 5.2 of JLS 11:
In addition, if the expression is a constant expression (§15.28) of type byte, short, char, or int:
A narrowing primitive conversion may be used if the variable is of type byte, short, or char, and the value of the constant expression is representable in the type of the variable.
Looks like the book missed mentioning this point.
If you like our products and services, please help us by posting your review here.

BSilviu
Posts: 7
Joined: Wed Feb 01, 2023 9:00 am
Contact:

Re: [HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Post by BSilviu »

Cool! Thanks for the clarification. I also ordered the printed version of the book and also the mockup exams from Enthuware.
Reading this book, and taking the mockup exams, should be enough in order to get the certification for 1Z0-808? :cheers:

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Post by admin »

Yes, that should be enough.
Please see this to take full advantage of going through the mock exams: https://enthuware.com/help/faq/112-faq/ ... -enthuware
If you like our products and services, please help us by posting your review here.

BSilviu
Posts: 7
Joined: Wed Feb 01, 2023 9:00 am
Contact:

Re: [HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Post by BSilviu »

One more thing. I've ordered my printed version from amazon.de.
I've read that the book is printed on demand, so I should get the latest version. Can you confirm this? What's the latest version of the printed book?

Thanks.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD Pg 0, Sec. 3.3.3 - primitive-assignment]

Post by admin »

Yes, you should get the latest version as mentioned on Amazon (Last Updated: 5th Jun 2020 Build 31).
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 39 guests