Posts tagged OCJP
Basics of the switch statement in Java
1If statements are great, but sometimes they are just not very practical when you have to test for more than a handful of conditions, have a look at this very poor example of how you build up a long set of if-else statements, its fine for a few checks, but if you’re doing multiple equality checks on a value, its best to use a switch statement for flexibility and readability:
String concatenation operator
0The string concatenation operator is a bit like a hedgehog, it looks cute and sweet, but try to grab hold of it quickly and you’ll soon know about it…

It’s actually quite simple, however there are some traps that are very easily overlooked (just got caught out with it on a mock test, so I’m here to rant).
The rule of thumb is, that if either one of the operands being “+” is a String, then concatenation will occur, but if they are both numbers, arithmetic addition will occur. Sounds simple right? What do you think the following equate to?
The instanceof Operator
1The instanceof operator is a great way for checking if a reference variable is of a given type, in other words, does it pass the IS-A inheritance test. Ask yourself, is a Cat a Dog? A Dog a Cat? Or is a Cat an Animal and a Dog an Animal? In true Harry Hill style, there is only one way to decide….FIGHT!!!!

Have a look at my following example :
Compound Assignment Operators
0There are various compound assignment operators, however it is only necessary to know the 4 basic compound assignment operators for the exam, being as follows:
- += (Addition compound)
- -= (Subtraction compound)
- *= (Multiplication compound)
- /= (Division compound)
Essentially, they’re just a lazy way for developers to cut down on a few key strokes when typing their assignments. Consider the following examples that I’ve created to demonstrate this:
Other modifiers, for members…
0We’ve already touched upon various modifiers, for classes (both access and non-access), but there are also some more modifiers for members, as detailed here.
We have the following modifiers for members :
- final - Can’t be overridden
- abstract - No implementation specified, subclass must implement
- synchronized - Only a single thread of execution can pass through at a given time
- native - Implemented by a 3rd party piece of code (C++ for example)
- strictfp - Enforces foating point precision and doesn’t let the JVM do its own way
Class Modifiers (non-access)
1In addition to class access modifiers, classes can also be marked with non-access modifiers. These modifiers imply rules on a class, but are not necessarily linked to access rights.
The following non-access modifiers are available:
- final - The class can’t be extended.
- abstract - The class has to be extended, and can’t be instantiated on its own.
- strictfp - Ensures that you always get exactly the same results from your floating point calculations on every platform your application runs on. If you don’t use strictfp, the JVM implementation is free to use extra precision where available.
Class Access Modifiers
2Class access modifiers define who can see the class, you use it on a daily basis, have a look at the following :
public class ClassAccessModifierExample
{
public static void main(String[] args)
{
// do something!
}
}
There you go, you said “public class”, thats you saying that this class is public, and anything can access it.
It is important to note, that even though a class may be declared as public, you can still have private members within that class. The class access modifier merely states who can gain access to the class.
Varargs, for those times when you’re not sure how many parameters you’ll have…
0As of Java 5, methods are now able to accept from 0 to many arguments. Sounds confusing, but you could actually be using it already without knowing, how about looking at your main methods?
public static void main(String... args)
As we can see above, var args are declared as TYPE… NAMEOFVARIABLE. Lets take a look at a basic example I’ve created :

