Java char type

We’ll cover the following

  • Introduction
  • Characters
  • Unicode
  • Escape Sequence
  • Examples
  • char vs int
  • NOTE

Introduction

In Java, the data type used to store characters is char. However, C/C++ programmers
beware: char in Java is not the same as char in C or C++. In C/C++, char is an integer
type that is 8 bits wide.

This is not the case in Java. Instead, Java uses Unicode to represent characters. Unicode defines a fully international character set that can represent all of the characters found in all human languages. It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more. For this purpose, it requires 16 bits.

Thus, in Java char is a 16-bit type. The range of a char is 0 to 65,536. There are no negative chars. The standard set of characters known as ASCII still ranges from 0 to 127 as always, and the extended 8-bit character set, ISO-Latin-1, ranges from 0 to 255. Since Java is designed to allow applets to be written for worldwide use, it makes sense that it would use Unicode to represent characters. Of course, the use of Unicode is somewhat inefficient for languages such as English, German, Spanish, or French, whose characters can easily be contained within 8 bits. But such is the price that must be paid for global portability.

Characters

The char type represents a single character from the Unicode character set.

A Java character type is not the same as a String.

A char variable can store just one character, not a sequence of characters, as a String can.

To assign a value to a char variable, you use a character literal, which is always enclosed in apostrophes(single quote) rather than quotes. Here’s an example:

char code = 'X';

Here the character X is assigned to the variable named code.

The following statement won’t compile:

char code = "X"; // error -- should use apostrophes, not quotes

The quotation marks in Java are used to mark Strings, not character constants.

You can also assign an integer value from 0 to 255 to a char variable, like this:

char cr = 13;

Here, the decimal value 13, which represents a carriage return, is assigned to the variable named cr.

Unicode

In Java, the data type used to store characters is char.

char in Java is not the same as char in C or C++.

Instead, Java uses Unicode to represent characters.

Unicode defines a fully international character set that can represent all of the characters found in all human languages.

It is a unification of dozens of character sets, such as Latin, Greek, Arabic, Cyrillic, Hebrew, Katakana, Hangul, and many more.

At the time of Java’s creation, Unicode required 16 bits.

Thus, in Java char is a 16-bit type.

The range of a char is 0 to 65,536.

There are no negative chars.

Currently, about 35,000 codes in the Unicode character set are defined, which leaves another 29,000 codes unused.

The first 256 characters in the Unicode character set are the same as the characters of the ASCII character set, which is the most commonly used character set for computers with Western languages.

More information about Unicode can be found at http://www.unicode.org.

Escape Sequence

Character literals can also use special escape sequences to represent special characters.

The following table lists the allowable escape sequences.

These escape sequences let you create literals for characters that can’t otherwise be typed within a character constant.

Escape SequenceExplanation
\bBackspace
\tHorizontal tab
\nLine feed
\fForm feed
\rCarriage return
\”Double quote
\’Single quote
\\Backslash

Example

Here is a program that demonstrates char variables:

package JAVA.Basic;

public class CharDemo {
    public static void main(String args[]) {
        char ch1, ch2;
        ch1 = 88; // code for X
        ch2 = 'Y';
        System.out.print("ch1 and ch2: ");
        System.out.println(ch1 + " " + ch2);
    }
}

This program displays the following output:

Notice that ch1 is assigned the value 88, which is the ASCII (and Unicode) value that corresponds to the letter X.

The ASCII character set occupies the first 127 values in the Unicode character set.

For this reason, all the “old tricks” that you may have used with characters in other languages will work in Java, too.

char vs int

Although char is designed to hold Unicode characters, it can also be used as an integer type on which you can perform arithmetic operations.

For example, you can add two characters together, or increment the value of a character variable.

Consider the following program:

// char variables behave like integers.

package JAVA.Basic;

public class CharVSInt {
    public static void main(String args[]) {
        char ch1;

        ch1 = 'X';
        System.out.println("ch1 contains " + ch1);

        ch1++; // increment ch1
        System.out.println("ch1 is now " + ch1);
    }
}

The output generated by this program is shown here:

In the program, ch1 is first given the value X.

Next, ch1 is incremented. This results in ch1 containing Y, the next character in the ASCII (and Unicode) sequence.

NOTE

In the formal specification for Java, char is referred to as an integral type, which means that it is in the same general category as int, short, long, and byte.

That’s it!
You have successfully completed the post. Do Share : )

Peace Out!

Also Read – Java Floating point types


Check Out Deals on -> Amazon , Flipkart , Myntra , Adidas , Apple TV , Boat , Canva , Beardo , Coursera , Cleartrip , Fiverr , MamaEarth , Swiggy


One comment

Leave a Reply

Your email address will not be published.