0

The below java(android) source works on debug build, release(signed) build, and allatori obsfucation build(signed).

SomeActivity.java

//Sends event to an async event processing class
public void emitEvent(){

    EventHandlerClass.emit("myEvent");

}

....
//Recieves callback from EventHandlerClass
public void handleEvent(Event event){//Event object has a String field named eventField;

    if(event.eventField == "myEvent"){


    }else if (event.eventField == "myEvent2"){

             .......

    }
}

EventHandlerClass.java

public void emit(String eventName){

 ...//process async event

    eventSource.handleEvent(new Event(eventName));

}

As you can see, the callback gets a string and compares it with the "==" operator. Somehow, this works, but I really don't know why this should work. As far as I know, the "==" should be a reference comparison.

This means that the 'event.eventField' has the same reference as the hardcoded String "myEvent". Is there a special rule on java for storing hardcoded Strings?

March3April4
  • 2,131
  • 1
  • 18
  • 37
  • 4
    *Is there a special rule on java for storing hardcoded Strings?* Yes – shmosel Jun 26 '17 at 01:56
  • Literals always point to the same instance of String if they have the same content thus have the same reference. – Andrew Li Jun 26 '17 at 01:57
  • @Andrew Li Is this bound to all Strings stored inside memory, or is it only for hardcoded Strings? – March3April4 Jun 26 '17 at 01:59
  • @March3April4 What do you mean? *Objects are stored in memory*, there's no difference. – Andrew Li Jun 26 '17 at 02:00
  • 1
    Literal = hardcoded – shmosel Jun 26 '17 at 02:00
  • And the reason I found out about this is that after secondary obsfucation(I do not know what it is, since it was done by my client), the above code will not properly work. – March3April4 Jun 26 '17 at 02:00
  • @Andrew Li I understand what you are saying, but becoming very confused. Then, in what situation should the '==' operator fail ? – March3April4 Jun 26 '17 at 02:03
  • `"a" == new String("a")` – shmosel Jun 26 '17 at 02:04
  • @shmosel I'm very surprised that it works. I've never been through this after I failed comparing with the '==' operator when I was just a student. Can I understand that 'IF one of the operend is not a hardcoded string, the '==' operator will not work' ? – March3April4 Jun 26 '17 at 02:10
  • I added a JLS citation to the canonical Q&A about this in case it wasn't clear enough. `String` literals always refer to the same instance. https://stackoverflow.com/a/513839/2891664 So more or less, yes, you are right, `==` generally won't work if at least one operand is not a literal. – Radiodef Jun 26 '17 at 02:17
  • @Radiodef Is it a coincidence that this exact question was raised just yesterday and we discussed it? :P – Andrew Li Jun 26 '17 at 02:21
  • @AndrewLi Yeah, I guess so. To be honest I don't really know why there wasn't a JLS citation over there since this comes up from time to time. – Radiodef Jun 26 '17 at 02:22
  • 2
    It is a duplicate because Java is Java, @GoJavaAndCSharp. – Lew Bloch Jun 26 '17 at 02:48
  • 1
    @GoJavaAndCSharp You have yet to back up that assertion. – shmosel Jun 26 '17 at 03:38
  • 1
    @GoJavaAndCSharp There's no way Android does it differently because Android **uses Java** -- Java has the same syntax and rules as Java. – Andrew Li Jun 26 '17 at 03:43
  • @AndrewLi I'm sorry I was wrong. I am human, and humans sometimes are wrong. I have deleted my answer seeing it is useless. I got that from a source(which I can't find) a long time ago, so I guess that source was incorrect. I'm sorry for all of the trouble –  Jun 26 '17 at 03:47

0 Answers0