In java there are a few constructs that make it easier for you to create and simplify some things, but they will also make it harder for you to read other peoples code if you don't know them.
a = b = c;
This is a statement that saves the value c
in the variables a
and b
. This is an easy way to initialize multiple variables to the same value. It can also be applied to as many variables as you want.
do {…} while(…);
Sometimes you want to have a loop that is executed once without checking the condition and then repeating it until the condition is no longer true. In that case you can use a do-while-loop:
do {
// Whatever you want to do
// Anything in here will be executed at least once regardless of the condition.
} while(condition);
do
can spare you some copied and pasted code, that would fill your file with junk.
Fast creation of small subclasses or interfaced classes
Sometimes you can get into the situation that you need to create multiple different classes that either implement an interface or extend another class. You could just create a new class, but that takes a new file or a lot of space. Also the class itself is usually far away from the one point where it is used and needed. Therefor it is hard to find the class when you search for it.
An example of such a situation described is Multithreadding. Any time you need to create multiple threads you need to have either some Runnable
s or some Thread
s. Both can be easy created using this construct:
// Version one: Creating the runnable interface:
…
new Thread( // Creating a new thread…
new Runnable() { // …using a new class that implements Runnable
public void run() {
// Put whatever the thread should do here.
}
});
…
// Version two: Creating a new Thread-subclass:
…
new Thread() { // Create a new Thread subclass
public void run() {
// Put whatever the thread should do here.
}
};
…
?
(aka ternary operator)
When programming you will often come across the following construct:
int someVariable; // Create any variable
if(someCondition) { // Assign some value x to the variable if the ondition is true
someVariable = x;
}
else { // otherwise assign y to the variable
someVariable = y;
}
Using the ternary operator this can be simplified as:
int someVariable = someCondition ? x : y;
The ternary operator can also be used for functions:
if(someCondition) { // Call some function if the ondition is true
someFunction();
}
else { // otherwise call another function
someOtherFunction()
}
↓
someCondition ? someFunction() : someOtherFunction
shorter if/else/for/while
If you have an if/else/for/while with only one expression afterwards like this:
if(someCondition) {
doSomething();
}
then you can remove the {
/}
-brackets:
if(SomeCondition)
doSomething();
switch
switch
is a faster and more readable way when you need to have a variable and need to do different behaviour at multiple different values. For example if you would have this code:
// There is an int x, which can take the values 0-5:
if(x == 5)
function1();
else if(x == 4)
function2();
…
else if(x == 2)
function4();
else // Call function5 if x = 1 or x = 0
function5();
This can be simplified using switch:
switch(x) {
case 5:
function1();
break; // If you don't break, the functionality defined after the next case will also be called.
case 4:
function2();
break;
…
case 2:
function4();
break;
case 1:// 0 and 1 lead to the same behaviour → no break between them.
case 0:
function5();
break;
}
switch
may seem a bit bulky on first sight due to its many breaks, but it is faster then the if-else if-else structure. Also it is simpler to read because of only having simple cases.
Additional to all those cases you can add a default:
which will be called when the value doesn't fit any of the cases.
Unicode
Java allows you to use any Unicode symbols in code(e.g. as variable names) or String values. So in java you can name your variables ħ ĸ λ đ ŋ Ħ or whatever else you want. This includes indices like ₁₂₃₄₅₆₇₈₉₀₍₎₊₋ and over 137,000(claim by wikipedia) more.
Today another piece of code from my quantum physics app that contains (part of) a big switch(to replace normal letters in a text by greek ones):