This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why StringTokenizer Discouraged nowadays ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why StringTokenizer Discouraged nowadays ?" Watch "Why StringTokenizer Discouraged nowadays ?" New topic

Why StringTokenizer Discouraged nowadays ?

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Hi Folks,

String split is taken advantage over StringTokenizer . please anyone can explain me the reason?
naresh voota

Joined: Jun 04, 2007
Posts: 9
String Tokenizer does not accept(correction) regular expressions but split does. I believe that is why it is not encouraged these days.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

well, but still you can manage with delimiter . i guess some other reason would be there
Sultan Khadar
Ranch Hand

Joined: Sep 12, 2006
Posts: 75
Hi Venkatasamy,

If you want to tokenize a String Object then go for split instead of StringTokenizer
If the data is outside from your program (From a File or from a DataSource) and if you want to parse it then prefer Scanner

Hope this will clarify your doubt.

Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

What if you don't need a regex to match what you're doing? What if it's just Strings separated by commas like so:


Is string split still easier to use than StringTokenizer for this ???


"If the facts don't fit the theory, get new facts" --Albert Einstein
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296
Easier to use is a matter of opinion, not fact. Do either of these look easier to use or understand than the other?

Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
naresh voota

Joined: Jun 04, 2007
Posts: 9
StringTokenizer is faster than String.split() but does not offer functionality like splitting basing on regex expression. But when you look at real world applications, the speed offered by StringTokenizer compared to String.split does not matter much. Sun is keeping StringTokenizer as it is used in legacy applications and for backward compatibility. Hence the advice to switch to String.split for newer code. While the case of String and Scanner is different. It is more of a case of different horses for different courses.
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32815

More reasons:
1) What if your requirement changes from comma to comma with optional whitespace? It's much easier to change the code using split.
2) split is an idiom that reads more naturally
3) It's harder to misuse split. I've seen a delimiter of ", ". Upon first read, many people think this means matching a comma followed by a space. It doesn't of course, which wastes time in troubleshooting.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Thanks Folks for your earnest reply
I agree. Here's the link:
subject: Why StringTokenizer Discouraged nowadays ?
It's not a secret anymore!