' '
.
char ch = 'A';
char digit = '5';
char symbol = '#';
'\n'
for newline, '\t'
for tab).
char[] ch = {'d', 'e', 'e', 'p', 'a', 'k'};
0
).
System.out.println(ch[0]); // prints 'd'
" "
.
String name = "Deepak";
String message = "Hello Deepak, how are you ?";
String s = "A";
"A"
has ASCII value 65,
String hindi = "नमस्ते";
String emoji = "😊";
String
class is the most commonly used class to handle text data in Java.
length()
, substring()
, toUpperCase()
, concat()
, etc.
String s1 = "Hello";
System.out.println(s1.toUpperCase()); // HELLO
StringBuffer
is used to create mutable strings (content can be modified).
append()
, insert()
, delete()
, reverse()
.
StringBuffer sb = new StringBuffer("Hello");
sb.append(" World");
System.out.println(sb); // Hello World
StringBuilder
is used to create mutable strings,
meaning the content of the string can be modified after creation.
StringBuffer
but non-synchronized: append()
, insert()
, delete()
, reverse()
.
StringBuilder sb = new StringBuilder("Deepak");
sb.insert(6, " Panwar");
System.out.println(sb); // Deepak Panwar
String
→ Immutable class (content cannot change once created).
StringBuffer
→ Mutable & Thread-safe (slower than StringBuilder).
StringBuilder
→ Mutable & Non-thread-safe (faster than StringBuffer).
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"; |
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.