In Java, the “char” keyword is used by programmers to declare variables and methods of the character type. Moreover, it can be used to represent symbols, alphabets, and special characters. This keyword is also compatible with storing the unsigned 16-bit Unicode characters. It can be used via the keyword directly or using the “Character” wrapper class(enables the use of the “char” concept in an object-oriented way).
Contents Overview
- What is the “char” Keyword in Java?
- Characteristics of “char”
- Why Do We Need “char” in Java?
- How to Use the “char” Keyword in Java?
What is the “char” Keyword in Java?
The “char” keyword is a primitive fixed-length datatype utilized to store a single character. It stores the values within the single quotes i.e., ‘a’, ‘A’ etc.
Syntax
char varname = 'varvalue';
In this syntax, “varname” represents the variable, and “varval” is the character value.
Characteristics of “char”
- The “char” lies between “0” to “65,535(inclusive)” integers which are dealt with as ASCII characters.
- Its default value is ‘\u0000’(lowest range of the Unicode system) and the max value is ‘uffff’.
- Its default size is “2” bytes since Java utilizes the Unicode system.
Bonus Tips
- “char” can be a number in Java since it is a 16-bit unsigned integer.
- To enable user input with “char”, the “nextChar()” method is used.
Why Do We Need “char” in Java?
A “char” only needs one character explicitly. It is such that it is not compatible with two values, zero, or a null value. This resultantly increases type safety. Moreover, “char” is faster as compared to the “string” type and is handy when there is a need to store data of consistent length.
How to Use the “char” Keyword in Java?
The following code example explains the use of the “char” keyword to initialize the character values of upper and lower cases and display them:
package jbArticles;
public class Charkeyword {
public static void main(String[] args) {
char value1 = 'A';
char value2 = 'a';
System.out.println("Character Value -> "+value1);
System.out.println("Character Value -> "+value2);
}}
Code Explanation
Initialize the two character values of both the upper and lower cases, respectively using the “char” keyword. After that, display the character values using the “print” statement.
Output
The next section contains multiple vital use cases of the “char” keyword differently:
Example 1: Passing ASCII Values to the “char” Variable in Java
In this specific example, the ASCII values will be stored in the “char” variables such that the corresponding characters will be returned:
package jbArticles;
public class Charkeyword {
public static void main(String[] args) {
char value1 = 120;
char value2 = 65;
System.out.println("Character Value -> "+value1);
System.out.println("Character Value -> "+value2);
}}
Code Explanation
Initialize the two “char” type values by referring to the ASCII values and return the character values.
Output
In this output, it can be observed that the corresponding character values against the specified ASCII are returned.
Example 2: Incrementing and Decrementing the “char” Values in Java
In this demonstration, the initialized “char” values will be incremented and decremented based on alphabets:
package jbArticles;
public class Charkeyword {
public static void main(String[] args) {
char value1 = 'B';
char value2 = 'b';
System.out.println("First Character Value -> "+value1);
System.out.println("Second Character Value -> "+value2);
System.out.println("\nFirst Character Value Before Incrementing -> "+value1);
value1++;
System.out.println("First Character Value After Incrementing -> "+value1);
System.out.println("First Character Value Before Incrementing -> "+value2);
value2++;
System.out.println("First Character Value After Incrementing -> "+value2);
System.out.println("\nFirst Character Value Before Decrementing -> "+value1);
value1--;
System.out.println("First Character Value After Decrementing -> "+value1);
System.out.println("First Character Value Before Decrementing -> "+value2);
value2--;
System.out.println("First Character Value After Decrementing -> "+value2);
}}
Code Explanation
- Use the “char” keyword to define the two character values of upper and lower cases, respectively, and display them.
- After that, use the increment operator “++” to increment both the values and display the characters now.
- Lastly, utilize the decrement operator “—” to decrement both the values and analyze the outcome.
Note: The incremented values will be decremented, thereby resulting in returning the default initialized character values.
Output
Here, it can be seen that the character values are incremented and decremented accordingly.
Example 3: Representing the “char” Keyword in the Unicode System
This demonstration initializes the characters with the Unicode values and these values will be converted into the corresponding characters:
package jbArticles;
public class Charkeyword {
public static void main(String[] args) {
char value1 = '\u0041';
char value2 = '\u0042';
System.out.println("First Value -> "+value1);
System.out.println("Second Value -> "+value2);
}}
Code Explanation
First, store the given Unicode values using the “char” keyword and then display the corresponding character values.
Output
The Unicode values are converted into their corresponding characters appropriately.
Example 4: TypeCasting “int” to “char” in Java
The following code example typecasts the “int” data type to the “char” type such that the corresponding character according to the ASCII representation is returned:
package jbArticles;
public class Charkeyword {
public static void main(String[] args) {
int value1 = 68;
int value2 = 71;
System.out.println("First Integer Value -> "+value1);
System.out.println("Second Integer Value -> "+value2);
char val1 = (char)value1;
char val2 = (char)value2;
System.out.println("First Integer Casted to Character -> "+(val1));
System.out.println("Second Integer Casted to Character -> "+val2);
}}
Code Explanation
- First, initialize the given integer values and display them.
- Now, cast the “int” values to “char” type values using the “char” keyword for both the casting and return type.
- Lastly, display the casted character values according to ASCII representation.
Output
Here, it can be observed that the casting from “int” to “char” is done appropriately.
Example 5: Returning a “char” Array From String in Java
This example uses the “char” keyword to return an array from the defined string:
package jbArticles;
import java.util.Arrays;
public class Charkeyword {
public static void main(String[] args) {
String x = "Javabeat";
char[] charArr = x.toCharArray();
System.out.println("String Value -> "+x);
System.out.println("char Array -> "+Arrays.toString(charArr));
}}
Code Explanation
- Import the given library to work with the arrays.
- Initialize the given string and apply the “toCharArray()” method to return a character array from the string.
- Lastly, return both the string and the character array.
Output
This output confirms that a character array is retrieved appropriately from the initialized string.
Conclusion
The “char” keyword is a primitive datatype in Java used to store a single character within the single quotes i.e., ‘a’, ‘A’ etc. Its default size is “2” bytes and lies between “0” to “65,535(inclusive)” integers. Moreover, it can be used to return the ASCII representation of values, deal with the Unicode system, cast from another data type, or extract an array of characters from the string.