Duplicate :
|
FULL PRODUCT VERSION : 1.6.0_20-64Bit SDK, IDE: IntelliJ IDEA 8.1 ADDITIONAL OS VERSION INFORMATION : Windows 7 64Bit Professional EXTRA RELEVANT SYSTEM CONFIGURATION : machine: Intel I5, 4GB A DESCRIPTION OF THE PROBLEM : The String.indexof()-method sometimes returns an invalid value: Code-Sample: -------------------- public static String replaceChars(String name, String old, String newDelimiter) { if (name == null) return name; int index = name.indexOf(old); while (index != -1) { if (index == 0) { name = newDelimiter + name.substring(old.length()); } else { name = name.substring(0, index) + newDelimiter + name.substring(index + old.length()); } index = name.indexOf(old); } return name; } ________________ Problem (Example): name.length() returns '44' --> 'name.indexof(old)' returns '44' (!!!) -> (should return '-1' if value of variable 'old' is not included in 'name', but 44 doesn��t even exit in index-range!) This Problem does not occur if '-Xint' is used as Parameter. --> Assumption: It��s an optimization-problem, we had that problem in the Java-SDK-versions 1.6.0_19 and 1.6.0_20, both 64 bit. The problem does not occur in Java-Version 1.6.0_12, 32Bit. Info: variable-values were checked using debug-mode, the problem is non-deterministic, meaning that it cannot be reproduced in the same loop-itarations, but often occured in case of a name.length() - value of 44. STEPS TO FOLLOW TO REPRODUCE THE PROBLEM : run example-code (see Description): public static String replaceChars(String name, String old, String newDelimiter) { if (name == null) return name; int index = name.indexOf(old); while (index != -1) { if (index == 0) { name = newDelimiter + name.substring(old.length()); } else { name = name.substring(0, index) + newDelimiter + name.substring(index + old.length()); } index = name.indexOf(old); } return name; } EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - Example: name.length() returns '44' --> 'name.indexof(old)' returns '44' (!!!) -> (should return '-1' if value of variable 'old' is not included in 'name', but 44 doesn��t even exit in index-range!) ACTUAL - name.indexof(var) should return '-1' if value of 'var' is not included in 'name', but returns an index that��s not even in the range (see Desc.) REPRODUCIBILITY : This bug can be reproduced always. ---------- BEGIN SOURCE ---------- public static String replaceChars(String name, String old, String newDelimiter) { if (name == null) return name; int index = name.indexOf(old); while (index != -1) { if (index == 0) { name = newDelimiter + name.substring(old.length()); } else { name = name.substring(0, index) + newDelimiter + name.substring(index + old.length()); } index = name.indexOf(old); } return name; } ---------- END SOURCE ---------- CUSTOMER SUBMITTED WORKAROUND : using the indexof()-method twice, it returns -1 on the second use, even if the first one, direcly performed before, returned another value: int index: index = name.indexof(var); --> wrong value index = name.indexof(var); --> correct value