Non-numericinformation
(e.g.: letters)
can be represented using
a code
The chardata type
in Java:
chardata type
represents
a single character
The Unicode is used
as the standardrepresentation method
for
characters
A Unicodecharacter chart can be found
here:
Someinterestingcharacter codes
within the
Unicode:
English alphabet:
Greek alphabet:
Hebrew alphabet:
Chinese characters:
Defining and using
char typed
variables
Syntax to
define a
char typedvariable:
char variableName; // The value (= number) in variable will be
// interpreted as an Unicode
Example:
public static void main(String[] args)
{
char letter = 'A'; // Will store 65 in variable
System.out.println( letter );
letter = 65; // Will store 65 in variable
System.out.println( letter );
letter = 946; // 946 = Unicode for Greek letter beta
System.out.println( letter );
letter = 24373; // My Chinese name
System.out.println( letter );
}
DEMO:
demo/04-Math+String/02-char/Intro.java
Size of
Java's
primitive data types
The memory cellsused to
storevariables
of primitive types:
Data type # Bytes
------------------------------------------
double 8 bytes
float 4 bits
long 8 bytes
int 4 bytes
short 2 bytes
byte 1 byte
char 2 bytes
boolean 1 byte
A char typed variable
contains
an integer of
2 bytes or
16 bits
How are
char symbols
stored inside the computer ?
Remember:
Everythingstoredinside the
computer (memory) is a
(binary) number
A character is:
A (binary) number where
the computer is
"told" to
interprete
the
numberusing the
Unicode table.
Example:
public static void main(String[] args)
{
char x = 65; // Interprete number with Unicode
System.out.println( x );
int y = 65; // Interprete number as binary number
System.out.println( y );
}
DEMO:
demo/04-Math+String/02-char/Typing.java
Computations with
char data
You can performarithmetic operations
on char typedvalues
Example:
get the nextletter:
public class NextChar
{
public static void main(String[] args)
{
char c = 65; // 'A'
System.out.println(c); // Print 'A'
System.out.println( (int) c ); // Tell Java to print c as an int
c = (char) (c + 1); // Add 1 to c
System.out.println(c); // Prints 'B'
System.out.println( (int) c ); // Tell Java to print c as an int
}
}
DEMO:
demo/04-Math+String/02-char/NextChar.java
Bit of history:
char in
older programming languages...
Java was
invented in
1995 and it's
a pretty recentprogramming language
That's whyJavadecided to use
the newerUnicode to
representcharacters
Olderprogramming languages
(such as
C, C++)
were inventedbefore
the Unicode:
That's whyolder programming languages
will use
the olderASCII code to
representcharacters
The ASCII code:
The ASCII code is
an 8 bitscode
(Unicode is a
16 bitscode)
The ASCII codeonlyrepresent the
English alphbet
The ASCII code is
a subset
(= contained by) the
Unicode