I’ve started learning for Oracle Java certificate 1Z0-819. Although experience Java developers do not seem to bother much about certification, for a newcomer like me it is a good thing to do and I actually learn an incredible lot from it.

The test is multiple choice, in which you often need to select more than one good answer. Guessing might give you a 10% score, while 68% is required to pass.

I started with buying a book with exercises and what I learned thus far is that over the years the makers of Java have introduced quite a lot of new syntax that let you do things you could already do before but with a lot more code. The language got richer with more efficient ways of saying things and that requires some more learning.

For example, the simple switch statement would normally look like this:

switch (variable) {
	case "a": case "b":
		<some statement(s)>
		break;
	case "c":
		<some other statement(s)>
		break;
	default:
		<some other statement(s)>
		break;
}

But you can also do this:

Integer myInteger = switch (variable) {
	case "a","b" -> 27;
	case "c" -> 29;
	case "d" -> {
		yield 51;
	}
	default -> 0;
}

With this construct you can turn the switch-statement into an expression that returns a value. It is not called a switch statement anymore but a switch expression, as it has become part of an assignment.

Furthermore, you can work with yield, you can put the default case at some other place, and you can sometimes use ‘return’ (only in switch statements, not in switch expressions). And by the way, before Java 7, it was not possible to use strings in the switch statement expression. And of course you need to be aware of things that happen when you forget the break (or return) statement, and you must know that the continue statement is not allowed (and doesn’t make sense), and that in switch expressions (but not statements) the cases must be exhaustive (as you would otherwise would have no return value to assign to the variable at the other side of the =.

So, I learned this one. Few more to go.


<
Previous Post
Swing, threads and code structure
>
Next Post
Interfaces ii