What is the difference between 'String' and 'char' in Java?
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
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
| Feature | char | String |
|---|---|---|
| Type | Primitive data type | Object (reference type) |
| Content | A single Unicode character | A sequence of Unicode characters |
| Literal Enclosure | Single quotes ('') | Double quotes (" ") |
| Memory | Fixed 2 bytes (16 bits) | Variable (depends on length + overhead) |
| Mutability | N/A (primitive, value can be reassigned) | Immutable (content cannot change once created) |
| Associated Class | Character (wrapper class) | java.lang.String (the class itself) |
| Empty Value | Not applicable (must hold a char) | Empty string: "" |