Java String - 1 is not 1?

607 Views Asked by At

I'm a c++ programmer who wrote some php server code to be translated to java and pretty new to Java. But it feels quite common coming from c++. I have a server up and running and this code:

String req = "<xml><version>1</version><test></test></xml>";
Document doc = loadXMLFromString (req);
Element root = doc.getDocumentElement();    // "xml"

// check version
Element e = firstChildElement (root, "version");
String result = e.getTextContent(); // returns "1"!
String expected = "1";
if (result != expected)
{
    out.printLn ("wrong version: (" + result + "), expected: (" + expected + ")!");
    return;
}

This prints "wrong version: (1), expected (1)". The same holds with if (result != "1"). Now even the debugger (eclipse) shows that result is actually "1", so I'm truly lost here. I seem to be missing the obvious but just can't see it...can you help pls? Thanks!

1

There are 1 best solutions below

0
user1438038 On

Use .equals() to compare String objects.