How do I check if a character is a whitespace in Java?

Whitespace characters in Java (or programming in general) aren’t just the space ' ' character. It also includes other characters that create some form of space or break in the text. The most common ones include:

  • space ' '
  • tab '\t'
  • newline '\n'
  • carriage return '\r'
  • form feed '\f'.

All these characters fall into the category of whitespace characters.

Now, if we want to check if a character in Java is one of these whitespace characters, we can make use of the built-in method Character.isWhitespace(char ch). Character is a class in Java that provides a number of useful class (i.e., static) methods for working with characters. And the isWhitespace() method is one of them which checks if the provided character is a whitespace character.

Here is a simple code snippet:

package org.kodejava.lang;

public class CharacterIsWhitespace {
    public static void main(String[] args) {
        char ch = ' ';

        if (Character.isWhitespace(ch)) {
            System.out.println(ch + " is a whitespace character.");
        } else {
            System.out.println(ch + " is not a whitespace character.");
        }
    }
}

This code first defines a character ch and then uses Character.isWhitespace(ch) to check if it is a whitespace character. The isWhitespace() method returns true if the given character is a space, new line, tab, or other whitespace characters, false otherwise.

Here’s a little more expansive example:

package org.kodejava.lang;

import java.util.Arrays;
import java.util.List;

public class CharacterIsWhitespaceDemo {
    public static void main(String[] args) {
        List<Character> characters = Arrays.asList(' ', '\t', '\n', '\r', '\f', 'a', '1');
        for (char ch : characters) {
            if (Character.isWhitespace(ch)) {
                System.out.println("'" + ch + "' is a whitespace character.");
            } else {
                System.out.println("'" + ch + "' is not a whitespace character.");
            }
        }
    }
}

Output:

' ' is a whitespace character.
'   ' is a whitespace character.
'
' is a whitespace character.
' is a whitespace character.
'' is a whitespace character.
'a' is not a whitespace character.
'1' is not a whitespace character.

In this code snippet, we are checking and outputting whether each character in a list of characters is a whitespace character or not. The list includes a space, a tab, newline, carriage return, form feed, an alphabetic character, and a digit. The isWhitespace() method identifies correctly which ones are the whitespace characters.

The Character.isWhitespace(char ch) method in Java also considers Unicode whitespace. It checks for whitespace according to the Unicode standard. The method considers a character as a whitespace if and only if it is a Unicode space separator (category “Zs”), or if it is one of the following explicit characters:

  • U+0009, HORIZONTAL TABULATION (‘\t’)
  • U+000A, LINE FEED (‘\n’)
  • U+000B, VERTICAL TABULATION
  • U+000C, FORM FEED (‘\f’)
  • U+000D, CARRIAGE RETURN (‘\r’)

Here is an example of checking Unicode whitespace:

package org.kodejava.lang;

public class CharacterIsWhitespaceUnicode {
    public static void main(String[] args) {
        char ch = '\u2003';  // EM SPACE

        if (Character.isWhitespace(ch)) {
            System.out.println("Character '" + ch + "' (\\u2003) is a whitespace character.");
        } else {
            System.out.println("Character '" + ch + "' (\\u2003) is not a whitespace character.");
        }
    }
}

Output:

Character ' ' (\u2003) is a whitespace character.

In this example, \u2003 is a Unicode representation of the “EM SPACE” character, which is a type of space character in the Unicode standard. The isWhitespace() method correctly identifies it as a whitespace character.

What is the purpose of String.strip() method of Java 11?

The purpose of the String.strip() method in Java 11 is to remove whitespaces from both the beginning and end of a string. This is very similar to the String.trim() method available in earlier versions of Java, but there is a key difference between them.

Here’s the difference:

  • String.strip(): Introduced in Java 11, strip() uses the unicode definition of whitespace. It removes not only space characters but also all other types of unicode-defined spaces, such as the thin space \u2009, etc.
  • String.trim(): Available from Java 1.0, trim() is more limited. It considers a whitespace to be any character whose ASCII value is less than or equal to 32 (a space, tab, newline, and a few other control characters).

Here are examples of how they work:

package org.kodejava.lang;

