Syntax
var VARIABLE = TYPED_VALUE;
Token | Description |
VARIABLE | An identifier the can hold a value. |
TYPED_VALUE | The value that has an identifiable type. |
Extremely Serious
var VARIABLE = TYPED_VALUE;
Token | Description |
VARIABLE | An identifier the can hold a value. |
TYPED_VALUE | The value that has an identifiable type. |
switch(VARIABLE) { case MATCH_1 [, MATCH_2][, MATCH_N] -> EXPRESSION; | THROW-STATEMENT; | BLOCK default -> EXPRESSION; | THROW-STATEMENT; | BLOCK }
Token | Description |
VARIABLE | The variable to test. |
MATCH_1, MATCH_2, MATCH_N | The value or values that can match the VARIABLE. |
EXPRESSION | A expression to execute. |
THROW-STATEMENT | A expression that throws an exception. |
BLOCK | A block of statements to execute.
If a return value is required use the yield keyword instead of return. |
public enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY; } Day day = Day.WEDNESDAY; int numLetters = switch (day) { case MONDAY, FRIDAY, SUNDAY -> 6; case TUESDAY -> 7; case THURSDAY, SATURDAY -> { System.out.println(8); yield 8; } case WEDNESDAY -> { System.out.println(9); yield 9; } default -> { throw new IllegalStateException("Invalid day: " + day); } }; System.out.println(numLetters);
Use the following java option to have a more verbose exception message especially for NullPointerException:
-XX:+ShowCodeDetailsInExceptionMessages
For example, if we have the following snippet:
var a = null; a.b = 1;
Running the above snippet will have the following exception:
Exception in thread "main" java.lang.NullPointerException: Cannot assign field "b" because "a" is null
Note: This is more useful if the classes were compiled with debugging info (i.e. javac -g )
This extension calculates the most efficient way of dividing the task to the different cores available.
Note: Parallel extensions will block the calling thread.
Method | Description |
Parallel.For | Executes for that may run in parallel. |
Parallel.ForEach | Executes foreach that may run in parallel. |
Parallel.Invoke | Executes actions that may run in parallel. |
A modifier that indicates that the method can be run asynchornously.
Wait for a result of the asynchronous operation once the data is available without blocking the current thread. It also validates the success of the asynchronous operation. If everything is good, the execution will continue after the await keywords on its original thread.
Run a task on a separate thread
ContinueWith Method
A task method that will be invoked after the running async task has returned a result.
The parameter TaskContinuationOption of this method can be use to control if the continue must be invoked if there are no exception or only with exception.CancellationTokenSource
CancellationTokenSource is used to signal that you want to cancel an operation that utilize the token. This token be passed as a second parameter for the Task.Run method.
Communicate with the thread that owns our UI.
A thread safe collection.
Cyclomatic complexity is the count of linearly independent paths in a program's source code. With the help of control flow graph, we can use the following formula to calculate this:
CC = E - N + 2P
Variable | Description |
E | The number of edges in the control flow graph. |
N | The number of nodes in the control flow graph. |
P | The number of connected components. This has a value of 1 if you are computing at the method (i.e. function or subroutine) level. |
Related
Control Flow Graph
Using some functionalities of a class but doesn't inherit (i.e. is-a relationship) it, is known as composition (i.e. has-a relationship).
Reasons to Favor Composition Over Inheritance
A method that accepts the same set of inputs will always produce the same output.
Java Example
java.lang.Boolean.valueOf("TRUE")
© 2025 Ron and Ella Wiki Page
Theme by Anders Noren — Up ↑
Recent Comments