Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Starting out string question

Thread view: 
Alex Schaft - 25 May 2006 19:14 GMT
Hi,

I'm starting to teach myself Java, and have a quick question:

Why does the first if statement return false, and the second return true?

cParameter = "/u:001"

if (cParameter.substring(0,3) == "/u:")

if (cParameter.substring(0, 3).equals("/u:"))

Thanks for enlightening this Java newbie

Alex
Gordon Beaton - 25 May 2006 19:47 GMT
> Why does the first if statement return false, and the second return
> true?
[quoted text clipped - 6 lines]
>
> Thanks for enlightening this Java newbie

The first compares the strings for *identity* (are they the same
object), the second compares them for *equality* (do they have the
same contents).

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e

Alex Schaft - 26 May 2006 05:10 GMT
>> Why does the first if statement return false, and the second return
>> true?
[quoted text clipped - 10 lines]
> object), the second compares them for *equality* (do they have the
> same contents).

OK. I saw in the JIAN book that == does equal value for primitive, and
equal identity for reference. Any way to convert a reference type to a
primitive type so that the == can be used for strings?

We're looking at converting some legacy code to Java, and this would be
more "natural" for us :)

Thanks for the help,
Alex
Gordon Beaton - 26 May 2006 07:56 GMT
> OK. I saw in the JIAN book that == does equal value for primitive, and
> equal identity for reference. Any way to convert a reference type to a
> primitive type so that the == can be used for strings?
>
> We're looking at converting some legacy code to Java, and this would
> be more "natural" for us :)

...but less natural for Java.

At any rate, look into String.intern(). Interned Strings can be
compared using ==, however that requires you to intern() all of the
Strings you encounter. I think you'll find that using equals() is less
trouble, and less error prone.

The topic has often been discussed in comp.lang.java.programmer, where
you'll also discover much more traffic than in this (defunct) group.

/gordon

Signature

[  do not email me copies of your followups  ]
g o r d o n + n e w s @  b a l d e r 1 3 . s e



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.