Ray Kolbe

+ Follow
since Feb 21, 2016
Cows and Likes
Total received
In last 30 days
Total given
Total received
Received in last 30 days
Total given
Given in last 30 days
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Ray Kolbe

Jeanne Boyarsky wrote:

Ray Kolbe wrote:Sound about right?

Yes. It amounts to "when the compiler finds an error, it sometimes stops looking for more"

Right. But in this case an error wasn't found until the second case label (in both scenarios you listed above and what is shown/explained on the mock exam).
Hi Jeanne,

Thanks for the response. I'm still not reading the explanation (in the mock exam) that way but that's ok.

I believe I understand the explanation(s) and the takeaway (correct me if I'm wrong) is that the compilation error in both cases is with the second case label: scenario 1 being that the case label is not compile-time constant and scenario 2 is that there are two case labels that are the same, which is a no-no! However, in scenario 2 both case labels play a role leading to the compilation error.

Sound about right?

Thanks for your time! It's much appreciated.

Jeanne Boyarsky wrote:
Compile #2

  • Case statement has value 0. Good.
  • Case statement has value x. That's a final variable so no problem there.
  • The value of x is 0. Uh oh. I already have a 0 in this switch. Compilation fails! Fix please.

  • Correct. I understand this and I think you demonstrated just now that the explanation in the mock exam is either incorrect or needs further clarification, unless of course I'm misinterpreting the explanation.

    In the quote above you state "Case statement has value 0, Good.", which means no compilation error should happen here. However, in the explanation in the mock exam it states that this scenario would cause a compilation error.

    Excerpts from the mock exam:

    A. the code will not compile because of line 6.

    Excerpt from explanation:

    Note that if badMovie was made a final constant variable with the same value of 0, then option A would be considered correct too.

    Hope my explanation was clear.

    Clarification on my post.

    I *do* receive different errors when "x" is final vs. when it is not.

    When x is not final I receive:

       error: constant expression required
         case x: break;

    1 error

    and when x is final:

       error: duplicate case label
         case x: break;

    1 error

    I expect both of those compilation errors. However, my request for clarification remains. How is the first case statement an issue whether x is final or not?


    Mock exam 3 in Sybex study guide has a question about a switch statement using badMovie and movieRating as a couple of variable names.

    For brevity let's use a more concise but similar code example as the book:

    If I'm reading the explanation in the book correctly, it states that if "x" is set to final then the first case statement would cause a compilation error.

    From the book:

    Note that if badMovie was made a final constant variable with the same value of 0, then option A would be considered correct too.

    Option A refers to the first case statement causing a compilation error.

    However, I receive the same compilation error whether x is final or not, as I would expect. Specifically:

       error: duplicate case label
         case x: break;

    1 error

    Any clarification is appreciated. Thanks!

    Julian West wrote:

    Ray Kolbe wrote: https://gist.github.com/raykolbe/579bba00941ec16b3bce975679abbc8b

    I'm not sure what you were trying to do with #6, the . operator on line 6

    Sorry about that. Looks like it was a copy/paste error on my part. I think I fixed it  

    Julian West wrote:

    Ray Kolbe wrote: Is this the right thing to do?

    I think so; it's what I've been doing.

    Those were good exercises you posted.  Sneaky.

    If you're looking for more (or anyone else) this is my WIP. I have a ton of other questions I've come up with that have not made the list yet: https://gist.github.com/raykolbe/579bba00941ec16b3bce975679abbc8b

    Stephan van Hulst wrote:
    Let's take a look at
    You'd expect the last expression to evaluate to 5, with x becoming 5 and y becoming 3.
    This however is not the case. This expression will evaluate to 4, with x becoming 4 and y becoming 3.

    Why? Here's why:

    Even though this thread is a bit old it's probably worth supplementing the knowledge here with the JLS. It spells this out quite nicely IMHO (emphasis below is mine).

    From https://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.7.1

    The left-hand operand of a binary operator appears to be fully evaluated before any part of the right-hand operand is evaluated.

    If the operator is a compound-assignment operator (§15.26.2), then evaluation of the left-hand operand includes both remembering the variable that the left-hand operand denotes and fetching and saving that variable's value for use in the implied binary operation.

    There are some really good examples in §15.7 as well.
    Ah shoot. I actually meant to post these replies to https://coderanch.com/t/661830/ocajp/certification/Garbage-Collection which links to *this* thread. I've made a mess of things  
    Perhaps the JLS is wrong since this appears to be cached:

    My other questions still stand  
    Re: my last post, the difference between SE7 and SE8 appear to be that short is not cached, unless that language is buried somewhere else.

    Roel De Nijs wrote:

    Guillermo Ishi wrote:I think I remember this now.  So what's being said is that being eligible to be cached keeps an Object from being made eligible for garbage collection?

    Yes, because the cache will always keep a reference to the object (which is by the way only created just once, when the primitive wrapper class is loaded). This of course only applies if you use the autoboxing feature or the valueOf method with the primitive parameter, in all other scenarios the object is not cached.

    Not cached:

    I need a bit of clarification here. Are we talking about SE7 here or SE8 or both? Although this is good information to learn is it relevant to 1Z0-808?

    Roel you say in another post in this thread:

    Roel De Nijs wrote:
    This caching mechanism is only guaranteed for the following values:
    Character from \u0000 to \u007f (7f is 127 in decimal)
    Short and Integer from -128 to 127 (inclusive)

    It appears this is different in SE8.

    Per JLS (https://docs.oracle.com/javase/specs/jls/se8/html/jls-5.html#jls-5.1.7):

    If the value p being boxed is an integer literal of type int between -128 and 127 inclusive (§3.10.1), or the boolean literal true or false (§3.10.3), or a character literal between '\u0000' and '\u007f' inclusive (§3.10.4), then let a and b be the results of any two boxing conversions of p. It is always the case that a == b.

    Whereas SE7 JLS (http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.7):

    If the value p being boxed is true, false, a byte, or a char in the range \u0000 to \u007f, or an int or short number between -128 and 127 (inclusive), then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.

    Furthermore it looks like "valueOf" with a string param works differently in SE8.


    I get back "Integers are equal: true". I've tried changing "t" to "Integer t = 126" and I get the same output. It's only when I do "Integer t = new Integer(126)" that I get "false".
    Howdy all,

    As I continue my journey in preparing for 1Z0-808 I find that I do my best learning when solving challenging problems. It helps me see/study what I screwed up on and to avoid repeating it in the future.

    I've studied (and continue to) Java A Beginner's Guide - 6th ed." (Schildt) and "OCA Oracle Certified Associate Java SE 8 Programmer I" (Boyarsky/Selikoff). I also frequent these forums while also creating my own problems and searching the internet for some as well as reading/studying Oracle's site (I've been through some of the tutorials and quizzes) and creating a large stack of my own flash cards that I use almost every day. I also have a large running file that I continually add expressions to that get printed to screen where I answer all questions at least once a week (I'm up to ~89 expressions; some easy, some hard)...something like:

    So far I have taken the chapter review questions twice in Boyarsky/Selikoff and I have taken 2 of the 3 practice exams in the same book. I got a 60% on the first one (dumb mistakes on my part) and a ~68% (after adjusting my score based on the errata) -- so, not so great. I do have Enthuware sitting on my laptop but I don't want to start taking the mock exams until I am a bit more ready. Is this the right thing to do?

    What I *think* I'm looking for are questions like these (these are created by me):

    Assuming these classes are in the file Scratch.java, what is the output when compiled and run?


    Assuming these classes are in the file Scratch.java, what is the output when compiled and run?

    Does anyone know of or have a collection of questions like these? I don't *need* explanations to go with the problems since that is part of the fun in figuring out what the hell is going on  

    I'd like to share my content too at some point if anyone would find it helpful. Here's are some examples of some early study material I used (again, created by me): https://gist.github.com/anonymous/5bf35d2599ef7f19c7fe and https://gist.github.com/anonymous/098294f20a3b5b27611f  and https://twitter.com/raykolbe/status/701593044114518020 and https://twitter.com/raykolbe/status/752173164499656704

    Any help is appreciated. Thanks!