0

I try to program a calculator in order to learn the very begining in android dev, but I'm facing to an issue that an human can't understand. I hope you're not human.

Take a look at my If condition :

    if (result != "")
    {
        textView.append("0");

    }

(You may notice that I used « result != "" » instead of isEmpty() method because isEmpty() isn't supported in API7).

Well. Now take a look at my two "result" variable

result  "" (id=830012674816)    
count   0   
hashCode    0   
offset  0   
value    (id=830012674848)  

result  "" (id=830022154000)    
count   0   
hashCode    0   
offset  0   
value    (id=830022154032)  

(I copied that two results from Eclipse Debugger)

The first result is OK : that's the one I get when I start the program : the if does its job and pass over. The second one seems to be exactly the same, but for a unknown reason, it gets inside the if and appends the zero. I get this issue after pushing the "plus" button.

Any idea ?

If you find there is a lack of information or you don't understand the issue, you can find here the whole workspace (in progress) : http://www.sendspace.com/file/udp5d3 . To reproduce the issue, push "zero" button when programs launchs and note that it normally does not appear. Then enter any number such as "104", "7" or "73", push "Plus" button, then "zero". Zero should not appear here.

Thank you :)

user1527491
  • 895
  • 10
  • 22

7 Answers7

5

Don't compare Strings (or any Objects) by !=. Use equals() like !("".equals(result)) or !(result.equals(""))

== is used to check if references contains same Objects, not if Objects contains same values,

for example

Integer i1=new Integer(1);
Integer i2=new Integer(1);
Integer i3=i1;
//checking references
System.out.println(i1==i2);//false
System.out.println(i1==i3);//true

//checking values
System.out.println(i1.equals(i2));//true
System.out.println(i1.equals(i3));//true
Pshemo
  • 122,468
  • 25
  • 185
  • 269
1

Change:

if (result != "")

To:

if (!result.equals(""))

or more preferably, use TextUtils which checks for null too:

if(!TextUtils.isEmpty(result))
waqaslam
  • 67,549
  • 16
  • 165
  • 178
0

Try using contentEquals instead. E.g.

if (!result.contentEquals(""))

http://developer.android.com/reference/java/lang/String.html#contentEquals(java.lang.CharSequence)

The reason is that in Java == returns true if two object references point to the same instance, whereas you want to compare two strings to check if they contain the same characters in the same order, which is not the same thing.

Ian Newson
  • 7,679
  • 2
  • 47
  • 80
  • Okay, thank you for answer. It didn't worked, I still get the same bug, but it looks proper now :) . – user1527491 Jul 16 '12 at 12:13
  • @user1527491 I've tested this change with your app and it works fine. Pushing '5' then '0' results in '50' being displayed, but pushing '0' results in no change to the textview. – Ian Newson Jul 16 '12 at 12:17
  • @Ian: why `contentEquals()` and not simply `equals()`? – Joachim Sauer Jul 16 '12 at 12:21
  • Yes, sorry, I did not replace the right portion of code X) . It works perfect now. I used to know that it didn't worked the same way in java, but I had had to do this mistake again :p . Thank you very much to the bunch of people that answered me :) . – user1527491 Jul 16 '12 at 12:26
  • @JoachimSauer The main reason I prefer `contentEquals` is for clarity as it's clearer what your intention is, but also type safety. If you accidentally pass an object to `equals` which is not a String/CharSequence then you wouldn't know about it until your code doesn't work as intended. – Ian Newson Jul 16 '12 at 12:36
0

The expresion in your if clause checks if the reference, not the value, in the variable result is not equals the object "" that's why you get always true. Instead of result != "" use !result.equals("").

AggelosK
  • 4,313
  • 2
  • 32
  • 37
0

you need to call this method !result.equals("") ,

result == " " will compare object instance instead of what String object contents .

AAnkit
  • 27,299
  • 12
  • 60
  • 71
0

1. Objects are compared using .equals() in Java, and String is an Object in Java.

2. So String must follow the same rule.

Example:

 if (!(result.equals("")))
    {
        textView.append("0");

    }
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
0

As other answers said, one should use String.equals() to compare actual contents of two Strings (see description of equals in javadocs), rather than testing for equality of object references by ==.

But, as you noted in your question, using == on the two strings evaluated to true when your application ran the comparsion for the first time. This happened because of String interning. Most likely, during initialization of your variables, you've assigned the result to a constant of the same value you've compared it with later - the "". Then, since the JVM uses interning to keep only single instance of String constant of a given value, the reference contained by result variable was actually the same as the reference to constant in your comparsion and the equality test evaluated to true.

So, there are (rather special) cases, when it is apropriate to use == to compare Strings. See this question and it's best answer for a list and a further discussion.

Community
  • 1
  • 1
david a.
  • 5,283
  • 22
  • 24