Scanner vs. StringTokenizer vs. String.Split
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Java, parsing strings is a common task that can significantly impact the performance and readability of your code. Three popular classes to accomplish string parsing include Scanner, StringTokenizer, and String.split(). Each has its own strengths and weaknesses, which makes it essential to understand their differences and use-cases. Let's explore these options in detail, comparing their functionality, performance, and typical applications.
Scanner
Scanner is a versatile and powerful utility class, primarily used for parsing input from various input streams like System.in, files, and strings. It provides methods to read and parse primitive data types and strings using regular expressions or delimiters.
Key Features
- Flexibility: Offers extensive methods for parsing different data types (e.g.,
nextInt(),nextDouble()). - Regular Expressions: Uses regular expressions for delimiter splitting, which allows complex parsing operations.
- Buffered Input: Internally uses buffering, which optimizes performance when reading from streams or files.
Example
Considerations
- Performance: Generally slower compared to
StringTokenizerorString.split()for simple parsing tasks due to the overhead of regex processing. - Resource-Intensive: Not suitable for very large datasets or high-performance applications due to memory consumption.
StringTokenizer
StringTokenizer is an older utility used for splitting strings into tokens based on specified delimiters. It is part of the java.util package and provides a basic mechanism for tokenization.
Key Features
- Simplicity: Designed for straightforward tokenization, often faster for simple tasks.
- Ease of Use: Provides methods like
nextToken()for sequential token iteration. - Delimiter-Based: Primarily uses single-character delimiters, limiting its flexibility.
Example
Considerations
- Obsolescence: Considered a legacy class; not recommended for new applications.
- Limited Flexibility: Cannot handle complex scenarios like multi-character delimiters without substantial workarounds.
String.split()
The String.split() method uses regular expressions to divide a string into an array of substrings. Introduced in JDK 1.4, it is widely used due to its simplicity and power.
Key Features
- Regular Expressions: Utilizes regex for defining delimiters, offering flexibility.
- Simplicity: Easy to use for straightforward splitting tasks, returns an array of strings.
- Performance: Efficient for moderate-sized inputs with simple splitting rules.
Example
Considerations
- Complexity Limit: For very complex tokenization, managing regex expressions can become cumbersome.
- Resource Consumption: The split operation creates an array, which could be a memory concern for extremely large strings.
Summary Table
| Feature/Aspect | Scanner | StringTokenizer | String.split() |
| Flexibility | High (supports multiple data types and regex) | Low (single-character delimiters only) | Medium (regex support) |
| Ease of Use | Moderate (more setup required) | High (straightforward to use) | High (simple API) |
| Performance | Moderate (regex overhead) | Fast (minimal processing) | Fast (depends on regex complexity) |
| Use-Cases | Complex parsing tasks with mixed data types | Simple tokenization with static delimiters | Moderate complexity, regex-based splitting |
| Legacy Status | Modern API | Legacy | Modern API |
Additional Considerations
Locale-Sensitive Parsing
When dealing with Scanner, it is crucial to note that it is locale-sensitive, which means that it can interpret numbers differently based on the set locale (e.g., decimal points versus commas). This may lead to inconsistent behavior unless correctly configured.
Handling Edge Cases
- Empty Strings: When using
String.split(), an empty input string returns an array with a single empty element, which can be counterintuitive. - Trimming and Whitespace: Consider additional trimming or stripping of whitespace when parsing with
StringTokenizerandString.split(), as they do not handle these inherently.
Choosing the Right Tool
When selecting a utility for parsing tasks, consider the complexity of your input strings, the need for regular expressions, and performance constraints. For simple tasks, StringTokenizer or String.split() might be suitable, whereas Scanner shines in scenarios requiring multi-type parsing.
In conclusion, understanding the distinctions among Scanner, StringTokenizer, and String.split() enables Java developers to choose the most effective tool for their string parsing needs, balancing performance, ease of use, and flexibility.
Related reading
- Scanning Java annotations at runtime
- Scheduled websocket push with Springboot
- ScheduledExecutorService Exception handling
- Scheduler not running in Spring Boot
- Securing Spring Boot API with API key and secret
- SecurityFilterChain fails to instantiate after spring boot migration from 3.0.8 to 3.0.9
- See all the Java versions installed on Mac
- Seeking useful Eclipse Java code templates

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.