Inefficient code of String.indexOf(String)

Alan Bateman Alan.Bateman at
Sun Jun 16 12:59:40 UTC 2013

On 16/06/2013 11:53, Anubhav Chaturvedi wrote:
> Hello,
> I have recently started to explore the source code and am new to the open
> source community. I observed that in String.class within java.lang , the
> indexOf method, line 1715, uses the bruteforce approach when it comes to
> string matching. This method is used by the contains(CharSequence) method.
> There are a number of algorithms that can perform the task more efficiently.
> I would like to bring the required changes and needed your advice on this.
The "How to contribute" page [1] is a good start to understand how to 
contribute changes.

In general, I think it's fair to say that we want the JDK to use the 
best-in-class algorithms and implementations (and there is always things 
to improve).

On String.indexOf then it I has an intrinic implementation in HotSpot 
(at least in the server VM) so you may have to disable that in order to 
test and compare alternative implementations. There is string matching 
in the regex code (java.util.regex.Pattern) where Boyer-Moore is used, 
that might be interesting to look at too.



More information about the core-libs-dev mailing list