Switch statement with string as an argument in Android

I would like to use a switch statement, as in Java 1.7, which also allows switch(someString) . However, if I changed the java compiler to 1.7, the project crashed, and I either have to go back to 1.5, or use android tools-> fix project .

Can I use a string switch in Android development?

+6
source share
3 answers

EDIT

Personally, I have not tried it, but according to the Android system requirements, the page is not supported. However, this does not mean that it will not work, you just are unlikely to get much / any help on this from Google / Android.

JDK 5 or JDK 6 (only JRE is not enough)

In a potentially positive note (found using @Emil H and his comment) I found this information on the Internet:

From Dalvik to Wikipedia :

Programs are usually written in the Java dialect and compiled into bytecode. They are then converted from Java Virtual Machine compatible .class files to Dalvik.dex (Dalvik Executable) compatible files ...

If the bytecode does not change from version 6 to 7 (excluding the added invokedynamic mentioned by @David Schwartz, which switch should not be used.), Then it should work ...

+5
source

You can use JDK7 with Android. Check this answer to the corresponding question:

fooobar.com/questions/582556 / ...

EDIT:

I tried it locally and everything works fine with JDK7 (this is what I use in development). However, I cannot find a way to change the level of compliance from 1.6 to 1.7 in my Android project. If you really want to do this, I think you will have to resort to creating your project in ways other than using Eclipse. For example, using ant script or similar. Perhaps it will be a little more than worth it.

+1
source

While this is not possible: although a switch statement with String was introduced in the Java 1.7 compiler, unfortunately, Android requires version 1.6 or 1.5. Therefore, we must wait.

0
source

Source: https://habr.com/ru/post/908460/


All Articles