user1908746
user1908746

Reputation:

Is it safe to use Switch statements with strings

I've got JDK7 and switches with strings work on my development machine.

My questions is, would the app breakdown on some users' systems? In other words, is it safe to use them at will?

I am aware I could be confusing the JDK with the VM but please forgive my ignorance.

Upvotes: 1

Views: 266

Answers (1)

Philip Whitehouse
Philip Whitehouse

Reputation: 4157

Yes it is safe to use them.

switch on String was introduced into Java by changing the compiler to implement the necessary conversion into byte-code using no new instructions. That is, the change is a compiler one only.

Providing the implementation is similar in Android, there is no technical restriction preventing an APK compiled under SDK 19, which supports switch on String being used on a device running prior to version 19.

Indeed, I'm fairly sure I used this property myself. All you need to do is make sure that the targetSDKversion and maxSDKversion are >= 19. The minSDKversion can be lower, because of the property I discussed above.

The Java implementation of this was under Project Coin and is covered in detail at the Oracle website. https://blogs.oracle.com/darcy/entry/project_coin_string_switch_anatomy

See also this answer: Android coding with switch (String)

See also the developer docs for Gradle plugin

Upvotes: 2

Related Questions