public class StringStripExample {
    public static void main(String[] args) {
        // String.strip()
        String first = " \u2009Hello  ";
        System.out.println(first.strip()); // Outputs "Hello"

        // String.trim()
        String second = " \u2009Hello  ";
        System.out.println(second.trim()); // Outputs "\u2009Hello"
    }
}

Output:

Hello
 Hello

Thus, strip() method is more comprehensive in removing different types of whitespace defined in Unicode, while trim() only removes ASCII control characters and spaces.

There are also String.stripLeading() and String.stripTrailing() methods that were introduced in Java 11, and they are similar to the strip() method, but they only remove the whitespace characters from either the beginning or the end of the string, respectively.

Here is what they do:

  • String.stripLeading(): This method removes any leading whitespace from the string. “Leading” in this context means any whitespace characters at the beginning of the string.
  • String.stripTrailing(): This method removes any trailing whitespace from the string. “Trailing” in this context means any whitespace characters at the end of the string.

Both stripLeading() and stripTrailing() use the Unicode definition of whitespace, the same as strip() method.

Here are examples of how they work:

package org.kodejava.lang;

public class StringStripLeadingTrailingExample {
    public static void main(String[] args) {
        // Strip leading whitespace
        String first = " \u2009Hello World  ";
        System.out.println(first.stripLeading());  // Outputs "Hello World  "

        // Strip trailing whitespace
        String second = " \u2009Hello World  ";
        System.out.println(second.stripTrailing()); // Outputs " \u2009Hello World"
    }
}

Output:

Hello World  
  Hello World

As demonstrated, stripLeading() removed the whitespace characters from the front of the string, and stripTrailing() removed the whitespace characters from the end of the string.

While \u00A0 is technically a type of whitespace (specifically, a non-breaking space or NBSP), it isn’t considered as such by the strip(), stripLeading(), and stripTrailing() methods, which follow the Character.isWhitespace(char) method’s definition of what constitutes a whitespace character.

According to the Java documentation, the Character.isWhitespace(char) method, which the strip() methods use, considers the following characters as whitespace:

  • ‘\t’ U+0009 HORIZONTAL TABULATION
  • ‘\n’ U+000A LINE FEED
  • ‘\u000B’ U+000B VERTICAL TABULATION
  • ‘\f’ U+000C FORM FEED
  • ‘\r’ U+000D CARRIAGE RETURN
  • ‘\u001C’ U+001C FILE SEPARATOR
  • ‘\u001D’ U+001D GROUP SEPARATOR
  • ‘\u001E’ U+001E RECORD SEPARATOR
  • ‘\u001F’ U+001F UNIT SEPARATOR
  • SPACE_SEPARATOR category types

The \u2009 (thin space) and \u0020 (space) are part of SPACE_SEPARATOR category according to Unicode standard and will be correctly stripped.

The \u00A0 (non-breaking space) is actually part of a different category called the NO-BREAK_SPACE and is not considered whitespace by Character.isWhitespace(char), so it won’t be stripped.

How do I use String.join() method in Java?

The String.join() method in Java is a static method added in Java 8 to the java.lang.String class. The String.join() is a static utility method used to concatenate multiple strings, arrays or collections (like lists and sets) of strings. This method makes it easier to join multiple strings with a specific delimiter. A delimiter is a sequence of characters used to separate strings.

This method returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter. This method saves us from writing boilerplate loop code just for concatenating strings with a delimiter.

Here is an example of how you can use it:

package org.kodejava.lang;

import java.util.Arrays;
import java.util.List;

public class StringJoinList {
    public static void main(String[] args) {
        List<String> list = Arrays.asList("Java", "is", "cool");
        String result = String.join(" ", list);
        System.out.println(result);
    }
}

Output:

Java is cool

In this example, String.join() takes two parameters:

  1. A delimiter that is a CharSequence (like a String) that is placed between each joined String.
  2. An Iterable object like a List or a Set, over which the method iterates and joins all elements into a single String.

You can also use String.join() with an array of elements:

package org.kodejava.lang;

public class StringJoinArray {
    public static void main(String[] args) {
        String[] array = new String[]{"Java", "is", "cool"};
        String result = String.join(" ", array);
        System.out.println(result);
    }
}

