0

The following routine behaves differently on WIN XP x32, JAVA Version 7 Update 9 and on WIN7 x64, JAVA version 6 Update 32.

private int getNrOfMatches(String temp, String regex) {
            String prev;
            int nrOfIterations = -1;
            do {
                nrOfIterations++;
                prev = temp;
                temp = temp.replaceFirst(regex, " ");
            } while (temp != prev);
            return nrOfIterations;
        }

replaceFirst() returns the same object if it didn't modify anything and the loop ends on WIN XP. On Win7 it goes on an endless loop as != will always return false because the routine returns a new object even if it didn't change anything. Using .equals() instead of != solves this issue but my question is can anyone explain this behaviour?

TomaC
  • 89
  • 6
  • 1
    While I can't comment on why it would work in one os and not the other, I can say that temp != prev is not what you think it is. that is comparing object references not values – Jason Sperske Nov 29 '12 at 17:33
  • 1
    if your observation is correct, in one version replaceFirst() returns the reference to the same object, but in the other just a reference to a String with same value. Guess the implementation differs. – Aksel Willgert Nov 29 '12 at 17:36
  • when comparing strings you use .equals() to check to see if the value is the same and = checks to see if the object references are the same (exact same object) .equals() is not a replacement for != unless you meant you used .equals() == false or something this could be part of the problem – leigero Nov 29 '12 at 17:36
  • what are the values for `temp` and `regex`? – jlordo Nov 29 '12 at 17:38

2 Answers2

2

Never compare Strings with == or !=

Use (str1.equals(str2)) or (!str1.equals(str2))

== and != compare object references for non primitive types.

equals() compares the content of objects when the class overrides this method it inherits from Object.

jlordo
  • 37,490
  • 6
  • 58
  • 83
2

So to take a stab at answering the part of the question of why it is working some of the time. The JVM has some cleaver optimizations that try and reuse references of the same string instead of creating new references with the same content. It is possible that while running on Win XP your JVM has these optimizations while your Win 7 does not (are they the same version?)

This SO Question covers this topic fairly well and the accepted answer talks about ways to actually add strings to a constant pool (calling .intern()). In terms of determining equality, this is not a stable solution because the JVM will always take priority over any memory management choices you make in your Java code.

Also digging into the JVM options I did find this interesting option (-XX:+UseStringCache) and this SO question that talks about what it does. Maybe your Win XP JVM has the -server optimizations while your Win 7 does not?

Community
  • 1
  • 1
Jason Sperske
  • 29,816
  • 8
  • 73
  • 124
  • thanks for trying to answer the slightly more interesting part of the question. But to score points I think it should also contain some references to what 'improvements' introduced this new behavior :) – Aksel Willgert Nov 29 '12 at 17:41