Java Character Classes With Best Examples

In this tutorial, we’ll talk about Java Character Classes, this is a type of data that can be stored using just one character. A single quote must surround a character value, like “G” or “m.” I will explain these Char Class In Java in a simple way so that you can understand and use them in your future projects.

What Is Java Character Classes

The Java Character Class is an instance of a type Character has a single field whose type is char. The Character class has a number of useful (i.e., static) methods for manipulating characters. You can use the character constructor to make a character object.

How To Declare Char In Java

To declare a Char In Java, we can use any char value, like an empty char, 0, or even another char value. When making a char variable, we have to figure out if the declared variable is local or instance. If it’s local, we have to set it up when we declare it.

Without further ado, I will explain it in a simple way.

When working with characters, we usually use the char primitive data type.

Java-S: Character Stream
Java-S: Character Stream

Java Character Classes Example

char chr = 'g';

// sample created by Glenn Magada Azuelo
char uniChr = '\u039A'; 

// an array of chars
char[] charArray ={ 'm', 'd', 'g', 'd', 'e' }; 

But there are times in development when we have to use objects instead of primitive data types. To do this, Java has a class called Java Character Class that wraps the primitive data type char.

Character chr = new Character(‘g’);

Some of the time, the Java compiler will also make a Character object for you. For example, if you pass a primitive char to a method that expects an object, the compiler changes the char to a Character for you. This is called autoboxing or unboxing, depending on which way the conversion goes.

// Here following primitive char 'g'
// is boxed into the Character object ch
Character chr = 'g';

// Here primitive 'x' is boxed for method test,
// return is unboxed to char 'm'
char m = test('x');

Escape Sequences In Java

The Escape Sequences In Java are used to show that a group of characters should be read differently. A character followed by a backslash () in Java is called an escape sequence. The Java compiler sees an escape sequence as one character with a special meaning.

A character that comes before a backslash () is called an escape sequence, and the compiler knows what it means.

In many of the System.out.println() statements in this tutorial, the newline character (n) has been used to move to the next line after the string has been printed.

The Java escape sequences are shown in the table below.

Escape SequenceDescription
\tInserts a tab in the text at this point.
\bInserts a backspace in the text at this point.
\nInserts a newline in the text at this point.
\rInserts a carriage return in the text at this point.
\fInserts a form feed in the text at this point.
\’Inserts a single quote character in the text at this point.
\”Inserts a double quote character in the text at this point.
\\Inserts a backslash character in the text at this point.
Escape Sequences In Java

When a compiler sees an escape sequence in a print statement, it knows how to interpret it.

Escape Sequences In Java Example Program

If you want to put quotes inside of quotes, you must use the escape sequence, “”, on the inner quotes.

// program by Glenn Magada Azuelo
public class Test {

   public static void main(String args[]) {
      System.out.println("She told \"that she likes!\" me.");
   }
}

This will lead to the following result

Output

She told “that she likes!” me.

In order for you to test your Java code provided in this lesson, you must test the code in your code editor. But if you wish to run this code online, we also have an online compiler in Java for you to test your java code for free.

We also provide a Java Online Compiler for every code below. Just scroll down so that you can easily test or execute the source code.

Character Methods

Here is a list of the most important methods that all subclasses of the Character class use.

Sr.No.Method & Description
1isletter()
Determines whether the specified char value is a letter.
2isDigit()
Determines whether the specified char value is a digit.
3isWhitespace()
Determines whether the specified char value is white space.
4isUpperCase()
Determines whether the specified char value is uppercase.
5isLowerCase()
Determines whether the specified char value is lowercase.
6toUpperCase()
Returns the uppercase form of the specified char value.
7toLowerCase()
Returns the lowercase form of the specified char value.
8toString()
Returns a String object representing the specified character value that is, a one-character string.
Character Methods

Refer to the java.lang.Character API specification for a full list of methods.

Conclusion

This article was about Java Character Classes. We talked about everything you need to know and how this class can help you develop in Java so you can make your own unique application. This chapter can help you learn Java faster. This could be one of the best ways to get better at Java development.

What’s Next

In the next section, we’ll talk about String Class In Java. You will learn how to declare Strings and use them effectively, as well as how to use some of the most important String methods.


Leave a Comment