🎉 Special Offer !    Code: GET300OFF    Flat ₹300 OFF on every Java Course
Grab Deal 🚀

Character Literal ; Character Array ; String  


Introduction

  • Before learning about Strings in Java, it is important to first understand Character Literals and Character Arrays, because Strings are ultimately made up of characters.

Character Literal in Java:
  • A character literal represents a single character enclosed within single quotes ' '.
  • Example:
    • char ch = 'A';
      char digit = '5';
      char symbol = '#';
  • Key Points:
    • Only one character is allowed inside single quotes.
    • It can be a letter, digit, symbol, or escape sequence.
    • Escape sequences are special characters written with a backslash (e.g., '\n' for newline, '\t' for tab).
    • The char data type in Java is 16-bit Unicode, meaning it can represent characters from almost all languages.
    • Java char is a 16-bit Unicode character (UTF-16 code unit), not limited to ASCII.

Character Array in Java:
  • A character array is a collection of characters stored together in a continuous memory location.
  • Example:
    • char[] ch = {'d', 'e', 'e', 'p', 'a', 'k'};
  • Key Points:
    • Used to store multiple characters at once.
    • Each element is accessed using index numbers (starting from 0).
      • System.out.println(ch[0]); // prints 'd'
    • Useful when we want low-level manipulation of characters.
    • Limitation: Once declared, the size of the array is fixed.

String in Java:
  • A String is a sequence of characters enclosed within double quotes " ".
  • Example:
    • String name = "Deepak";
      String message = "Hello Deepak, how are you ?";
  • Key Points:
    • Strings in Java are objects, not just a data type.
    • Strings are widely used in programs to store and process text.
    • Unlike character arrays, Strings are immutable (cannot be changed once created).
    • Java String stores data as UTF-16 encoded Unicode characters.
      • If we write: String s = "A";
        • "A" has ASCII value 65,
        • But Java actually stores it as the Unicode value U+0041 in UTF-16 format.
      • That’s why Java can easily handle characters like:
        String hindi = "नमस्ते";
        String emoji = "😊";
        which are impossible in plain ASCII.
  • Different Classes Used to Handle Strings in Java:
    1. String Class:
      • The String class is the most commonly used class to handle text data in Java.
      • Strings are immutable – once created, their content cannot be changed.
      • Provides many methods such as length(), substring(), toUpperCase(), concat(), etc.
      • String s1 = "Hello";  
        System.out.println(s1.toUpperCase()); // HELLO
    2. StringBuffer Class:
      • StringBuffer is used to create mutable strings (content can be modified).
      • It is synchronized (thread-safe), making it suitable for multi-threaded environments.
      • Common methods: append(), insert(), delete(), reverse().
      • StringBuffer sb = new StringBuffer("Hello");  
        sb.append(" World");  
        System.out.println(sb); // Hello World
    3. StringBuilder Class:
      • StringBuilder is used to create mutable strings, meaning the content of the string can be modified after creation.
      • It is non-synchronized (not thread-safe), so it’s not suitable for multi-threaded environments.
      • Provides similar methods as StringBuffer but non-synchronized: append(), insert(), delete(), reverse().
      • StringBuilder sb = new StringBuilder("Deepak");  
        sb.insert(6, " Panwar");  
        System.out.println(sb); // Deepak Panwar

Difference between Character Literal, Character Array & String:
Feature Character Literal Character Array String
  Definition Represents a single character enclosed in single quotes ('A'). A collection of characters stored in a sequence ({'J','a','v','a'}). A sequence of characters enclosed in double quotes ("Java").
  Data Type char (primitive type) char[] (array of characters) String (class in java.lang)
  Storage Stores a single Unicode character (UTF-16 unit). Stores multiple Unicode characters (UTF-16 units). Stores UTF-16 encoded Unicode characters as an object.
  Mutability Single value, cannot be changed to multiple. Mutable - elements can be updated. Immutable - cannot be changed once created.
  Example char ch = 'A'; char[] arr = {'J','a','v','a'}; String str = "Java";