Java Tutorial
Java Strings
String holds text. Concatenate with +, measure length, and read a line with nextLine.
Create and join
A string literal uses double quotes. + concatenates. length() is the character count.
Example
public class Main {
public static void main(String[] args) {
String a = "Try ";
String b = "Java";
System.out.println(a + b);
System.out.println((a + b).length());
}
}Index and substring
charAt(0) is the first character. Indexes start at 0. substring(0, 5) is five characters starting at 0. indexOf finds a piece or returns -1.
Example
public class Main {
public static void main(String[] args) {
String word = "StudyGrid";
System.out.println(word.charAt(0));
System.out.println(word.substring(0, 5));
System.out.println(word.indexOf("Grid"));
}
}Equals, not ==
== on strings asks whether two variables point at the same object. equals compares the letters. Use equals for text you read from input.
Example
public class Main {
public static void main(String[] args) {
String a = "ada";
System.out.println(a.equals("ada"));
}
}Reverse with StringBuilder
String is immutable. To reverse, build a StringBuilder, call reverse, then print it.
Example
public class Main {
public static void main(String[] args) {
String word = "compiler";
System.out.println(new StringBuilder(word).reverse());
}
}Clean and change the text
Because a String never changes in place, every one of these methods returns a new string. If you do not store the result, the work is thrown away.
Example
public class Main {
public static void main(String[] args) {
String raw = " Hello, World ";
System.out.println(raw.trim()); // "Hello, World"
System.out.println(raw.trim().toUpperCase()); // "HELLO, WORLD"
System.out.println("a,b,c".replace(",", "-")); // "a-b-c"
System.out.println("java".contains("av")); // true
}
}name.toUpperCase(); on its own does nothing visible — you must capture it:name = name.toUpperCase();. This is the most common string mistake in Java.
Handy String methods
| Method | Does | Example → result |
|---|---|---|
length() | Character count | "hi".length() → 2 |
charAt(i) | Character at index i | "hi".charAt(0) → 'h' |
substring(a, b) | Slice from a up to b | "study".substring(0, 3) → "stu" |
indexOf(x) | Position, or -1 | "abc".indexOf("c") → 2 |
toUpperCase() | ALL CAPS copy | "hi".toUpperCase() → "HI" |
trim() | Drop outer spaces | " x ".trim() → "x" |
equals(s) | Same text? | "a".equals("a") → true |
Try It Yourself
Exercise: A user typed " ADA@Example.COM ". Produce the tidy lowercase email ada@example.com.
Show solution
String raw = " ADA@Example.COM ";
System.out.println(raw.trim().toLowerCase()); // ada@example.comChain the calls left to right: trim() removes the spaces, then toLowerCase() runs on that result.
Key Takeaways
- Strings are immutable — methods return a new string, so capture the result.
- Indexes start at 0;
substring(a, b)stops just beforeb. - Compare text with
.equals(), never==. - To edit a string a lot (like reversing or building in a loop), use
StringBuilder.
Worked examples
The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.
Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.
Biology
An RNA codon
A codon is three bases. String.length tells you the token is complete before you look it up in a table.
Example
public class Main {
public static void main(String[] args) {
String codon = "AUG";
System.out.println(codon + " " + codon.length());
}
}