If numbers are the language of calculation, strings are the language of communication. Almost every useful Java application works with text: names, email addresses, messages, product descriptions, URLs, file paths, search queries, and even data received from external systems.
In Java, a string is a sequence of characters represented by the String class. Although String looks simple when you first encounter it, it has several important characteristics that affect performance, memory usage, comparison, and application design.
What Is a String?
A string is a sequence of characters treated as a single value. For example, "Java" contains four characters: J, a, v, and a.
String language = "Java"; String message = "Hello, Java!"; String city = "Bengaluru";
Here, language, message, and city are variables that refer to string objects. The text itself is not a primitive data type such as int or double.
String is a class in Java, not a primitive data type. This distinction becomes important when you study object references, immutability, memory management, and string comparison.
Why Does Java Have a String Class?
A primitive type can represent a single simple value, but text requires much richer behaviour. Applications need to find characters, combine text, compare values, extract portions, change case, remove unwanted spaces, search for patterns, and format information.
Instead of forcing programmers to implement these operations repeatedly, Java provides the String class with a large collection of ready-to-use operations.
String name = "Rahul"; System.out.println(name.length()); System.out.println(name.toUpperCase()); System.out.println(name.charAt(0));
The same string can therefore participate in many operations without you having to manually manage an array of characters.
A Simple Real-World Analogy
Think of a string as a printed sentence on a card. You can read the sentence, find a particular word, check its length, compare it with another sentence, or take a portion of it. The card represents the text value, while the operations provided by Java give you convenient ways to work with that text.
This analogy also reveals an important idea: a string represents text as a value. You do not normally manipulate individual characters one by one unless your task actually requires it.
String Literals
The most common way to write a string directly in Java is by using a string literal. A string literal is text enclosed within double quotation marks.
String name = "Amit"; String course = "Java Programming"; String empty = "";
The quotation marks tell the Java compiler that the enclosed characters represent text rather than an identifier or some other expression.
For example, Java without quotation marks is interpreted as an identifier, while "Java" is a string literal.
Strings Can Contain Spaces and Symbols
A string is not limited to alphabetic characters. It can contain spaces, numbers, punctuation marks, and many other characters.
String name = "Anita Sharma"; String orderId = "ORD-2026-1045"; String message = "Java is fun!"; String expression = "10 + 20 = 30";
Notice that the numbers inside the last two examples are still part of text. Java does not treat "10" as the integer value 10. The quotation marks make it a string.
10 is a number, but "10" is text. They may look similar on the screen, but Java treats them as different types and they behave differently in expressions.
String Length
One of the simplest operations performed on a string is finding how many characters it contains. Java provides the length() method for this purpose.
String language = "Java"; System.out.println(language.length());
The output is:
4
The important detail is that length() is a method, so it uses parentheses. This is different from an array, where length is a field.
Accessing Characters
A string contains individual characters, and Java allows you to access a character at a specific position using charAt().
String language = "Java"; System.out.println(language.charAt(0)); System.out.println(language.charAt(2));
The output is:
J v
Java uses zero-based indexing. That means the first character is at position 0, the second is at position 1, and so on.
| Character | J | a | v | a |
|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 |
This zero-based indexing rule appears throughout Java and is especially important when working with strings, arrays, collections, and loops.
Strings and the Empty String
An empty string is a valid string containing no characters.
String value = ""; System.out.println(value.length());
The result is 0. An empty string is not the same thing as null.
String a = ""; String b = null;
a refers to a string containing zero characters, while b does not refer to a string object at all. Confusing these two concepts is a common source of errors in Java programs.
An empty string has a value and a length of zero. null means that no string object is being referenced. Calling a string method on a null reference can cause a NullPointerException.
String Concatenation
One of the most frequently used string operations is joining pieces of text. Java allows strings to be concatenated using the + operator.
String firstName = "Asha"; String lastName = "Patel"; String fullName = firstName + " " + lastName; System.out.println(fullName);
The output is:
Asha Patel
Java can also concatenate strings with numbers.
String product = "Laptop"; int price = 55000; System.out.println(product + " costs " + price);
The result is a string containing both the text and the numeric value.
When the + operator is used with a string, Java performs string concatenation rather than ordinary numeric addition for that part of the expression.
A Beginner Mistake with +
Consider this expression:
System.out.println("Total: " + 10 + 20);
The output is:
Total: 1020
Why did Java not produce Total: 30? Once Java encounters the string, the remaining + operations are treated as concatenation from left to right.
If you want the numbers added first, use parentheses.
System.out.println("Total: " + (10 + 20));
Now the result is:
Total: 30
Strings Are Objects
Although string syntax looks almost as convenient as primitive values, String is an object type.
String language = "Java";
The variable language holds a reference to a string object. This is why you can call methods such as length(), charAt(), toUpperCase(), and many others on it.
This also explains why strings have behaviour beyond simply storing characters. The String class provides the operations required to work effectively with textual data.
Common Beginner Mistakes
- Using single quotes for a string. In Java, single quotes represent a character, while double quotes represent a string.
- Forgetting that string indexes begin at zero.
- Confusing an empty string "" with null.
- Using == when the intention is to compare string contents. String comparison deserves special attention and is covered separately.
- Calling charAt() with an invalid index.
- Forgetting that "25" is text, while 25 is an integer.
Quick Learning Checkpoint
Before moving forward, make sure you can answer these questions without looking at the code:
- Is String a primitive type or a class?
- What is the difference between "100" and 100?
- What does length() return?
- What is the index of the first character?
- What is the difference between "" and null?
- Why does "Total: " + 10 + 20 produce Total: 1020?
Interview Insight
A common interview starting point is the simple question: “Is String a primitive data type in Java?” The correct answer is no. String is a class, and string values are objects. However, Java gives strings special language-level support through string literals and the + concatenation operator, which makes them feel more convenient than ordinary objects.
String Basics at a Glance
| Concept | Key Point | Example |
|---|---|---|
| String | Class used to represent text | String name = "Asha"; |
| Literal | Text written inside double quotes | "Java" |
| Length | Returns the number of characters | text.length() |
| Character access | Gets a character using a zero-based index | text.charAt(0) |
| Empty string | Valid string containing zero characters | "" |
| Null reference | No string object is referenced | null |
| Concatenation | Joins strings and values | "Hello " + name |
Final Takeaway
Strings may look like simple pieces of text, but they are one of the most important building blocks in Java development. A string is an object represented by the String class, string literals use double quotes, indexing starts at zero, length() tells you how much text you have, and + can combine text with other values. Once these fundamentals are clear, the next step is understanding how Java creates and manages string objects—and that leads directly to one of the most important ideas in Java: String Immutability.
