Tutorial by Examples

Even experienced Java developers tend to think that Java has only three protection modifiers. The language actually has four! The package private (a.k.a. default) level of visibility is often forgotten. You should pay attention to what methods you make public. The public methods in an application a...
These Java issues can be very embarrassing, and sometimes remain undiscovered until run in production. Fallthrough behavior in switch statements is often useful; however, missing a “break” keyword when such behavior is not desired can lead to disastrous results. If you have forgotten to put a “break...
This is a mistake that causes real confusion for Java beginners, at least the first time that they do it. Instead of writing this: if (feeling == HAPPY) System.out.println("Smile"); else System.out.println("Frown"); they accidentally write this: if (feeling == HAP...
The latest version of the Oracle Java style guide mandates that the "then" and "else" statements in an if statement should always be enclosed in "braces" or "curly brackets". Similar rules apply to the bodies of various loop statements. if (a) { //...
Consider the following example: public final class Person { private final String firstName; private final String lastName; public Person(String firstName, String lastName) { this.firstName = (firstName == null) ? "" : firstName; this.lastName = (lastN...
Consider the following code snippet: // Print the sum of the numbers 1 to 10 int count = 0; for (int i = 1; i < 010; i++) { // Mistake here .... count = count + i; } System.out.println("The sum of 1 to 10 is " + count); A Java beginner might be surprised to know that the...
Sometimes, programmers who are new to Java make the mistake of defining a class with a name that is the same as a widely used class. For example: package com.example; /** * My string utilities */ public class String { .... } Then they wonder why they get unexpected errors. For ex...
Sometimes a new Java programmer will write code like this: public void check(boolean ok) { if (ok == true) { // Note 'ok == true' System.out.println("It is OK"); } } An experienced programmer would spot that as being clumsy and want to rewrite it as: publ...
Consider the following partial example: import com.example.somelib.*; import com.acme.otherlib.*; public class Test { private Context x = new Context(); // from com.example.somelib ... } Suppose that when when you first developed the code against version 1.0 of somelib and versi...
A question that occasionally on StackOverflow is whether it is appropriate to use assert to validate arguments supplied to a method, or even inputs provided by the user. The simple answer is that it is not appropriate. Better alternatives include: Throwing an IllegalArgumentException using cust...
public class Foobar { public static void main(String[] args) { // example: Boolean ignore = null; if (ignore == false) { System.out.println("Do not ignore!"); } } } The pitfall here is that null is compared to false. Since w...

Page 1 of 1