I ran into this block of code today, and I don't know how it works. I know how to make anonymous classes, but I'm used to seeing a method signature and not just a pair of braces. Is the code between those braces put into a static block? Does it go into the constructor? Or is it something else altogether?
conext.checking(new Expectations() {
{ // <- what does this pair of braces do?
oneOf(alarm).getAttackAlarm(null);
}
});
“Syntactic sugar” is a term for syntax changes in computer programming which make it easier for humans to code. There are several different types of syntactic sugar and synonyms include “syntactic saccharine” and even “candygrammar,” which is often used to describe superfluous or unhelpful “syntactic sugar” changes.
A common example of syntactic sugar is an augmented assignment operator, such as +=, which simplifies the expression x = x + y to the compound operation x += y.
for-in-loops, or to be more precise, iterator loops, are a simple syntactic sugar over a common practice within Rust, which is to loop over anything that implements IntoIterator until the iterator returned by .
1) Strings in Switch are syntactic sugar, no change in JVM level. 2) Internally it uses equals method to compare, which means, if you pass null it will throw java.
It's an instance initializer that calls the code within the context of the created object.
This is equivalent to
Expectations exp = new Expectations();
exp.oneOf(alarm).getAttackAlarm(null);
conext.checking(exp)
Whoever wrote it might have thought he was being more efficient by not declaring a variable (not true) or that it was cleaner code (I disagree).
The primary place that these initializers are useful like this is when instantiating maps, ie:
Map map = new HashMap() {{
put("key1", "value1");
put("key2", "value2");
}};
which I think actually is slightly more readable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With