-1

I'm using this code and it retrieve incoming message successfully:

Bundle bundle = intent.getExtras();
Object[] object = (Object[]) bundle.get("pdus");
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) object[0]);
String message = smsMessage.getMessageBody().trim();

The message is exactly equal to "test" and message.length() is equal to 4 (no hidden chars) but the bellow command returns false!

if (message == "test") ...

How to use == instead equals()?

Thanks...

Mohammad Lotfi
  • 369
  • 5
  • 16

2 Answers2

2

You should use below:-

message.equals("test")

for more info read below question:-

How do I compare strings in Java?

Community
  • 1
  • 1
duggu
  • 37,851
  • 12
  • 116
  • 113
0

Use

message.equals("test")

“==” operator is actually checking to see if the string objects (obj1 and obj2) refer to the exact same memory location.

But, the equals() method is actually meant to compare the contents (values) of 2 objects, and not their location in memory.

see What's the difference between ".equals" and "=="? for more details

Community
  • 1
  • 1