Comparing String objects:

Comparing with the == Operator
The == operator works on String object references. If two String
variables point to the same object in memory, the comparison returns a
true result. Otherwise, the comparison returns false, regardless
whether the text has the same character values. The == operator does
not compare actual char data. Without this clarification, you might be
surprised that the following code snippet prints The strings are
unequal.

Comparing with the equals Method
The equals method compares the actual char content of two strings.
This method returns true when two String objects hold char data with
the same values. This code sample prints The strings are equal.

In short you should use equals method to compare strings. For more
detail visit http://java.sun.com/mailers/techtips/corejava/2006/tt0822.html
Tech Tip #2: How should I compare String objects

Regards
Atif


On Jun 30, 8:56 pm, Maul <[email protected]> wrote:
> The following code is from "Java Collection Framework" tutorial from
> JavaIntro, modified to want I wanna do:)
>
> What I wanna find out is how you would look for an object of a
> specific element in an ListIterator. Let's say I want to find out if
> the next() element in ListIterator is an instance of the String Class
> and do something if this is true. What I have tried is
>
>   // Create ListIterator and iterate entries in it
>         ListIterator li = al.listIterator();
>         while (li.hasNext())
>             If(li.next().getClass().getName() == "java.lang.String")
>                  System.out.println("From ListIterator = " + li.next()
> + " is a String object");
>             else
>                  System.out.println("From ListIterator = " + li.next
> ());
>
> The problem with the above code is that the next() method seems to
> move the cursor of the ListIterator one step forward and therebye
> skipping an element because there is a next() method in the if(....)
> and in the println(), how do I sort this?
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to