6

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?

Thomas
  • 2,751
  • 5
  • 31
  • 52
JustCurious
  • 1,848
  • 3
  • 30
  • 57
  • 1
    @Deepak It is in Java7, thus the reason for this question. – nicholas.hauschild Feb 14 '12 at 19:41
  • The answer provided in this link (http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java) shows you how to somewhat work around the switch with string restriction before java 7. If you do not want this approach, you're stuck with if/elses. – bsimic Feb 14 '12 at 19:45
  • Possible duplicate of http://stackoverflow.com/questions/14367629/android-coding-with-switch-string/32225734#32225734 – Shubham Chaudhary Aug 26 '15 at 11:44

3 Answers3

5

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...

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120
  • Are you sure about this. I thought that the dex compiler worked with the byte code. And as far as I know there is java 7 does not add anything new at the byte code level... I could be wrong thou... – Emil L Feb 14 '12 at 19:44
  • 1
    @EmilH Java 7 adds at least `invokedynamic` to the Java byte code per [JSR292](http://jcp.org/en/jsr/detail?id=292). – David Schwartz Feb 14 '12 at 19:48
  • 1
    @David Scwartz Yeah, but Java doesn't use that right? It is mainly to increase support for dynamic languages on the jvm like Jython etc? – Emil L Feb 14 '12 at 19:50
  • Excellent points. I have updated my answer to reflect this information. – nicholas.hauschild Feb 14 '12 at 20:18
1

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.

Community
  • 1
  • 1
Emil L
  • 20,219
  • 3
  • 44
  • 65
0

For now it is no possible: although in Java 1.7 compiler it was introduced the switch statement with String, unfortunately Android requires version 1.6 or 1.5. So we have to wait.

lory105
  • 6,112
  • 4
  • 31
  • 40