In order to capture and process the textual information in Java, we use String. If we look closely, String is nothing but a collection/group of characters. So we can say, characters are the basic building unit of string.
Let us discuss Character in this lecture.
As already discussed in Java data types, we have a primitive data type char
that can be used when working with characters.
The character can be declared in the following ways:
Default declaration
char myChar1; // This assigns default value to myChar1
Declaration with value
char myChar2 = 'g'; //assigning lowercase g to myChar2
char myChar3 = '8'; //assigning 8 as character to myChar3
char myChar4 = (char) 76; //assigning type casted integer to myChar4
char myChar5 = '\n'; //assigning escape sequence in single quotes to myChar5
We have seen default declaration and declaration with value(the first two examples). However, there are two new things which we will discuss here.
Java uses number code to represent a character. Java earlier used ASCII (American Standard Code for Information Interchange). It is a collection of 127 unique characters which include numbers, alphabets(both lowercase and uppercase), special character, escape sequence(we will discuss it). But in order to extend this set to 65536 unique characters comprising alphabets of almost every language, Java started using Unicode Character Set.
Let us look into some commonly used character codes, that are used frequently.
Characters | ASCII Code |
---|---|
A to Z |
65 to 90 |
a to z |
97 to 122 |
0 to 9 |
48 to 57 |
So it means
If we do the vice versa
Java has predefined escape sequences and holds a special meaning to its compiler. These are represented by a character with a backslash (\
). It is used to store non-printable characters or print characters with a pre-defined value in Java.
Escape Sequence | Description |
---|---|
\n |
Inserts newline |
\b |
Inserts backspace |
\t |
Inserts tab |
\r |
Inserts carriage return |
\f |
Inserts form-feed |
Escape Sequence | Description |
---|---|
\’ |
Inserts single quote |
\” |
Inserts double quote |
\\ |
Inserts backslash |
As mentioned earlier in this lecture, the characters are represented as a number code. Hence arithmetic operations are also possible on characters.
public class MyChar{
public static void main(String args[])
{
char myNormalChar = 'h';
char myNormalChar2 = 'M';
System.out.println("\nBefore: myNormalChar = "+ myNormalChar);
System.out.println("Before: myNormalChar2 = "+ myNormalChar2);
myNormalChar++; //This increments the ASCII number code by one.Hence, h turns to i
myNormalChar2--;//This decreases the ASCII number code by one. Hence, M turns to L
System.out.println("\nAfter: myNormalChar = "+ myNormalChar);
System.out.println("Before: myNormalChar2 = "+ myNormalChar2);
}
}
Before: myNormalChar = h
Before: myNormalChar2 = M
After: myNormalChar = i
Before: myNormalChar2 = L
Similar to class Number, we have a class Character, which is an alternative to primitive data type char. In our normal usage, we use primitive data type, but in order to make code strictly object-oriented, Character is used. The Character class is convertible to its primitive type char and vice versa. The concept of Autoboxing and Unboxing is applicable here as well. So there are two ways of creating a Character object:
1. Using Constructors: The Syntax to create a new Character object is
2. Autoboxing
Following is the list of the important instance methods that all the subclasses of the Character class implement −
There are several methods of Character class each dedicated to its purpose. Let us explore a few of them.
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.isDigit('G')); //This is digit
System.out.println(Character.isDigit('2')); //This is not a digit
}
}
false
true
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.isLetter('G')); //This is letter
System.out.println(Character.isLetter('2')); //This is not a letter
}
}
true
false
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.isLowerCase('G')); //This is upper-case letter
System.out.println(Character.isLowerCase('g')); //This is lower-case letter
}
}
false
true
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.isUpperCase('G')); //This is upper-case letter
System.out.println(Character.isLowerCase('g')); //This is lower-case letter
}
}
true
false
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.toLowerCase('M'));
System.out.println(Character.toLowerCase('G'));
}
}
m
g
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.toUpperCase('m'));
System.out.println(Character.toUpperCase('g'));
}
}
M
G
public class MyChar{
public static void main(String args[])
{
System.out.println(Character.toString('M'));
}
}
M
This was just a glimpse of methods and features that are provided by Character class. We will discuss Java String in the next class.