Java Day_12
Java Notes: Character Literals (char)
A character literal in Java represents a single character. It’s associated with the char primitive data type.
- Data Type:
char - Size: 16 bits (2 bytes)
- Encoding: Java uses the Unicode character set, which is a superset of ASCII. This allows it to represent characters from virtually all languages.
- Range: The range of Unicode values is from
0to65,535.
Ways to Represent a char Literal
There are several ways to define a character literal in Java.
1. Single Character in Single Quotes
This is the most common method. Any single character, digit, or special symbol is enclosed in single quotes (' ').
Examples:
Java
char letter = 'A';
char digit = '9';
char symbol = '$';2. As a Unicode Integer Value (Decimal)
You can directly assign an integer corresponding to the character’s Unicode value.
- Older languages like C/C++ often used ASCII (range 0–255).
- Java uses the broader Unicode standard (range 0–65535).
Example:
Java
// Assigning the Unicode value for 'A', which is 65
char c1 = 65; // This is valid and c1 will hold the character 'A'// The maximum value is 65535
char c2 = 65535; // Valid3. Unicode Hexadecimal Representation
You can represent a char using its 4-digit hexadecimal Unicode value.
- Format:
'\uXXXX' \u: Represents a Unicode literal. Theumust be lowercase.XXXX: Represents exactly four hexadecimal digits.- Minimum Value:
'\u0000' - Maximum Value:
'\uffff'
Example: Representing ‘A’
- The decimal Unicode value for ‘A’ is 65.
- Convert this decimal value to hexadecimal:
65 / 16 = 4with a remainder of1.4 / 16 = 0with a remainder of4.- Reading the remainders from bottom to top gives
41. - The hexadecimal representation must be 4 digits, so we pad it with leading zeros:
0041. - Combine with the
\uprefix and enclose in single quotes.
Java
// Correct 4-digit hexadecimal representation of 'A'
char c3 = '\u0041'; // Valid// Incorrect, as it's not 4 digits
// char c4 = '\u041'; // COMPILE ERROR4. Escape Sequences
Java provides special backslash (\) literals, known as escape sequences, to represent non-printable or special characters. These can also be assigned to a char variable.
Common Examples:
Get Aman Verma’s stories in your inbox
Join Medium for free to get updates from this writer.
Java
char newline = '\n'; // Newline character
char tab = '\t'; // Tab character
char backslash = '\\'; // Backslash character
char singleQuote = '\''; // Single quote characterAssigning char to int
You can assign a char literal to an int variable. This will store the character's decimal Unicode value in the integer variable. This is a useful way to see the underlying numeric value of a character.
Example:
Java
// Assign the character 'a' to an int variable
int valueOfA = 'a';// The Unicode value for 'a' is 97
System.out.println(valueOfA); // This will print 97Of course! Based on the code snippets you provided, here are some structured Java notes covering the char data type and its various representations.
Java Notes: The char Data Type & Literals
In Java, the char data type is a 16-bit unsigned integer used to store a single Unicode character. Its value can range from 0 to 65,535.
1. Declaring char Literals
You can declare a char literal in several ways.
- a) In Single Quotes: The most common way is to enclose a single character in quotes.
- Java
char c1 = 'a';
b) As a Decimal Unicode Value: You can use the character’s decimal Unicode (or ASCII) value.
- Java
// The ASCII/Unicode value for 'a' is 97 char c2 = 97; System.out.println(c2); // Prints: a
- c) As a Hexadecimal Unicode Value: You can use the
\uprefix followed by exactly four hexadecimal digits. - Java
// The hexadecimal value for 'a' is 0061 char c3 = '\u0061'; System.out.println(c3); // Prints: a
d) As an Escape Sequence: Special characters are represented using a backslash.
- Java
char newline = '\n'; // Represents a line break char tab = '\t'; // Represents a tab space
2. Interaction Between char and int
A char can be seamlessly assigned to an int variable. This is known as a widening primitive conversion. The int variable will store the character's decimal Unicode value.
This is very useful for performing arithmetic operations or finding a character’s numeric code.
Example:
Java
// Assigning a char to an int
int value = 'a';System.out.println(value); // Prints the Unicode value: 973. Properties and Range of char
The Character wrapper class provides useful constants for working with char values.
Character.MIN_VALUE: The smallestcharvalue (\u0000or0).Character.MAX_VALUE: The largestcharvalue (\uffffor65535).
Example:
Java
// Using the wrapper class to see the range
int minValue = Character.MIN_VALUE; // Result is 0
int maxValue = Character.MAX_VALUE; // Result is 65535System.out.println("Min char value: " + minValue);
System.out.println("Max char value: " + maxValue);String Literal:
String is a predefined class available in java.lang Package.
String is a collection of alpha-nemeric character which is enclosed by double quotes. These charact can be alphabets, numbers, symbol or any special character.
In java we can create String object by using following 3 ways:
1) By using String Literal String str1 = “india”;
2) By using new keyword String str2 = new String(“Hyderabad”);
3) By using Character array [Old Technique] char ch[] = {‘R’, ‘A’, ‘]’};
//Three Ways to create the String Object
public class StringTest1
{
public static void main(String[] args) {
String s1 = “Hello World”; //Literal
}
}
System.out.println(s1);
String s2 = new String(“Ravi”); //Using new Keyword System.out.println(s2);
char s3[] = {‘H’,’E’,’L’,’L’,’0'}; //Character Array
System.out.println(s3);
//String is collection of alpha-numeric character
public class StringTest2
{
public static void main(String[] args) {
}
String x = “B-61 Hyderabad”; System.out.println(x);
String y= “123”; System.out.println(y);
String z = “67.90”; System.out.println(z);
String p = “A”; System.out.println(p);
}
//IQ
public class String Test3
{
public static void main(String []args)
{
String s = 15+ 29 +”Ravi”+40+40;
System.out.println(s);
}









