Tutorial by Examples

TRACE and DEBUG log levels are there to be able to convey high detail about the operation of the given code at runtime. Setting the log level above these is usually recommended, however some care must be taken for these statements to not affect performance even when seemingly "turned off"....
Consider the following code as an illustration: public String joinWords(List<String> words) { String message = ""; for (String word : words) { message = message + " " + word; } return message; } Unfortunate this code is inefficient if the w...
The Java language allows you to use new to create instances Integer, Boolean and so on, but it is generally a bad idea. It is better to either use autoboxing (Java 5 and later) or the valueOf method. Integer i1 = new Integer(1); // BAD Integer i2 = 2; // BEST (autoboxing)...
Using new String(String) to duplicate a string is inefficient and almost always unnecessary. String objects are immutable, so there is no need to copy them to protect against changes. In some older versions of Java, String objects can share backing arrays with other String objects. In those ver...
It is (almost always) a bad idea to call System.gc(). The javadoc for the gc() method specifies the following: "Calling the gc method suggests that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick re...
Consider these two pieces of code: int a = 1000; int b = a + 1; and Integer a = 1000; Integer b = a + 1; Question: Which version is more efficient? Answer: The two versions look almost the identical, but the first version is a lot more efficient than the second one. The second version is...
The following example code is slower than it needs to be : Map<String, String> map = new HashMap<>(); for (String key : map.keySet()) { String value = map.get(key); // Do something with key and value } That is because it requires a map lookup (the get() method) for each ...
The Java Collections Framework provides two related methods for all Collection objects: size() returns the number of entries in a Collection, and isEmpty() method returns true if (and only if) the Collection is empty. Both methods can be used to test for collection emptiness. For example: C...
Regular expression matching is a powerful tool (in Java, and in other contexts) but it does have some drawbacks. One of these that regular expressions tends to be rather expensive. Pattern and Matcher instances should be reused Consider the following example: /** * Test if all strings in a lis...
When some programmers see this advice: "Testing strings using == is incorrect (unless the strings are interned)" their initial reaction is to intern strings so that they can use ==. (After all == is faster than calling String.equals(...), isn't it.) This is the wrong approach, from...
Consider the following code to copy one file to another: import java.io.*; public class FileCopy { public static void main(String[] args) throws Exception { try (InputStream is = new FileInputStream(args[0]); OutputStream os = new FileOutputStream(args[1])) { ...

Page 1 of 1