char

Used to represent a single character. Example:

char grade = 'A';

In Java, the number of bytes a char, String, or any text content (like a chat message) takes depends on character encoding. Here’s a breakdown:


Java char Size

Estimating Chat Message Size in Bytes

Let’s say your chat message is stored as a String:

String chat = "DIY Spring JDBC Client ...";

Option A: Memory Size in Java (UTF-16)

Option B: Storage or Network Size (Encoded like UTF-8 or UTF-16)


Example

String chat = "DIY Spring JDBC Client";
System.out.println("UTF-8 Bytes: " + chat.getBytes(StandardCharsets.UTF_8).length);
System.out.println("UTF-16 Bytes: " + chat.getBytes(StandardCharsets.UTF_16).length);

Output:

UTF-8 Bytes: 24
UTF-16 Bytes: 50

Note: UTF-16 adds a 2-byte BOM (Byte Order Mark) in many cases, hence 50 instead of 48.

Context Encoding Size per char Notes
In Java memory UTF-16 2 bytes Fixed per char
UTF-8 encoding UTF-8 1–4 bytes ASCII chars take 1 byte, others more
UTF-16 encoding UTF-16 2–4 bytes Includes BOM if written to file

String

Strings lie at the heart of almost every Java program. You’ll learn how they’re represented under the hood, how to build and manipulate them efficiently, and the rich set of tools Java provides for searching, splitting, and templating text.


Internal Representation


StringBuilder


Regular Expressions (RegEx)

String Templating (String.format)

With this toolkit—knowing how strings live in memory, when to use mutable builders, how to leverage regex, and how to split or template text—you’ll handle almost any textual requirement efficiently and cleanly.

Escape Sequences

In Java, special character sequences like \n, \t, etc., are called escape sequences.

Escape sequences are used in Java (and many programming languages) to represent special characters that cannot be typed directly or would be interpreted differently in a string or character literal.

Each escape sequence starts with a backslash (\), which tells the compiler to interpret the following character(s) in a special way.

Escape Sequence Name Meaning
\n Newline Moves the cursor to the next line
\t Tab Inserts a horizontal tab
\b Backspace Moves the cursor one position back
\r Carriage Return Moves the cursor to the beginning of the line
\' Single Quote Inserts a single quote character
\" Double Quote Inserts a double quote character
\\ Backslash Inserts a backslash (\)
\f Form Feed Advances the printer to the next page (rarely used today)
\uXXXX Unicode Character Represents a Unicode character using its hexadecimal code (e.g., \u0B85 for Tamil letter அ)
public class EscapeDemo {
    public static void main(String[] args) {
        System.out.println("Line1\nLine2");      // Prints in two lines
        System.out.println("Name:\tSathish");    // Adds tab space
        System.out.println("She said: \"Hello\"");// Prints double quotes
        System.out.println("Path: C:\\Users\\");  // Prints backslash
    }
}

Classes
Quiz
Videos
References
Books