Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch statement with string as argument in Android

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

Is there any way to use switch with Strings in android development?

like image 555
JustCurious Avatar asked Feb 14 '12 19:02

JustCurious


2 Answers

EDIT

Personally, I have not tried, but according to the Android System Requirements page, it is unsupported. That said, it does not mean that it wouldn't work, just that you are unlikely to receive much/any help on the subject from Google/Android.

JDK 5 or JDK 6 (JRE alone is not sufficient)

On a potentially positive note, (found with the help of @Emil H and his comment) I have found these bit of information online:

From Dalvik on Wikipedia:

Programs are commonly written in a dialect of Java and compiled to bytecode. Then they are converted from Java Virtual Machine-compatible .class files to Dalvik-compatible .dex (Dalvik Executable) files...

If the bytecode doesn't change from version 6 to 7 (excluding the added invokedynamic mentioned by @David Schwartz, which switch statements should not be using.), then it should work...

like image 135
nicholas.hauschild Avatar answered Oct 17 '22 23:10

nicholas.hauschild


You should be able to use JDK7 with android. Check this answer to a related question:

https://stackoverflow.com/a/7481063/355499

EDIT:

Tried it out locally and everything works fine with JDK7 (it's what I use when developing). However, I can't find a way to change the compliance level from 1.6 to 1.7 in my android project. If you really want to do this I guess you would have to resort to building your project by some other means than using Eclipse. For instance using an ant script or similar. Might be a bit more work than it is worth though.

like image 36
Emil H Avatar answered Oct 17 '22 21:10

Emil H