Java Char Keyword
- The java char keyword is a data type which is defined as character data type.
- Char keyword belongs to a primitive data type where the data types are classified into primitive and non-primitive.
- The char keyword is mainly used for declaring the character-type variables and character type methods.
- The character data type or keyword has the capability or capacity of storing of holding or provides the unsigned 16-bit character of Unicode.
- The char data type or keyword is considered as a character's group which provides the symbols of alphabets and numbers in a character set.
- The size of the keyword char provided with 16 bits.
- The Char data type or keyword value range is defined between 0 to 65,536.
- And the characters range according to the standard ASCII is defined from 0 to 127.
Points to be remembered are:
- The Range of the char lies between 0 to 65,535(inclusive).
- The default value of the char keyword is given as ‘\u0000’.
- The Default size of the char data type or keyword is given by 2 bytes.
- The characters are stored by using or implementing the char data type or keyword.
Characteristics of Char Keyword
The essential characteristics of the char keyword are given below:
- As mentioned in the above information that char contain the range between 0 to 65,535.
- The default value of the char ‘\u0000’ is the lowest range of the Unicode.
- Java uses the Unicode system not ASCII code system that is why the default value is 2 bytes.
Syntax for Java Char Keyword
char_variable name =’variable value’;
For example,
char a =’x’; // where a is the variable name and x is the variable value.
Let us understand more about the char keyword using the example programs
Example 1:
In this simple example we will learn to display the characters that are been implemented using the char data type or keyword.
public class ExOfChar1 {
public static void main (String [] args) {
char ch1='R';
char ch2='D';
System.out.println("ch1: "+ch1);
System.out.println("ch2: "+ch2);
}
}
Output:
ch1: R
ch2: D
Example2:
Printing or executing Characters using ASCII value
Here in this example the three char variables or declared with the integers. So, on executing those variables, the integer values which are assigned to the variables will be converted to their ASCII values and shown or displayed in the output window.
public class ex {
public static void main (String [] args)
char s1, s2, s3;
/*
* Since 68 and 69 are the ASCII value for D and E,
* We have given s1 as 68 and s3 as 69.
*/
s1 = 68;
s2 = 'B';
s3 = 69;
System.out.println("The characters are: " + s1 + s2 + s3)
}
}
Output:
The characters are: DBE
Example3:
Typecasting the integer value to char explicity in java
Here for type casting the integer valueto char explicity we declare the variables and assign them with the integer value.All the numeric value will change to the characters as every numeric belong to some character.
For example, 65 belong to A, 78 belongs to N etc.
public class TypecastExample3 {
public static void main (String [] args) {
int n1=97;
char D1=(char)n1;
int n2=65;
char D2=(char)n2;
System.out.println("D1: "+D1);
System.out.println("D2: "+D2);
}
}
Output:
D1: a
D2: A
OR
import java. Util.*;
public class ex {
public static void main(String[] args) {
int n1 = 66;
char G1 = (char)n1;
int n2 = 76;
char G2 = (char)n2;
int n3 = 79;
char G3 = (char)n3;
int n4 = 71;
char G4 = (char)G4;
System.out.println(G1);
System.out.println(G2);
System.out.println(G3);
System.out.println(G4);
}
}
Example4:
Implementing the char value in Unicode System
public class Ex4 {
public static void main (String [] args) {
char D1='\u0061';
char D2='\u0041';
System.out.println("D1: "+D1);
System.out.println("D2: "+D2);
}
}
Output:
D1: a
D2: A
Example5:
Here in this program, we implement the code for increment and decrement of the char value by 1.
For increment and decrement we use the ++ and – operators. Here we also include the
Print statement that executes before and after values of the given variable.
public class ex {
public static void main (String [] args) {
char A1 = 'A';
System.out.println("The value of A1 is: " + A1);
A1++;
System.out.println("After incrementing: " + A1);
A1--;
System.out.println("After decrementing: " + A1);
}
}
Output:
The value of A1is: A
After incrementing: B
After Decrementing: A