Simon Roberts

+ Follow
since Oct 24, 2000
Simon likes ...
Scala IntelliJ IDE Netbeans IDE Java Ubuntu Linux
Denver, Colorado
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 Simon Roberts

Frank Mi wrote:...
What is the sidetrack? I didn't get it. Maybe I would like to join the discussion.  

Perspective (and to an extent perhaps terminology) We do indeed agree on the facts, now that Stephan corrected me. Very useful discussion; I love the smell of fresh new learning in the morning!
1 year ago
Well, I'm inclined to disagree there. For two reasons:

1. the spec calls out two distinct situations, and gives them different (and not equivalent, see point 2) descriptions.

2. the situation we've just encountered shows the difference. The execution of a body of a method that has a return statement does not "complete normally", and yet, that same method's invocation does complete normally ;)

1 year ago
The essence of protected is that it gives access from subclass methods to fields and methods in instances of the subclass only you can't use this to mess with instances that are actually of the parent type.

The only way the compiler knows whether to permit this, of course, is by the type of the reference. So, if you have a reference that's of the subclass type, the access is allowed. If, however, the reference is of the parent class type, the compiler won't compile the code, so you never get to find out. Java's compiler, generally speaking, won't compile something unless it can prove the code will be well behaved. You believing it will be isn't good enough--but to bypass that, we're given casts

1 year ago

Stephan van Hulst wrote: ... Normal completion is when a method runs to the end without encountering any keyword that will cause it to return prematurely. This includes the 'return' keyword.

Good catch, I didn't pay enough attention to the lambda-related descriptions of this. Let me try to be both precise and helpful, let's see how this goes

First, in my "defense", Java has (at least) two different definitions of "Normal and Abrupt Completion"

One relates to statements, and is in JLS 14.1. It seems to boil down to "normal is anything except acting on: break, continue, return, throw. This seems to be the one that is referred to in the pieces that Frank was asking about, since it describes the execution of a method's body.

The second definition relates to evaluation (which includes expressions, and thereby the invocation of a method call), and is in JLS 15.6. This one (which is what I was thinking of) says:

If all the steps are carried out without an exception being thrown, the expression is said to complete normally

Now, in the situation Frank was asking about in that follow-up question the point is that if a method's execution "completes normally" (governed by JLS 14.1) that means in effect that it "ran off the end of the method body". And if it runs off the end of the method body, it cannot have executed a return xxx statement (indeed it cannot have executed any return statement, whether with or without a value). But, to be "value compatible, all return statements must return a (compatible with the declared return type) return statement. Hence, anything that completes normally in this sense cannot be "value compatible".

As a teacher, I am again reminded of the huge distinction between specification and explanation!

Anyway, Stephan, perhaps you'd like to comment on/confirm/deny my expansion there?

1 year ago
Oh, if the code in a block lambda simply ends with return-semicolon (i.e. exactly return; ) then it's explicitly a void return, right? Just like if you have a function

Void compatible simply means you'll be able to use it--that is, it will compile OK if used--in a context where the lambda needed is implementing a method that returns void. That, as you've seen, includes some method bodies that actually do appear to return values, but not if they do so explicitly.

Value compatible means the lambda body does return a value.

A lambda's body can be both--that just means it returns a value, but in a way that's OK to ignore.  So, in my examples, the lambdas that were Consumer or Runnable implement methods that return void. But it's possible to do it with some types of lambda body that do return something. Take another look at  the bodies of those lambda examples, I also tried to be a bit more explicit in the preamble to the pairs of examples.

Any better?
1 year ago
Not sure if I'll answer the question you actually asked, or just the one I think you're asking, but let me try:

If you have, for example, a Consumer, the expected return of the lambda is void, but as you've noted, returning something implicitly is actually OK in this situation, just like calling:

as a statement is OK, even though it's technically an expression. That's "void compatible". But, if you return something explicitly, that's not void compatible.

So, these work as "void compatible" even though the expressions in them appear to return values:

But these are not void compatible, because the expressions that they return are not in acceptable forms:

Quite separately, a lambda that returns something is value compatible (which does not prevent it being void compatible too, as above). A lambda for which the type is determined to such that a returned value is expected must be value compatible to have a chance of compiling (and of course the types must match, and so must the argument lists).

However, it's conceptually possible to create a lambda looking thing that's neither one thing nor the other, this would be the situation with the example you gave. These both fail, that is, the lambda doesn't work for either returning a value, or for being void compatible:

I think the point is simply to give the same flexibility to lambdas as exists for statements made of expressions that are being used for their side-effects i.e. the situation you already called out:

Does this help?
1 year ago
Well, I decided I should put my money where my mouth is and take this exam. I passed, although I'd have liked a higher score still!

My immediate observation about the questions is that some of the "easiest" questions are built in a way that makes them hardest of all. That is, if the topic is really quite simple (e.g. "scope of variables") you're left with a question that's like an IQ test; it's hugely complex and the only way you'll find an answer to it in less than two minutes is if you notice one little detail which indirectly makes the whole thing simple.

I took the exam using Pearson Vue's "at home" capability, which was interesting and while not perfect (you're not allowed anything to write on, but there's a "whiteboard" app--which leaves a lot to be desired--that you can use instead, kinda.) All things considered, however, I'd do it that way again, even though I had to build a windows machine from spare parts for the purpose (I'm a Linux user through and through and a VM isn't allowed!)

Ah well, I'll be updating my live training class on this (on safaribooksonline) with more comments and observations about this, and will be releasing a series of updated pre-recorded video training classes built around this new exam. The recorded videos will, inevitably, take some time to create, hence it'll be a series releasing parts as available rather than waiting for all of them at once.

Good luck to others taking this exam, advice #1 from me at this point is that it's probably more important to have the old 815 (basic language) material so well understood you don't have to think about it, than it is to have the myriad APIs down word perfect (though you'll want a more than passing understanding of those too to get past the 68% threshold in the time available).
My publisher told me that they're having a sale through 12/31

Offer: Save 65% on Video and Simulators Save 40% on books, ebooks, and practice tests

Enter to win Kindle fire 8HD (32 GB) + 2,000 in Cisco Press / Pearson IT Certification Learning.

Cisco Press:

4 years ago
This help?

Tim Cooke wrote:

Campbell Ritchie wrote:A hoofed panda

I don't get it.

4 years ago
This clearly went to sleep a while ago, and has many very imaginative answers. I also don't know what you deem "elegant", but I didn't notice the "obvious" (I could have missed it, given so many) among them (which I hope has the merit of being readily understood by junior programmers, something I think we tend to undervalue in the corporate world!). Herewith, and hoping I didn't misunderstand the requirements:

4 years ago
Hmm, can't work out how to edit that, but please send Breanne email at

Sorry about the confusion!

4 years ago
Ahhhh whoops, that's embarrassing, I just assumed the link had what was needed. Sigh!

I have asked how she'd prefer to be contacted... I'll post that as soon as she gets back to me.

4 years ago
Hi all,

A colleague of mine at Oracle is looking for people who would like to write material for an Oracle blog and asked me if I could share this message here on JavaRanch. I hope it's OK to post this here, but here is her message:


My name is Breanne Wilson. I'm the editor of the Oracle University blog:

We're looking for Java related content to help educate our technical audience. I'd like to invite you to guest post on our blog.

You can of course link directly back to your website or LinkedIn Profile within the post. We will also share the guest post on our Facebook page, which has over 94,000 followers, as well as our Twitter, which has 36,000+ followers.

Please let me know if you're interested.

Thank you!

4 years ago