LHA
LHA

Reputation: 9655

Concatenate String using '+' in Android

I am implementing toString method (Android) that I need to concatenate all fields in an object

For Java (JDK) I can do as below:

@Override
public String toString() {
    return "{_id=" + _id + 
            ", name=" + name +  
            "}";        
}

Behind the scene, Java Compiler will use StringBuilder to build String concatenation for me so I don't have to use StringBuilder explicitly.

My question is: Can I use this implementation way in ANDROID code. OR I have to use StringBuilder explicitly? I don't know how Android Compilier work for this case.

Anyone has any ideas? Thank you!

Upvotes: 0

Views: 91

Answers (1)

Tim B
Tim B

Reputation: 41208

Yes, you can do this in Android. Whether it will be optimized in the same way is down to the specific compiler and JVM you are using but its very rare to find one that will not optimize that any more.

Upvotes: 3

Related Questions