Slide 1
Slide 2
Slide 3

Char data type in java programming (V16)

No Comments

 

 Char Data Types

        The char data type is a single 16-bit Unicode character that represents a wide range of characters and symbols. It has a range from '\u0000' (or 0) to '\uffff' (or 65,535). This data type is mainly used to store individual characters.

Syntax:

        
Example: Declaring char Data Type

        

Declaring Escape Sequences in Java

        

Source Code:

Copied!
public class CharExample {
    public static void main(String[] args) {
        char c1 = 'A';           // Regular character
        char c2 = 65;            // ASCII value for 'A'
        char c3 = '\u0041';      // Unicode representation of 'A'
        char c4 = '\n';          // Newline character
        
        System.out.println(c1); // Output: A
        System.out.println(c2); // Output: A
        System.out.println(c3); // Output: A
        System.out.println("Hello" + c4 + "World!"); // Output: Hello
                                                   //         World!
    }
}

Result:

Source Code:

Copied!
import java.util.Scanner;
public class CharGrade {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt the user to enter a character
System.out.print("Enter a grade (A, B, C, D, or F): ");
char grade = scanner.next().charAt(0); // Read the first character input
// Evaluate the character using switch
switch (grade) {
case 'A':
System.out.println("Excellent! Keep up the great work.");
break;
case 'B':
System.out.println("Good job! You can aim higher.");
break;
case 'C':
System.out.println("Fair. There's room for improvement.");
break;
case 'D':
System.out.println("You passed, but consider reviewing the material.");
break;
case 'F':
System.out.println("Failed. Don't give up—study and try again.");
break;
default:
System.out.println("Invalid grade entered. Please enter A, B, C, D, or F.");
}
scanner.close();
}
}

Result:

Watch the video:


Ebook: https://softkhpc.blogspot.com/2025/05/java-programming-ebooks.html

back to top