To conquer without peril, one ends up triumphing without glory
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:Tiam,
You need to have constants in the case. So "L" is ok, but arr[0] is not.
To conquer without peril, one ends up triumphing without glory
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:You can think of it like the compiler turns the switch into if statements for you. But it needs to know what value to put. The array value can change so the compiler doesn't know what to put. You are probably still wondering why. It's how they optimize it. If the value can change, it can't be stored directly in the bytecode.
To conquer without peril, one ends up triumphing without glory
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Jeanne Boyarsky wrote:Compile time constants. That means the compiler can determine the value rather than there being a "final" keyword.
[OCP 21 book] | [OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Stephan van Hulst wrote:The most important reason is that the compiler can ensure that the cases are mutually exclusive, and therefore there will be no ambiguity what case will be executed after the switch expression evaluates.
I think what Jeanne meant is that variables can act as compile time constants if they're effectively final, in which case using the final keyword is not strictly necessary.
"if the compiler is sure that the "case expression" will not change during execution, there will be no compiler error. but it suspects that the case expression may vary, so it complains about a potential value variation."
To conquer without peril, one ends up triumphing without glory
Simon Roberts wrote:More than that. The *compiler* must be able to calculate the case value. It's literally a compile-time constant. It's not a variable in any way.
But when you get past the certification exam, know that there's a whole new case mechanism and much of this discussion becomes true only for the traditional form.
To conquer without peril, one ends up triumphing without glory
Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
Paul Anilprem wrote:Simon is probably talking about switch expressions but you don't have to worry about it for the 1z0-808 exam because this exam is based on Java 8 and the new switch expressions feature has been available only since Java 12.
Simon Roberts wrote:
Paul Anilprem wrote:Simon is probably talking about switch expressions but you don't have to worry about it for the 1z0-808 exam because this exam is based on Java 8 and the new switch expressions feature has been available only since Java 12.
Actually I was talking about pattern matching, which isn't even fully released in Java 19, it's still a preview. But exactly, it's not relevant, which was why I said "... when you get past the certification ..."
That said, for anyone taking the Java 17 exam (1Z0-829) the switch expression and arrow-form material (none of which has also not been discussed here) are relevant. But none of that matters to you since you have mentioned that you're taking 808.
To conquer without peril, one ends up triumphing without glory
Simon Roberts wrote:More than that. The *compiler* must be able to calculate the case value. It's literally a compile-time constant. It's not a variable in any way
Mike Simmons wrote:
Well, except in the sense that a "variable" in Java can be a constant variable, despite the linguistic non sequitur.
A lookup table is created and populated at compile time, which allows for very fast execution, but requires compile‑time constants. That means that Integers, Shorts, Bytes, and Characters are not permissible as cases because only primitives and Strings can be compile‑time constants.Simon Roberts wrote:. . . . The *compiler* must be able to calculate the case value. . . ..
Did you just should on me? You should read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|