Output:

Java is cool

In this case, String.join() still takes a delimiter as the first argument, but the second argument is an Array of elements to be joined.

How do I get the number of processors available to the JVM?

The Runtime.getRuntime().availableProcessors() method returns the maximum number of processors available to the Java virtual machine, the value will never be smaller than one. Knowing the number of available processor you can use it for example to limit the number of thread in your application when you are writing a multi-thread code.

package org.kodejava.lang;

public class NumberProcessorExample {
    public static void main(String[] args) {
        final int processors = Runtime.getRuntime().availableProcessors();
        System.out.println("Number of processors = " + processors);
    }
}

Running the code snippet give you something like:

Number of processors = 8

How do I find Java version?

The simplest way to get the Java version is by running the java -version command in your terminal application or Windows command prompt. If Java is installed and available on your path you can get information like below.

java -version                                     
java version "17" 2021-09-14 LTS
Java(TM) SE Runtime Environment (build 17+35-LTS-2724)                       
Java HotSpot(TM) 64-Bit Server VM (build 17+35-LTS-2724, mixed mode, sharing)

Using System Properties

But if you want to get Java version from your Java class or application you can obtain the Java version by calling the System.getProperty() method and provide the property key as argument. Here are some property keys that related to Java version that you can read from the system properties.

package org.kodejava.lang;

public class JavaVersion {
    public static void main(String[] args) {
        String version = System.getProperty("java.version");
        String versionDate = System.getProperty("java.version.date");
        String runtimeVersion = System.getProperty("java.runtime.version");
        String vmVersion = System.getProperty("java.vm.version");
        String classVersion = System.getProperty("java.class.version");
        String specificationVersion = System.getProperty("java.specification.version");
        String vmSpecificationVersion = System.getProperty("java.vm.specification.version");

        System.out.println("java.version: " + version);
        System.out.println("java.version.date: " + versionDate);
        System.out.println("java.runtime.version: " + runtimeVersion);
        System.out.println("java.vm.version: " + vmVersion);
        System.out.println("java.class.version: " + classVersion);
        System.out.println("java.specification.version: " + specificationVersion);
        System.out.println("java.vm.specification.version: " + vmSpecificationVersion);
    }
}

Running the code above give you output like the following:

java.version: 17
java.version.date: 2021-09-14
java.runtime.version: 17+35-LTS-2724
java.vm.version: 17+35-LTS-2724
java.class.version: 61.0
java.specification.version: 17
java.vm.specification.version: 17

Using Runtime.version()

Since JDK 9 we can use Runtime.version() to get Java runtime version. The feature(), interim(), update and patch() methods of the Runtime.Version class are added in JDK 10. These methods is a replacement for the major(), minor() and security() methods of JDK 9.

Below is the code snippet that demonstrate the Runtime.version().

package org.kodejava.lang;

public class RuntimeVersion {
    public static void main(String[] args) {
        System.out.println("Version: " + Runtime.version());
        System.out.println("Feature: " + Runtime.version().feature());
        System.out.println("Interim: " + Runtime.version().interim());
        System.out.println("Update: " + Runtime.version().update());
        System.out.println("Patch: " + Runtime.version().patch());
        System.out.println("Pre: " + Runtime.version().pre().orElse(""));
        System.out.println("Build: " + Runtime.version().build().orElse(null));
        System.out.println("Optional: " + Runtime.version().optional().orElse(""));
    }
}

Running the code snippet above produce the following output:

Version: 17+35-LTS-2724
Feature: 17
Interim: 0
Update: 0
Patch: 0
Pre: 
Build: 35
Optional: LTS-2724

Here are the summary of outputs running the above code using some JDKs installed on my machine.

Version Feature Interim Update Patch Pre Build Optional
10.0.2+13 10 0 2 0 13
11.0.6+8-LTS 11 0 6 0 8 LTS
12.0.2+10 12 0 2 0 10
13.0.2+8 13 0 2 0 8
14+36-1461 14 0 0 0 36 1461
15.0.2+7-27 15 0 2 0 7 27
17+35-LTS-2724 17 0 0 0 35 LTS-2724