Java: Char Data Type

Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

Whether you pronounce it 'char' or 'care', the Java char data type has no relation to burned beef or care. It is simply short for character (as on a keyboard). This lesson will provide an overview of the char data type, and some examples of its use.

The Java Char

In Java, char is short for character. It is 16 bits in size, double that of a byte. Most of the time however, the actual data stored in the char data type doesn't take up more than 8 bits; the reason Java allows 16 bits is so that all characters in all languages can be represented. This representation is in the Unicode format.

Let's take a quick look at Unicode, since it will help us to better understand how the char works.

Unicode

Unicode is a computer encoding methodology that assigns a unique number for every character. It doesn't matter what language, or computer platform it's on. This is important in a global, networked world, and for computer systems that must accommodate multiple languages and special characters. Unicode truly unifies all of these into a single standard.

In Unicode, all characters are represented by numeric values. For example, 65 is the letter A. 66 is B and so on. The lower-case letters start at 97. There are even special system-only characters in the list: carriage returns, tabs, etc. These can be useful in displaying text; while others are leftovers from older systems.

Examples: Declaring Char Variables

You'd think that a char could be any value from a to Z, and all the numbers. That is true, except for one key item. You can either declare a char variable with single quotes, e.g., setting a char's value to the letter a. Or, you could omit the quotes, and set the Unicode representation of the value. Take a look at the following code for declaring a char variable equal to 77.

char eighty_eight = 77;

The output is not going to be 77, but the Unicode representation. Remember when we said that a char is a single character of 16 bits?


Java Char Output Unicode


If we had tried to declare the char as the actual 77, with quotes, there would be an error. Most tools, such as NetBeans, tell you right away if there is an error, as seen in the following screen capture:


Java NetBeans Char Error


But it's perfectly fine to create a char and give it a value, as long as it's a single character!

char real_seven = 'M';

To unlock this lesson you must be a Study.com Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use Study.com

Become a Study.com member and start learning now.
Become a Member  Back
What teachers are saying about Study.com
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? Study.com has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account
Support