20 - The Character Data Type in Java Programming

Sharing buttons:

hello welcome to lesson 20 of mastering

Java I'm Jason with code learner comm

here we're going to learn about a new

data type one that we do have occasion

to use pretty often and it's called the

character data type and it basically

holds characters it holds the letters of

the alphabet single digit single

character so it's what it's called now

to declare a character data type the

keyword is much like we've have int and

float and double we have car CH AR or

care for character we can really name it

anything we want let's call it a

character okay now when you want to

assign or initialize this character with

some value you can put any letter of the

alphabet or any number or whatever you

want in there so let's do a single quote

or like that

what's going on here is we're putting

the character R into this variable so

let me show you if we want to print this

just like we always do

then typically we can put like this and

we're just going to print out what we

know to be true which is the letter R

here so simply a character data type is

a data type that stores these letters of

the alphabet and also symbols on your

keyboard like exclamation point even the

numbers and things like that and we

store it in there and we can print them

out so depending on the kind of program

you're writing you may need to access

individual characters like this and this

is a good way to store them now one

thing I want to show you and tell you is

that behind the scenes every letter of

the alphabet every symbol on your

keyboard such as the ampersand or the

number sign or the exclamation mark any

of those symbols really is just what we

display on the screen but there's a code

behind the scenes a number code

corresponds to each one of those

characters on the keyboard Java uses the

unicode character set and so that means

there's a digit that is represented

behind the scenes that represents our

like this there's a digit behind the

scenes that represents a right now I'm

putting the capital A in here and I'm

running it and Java is printing a

capital A on the console but behind the

scenes there's some number that

represents capital A so even though

these characters variables are really

intended to hold letters like this and

symbols really you can kind of think of

them as integers and when you change the

number behind the scenes you're getting

a different letter and to kind of

illustrate that a little bit more we

have the character a right here and

we're putting that on the screen as

capital a because numbers really

represent characters on every computer

if you take character and increment it

by 1 that means character is equal to

character plus 1 and then take the exact

same statement so I'll just copy this

and I'll bring it down and I'll print

the same thing out again then what's

going to happen is we're going to get B

and if we increment character again I'll

copy this again and I'll drag it down

here if we increment things again and

print the same thing out again what do

you think is gonna happen we get C so

what I'm trying to say is whenever

you're assigning these characters in

single quotes to a care variable

variable like that you can think of it

in terms of the letter that's being

assigned but you can also think about it

as some code behind the scenes so that

you can increment and decrement the

character even though it's of care of

type care you're still allowed to

increment and decrement it which is

basically increasing and decreasing the

number that represents the letter so

when you print it on the screen you see

it updated accordingly all right so what

I'll do now is I'll erase all this stuff

actually I'll erase this stuff right

here and I'll kind of show you something

that's very very similarly related here

we've taken the variable that we have

and we have signed the letter A and

we've printed it out to the screen but I

could also say character and I can just

assign a number to it I could say 85 and

then I can print this guy back out to

the screen like this and let's see what

it prints it prints the letter U capital

u so the number 85 behind the scenes in

Unicode represents the letter capital u

so that even though this is of type care

we can still assign numbers to

and the system knows that when we try to

print them out and we want to print them

as characters because it's a character

variable so you can kind of think of a

character variable as really an integer

datatype with a with a smaller range

obviously but when we do things like

print them out java knows that we're

really interested in the symbol than it

represents now you may be wondering what

are these symbols that we're talking

about so much let's go ahead and write a

quick little program to see what these

symbols are so let's declare an integer

I that we're going to use and let's do a

for loop I is equal to let's start at 30

and I'll show you why in a minute so

we'll go from I is equal to 30 and let's

do I is less than or equal to let's do

100 and we'll adjust these in a minute I

plus plus and then we'll open a bracket

and we'll hit enter we have another Club

closing curly brace here so we have a

loop going from 30 up to 100 and

incrementing so let's say that character

is equal to I so basically as we loop

through we take the integer that we have

and we assign it to this character

variable and look what happens it says

type mismatch cannot convert into care

because they're two different kinds of

data types but we can force this to

occur as we've said before by typing in

care like this and here the underline

goes away so whenever you see things

where it says the conversion is not able

to be done you can force the a

conversion to happen by telling it on

the right-hand side hey I want you to

treat this as a character and then save

it over there we've done that for

integers and we've done that for floats

up until this point so that's what's

going on there so let's do system dot

out dot print Ln like this and then we

will say the code the code is I and then

we'll say corresponds to and then we'll

do character so basically we're creating

a loop where we're printing out what the

code is and what it corresponds to let's

see if this worked at all

so let me drag this up and we can see

what the unicode set is code 30

corresponds to basically a hidden

character it's a non printable character

could be a tab or it could be a

backspace because everything on your

keyboard has a code associated with it

the first printable one that we have is

33 which corresponds to the exclamation

point then we have double quotes and all

of these symbols that are shifts on your

keyboard here then we get to the numbers

48 through looks like 57 corresponds to

the numbers and then we get to the

uppercase letters here and then after

the uppercase letters we have brackets

and so on and then we have ABCD those

are the lowercase in number letters we

stopped at 100 so it looks like we can

actually go farther than 100 let's go up

to 150 let's go ahead and do that and

you can see what happens here is ABCD

efg all the way through Z and then we

get some curly braces and then we have

question marks which are look like

they're not terribly valid for this kind

of thing so really the printable set

that we were interested in looking at

pretty much ends down here around 125

all right so just a neat little program

to write we had to put the typecast in

here of character because if we took it

out then Java starts to complain because

here we have an integer and we're trying

to save it into a datatype that it

doesn't know how to convert them here

we're forcing it and saying saying hey I

know this is a number treat it as a

character and store it in there and then

we can create our table which is what

we've been able to do here so now that

you understand that I want you to go off

to the exercise and try to do that

program yourself and check your work the

most important thing to realize is you

can declare character variables you can

print them to the screen with the print

statement you can actually assign

numbers into your character variables

which will just print the corresponding

letter of the keyboard you can increment

and decrement those characters which

come in handy for certain situations

down the road when we start talking

about strings and different types of

input/output with letters from the