Strings
Java's String class represents text and comes with a rich set of built-in methods. A key concept: Java strings are immutable — once created, a String's content can never change; operations like .toUpperCase() return a brand new string.
💡 Key insight
String immutability is a defining Java concept — every 'modification' actually creates a new String object in memory.
Syntax
syntax
String name = "text";
name.length();
name.toUpperCase();
name.equals(other);Example
.length() returns the character count; .toUpperCase() returns a new, uppercase copy of the string.
java
public class Main {
public static void main(String[] args) {
String name = "CodeyHub";
System.out.println("Name: " + name);
System.out.println("Length: " + name.length());
System.out.println("Upper: " + name.toUpperCase());
}
}outputName: CodeyHub
Length: 8
Upper: CODEYHUB
Try it yourself
Copy this example into the Java compiler and run it — try changing a value and see what happens.
