{"id":314,"date":"2010-09-04T15:07:00","date_gmt":"2010-09-04T15:07:00","guid":{"rendered":"http:\/\/www.javacodegeeks.com\/2012\/10\/java-best-practices-string-performance-and-exact-string-matching.html"},"modified":"2022-11-21T15:18:15","modified_gmt":"2022-11-21T13:18:15","slug":"string-performance-exact-string","status":"publish","type":"post","link":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html","title":{"rendered":"Java Best Practices \u2013 String performance and Exact String Matching"},"content":{"rendered":"<div dir=\"ltr\" style=\"text-align: left;\">Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> performance tuning. We will focus on how to handle <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> creation, <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> alteration and <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> matching operations efficiently. Furthermore we will provide our own implementations of the most commonly used algorithms for Exact String Matching. Many of these algorithms can achieve far more superior performance compared to the naive approach for exact <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> matching available with the Java Development Kit. This article concludes with a performance comparison between the aforementioned Exact String Matching algorithms.<\/p>\n<p>All discussed topics are based on use cases derived from the development of mission critical, ultra high performance production systems for the telecommunication industry.<\/p>\n<p>Prior reading each section of this article it is highly recommended that you consult the relevant Java API documentation for detailed information and code samples.<\/p>\n<p>All tests are performed against a Sony Vaio with the following characteristics :<\/p>\n<ul>\n<li>System : openSUSE 11.1 (x86_64)<\/li>\n<li>Processor (CPU) : Intel(R) Core(TM)2 Duo CPU T6670 @ 2.20GHz<\/li>\n<li>Processor Speed : 1,200.00 MHz<\/li>\n<li>Total memory (RAM) : 2.8 GB<\/li>\n<li>Java : OpenJDK 1.6.0_0 64-Bit<\/li>\n<\/ul>\n<p>The following test configuration is applied :<\/p>\n<ul>\n<li>Concurrent worker Threads : 1<\/li>\n<li>Test repeats per worker Thread : 1000<\/li>\n<li>Overall test runs : 100<\/li>\n<\/ul>\n<p><strong><span class=\"Apple-style-span\" style=\"font-size: x-large;\">String performance tuning<\/span><\/strong><\/p>\n<p>Many people do not have performance in mind when they utilize <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> objects. Nevertheless misuse of <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> classes can significantly degrade the performance of an application. The most important things you should keep in mind are :<\/p>\n<ol>\n<li><a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> objects are immutable. Once we create a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> object we cannot change it. Every operation that alters a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> results in the creation of at least one new object instance. For example concatenating two strings using the concatenation operator (+) results in the creation of two new objects, a temporary <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/StringBuffer.html\">StringBuffer<\/a> object used for the actual concatenation and the new <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> instance pointing to the concatenated result (the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/StringBuffer.html\">StringBuffer<\/a> \u201ctoString()\u201d operation is utilized to instantiate the resulting <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>). On the other hand using the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> \u201cconcat(String ..)\u201d operation to perform <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> concatenation will provide much better performance results compared to the concatenation operator (+) approach. Behind the scenes the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> \u201cconcat(String ..)\u201d operation utilizes the native \u201cSystem.arrayCopy\u201d operation to prepare a character array with the contents of the two concatenated Strings. Finally a new <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> instance is created that points to the concatenated result<\/li>\n<li><a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> references are pointers to the actual <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> object instances. Thus using the \u201c==\u201d operator to compare two <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> instances representing identical literal contents will return \u201cfalse\u201d in case the actual <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> objects are different. Furthermore using the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> \u201cequals(String ..)\u201d or&nbsp;<a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>&nbsp;\u201cequalsIgnoreCase(String ..)\u201d&nbsp;operations to compare two <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> instances provides valid results but performs a character to character comparison in case the two compared <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">Strings<\/a>&nbsp;are represented by different instances and their literal contents have the same length. As you can imagine the \u201cequals(String ..)\u201d and&nbsp;\u201cequalsIgnoreCase(String ..)\u201d&nbsp;operations are far more costly compared to the \u201c==\u201d operator which compares the strings at instance level. Nevertheless the aforementioned operations perform an instance equality check (<span class=\"Apple-style-span\" style=\"color: #333333; font-family: Arial,Tahoma,Helvetica,FreeSans,sans-serif; font-size: 14px; line-height: 18px;\">this == obj<\/span>) prior all literal content checks. In order to be able to benefit from the &#8220;<span class=\"Apple-style-span\" style=\"color: #333333; font-family: Arial,Tahoma,Helvetica,FreeSans,sans-serif; font-size: 14px; line-height: 18px;\">this == obj&#8221;<\/span>&nbsp;equality check when comparing <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> instances, you should define <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> values as literal strings and\/or string\u2013valued constant expressions. Doing so, your <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> instances will automatically be interned by the JVM. An alternate, but not favored, approach is the use of <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> \u201cintern()\u201d operation so as to intern a <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> manually. As clearly stated in the Java documentation for the \u201cintern()\u201d method,<br \/>\n<i><span class=\"Apple-style-span\" style=\"font-style: normal;\"><br \/>\n<\/span>\u201cA pool of strings, initially empty, is maintained privately by the class String.<\/i><\/p>\n<p>When the intern method is invoked, if the pool already contains a string equal to this String object as determined by the equals(Object) method, then the string from the pool is returned.<\/p>\n<p><i>Otherwise, this String object is added to the pool and a reference to this String object is returned.<\/i><i><\/i><\/p>\n<p><i> <\/i><i>It follows that for any two strings s and t, s.intern() == t.intern() is true if and only if s.equals(t) is true.<\/i><i><\/i><\/p>\n<p><i> <\/i><i>All literal strings and string-valued constant expressions are interned.\u201d<\/i><\/li>\n<\/ol>\n<p>Our proposed best practices when working with <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> classes are the following :<\/p>\n<ol>\n<li>Favor the creation of literal strings and string\u2013valued constant expressions rather than creating new <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> Objects using one of the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> constructor methods<\/li>\n<li>Utilizing character arrays to perform <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> transformation operations yields the best performance results but is the less flexible approach<\/li>\n<li>When performing <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> transformation operations such as removing, inserting, replacing or appending characters, concatenating or splitting Strings use either the <a href=\"http:\/\/download.oracle.com\/javase\/1.5.0\/docs\/api\/java\/lang\/StringBuilder.html\">StringBuilder<\/a> or the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/StringBuffer.html\">StringBuffer<\/a> class. The <a href=\"http:\/\/download.oracle.com\/javase\/1.5.0\/docs\/api\/java\/lang\/StringBuilder.html\">StringBuilder<\/a> class is introduced in Java 1.5 and is the non\u2013synchronized counterpart of the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/StringBuffer.html\">StringBuffer<\/a> class. Thus if only one Thread will be performing the <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> transformation operations then favor the <a href=\"http:\/\/download.oracle.com\/javase\/1.5.0\/docs\/api\/java\/lang\/StringBuilder.html\">StringBuilder<\/a> class because is the best performer<\/li>\n<\/ol>\n<p><strong><span class=\"Apple-style-span\" style=\"font-size: x-large;\">Pattern First Exact String Matching<\/span><\/strong><div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p>The Java language lacks fast <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> searching algorithms.&nbsp;<a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a>&nbsp;\u201cindexOf(&#8230;)\u201d and \u201clastIndexOf(&#8230;)\u201d operations perform a naive search for the provided pattern against a source text. The naive search is based on the \u201cbrute force\u201d pattern first exact string matching algorithm. The \u201cbrute force\u201d algorithm consists in checking, at all positions in the text, whether an occurrence of the pattern starts there or not. Then, after each attempt, it shifts the pattern by exactly one position to the right. Nevertheless several other algorithms exist that outperform by far the \u201cbrute force\u201d algorithm in both speed and efficiency.<\/p>\n<p>Applications require two kinds of solution depending on which string, the pattern or the text, is given first. In our case, the pattern is provided beforehand, meaning that we always search for a provided pattern against an unknown text. For all applications that need Full Text Search (Text First Exact String Matching) a different set of algorithms is needed that provide indexed scanning. Apache <a href=\"http:\/\/lucene.apache.org\/java\/docs\/index.html\">Lucene<\/a> is one of the most popular text search engine libraries that implement the latter family of algorithms. Nevertheless this article will only investigate algorithms of the first kind.<\/p>\n<p>Thanks to the great work, a book named \u201c<a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/\">Exact String Matching Algorithms<\/a>\u201d, of <i><strong>Christian Charras<\/strong> <\/i>and<i> <strong>Thierry Lecroq<\/strong> <\/i>from<i> <strong>Laboratoire d&#8217;Informatique de Rouen \u2013 Universit\u00e9 de Rouen<\/strong><\/i>, we were able to implemented in Java the most commonly used algorithms for Exact String Matching, where the pattern is given first. The list below displays the algorithm name as provided on <i>Christian Charras<\/i> and <i>Thierry Lecroq<\/i> book followed by our algorithm implementation \u201ccode name\u201d in parenthesis. For further information on each and every algorithm please click on the appropriate link so as to be redirected to the relevant section of the \u201cExact String Matching Algorithms\u201d book.<\/p>\n<ul>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node3.html#SECTION0030\">Brute Force algorithm<\/a> (BF)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node4.html#SECTION0040\">Deterministic Finite Automaton algorithm<\/a> (DFA)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node5.html#SECTION0050\">Karp-Rabin algorithm<\/a> (KR)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node6.html#SECTION0060\">Shift Or algorithm<\/a> (SO)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node7.html#SECTION0070\">Morris-Pratt algorithm<\/a> (MP)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node8.html#SECTION0080\">Knuth-Morris-Pratt algorithm<\/a> (KMP)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node9.html#SECTION0090\">Simon algorithm<\/a> (SMN)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node10.html#SECTION00100\">Colussi algorithm<\/a> (CLS)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node11.html#SECTION00110\">Galil-Giancarlo algorithm<\/a> (GG)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node12.html#SECTION00120\">Apostolico-Crochemore algorithm<\/a> (AC)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node13.html#SECTION00130\">Not So Naive algorithm<\/a> (NSN)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node14.html#SECTION00140\">Boyer-Moore algorithm<\/a> (BM)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node15.html#SECTION00150\">Turbo BM algorithm<\/a> (TBM)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node16.html#SECTION00160\">Apostolico-Giancarlo algorithm<\/a> (AG)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node17.html#SECTION00170\">Reverse Colussi algorithm<\/a> (RC)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node18.html#SECTION00180\">Horspool algorithm<\/a> (HP)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node19.html#SECTION00190\">Quick Search algorithm<\/a> (QS)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/tunedbm.html#SECTION00195\">Tuned Boyer-Moore algorithm<\/a> (BMT)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node20.html#SECTION00200\">Zhu-Takaoka algorithm<\/a> (ZT)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/berryravindran.html#SECTION00205\">Berry-Ravindran algorithm<\/a> (BR)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node21.html#SECTION00210\">Smith algorithm<\/a> (SMT)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node22.html#SECTION00220\">Raita algorithm<\/a> (RT)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node23.html#SECTION00230\">Reverse Factor algorithm<\/a> (RF)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node24.html#SECTION00240\">Turbo Reverse Factor algorithm<\/a> (TRF)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/fdm.html#SECTION00220\">Forward Dawg Matching algorithm<\/a> (FDM)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/bndm.html#SECTION00300\">Backward Nondeterministic Dawg Matching algorithm<\/a> (BNDM)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/bom.html#SECTION00245\">Backward Oracle Matching algorithm<\/a> (BOM)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node25.html#SECTION00250\">Galil-Seiferas algorithm<\/a> (GS)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node26.html#SECTION00260\">Two Way algorithm<\/a> (TW)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node27.html#SECTION00270\">String Matching on Ordered Alphabets algorithm<\/a> (SMOA)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node28.html#SECTION00280\">Optimal Mismatch algorithm<\/a> (OM)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node29.html#SECTION00290\">Maximal Shift algorithm<\/a> (MS)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node31.html#SECTION00310\">Skip Search algorithm<\/a> (SS)<\/li>\n<li><a href=\"http:\/\/www-igm.univ-mlv.fr\/%7Elecroq\/string\/node32.html#SECTION00320\">KMP Skip Search algorithm<\/a> (KPMSS)<\/li>\n<\/ul>\n<p>In the initial version (1.0.0) of the Exact String Search Algorithm suite, for every algorithm we have implemented three utility operations :<\/p>\n<ul>\n<li>compile(String pattern) \u2013 Static operation that performs all necessary preprocessing based on the provided pattern<\/li>\n<li>findAll(String source) \u2013 Returns a list containing all indexes where the search algorithm dictated a valid pattern match<\/li>\n<li>findAll(String pattern, String source) \u2013 This is a helper static operation that encapsulates the functionality of both aforementioned operations<\/li>\n<\/ul>\n<p>Below is an example use of the Boyer-Moore algorithm (BM) :<\/p>\n<p>Case #1<\/p>\n<div>\n<div id=\"highlighter_624417\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">BM bm = BM.compile(pattern);<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java plain\">List&lt;Integer&gt; idx = bm.findAll(source);<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java plain\">List&lt;Integer&gt; idx2 = bm.findAll(source2);<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java plain\">List&lt;Integer&gt; idx3 = bm.findAll(source3);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Case #2<\/p>\n<div>\n<div id=\"highlighter_833223\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java plain\">List&lt;Integer&gt; idx = BM.findAll(pattern, source);<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>In the first case we compile the pattern and perform the search in two distinct steps. This approach is appropriate when we have to search multiple source texts for the same pattern. By compiling the pattern once we can maximize performance results due to the fact that the preprocessing is usually a heavy operation. On the other hand, for once of searches, the second approach provides a more convenient API.<\/p>\n<p>We must pinpoint that our provided implementation is thread safe, and that we do not currently support regular expressions in patterns.<\/p>\n<p>What follows is an example performance comparison between the algorithm implementations of our Exact String Search Algorithms suite. We have searched a 1150000 character text for a 37 character phrase that intentionally did not exist, using the full alphabet size of 65535 characters. Please do not forget that this is a relative performance comparison. Performance results for the vast majority of the provided search algorithms are heavily depended on the provided text, the provided pattern and the alphabet size. Thus you should consider every performance comparison between <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> search algorithms only as a relative one.<\/p>\n<p>At the beginning of this section we have stated that the Java language lacks fast <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> searching algorithms. But how slow is the standard Java naive implementation compared to our algorithm suite? To answer the aforementioned question we have implemented two methods so as to retrieve all index values of potential pattern matches using the standard Java API :<\/p>\n<p>Method #1 \u2013 The indexOf() approach<\/p>\n<div>\n<div id=\"highlighter_127715\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">01<\/div>\n<div class=\"line number2 index1 alt1\">02<\/div>\n<div class=\"line number3 index2 alt2\">03<\/div>\n<div class=\"line number4 index3 alt1\">04<\/div>\n<div class=\"line number5 index4 alt2\">05<\/div>\n<div class=\"line number6 index5 alt1\">06<\/div>\n<div class=\"line number7 index6 alt2\">07<\/div>\n<div class=\"line number8 index7 alt1\">08<\/div>\n<div class=\"line number9 index8 alt2\">09<\/div>\n<div class=\"line number10 index9 alt1\">10<\/div>\n<div class=\"line number11 index10 alt2\">11<\/div>\n<div class=\"line number12 index11 alt1\">12<\/div>\n<div class=\"line number13 index12 alt2\">13<\/div>\n<div class=\"line number14 index13 alt1\">14<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">static<\/code> <code class=\"java plain\">List&lt;Integer&gt; findAll(String pattern, String source) {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">List&lt;Integer&gt; idx = <\/code><code class=\"java keyword\">new<\/code> <code class=\"java plain\">ArrayList&amp;ltInteger&gt;();<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">int<\/code> <code class=\"java plain\">id = -<\/code><code class=\"java value\">1<\/code><code class=\"java plain\">;<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">int<\/code> <code class=\"java plain\">shift = pattern.length();<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">int<\/code> <code class=\"java plain\">scnIdx = -shift;<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">while<\/code> <code class=\"java plain\">(scnIdx != -<\/code><code class=\"java value\">1<\/code> <code class=\"java plain\">|| id == -<\/code><code class=\"java value\">1<\/code><code class=\"java plain\">) {<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">idx.add(scnIdx);<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">id = scnIdx + shift;<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">scnIdx = source.indexOf(pattern, id);<\/code><\/div>\n<div class=\"line number10 index9 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<div class=\"line number11 index10 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">idx.remove(<\/code><code class=\"java value\">0<\/code><code class=\"java plain\">);<\/code><\/div>\n<div class=\"line number12 index11 alt1\"><\/div>\n<div class=\"line number13 index12 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code> <code class=\"java plain\">idx;<\/code><\/div>\n<div class=\"line number14 index13 alt1\"><code class=\"java spaces\">&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Method #2 \u2013 The Matcher find() approach<\/p>\n<div>\n<div id=\"highlighter_985878\" class=\"syntaxhighlighter  java\">\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td class=\"gutter\">\n<div class=\"line number1 index0 alt2\">1<\/div>\n<div class=\"line number2 index1 alt1\">2<\/div>\n<div class=\"line number3 index2 alt2\">3<\/div>\n<div class=\"line number4 index3 alt1\">4<\/div>\n<div class=\"line number5 index4 alt2\">5<\/div>\n<div class=\"line number6 index5 alt1\">6<\/div>\n<div class=\"line number7 index6 alt2\">7<\/div>\n<div class=\"line number8 index7 alt1\">8<\/div>\n<div class=\"line number9 index8 alt2\">9<\/div>\n<\/td>\n<td class=\"code\">\n<div class=\"container\">\n<div class=\"line number1 index0 alt2\"><code class=\"java keyword\">public<\/code> <code class=\"java keyword\">static<\/code> <code class=\"java plain\">List&lt;Integer&gt; findAll(String pattern, String source) {<\/code><\/div>\n<div class=\"line number2 index1 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">List&lt;Integer&gt; idx = <\/code><code class=\"java keyword\">new<\/code> <code class=\"java plain\">ArrayList&amp;ltInteger&gt;();<\/code><\/div>\n<div class=\"line number3 index2 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">Pattern ptrn = Pattern.compile(pattern);<\/code><\/div>\n<div class=\"line number4 index3 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java plain\">Matcher mtch = ptrn.matcher(source);<\/code><\/div>\n<div class=\"line number5 index4 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">while<\/code><code class=\"java plain\">(mtch.find())<\/code><\/div>\n<div class=\"line number6 index5 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;&nbsp;<\/code><code class=\"java plain\">idx.add(mtch.start());<\/code><\/div>\n<div class=\"line number7 index6 alt2\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><\/div>\n<div class=\"line number8 index7 alt1\"><code class=\"java spaces\">&nbsp;&nbsp;<\/code><code class=\"java keyword\">return<\/code> <code class=\"java plain\">idx;<\/code><\/div>\n<div class=\"line number9 index8 alt2\"><code class=\"java spaces\">&nbsp;<\/code><code class=\"java plain\">}<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Below we present the performance comparison chart between the aforementioned search algorithms<\/p>\n<div class=\"separator\" style=\"clear: both; text-align: center;\"><a href=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TIIpK8iq4GI\/AAAAAAAAACA\/Wd094OdtoZs\/s1600\/chart.png\"><img decoding=\"async\" src=\"http:\/\/3.bp.blogspot.com\/_tWwHCKnIbjs\/TIIpK8iq4GI\/AAAAAAAAACA\/Wd094OdtoZs\/s400\/chart.png\" width=\"400\" height=\"396\" border=\"0\"><\/a><\/div>\n<p>The horizontal axis represents the average time in milliseconds every algorithm needed so as to preprocess and parse the provided text. Thus lower values are better. As you can see Java naive implementation (indexOf() approach) outperforms the Java Matcher \u201cfind()\u201d approach along with almost all our search algorithm implementations. In other words, when you are dealing with small to medium sized string searches it is preferred to implement something like the code snippet we provided above rather than use a more sophisticated <a href=\"http:\/\/download.oracle.com\/javase\/1.4.2\/docs\/api\/java\/lang\/String.html\">String<\/a> search algorithm. On the other hand when dealing with large to very large documents one of the fastest algorithms from our suite will surely come in handy!<\/p>\n<p>You can download version 1.0.0 of our Exact String Search Algorithm suite distribution <a href=\"https:\/\/sourceforge.net\/projects\/jstringsearch\/\">here<\/a><\/p>\n<p>Happy coding<\/p>\n<p>Justin<\/p>\n<div style=\"margin: 0px;\"><strong><i>Related Articles :<\/i><\/strong><\/div>\n<ul>\n<li><a href=\"https:\/\/www.javacodegeeks.com\/2010\/07\/java-best-practices-dateformat-in.html\">Java Best Practices \u2013 DateFormat in a Multithreading Environment<\/a><\/li>\n<li><a href=\"https:\/\/www.javacodegeeks.com\/2010\/07\/java-best-practices-high-performance.html\">Java Best Practices \u2013 High performance Serialization<\/a><\/li>\n<li><a href=\"https:\/\/www.javacodegeeks.com\/2010\/08\/java-best-practices-vector-arraylist.html\">Java Best Practices \u2013 Vector vs ArrayList vs HashSet<\/a><\/li>\n<li><a href=\"https:\/\/www.javacodegeeks.com\/2010\/09\/java-best-practices-queue-battle-and.html\">Java Best Practices \u2013 Queue battle and the Linked ConcurrentHashMap<\/a><\/li>\n<li><a href=\"https:\/\/www.javacodegeeks.com\/2010\/11\/java-best-practices-char-to-byte-and.html\">Java Best Practices \u2013 Char to Byte and Byte to Char conversions<\/a><\/li>\n<\/ul>\n<div style=\"margin: 0px;\"><strong><i>Related Snippets :<\/i><\/strong><\/div>\n<ul style=\"text-align: left;\">\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/lang\/string\/convert-string-to-byte-array-utf-encoding\">Convert String to byte array UTF encoding<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/lang\/string\/convert-string-to-byte-array-ascii-encoding\">Convert String to byte array ASCII encoding<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/lang\/string\/search-string-with-indexof-method\">Search String with indexOf method<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/lang\/stringbuffer\/stringbuffer-append-method\">StringBuffer append method<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/stringtokenizer\/stringtokenizer-count-tokens\">StringTokenizer Count Tokens<\/a><\/li>\n<li><a href=\"https:\/\/examples.javacodegeeks.com\/core-java\/util\/stringtokenizer\/reverse-string-with-stringtokenizer\">Reverse String with StringTokenizer<\/a><\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String performance tuning. We will focus on how to handle String creation, String alteration and String matching operations efficiently. Furthermore we will provide our own implementations of the most commonly used algorithms for Exact &hellip;<\/p>\n","protected":false},"author":4,"featured_media":148,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[98,66,99],"class_list":["post-314","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-core-java","tag-exact-string-matching","tag-java-best-practices","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Java Best Practices \u2013 String performance and Exact String Matching - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Best Practices \u2013 String performance and Exact String Matching - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html\" \/>\n<meta property=\"og:site_name\" content=\"Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2010-09-04T15:07:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T13:18:15+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Byron Kiourtzoglou\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Byron Kiourtzoglou\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html\"},\"author\":{\"name\":\"Byron Kiourtzoglou\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9c0c8d27141b068173953202dd9aebeb\"},\"headline\":\"Java Best Practices \u2013 String performance and Exact String Matching\",\"datePublished\":\"2010-09-04T15:07:00+00:00\",\"dateModified\":\"2022-11-21T13:18:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html\"},\"wordCount\":1787,\"commentCount\":4,\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"keywords\":[\"Exact String Matching\",\"Java Best Practices\",\"String\"],\"articleSection\":[\"Core Java\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html\",\"name\":\"Java Best Practices \u2013 String performance and Exact String Matching - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"datePublished\":\"2010-09-04T15:07:00+00:00\",\"dateModified\":\"2022-11-21T13:18:15+00:00\",\"description\":\"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#primaryimage\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2012\\\/10\\\/java-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/2010\\\/09\\\/string-performance-exact-string.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Core Java\",\"item\":\"https:\\\/\\\/www.javacodegeeks.com\\\/category\\\/java\\\/core-java\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Java Best Practices \u2013 String performance and Exact String Matching\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#website\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Developers Resource Center\",\"publisher\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.javacodegeeks.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"contentUrl\":\"https:\\\/\\\/www.javacodegeeks.com\\\/wp-content\\\/uploads\\\/2022\\\/06\\\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/javacodegeeks\",\"https:\\\/\\\/x.com\\\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.javacodegeeks.com\\\/#\\\/schema\\\/person\\\/9c0c8d27141b068173953202dd9aebeb\",\"name\":\"Byron Kiourtzoglou\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g\",\"caption\":\"Byron Kiourtzoglou\"},\"description\":\"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\\\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.\",\"sameAs\":[\"https:\\\/\\\/www.pivotalgamers.com\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/byron-kiourtzoglou-530ab222\"],\"url\":\"https:\\\/\\\/www.javacodegeeks.com\\\/author\\\/byron-kiourtzoglou\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Java Best Practices \u2013 String performance and Exact String Matching - Java Code Geeks","description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html","og_locale":"en_US","og_type":"article","og_title":"Java Best Practices \u2013 String performance and Exact String Matching - Java Code Geeks","og_description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String","og_url":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html","og_site_name":"Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2010-09-04T15:07:00+00:00","article_modified_time":"2022-11-21T13:18:15+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","type":"image\/jpeg"}],"author":"Byron Kiourtzoglou","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Byron Kiourtzoglou","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#article","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html"},"author":{"name":"Byron Kiourtzoglou","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9c0c8d27141b068173953202dd9aebeb"},"headline":"Java Best Practices \u2013 String performance and Exact String Matching","datePublished":"2010-09-04T15:07:00+00:00","dateModified":"2022-11-21T13:18:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html"},"wordCount":1787,"commentCount":4,"publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","keywords":["Exact String Matching","Java Best Practices","String"],"articleSection":["Core Java"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html","url":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html","name":"Java Best Practices \u2013 String performance and Exact String Matching - Java Code Geeks","isPartOf":{"@id":"https:\/\/www.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#primaryimage"},"image":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#primaryimage"},"thumbnailUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","datePublished":"2010-09-04T15:07:00+00:00","dateModified":"2022-11-21T13:18:15+00:00","description":"Continuing our series of articles concerning proposed practices while working with the Java programming language, we are going to talk about String","breadcrumb":{"@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#primaryimage","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2012\/10\/java-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.javacodegeeks.com\/2010\/09\/string-performance-exact-string.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Java","item":"https:\/\/www.javacodegeeks.com\/category\/java"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https:\/\/www.javacodegeeks.com\/category\/java\/core-java"},{"@type":"ListItem","position":4,"name":"Java Best Practices \u2013 String performance and Exact String Matching"}]},{"@type":"WebSite","@id":"https:\/\/www.javacodegeeks.com\/#website","url":"https:\/\/www.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Developers Resource Center","publisher":{"@id":"https:\/\/www.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/www.javacodegeeks.com\/#\/schema\/person\/9c0c8d27141b068173953202dd9aebeb","name":"Byron Kiourtzoglou","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/45058c722c80f088c409be43e0f4be20dad81d55a7c9aefd75bab2e4253ae4e6?s=96&d=mm&r=g","caption":"Byron Kiourtzoglou"},"description":"Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications\/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.","sameAs":["https:\/\/www.pivotalgamers.com\/","https:\/\/www.linkedin.com\/in\/byron-kiourtzoglou-530ab222"],"url":"https:\/\/www.javacodegeeks.com\/author\/byron-kiourtzoglou"}]}},"_links":{"self":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=314"}],"version-history":[{"count":0,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/314\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media\/148"}],"wp:attachment":[{"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}