Yeah, you can get lucky a lot with using reference comparison with
Strings, because many applications (and big parts of Java itself) will
internalize things.  

Stop me if you've heard this, but Java tries to help you out with
having a hojillion references to "MyName" by allowing you to call
internalize() on the String.  That makes a call into something that
behaves like a big static Set, which returns a single reference for any
given String.  Its important to note here that this and other tricks
only work because String is final and immutable.

As for what Xerces "should" be doing, its hard to tell.  To be
pedantic, the interface doesn't specifically say it will return the
same reference on multiple calls, so it doesn't have to.  Some caching
might be nice, but as with all caching you're taking up resources and
adding complexity.  Maybe it was just easier to make another one.  And,
you wouldn't want to depend on it anyway.

At the end of the day, always use .equals() with strings.  It would
probably be nice if the IDE warned you when you're comparing String
references, though.

-Kevin
--- Michael Ryan Bannon <[EMAIL PROTECTED]> wrote:
> Yeah, I had thought about that, but I had similar equiv. tests in my
> code
> that worked.  The only difference is that they were pulling values
> from
> element names and attributes, NOT element values.
> 
> In short, I should be using .equals() for all string comparisons.
> Regardless, Xerces still should have returned that original element
> string
> value equiv. as true and not added white space or whatever, no?
> 
> Now that I look at this problem, I realize that I've come across it
> before.
> Ugh...I love Java, but this...ugh.
> 
> Thanks guys.
> 
> ----- Original Message ----- 
> From: "Kevin Klinemeier" <[EMAIL PROTECTED]>
> To: <[EMAIL PROTECTED]>
> Sent: Monday, October 20, 2003 6:59 PM
> Subject: Re: Node.getNodeValue() problem
> 
> 
> > Yep, stupid Java problem.
> >
> > You need to use .equals("and") to compare the strings.  the "=="
> > operator tests for reference equality.  You have two String objects
> in
> > memory, which have different references, so the "==" test fails.
> > However, if you tell them to compare themselves, they'll figure out
> > they mean the same thing.
> >
> > You can use internalize() on both to get the One True String for
> the
> > system, but you really don't want to bother with that here.
> >
> > -Kevin
> > --- Michael Ryan Bannon <[EMAIL PROTECTED]> wrote:
> > > Hello,
> > >
> > > This might be just a stupid Java problem I'm overlooking.
> > >
> > > The snippet below deals with a TEXT_NODE with a String value
> "and":
> > >
> > > ============================================
> > > ...
> > >
> > > // Output the value of a text node.
> > > // This should, and does, output "and".
> > > System.out.println(node.getNodeValue());
> > >
> > > // Test the node.
> > > if (node.getNodeValue() == "and")
> > >     System.out.println("it worked");
> > >
> > > ..
> > > ============================================
> > >
> > > Now, I've confirmed that it's a text node before the snippet. 
> The
> > > first println outputs "and".  However, the "if" statement is
> false,
> > > so I never see "it worked".  How is this possible?
> > >
> > > This seems so trivial...what am I missing?
> > >
> > > Thanks in advance,
> > >
> > > Ryan
> >
> >
> > __________________________________
> > Do you Yahoo!?
> > The New Yahoo! Shopping - with improved product search
> > http://shopping.yahoo.com
> >
> >
> ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > For additional commands, e-mail: [EMAIL PROTECTED]
> >
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 


__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to