- Class Character
- Unicode Conformance
- Unicode Character Representations
- How to Represent Empty char in Java
- How to Represent Empty char in Java?
- Method 1: Represent Empty char in Java Using Empty Single Quotes
- Method 2: Represent Empty char in Java Using Null Character
- Method 3: Represent Empty char in Java Using Unicode Value (\u0000)
- Method 4: Represent Empty char in Java Using MIN_VALUE Constant
- Conclusion
- About the author
- Farah Batool
- Class Character
- Unicode Conformance
- Unicode Character Representations
Class Character
The Character class wraps a value of the primitive type char in an object. An object of class Character contains a single field whose type is char .
In addition, this class provides a large number of static methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.
Unicode Conformance
The fields and methods of class Character are defined in terms of character information from the Unicode Standard, specifically the UnicodeData file that is part of the Unicode Character Database. This file specifies properties including name and category for every assigned Unicode code point or character range. The file is available from the Unicode Consortium at http://www.unicode.org.
Character information is based on the Unicode Standard, version 13.0.
The Java platform has supported different versions of the Unicode Standard over time. Upgrades to newer versions of the Unicode Standard occurred in the following Java releases, each indicating the new version:
Java release | Unicode version |
---|---|
Java SE 15 | Unicode 13.0 |
Java SE 13 | Unicode 12.1 |
Java SE 12 | Unicode 11.0 |
Java SE 11 | Unicode 10.0 |
Java SE 9 | Unicode 8.0 |
Java SE 8 | Unicode 6.2 |
Java SE 7 | Unicode 6.0 |
Java SE 5.0 | Unicode 4.0 |
Java SE 1.4 | Unicode 3.0 |
JDK 1.1 | Unicode 2.0 |
JDK 1.0.2 | Unicode 1.1.5 |
Variations from these base Unicode versions, such as recognized appendixes, are documented elsewhere.
Unicode Character Representations
The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the Unicode Standard.)
The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).
- The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter(‘\uD840’) returns false , even though this specific value if followed by any low-surrogate value in a string would represent a letter.
- The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).
In the Java SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode Glossary.
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.
How to Represent Empty char in Java
“char” is a primitive data type belonging to the Character class used to declare char type variables represented by the char Java keyword. Java allows you to use an empty char[] array, but not an empty char; as a char must represent at least one character, the null or an empty char makes no sense.
This tutorial will show you how to represent an empty string in Java. So, let’s start!
How to Represent Empty char in Java?
To represent an empty char in Java, use the following methods:
Let’s understand how these methods help in the specified purpose.
Method 1: Represent Empty char in Java Using Empty Single Quotes
When we come across such scenarios where it is required to declare an empty “char”, the first thing that comes to mind is to assign single quotes (”) as a “char”. A compile-time error is generated by Java as a result of this method because an empty value of char does not belong to any character.
Example 1
We will assign a single quote (”) to a character type variable named “ch”:
It will show an error “invalid character constant”:
For solving the above problem, we will enter a space between single quotes (‘ ‘) as a character. The space will act as a character, and Java will consider it as a null or empty char:
We will print the value of the char variable “ch” using print statement:
The output signifies that the char variable “ch” is empty:
Method 2: Represent Empty char in Java Using Null Character
You can also represent a char as an empty character using a “Null Character” (\0), where the character “\0” denotes a null value.
Let’s see an example of how \0 acts as an empty character.
Example
Here, we will assign a null character to a char variable “ch”:
Then, print the out the character value with the help of the “System.out.println()” method:
The output shows that the char “ch” is empty:
Method 3: Represent Empty char in Java Using Unicode Value (\u0000)
Another method to represent an empty char is utilizing the Unicode value “\u0000”. The given Unicode represents a null value.
Let’s move towards an example for better understanding.
Example
In this example, we will allocate a Unicode value “\u0000” to a “ch” variable:
Then, we will print the created empty char on the console:
The output indicates that char is null:
Method 4: Represent Empty char in Java Using MIN_VALUE Constant
“MIN_VALUE Constant” represents a character instance’s minimum value. In Java, the minimum value is an “u0000”, which can be retrieved by utilizing the MIN_ VALUE constant that belongs to the Character class, and passed to any char type variable for creating an empty character.
Example
We will now assign the constant of the Character class named “MIN_VALUE” constant to a char variable “ch”:
Then, print the variable “ch” to see either it is empty or not:
The output indicates that the “ch” is empty:
We gathered all the methods to represent an empty char in Java.
Conclusion
To represent empty char in Java, you can use different methods such as empty single quotes, null character (\0), Unicode value (\u0000), MIN_VALUE Constant of the Character class. In empty single quotes, if you do not enter space between quotes, it throws an error because an empty char value does not belong to any character. To resolve this issue, enter a space between single quotes. This tutorial explained the methods for representing empty char in Java with detailed examples.
About the author
Farah Batool
I completed my master’s degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.
Class Character
The Character class wraps a value of the primitive type char in an object. An object of class Character contains a single field whose type is char .
In addition, this class provides a large number of static methods for determining a character’s category (lowercase letter, digit, etc.) and for converting characters from uppercase to lowercase and vice versa.
Unicode Conformance
The fields and methods of class Character are defined in terms of character information from the Unicode Standard, specifically the UnicodeData file that is part of the Unicode Character Database. This file specifies properties including name and category for every assigned Unicode code point or character range. The file is available from the Unicode Consortium at http://www.unicode.org.
Character information is based on the Unicode Standard, version 15.0.
The Java platform has supported different versions of the Unicode Standard over time. Upgrades to newer versions of the Unicode Standard occurred in the following Java releases, each indicating the new version:
Java release | Unicode version |
---|---|
Java SE 20 | Unicode 15.0 |
Java SE 19 | Unicode 14.0 |
Java SE 15 | Unicode 13.0 |
Java SE 13 | Unicode 12.1 |
Java SE 12 | Unicode 11.0 |
Java SE 11 | Unicode 10.0 |
Java SE 9 | Unicode 8.0 |
Java SE 8 | Unicode 6.2 |
Java SE 7 | Unicode 6.0 |
Java SE 5.0 | Unicode 4.0 |
Java SE 1.4 | Unicode 3.0 |
JDK 1.1 | Unicode 2.0 |
JDK 1.0.2 | Unicode 1.1.5 |
Variations from these base Unicode versions, such as recognized appendixes, are documented elsewhere.
Unicode Character Representations
The char data type (and therefore the value that a Character object encapsulates) are based on the original Unicode specification, which defined characters as fixed-width 16-bit entities. The Unicode Standard has since been changed to allow for characters whose representation requires more than 16 bits. The range of legal code points is now U+0000 to U+10FFFF, known as Unicode scalar value. (Refer to the definition of the U+n notation in the Unicode Standard.)
The set of characters from U+0000 to U+FFFF is sometimes referred to as the Basic Multilingual Plane (BMP). Characters whose code points are greater than U+FFFF are called supplementary characters. The Java platform uses the UTF-16 representation in char arrays and in the String and StringBuffer classes. In this representation, supplementary characters are represented as a pair of char values, the first from the high-surrogates range, (\uD800-\uDBFF), the second from the low-surrogates range (\uDC00-\uDFFF).
- The methods that only accept a char value cannot support supplementary characters. They treat char values from the surrogate ranges as undefined characters. For example, Character.isLetter(‘\uD840’) returns false , even though this specific value if followed by any low-surrogate value in a string would represent a letter.
- The methods that accept an int value support all Unicode characters, including supplementary characters. For example, Character.isLetter(0x2F81A) returns true because the code point value represents a letter (a CJK ideograph).
In the Java SE API documentation, Unicode code point is used for character values in the range between U+0000 and U+10FFFF, and Unicode code unit is used for 16-bit char values that are code units of the UTF-16 encoding. For more information on Unicode terminology, refer to the Unicode Glossary.
This is a value-based class; programmers should treat instances that are equal as interchangeable and should not use instances for synchronization, or unpredictable behavior may occur. For example, in a future release, synchronization may fail.