Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String comparison - which way around?

Reply
Thread Tools

String comparison - which way around?

 
 
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
Posts: n/a
 
      12-11-2006
trippy wrote:
> In article <. com>,
> Daniel Pitts took the hamburger meat, threw it on the grill, and I said
> "Oh Wow"...
>> trippy wrote:
>>> In article <. com>,
>>> harryajh took the hamburger meat, threw it on the grill, and I said "Oh
>>> Wow"...
>>>
>>>> Which is best?
>>>>
>>>> String str = "hello";
>>>>
>>>> if("hello".equals(str))
>>>>
>>>> or
>>>>
>>>> if(str.equals("hello")
>>>>
>>>> does it make any difference
>>> No.
>>>

>> It does make a difference if str might be null.

>
> Not that you won't get an NPE from either of these statements but we'll
> play hypothetical coder just to look cool.


The first will not give a NPE the second will if str is null.

Arne
 
Reply With Quote
 
 
 
 
trippy
Guest
Posts: n/a
 
      12-11-2006
In article <457cbcec$0$49202$>, =?ISO-8859-1?Q?
Arne_Vajh=F8j?= took the hamburger meat, threw it on the grill, and I
said "Oh Wow"...

> trippy wrote:
> > In article <. com>,
> > Daniel Pitts took the hamburger meat, threw it on the grill, and I said
> > "Oh Wow"...
> >> trippy wrote:
> >>> In article <. com>,
> >>> harryajh took the hamburger meat, threw it on the grill, and I said "Oh
> >>> Wow"...
> >>>
> >>>> Which is best?
> >>>>
> >>>> String str = "hello";
> >>>>
> >>>> if("hello".equals(str))
> >>>>
> >>>> or
> >>>>
> >>>> if(str.equals("hello")
> >>>>
> >>>> does it make any difference
> >>> No.
> >>>
> >> It does make a difference if str might be null.

> >
> > Not that you won't get an NPE from either of these statements but we'll
> > play hypothetical coder just to look cool.

>
> The first will not give a NPE the second will if str is null.


Because it'll just compare "hello" to null. (in the first one)

Makes sense.

--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "Heart And Soul" -- T'Pau

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



 
Reply With Quote
 
 
 
 
Chris Uppal
Guest
Posts: n/a
 
      12-11-2006
trippy wrote:

> Not that you won't get an NPE from either of these statements but we'll
> play hypothetical coder just to look cool.
>
> if ("hello".equals(str)) --> "hello" != null so it'll be false.
> if (str.equals("hello"))--> null != "hello" so it'll be false.


The first will return false; the second will blow up with an NPE. They are
/not/ equivalent (as this thread has already discussed at some length).

-- chris


 
Reply With Quote
 
trippy
Guest
Posts: n/a
 
      12-12-2006
In article <457da818$0$628$>, Chris Uppal took
the hamburger meat, threw it on the grill, and I said "Oh Wow"...

> trippy wrote:
>
> > Not that you won't get an NPE from either of these statements but we'll
> > play hypothetical coder just to look cool.
> >
> > if ("hello".equals(str)) --> "hello" != null so it'll be false.
> > if (str.equals("hello"))--> null != "hello" so it'll be false.

>
> The first will return false; the second will blow up with an NPE.


Right. My bad. And thanks again to Arne for pointing that out too.

> They are
> /not/ equivalent (as this thread has already discussed at some length).


Yeah Trippy, you goofball. Gahd.

>
> -- chris


--
trippy
mhm31x9 Smeeter#29 WSD#30
sTaRShInE_mOOnBeAm aT HoTmAil dOt CoM

NP: "The American Way" -- Sacred Reich

"Now, technology's getting better all the time and that's fine,
but most of the time all you need is a stick of gum, a pocketknife,
and a smile."

-- Robert Redford "Spy Game"



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparison of 2 files and generating the output based on comparison Deepu Perl Misc 1 02-07-2011 03:09 PM
False Positives From String Comparison using string.Equals() Smithers ASP .Net 12 07-07-2009 01:23 AM
Price Comparison Service. Best Deal. Special Coupon atBest-Price-Comparison.com rapee Digital Photography 0 03-14-2008 06:46 AM
Is there systematic performance comparison of std::string and c style string? yu_kuo@sina.com C++ 21 08-18-2007 04:32 PM
Microcontrollers: which one ? which language ? which compiler ? The Jesus of Suburbia NZ Computing 2 02-11-2006 06:53 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57