Strings in Java
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.
Strings are objects
String is a class, not a primitive, but literals like "hello" can be written directly, without new String("hello").
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.
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.
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.
Common String methods
length, charAt, substring, indexOf, and toUpperCase cover most everyday string work without reaching for anything else.
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")); }}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in Java yourself — building beats watching.