Starting out string question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Schaft

    #1

    Starting out string question

    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.sub string(0,3) == "/u:")

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

    Thanks for enlightening this Java newbie

    Alex
  • Gordon Beaton

    #2
    Re: Starting out string question

    On Thu, 25 May 2006 20:14:55 +0200, Alex Schaft wrote:[color=blue]
    > Why does the first if statement return false, and the second return
    > true?
    >
    > cParameter = "/u:001"
    >
    > if (cParameter.sub string(0,3) == "/u:")
    >
    > if (cParameter.sub string(0, 3).equals("/u:"))
    >
    > Thanks for enlightening this Java newbie[/color]

    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

    --
    [ 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

    Comment

    • Alex Schaft

      #3
      Re: Starting out string question

      On 2006/05/25 08:47 PM, Gordon Beaton wrote:[color=blue]
      > On Thu, 25 May 2006 20:14:55 +0200, Alex Schaft wrote:[color=green]
      >> Why does the first if statement return false, and the second return
      >> true?
      >>
      >> cParameter = "/u:001"
      >>
      >> if (cParameter.sub string(0,3) == "/u:")
      >>
      >> if (cParameter.sub string(0, 3).equals("/u:"))
      >>
      >> Thanks for enlightening this Java newbie[/color]
      >
      > The first compares the strings for *identity* (are they the same
      > object), the second compares them for *equality* (do they have the
      > same contents).
      >[/color]
      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

      Comment

      • Gordon Beaton

        #4
        Re: Starting out string question

        On Fri, 26 May 2006 06:10:05 +0200, Alex Schaft wrote:[color=blue]
        > 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 :)[/color]

        ....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

        --
        [ 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

        Comment

        Working...