☕ Java Q109 / 162

What is the difference between 'String' and 'char' in Java?

AI-Powered Answer ✓ Answered

In Java, 'String' and 'char' are fundamental types used to represent textual data, but they differ significantly in their nature, storage, and capabilities. Understanding these distinctions is crucial for effective programming.

1. Fundamental Nature and Type

'char' is a primitive data type in Java. It is used to store a single 16-bit Unicode character. It is one of the eight primitive data types (like int, boolean, double).

'String' is an object (a class from the 'java.lang' package) that represents a sequence of characters. It is not a primitive type; rather, it is a reference type.

2. Value Representation and Literal Syntax

A 'char' literal is always enclosed in single quotes, for example: 'A', '1', '$', '\n' (for newline).

A 'String' literal is always enclosed in double quotes, for example: "Hello", "Java Programming", "123", "" (for an empty string).

3. Storage and Memory Footprint

'char' occupies a fixed 2 bytes (16 bits) of memory because it represents a single Unicode character directly in its value.

'String' objects occupy variable memory space, depending on the number of characters they contain, plus additional overhead for the object itself (e.g., length, hash code, internal char array reference). String literals are often stored in the String Pool, while objects created with 'new String()' are typically stored on the heap.

4. Mutability

As a primitive type, a 'char' variable's value can be reassigned. For instance, 'char c = 'a'; c = 'b';' is perfectly valid. The primitive itself doesn't have 'mutable state' in the object sense.

'String' objects are immutable. Once a String object is created, its content cannot be changed. Any operation that appears to modify a String (like concatenation or substring extraction) actually creates a brand new String object in memory.

5. Operations and Utility Methods

'char' has limited direct operations (e.g., arithmetic addition or comparison). Its wrapper class, 'Character', provides many static utility methods for character manipulation and testing, such as 'isDigit()', 'isLetter()', 'toUpperCase()', etc.

'String' objects come with a rich set of built-in methods for manipulation, comparison, searching, and conversion (e.g., 'length()', 'substring()', 'indexOf()', 'equals()', 'concat()', 'toCharArray()').

6. Examples

java
char initial = 'J';
String name = "Java";
String greeting = "Hello, " + name;

System.out.println(initial); // Output: J
System.out.println(greeting); // Output: Hello, Java

char[] charArray = name.toCharArray(); // Convert String to char array
System.out.println(charArray[0]); // Output: J

Summary Table

FeaturecharString
TypePrimitive data typeObject (reference type)
ContentA single Unicode characterA sequence of Unicode characters
Literal EnclosureSingle quotes ('')Double quotes (" ")
MemoryFixed 2 bytes (16 bits)Variable (depends on length + overhead)
MutabilityN/A (primitive, value can be reassigned)Immutable (content cannot change once created)
Associated ClassCharacter (wrapper class)java.lang.String (the class itself)
Empty ValueNot applicable (must hold a char)Empty string: ""