In Java, char keyword is a primitive data type used for character values. It can store 16 bit piece of information, ranging from 0 to 65,536 characters. Java uses Unicode to represent all the characters that includes symbols, alphabets, numbers and other characters as it covers more characters than ASCII system.
Syntax :
char variableName = 'a' // any value
These characters can also be converted to integer and manipulated with arithmetic operators.
A character is represented within a single quotes. Its default value is denoted by ‘/0000’, having default size of 2 byte.
Since, Java uses Unicode system to covers more characters than ASCII system. Below is the simple program to print various character literals.
Java program to print character literals such as symbol, alphabet, number.
public class CharacterExample { public static void main(String[] args) { char alphabet = 'A'; char symbol = '@'; char num = '2'; System.out.println("Printing alphabet character :: "+alphabet); System.out.println("Printing symbol character :: "+symbol); System.out.println("Printing number character :: "+num); } }
Output:
Printing alphabet character :: A Printing symbol character :: @ Printing number character :: 2
ASCII stands for American Standard Code for Information Interchange. It can hold values ranging from 0 to 127 characters. For example, 65 ASCII value represents A.
Java program to print ASCII value of a character
public class CharacterExample { public static void main(String[] args) { char alphabet = 67; char num = 48; char symbol= 60; System.out.println("Printing alphabet character of ASCII :: "+alphabet); System.out.println("Printing number character of ASCII :: "+num); System.out.println("Printing symbol character of ASCII :: "+symbol); } }
Output:
Printing alphabet character of ASCII :: C Printing number character of ASCII :: 0 Printing symbol character of ASCII :: <
ASCII characters can be put directly inside the quote i.e ‘a’. One needs to use an escape sequence i.e ‘\’ for other characters which can’t be directly used inside the quote, . To represent character values, one needs to use ‘\’ followed by the three-digit number i.e ‘ \141’ for ‘a’. To represent hexadecimal, one needs to use ‘\u’ followed by 4 digit number i.e ‘\u0061’ for ‘a’.
Java program to print characters from unicode values
public class CharacterExample { public static void main(String[] args) { char alphabet = '\141'; char hex = '\u0061'; System.out.println("Printing alphabet character of unicode system :: "+alphabet); System.out.println("Printing hexadecimal character of unicode system :: "+hex); } }
Output:
Printing alphabet character of unicode system :: a Printing hexadecimal character of unicode system :: a
In Java, a char type can be converted to an int type in various ways. However, if one directly assigns a char type to an int variable, an ASCII value corresponding to that character would be assigned. On other hand, if a char type is holding an integer value, then it could be converted to an integer type by using an inbuilt method named Character.getNumericValue.
There are following ways to convert a char to an int in Java:
1) Using implicit type casting, that gives corresponding ASCII value
Java program to convert char to int using implicit Typecasting
public class CharacterExample { public static void main(String[] args) { char chr = '6'; int num = chr; System.out.println("Converting chr using implicit typecasting = "+num); } }
Output:
Converting chr using implicit typecasting = 54
2) Character.getNumericValue(), that takes char as parameter and returns its corresponding int value.
Java program to convert char to int using getNumericValue method
public class CharacterExample { public static void main(String[] args) { char chr = '6'; int num = Character.getNumericValue(chr); System.out.println("Converting chr using getNumericValue = "+num); } }
Output:
Converting chr using getNumericValue = 6
3) Integer.parseInt(), it simply use char type as parameter and returns its int value.
Java program to convert char to int using parseInt()
public class CharacterExample { public static void main(String[] args) { char chr = '6'; int num = Integer.parseInt(String.valueOf(chr)); System.out.println("Converting chr using valueOf = "+num); } }
Output :
Converting chr using parseInt = 6
Java converts an int value to its corresponding ASCII character at time of typecasting.
Below is the example :
public class CharacterExample { public static void main(String[] args) { int num = 76; char chr = (char) num; System.out.println("76 converted to character = "+chr); } }
Output:
76 converted to character = L
Alternatively, one can use forDigit method to get actual int value in form of character.
Below is the example :
public class CharacterExample { public static void main(String[] args) { int num = 6; char chr = Character.forDigit(num,10); // for decimal will set redix 10, for hexa it would be 16 System.out.println("Converting int to char = "+chr); } }
Output:
Converting int to char = 6
Java provides toCharArray method, that is part of String class. It translates a string to array of characters.
Below is the program example :
public class CharacterExample { public static void main(String[] args) { String str = "ProgrammerBay"; System.out.println("String value :: "+ str); System.out.println("Character arr :: "+ Arrays.toString(str.toCharArray())); } }
Output :
String value :: ProgrammerBay Character arr :: [P, r, o, g, r, a, m, m, e, r, B, a, y]
This post was last modified on July 4, 2022