Learn Java
import com.learnjava.variablesdatatypes.Strings;

Strings in Java

@Concept

A String in Java is an object, not a primitive, even though string literals like "hello" look primitive-simple to write. Strings are also immutable — once created, a String's contents can never change; every method that appears to modify one, like toUpperCase or replace, actually returns a brand new String and leaves the original untouched.

Because strings are used constantly, Java gives them extra syntax privileges no other object type gets: they can be built directly from literals without new, and the plus operator concatenates them, which the compiler translates internally into efficient StringBuilder operations. String comparison has a well-known trap here too — == compares object references, not contents, so two strings with identical characters can still fail an == check depending on how they were created. The equals method is what actually compares content.

@HowItWorks
1

Strings are objects

String is a class, not a primitive, but literals like "hello" can be written directly, without new String("hello").

2

Strings are immutable

Once created, a String's characters never change; toUpperCase, substring, and similar methods all return a new String rather than modifying the original.

3

Concatenation with plus

The + operator joins strings — and converts other types to strings automatically — and the compiler translates a chain of + calls into efficient StringBuilder operations behind the scenes.

4

Comparing strings

equals compares actual character content; == compares whether two references point to the exact same object in memory, which is a different question and a classic source of bugs.

5

Common String methods

length, charAt, substring, indexOf, and toUpperCase cover most everyday string work without reaching for anything else.

@Example
public class StringDemo {
public static void main(String[] args) {
String greeting = "Hello";
String shouted = greeting.toUpperCase();
System.out.println(greeting + ", " + shouted + "!");
System.out.println("Same content: " + greeting.equals("Hello"));
}
}
Output
Hello, HELLO!
Same content: true
1greeting.toUpperCase() returns a brand new String; greeting itself is never modified.
2The + operator concatenates greeting, a literal, shouted, and another literal into a single line of output.
3greeting.equals("Hello") compares actual characters, so it correctly returns true even though it's comparing two separately-created strings.
4Both println calls run once each, giving exactly two lines of output.
@UseCases
Formatting user-facing text by joining variables and literals together
Debugging a false result from == on two strings that clearly contain the same text
Searching or slicing text with indexOf and substring when parsing simple input
Understanding why a method call like trim does nothing unless its result is actually assigned somewhere

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in Java yourself — building beats watching.