Results 1 to 4 of 4
  1. #1
    Mantisser is offline Member
    Join Date
    May 2012
    Posts
    6
    Rep Power
    0

    Default Differing types of equals operands??

    Hey guys was jusy messing around with a program taking in a user yes/no imput and it was not working

    XML Code:
    	      if (userResponse == "y" || userResponse == "Y")
    	      {
    	    	  proxypurchase_handler.getPurchase_session().setTotal_spent(proxypurchase_handler.getPurchase_session().getTotal_spent()-10);
    	    	  System.out.println("Your coupon has been cashed in!");
    	      }

    basically the code above was not working. However when conditions to:
    XML Code:
    		      if (userResponse.equals("y"))
    		      {
    		    	  proxypurchase_handler.getPurchase_session().setTotal_spent(proxypurchase_handler.getPurchase_session().getTotal_spent()-5);
    		    	  System.out.println("Your coupon has been cashed in!");
    		      }
    was just trying to understand this?? can anyone help explain the difference between the two equals conditional statements?
    thanks!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,040
    Rep Power
    10

    Default Re: Differing types of equals operands??

    I would recommend googling something like "java equals vs ==" for more info, but the gist is that == returns whether two Objects are the same instance. The equals() method can be overridden to mean anything- in the case of Strings, it returns whether the two instances contain the same character String.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Differing types of equals operands??

    Kevin is spot on. Since Strings are objects (not primitives) then the == has the same behavior as it does for all objects. You should always use .equals() or .compareTo() when comparing the value of an object.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Differing types of equals operands??

    For the record, neither of those are operands. The == is a boolean operator, and .equals(...) is a method that returns boolean.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. using .equals
    By droidus in forum New To Java
    Replies: 11
    Last Post: 01-21-2012, 04:33 AM
  2. c always equals a*b
    By imorio in forum New To Java
    Replies: 3
    Last Post: 11-12-2010, 03:32 PM
  3. == and equals()
    By arefeh in forum New To Java
    Replies: 13
    Last Post: 01-05-2010, 05:56 PM
  4. == is same as .equals()??
    By DrMath in forum New To Java
    Replies: 1
    Last Post: 09-30-2009, 05:57 AM
  5. Replies: 0
    Last Post: 01-26-2008, 12:13 AM

Posting Permissions

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