×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Regular Contributor
    Join Date
    Mar 2010
    Posts
    136

    the difference between if(c==cmdLogin) and if(c.equals(cmdLogin))?

    Code:
    public void commandAction(Command c, Displayable d) {
    		//if(c==cmdLogin)
    		if(c.equals(cmdLogin))
    What is the difference between if(c==cmdLogin) and if(c.equals(cmdLogin))?

    Which one should be used and Why?

  2. #2
    Registered User
    Join Date
    Sep 2007
    Location
    Bangalore
    Posts
    868

    Re: the difference between if(c==cmdLogin) and if(c.equals(cmdLogin))?

    Hi komomo,
    AFAI

    The better way is
    if(c==cmdLogin)
    It will check the object reference ID , It is simpler.

    If you use the
    if(c.equals(cmdLogin))
    It will Compare all the values which are there in the command class against to the other command class , I feel it is expensive , This is up to my understanding , If any one knows more about it please let know.

  3. #3
    Super Contributor
    Join Date
    Nov 2009
    Location
    Minnesota, USA
    Posts
    3,209

    Re: the difference between if(c==cmdLogin) and if(c.equals(cmdLogin))?

    This is a very fundamental aspect of Java (and most other pointer-based languages) that you need to understand. In your example both "c" and "cmdLogin" are pointers -- addresses of objects in the heap. When you say "c==cmdLogin" you're asking if the two pointers point to the EXACT SAME object. In many cases this is what you want. But if the objects in question are, for instance, Strings, then you may have two different Strings that both contain the value "ABC", but their addresses will be different (since every object has a unique address). So for that case "c==cmdLogin" will be false, even though both Strings contain "ABC" (and thus "c.equals(cmdLogin)" will be true).

    So the question you always have to ask is: Do I want to know if the two references are to the SAME OBJECT, or do I want to know if the two references reference the SAME VALUE? There's no single answer to this question, but it depends on the intent of your code.

    Once you've mastered this concept you can bone up on "interned strings", for extra credit.

Similar Threads

  1. What's the difference ?
    By amit_yadav in forum Mobile Java General
    Replies: 3
    Last Post: 2009-05-07, 06:08
  2. what's the difference
    By zanzon in forum Mobile Java General
    Replies: 8
    Last Post: 2006-05-24, 18:20
  3. what is the difference ?
    By white_dragon in forum Symbian Networking & Messaging (Closed)
    Replies: 5
    Last Post: 2004-12-03, 10:32
  4. HELP string.equals driving me nuts
    By mshita in forum Mobile Java General
    Replies: 2
    Last Post: 2004-08-19, 19:11
  5. What is the difference between ...?
    By coolcheena in forum Mobile Java General
    Replies: 1
    Last Post: 2003-12-23, 21:04

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×