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?

3
  • 1
    @Deepak It is in Java7, thus the reason for this question. Commented Feb 14, 2012 at 19:41
  • The answer provided in this link (stackoverflow.com/questions/338206/…) 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. Commented Feb 14, 2012 at 19:45
  • Possible duplicate of stackoverflow.com/questions/14367629/… Commented Aug 26, 2015 at 11:44

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

4 Comments

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...
@EmilH Java 7 adds at least invokedynamic to the Java byte code per JSR292.
@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?
Excellent points. I have updated my answer to reflect this information.
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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.