Re: "some text".equals(someString)
Francois BOTTIN <frbottin@yahoo.fr> wrote:
> --- "E.L. Willighagen" <egonw@sci.kun.nl> wrote:
> >
> > Hi all,
> >
> > can anyone tell me the difference (performance etc) between:
> >
> > 1. String someString = "text"
> > if ("some text".equals(someString)) {};
> >
> > and
> >
> > 2. String someString = "text"
> > if (someString.equals("some text")) {};
> >
>
> I don't know if there's a difference in performance (and it will
> certainly depend of the JVM implementation).
I do not think there is a difference.
> Personnaly I use the first alternative as it permits null values for
> someString without throwing a NullPointerException.
So do I.
> It's also possible to have someString that's not declared as a
> String (you don't need to cast it, and hence avoid a
> ClassCastException).
Well, .equals(Object) is an Object method, so you'll never have a
casting probleme. Even for a String, myString.equals(Object) and NOT
myString.equals(String)
java.lang
Class String
java.lang.Object
|
+--java.lang.String
+---------------+----------------------------------------------------+
| boolean | equals(Object anObject) |
| | Compares this string to the specified |
| | object. |
+---------------+----------------------------------------------------+
-- Arnaud, STE-Formations Informatiques, fapse, ULg, .BE
Reply to: