Why do I get different results with String.intern() vs. passing String object in Java?
<p>I have a command line program to validate an XML against an XSD file.  One of the command line options for this program is the namespace to use, which is stored in <code>String namespace</code>.  I get a different validation result depending on whether I pass the parsed option as <code>namespace</code> or pass the call to <code>namespace.intern()</code>.  The different results mean that somewhere in the XML validator the <code>String</code> comparisons performed on namespace have different results, even though they should have the same set of ASCII values.  </p>

<p>Is there a fundamental reason why these might produce different comparison results?</p>

<p>The <code>NamespaceFilter</code> class, see below, is where the namespace value is used.  This class compares <code>namespace</code> to the value found on the current element inside <code>startElement</code> and then assigns it.  <code>startElement</code> is called by the XML reader.</p>

<p>These are the line variations inside validateAgainstXSD:<br></p>

<h3><code>String.intern()</code><br></h3>

<p><code>NamespaceFilter nsf = new NamespaceFilter(XMLReaderFactory.createXMLReader(), namespace.intern());</code><br></p>

<p><em>Result</em>:<br> 
Validating uart.xml.</p>

<h3>String object as is<br></h3>

<p><code>NamespaceFilter nsf = new NamespaceFilter(XMLReaderFactory.createXMLReader(), namespace);</code><br></p>

<p><em>Result</em>:<br> 
Error at 4: cvc-complex-type.2.4.a: Invalid content was found starting with element 'fileVersion'. One of '{"myNamespace":fileVersion}' is expected.</p>

<h3>Source in context</h3>

<pre><code>public static void validateAgainstXSD(File file, File schemaFile, String namespace) {

    try {
        SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");

        Schema xsdScheme = factory.newSchema(schemaFile);

        Validator validator = xsdScheme.newValidator();
        ErrorHandler eh = new DefaultErrorHandler();

        validator.setErrorHandler(eh);

        // Create namespace replacement filter  
        NamespaceFilter nsf = new NamespaceFilter(XMLReaderFactory.createXMLReader(), namespace.intern());

        // Load the XML source
        SAXSource source = new SAXSource(nsf, new InputSource(new FileInputStream(file)));

        validator.validate(source, null);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

private static class NamespaceFilter extends XMLFilterImpl {

    private String requiredNamespace;

    public NamespaceFilter(XMLReader parent) {
        super(parent);
    }

    public NamespaceFilter(XMLReader parent, String namespace) {
        this(parent);

        requiredNamespace = namespace;
    }

    @Override
    public void startElement(String uri,
            String localName,
            String qName,
            Attributes atts)
            throws SAXException {

        if (!uri.equals(requiredNamespace)) {
            uri = requiredNamespace;
        }
        super.startElement(uri, localName, qName, atts);

    }
}
</code></pre>
