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

MethodDoesExample → 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.com

Chain 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 before b.
  • 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());
  }
}

FAQ: Java Strings

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java strings examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java strings in this Java Java lesson (Java Strings).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.