Why is StringTokenizer deprecated?

57,354

Solution 1

From the javadoc for StringTokenizer:

StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead.

If you look at String.split() and compare it to StringTokenizer, the relevant difference is that String.split() uses a regular expression, whereas StringTokenizer just uses verbatim split characters. So if I wanted to tokenize a string with more complex logic than single characters (e.g. split on \r\n), I can't use StringTokenizer but I can use String.split().

Solution 2

  1. Java 10 String Tokenizer -- not deprecated
  2. Java 9 String Tokenizer -- not deprecated
  3. Java 8 String Tokenizer -- not deprecated
  4. Java 7 String Tokenizer -- not deprecated
  5. Java 6 String Tokenizer -- not deprecated
  6. Java 5 String Tokenizer -- not deprecated

If it is not marked as deprecated, it is not going away.

Solution 3

StringTokenizer is not deprecated in fact StringTokenizer is 4X faster than String.split() and in competitive programming it is used by many developers.

Source :- Faster Input for Java

Solution 4

There is an issue with StringTokenize ...

Split have to use regex, StringTokenizer is using String or CharSequence,

but

"a.b..".split(".") will return {"a","b",""}

and StringTokenizer of "a.b.." ... will return only {"a","b"}

And this is very tricky!!! Be Carefull!!!

Better and safer alternatives to StringTokenizer are:

Much better StrongTokenizer is in org.apache.common.lang3 ... it have much more flexibility or

com.google.common.base.Splitter

Share:
57,354
donnyton
Author by

donnyton

Updated on July 09, 2022

Comments

  • donnyton
    donnyton almost 2 years

    The Java documentation doesn't seem to mention anything about deprecation for StringTokenizer, yet I keep hearing about how it was deprecated long ago. Was it deprecated because it had bugs/errors, or is String.split() simply better to use overall?

    I have some code that uses StringTokenizer and I am wondering if I should seriously be concerned about refactoring it to use String.split(), or whether the deprecation is purely a matter of convenience and my code is safe.

  • Ali
    Ali over 12 years
    You're right, it's not, it's just recommended that you dont use it. The difference being that there is no guaratee that deprecated classes will continue to be provided in future releases.
  • user207421
    user207421 over 12 years
    That's not correct either. There is an absolute guarantee of backwards binary compatibility.
  • Miuler
    Miuler over 11 years
    "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code. It is recommended that anyone seeking this functionality use the split method of String or the java.util.regex package instead."
  • StackOverflowed
    StackOverflowed over 11 years
    Legacy is different than deprecated.
  • Audrius Meškauskas
    Audrius Meškauskas over 11 years
    Personally for me, a code using StringTokenizer looks simpler and cleaner.
  • Scott Chu
    Scott Chu over 7 years
    Exactly. And this issues took our programmers tremendous efforts to find a 'butterfly effect' bug. Why Java team invents such terrible stuff in the beginning?... Sigh!
  • user1568901
    user1568901 over 7 years
    Yeah, I'm with @h22, often StringTokenizer code is more elegant/readable than using split. I save split for when I really need the newer behavior.
  • RBz
    RBz almost 7 years
    Is this still valid?
  • David 天宇 Wong
    David 天宇 Wong over 6 years
    according to other answers yes, it's just that split is more flexible since you can use regexes