StringTokenizer class — Java Technical Interview Questions & Answers

StringTokenizer class

StringTokenizer class

  • StringTokenizer class is about working with text in Java. Remember the base rule first: String is immutable. Compare content with equals, not ==.
  • For StringTokenizer class, common APIs from memory: length(), charAt(i), substring(start, end), contains, equalsIgnoreCase, toUpperCase. Index starts at 0.
  • If StringTokenizer class is StringBuilder/StringBuffer, you change text in place with append. If it is comparison, show == false and equals true for two new String("Hi").
  • For StringTokenizer class, say the output before you code. Off-by-one on substring is a common trap (end index is exclusive).
  • Let's take this on the board with one tiny Main class — no extra files. StringTokenizer class — shows length, substring, case change, and equalsIgnoreCase. Start from main, go line by line, and stop at each print. That output is the proof for StringTokenizer class.

Technical Interview Tip: == vs equals() on String is a classic trap.

Code Examples for StringTokenizer class

Example

// String methods
public class Main {
  public static void main(String[] args) {
    String s = "PrepPilot";
    System.out.println(s.length());
    System.out.println(s.substring(0, 4));
    System.out.println(s.toUpperCase());
    System.out.println("prep".equalsIgnoreCase("PREP"));
  }
}

StringTokenizer class — shows length, substring, case change, and equalsIgnoreCase.

How to Explain StringTokenizer class to an Interviewer

What is StringTokenizer class?

StringTokenizer class works with Java text. String itself is immutable. StringTokenizer class — compare content with equals, not ==. StringTokenizer class — length, charAt, substring. substring end index is exclusive.

Why do we use StringTokenizer class in Java?

Makes the program clearer or safer StringTokenizer class works with Java text. String itself is immutable.

Write one small example of StringTokenizer class.

Show syntax Say what prints

What mistake do beginners make with StringTokenizer class?

Only the name, no example StringTokenizer class — == compares references. User input almost never matches with ==.

Where is StringTokenizer class used in a real program?

Day-to-day Java code