Java Character Classes With Best Examples

What is Java Character Classes

The Java Character Class is an instance of a type Character that 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 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.");
   }
}

Output:

She told “that she likes!” me.

In order for you to test the Java code provided in this lesson, you must test the code in your code editor.

You can test the above example here! ➡Java Online Compiler 

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.


Leave a Comment