StudyFox

Using objects, methods, and Strings

Open in StudyFox Free, no ads. Practice, track progress and quiz yourself in the app.

AP Computer Science A · Lesson 2 of 6 · 9 min read

String s = "computer";
System.out.println(s.substring(3, 6)); // "put"
System.out.println(s.indexOf("t"));    // 5
int r = (int) (Math.random() * 6) + 1; // random int from 1 to 6
Compare Strings with .equals(), never ==. The == operator checks whether two references point to the same object.

Check your understanding

What does "apple".substring(1, 3) return?

  1. "ap"
  2. "pp"
  3. "ppl"
  4. "app"
Show answer

Answer: B. It starts at index 1 ("p") and stops before index 3, giving "pp".