02 - String
Strings
What is it?
A String is a sequence (chain) of characters - basically, any text. In Java, String is a full-blown object (not a primitive), which is why it behaves differently from int or double.
Why does it exist?
Computers fundamentally work with numbers. Strings are how Java lets you work with human-readable text - names, messages, sentences, file paths, etc.
How does it work?
Declaring a String
String greeting = "Hello, World!";
- The value must be wrapped in double quotes
"..."- these are called string literals Stringwith a capitalS- it’s a class, not a primitive
Concatenation - joining strings together
Use + to join (concatenate) strings:
String firstName = "Ada";
String lastName = "Lovelace";
String fullName = firstName + " " + lastName;
System.out.println(fullName); // Ada Lovelace
You can also concatenate strings with numbers - Java auto-converts the number to a string:
int age = 30;
System.out.println("Age: " + age); // Age: 30
Warning Be careful with
+when numbers are involved:System.out.println("Result: " + 1 + 2); // Result: 12 ← string concat! System.out.println("Result: " + (1 + 2)); // Result: 3 ← math first!Java evaluates left-to-right. Once it hits a String, everything after becomes concatenation.
String length
String word = "Hello";
System.out.println(word.length()); // 5
Useful String methods
| Method | What it does | Example |
|---|---|---|
.length() |
Number of characters | "Hi".length() → 2 |
.toUpperCase() |
ALL CAPS | "hi".toUpperCase() → "HI" |
.toLowerCase() |
all lowercase | "HI".toLowerCase() → "hi" |
.trim() |
Removes leading/trailing spaces | " hi ".trim() → "hi" |
.equals(other) |
Checks if two strings are equal | "a".equals("a") → true |
.contains(sub) |
Checks if a substring exists | "hello".contains("ell") → true |
.charAt(i) |
Gets character at index i |
"Hello".charAt(0) → 'H' |
.substring(start, end) |
Extracts a portion | "Hello".substring(1, 3) → "el" |
Variables
A variable is a named container that stores a value. It has a type and a name:
String message = "Hello!";
int age = 25;
double price = 9.99;
boolean active = true;
Primitive types (the basic building blocks)
| Type | What it stores | Example |
|---|---|---|
int |
Whole numbers | 42, -7 |
double |
Decimal numbers | 3.14, -0.5 |
boolean |
True or false | true, false |
char |
Single character | 'A' |
Note
Stringis NOT a primitive - it’s a class (object type). That’s why it starts with a capital S, and why you compare strings with.equals()instead of==.
Naming rules
- Variable names are camelCase in Java:
myVariable,userAge,firstName - Can’t start with a number
- No spaces allowed
- Names are case-sensitive:
ageandAgeare different variables
Variables are containers, not equations
int x = 5;
x = 10; // x now holds 10, the 5 is gone
x = x + 3; // x is now 13 (read old value, add 3, store back)
Gotchas
- Using
==to compare strings → doesn’t work reliably. Use.equals()instead (see Comparison) - Strings are immutable - methods like
.toUpperCase()return a new string; they don’t change the original Stringcan hold nothing:""is an empty string (notnull).nullmeans the variable points to nothing at all
Related: Printing, Input, Comparison