Pergunta

I have re-installed my Java 8 and get a new and fresh Eclipse version.

If I write the following code:

String a = "001100";

switch(a) {
   case "5": a = "25"; break;
   default: System.exit(0);
}

then Eclipse underlines the "a" in the switch-brackets red and says "Change project compliance and JRE to 1.7". I have not tried this because I'm using Java 1.8 and this should work? I don't want to use Java 1.7.

My Eclipse is setting up with Java 1.8, also this Project. There is no lower Java version in the settings....

Where is the problem?

enter image description here

Foi útil?

Solução

Your Eclipse is using a Java SDK version lower than 1.7. Check your Java compiler level in Eclipse:

Windows>Preferences>Java>Compiler>compiler compliance level

Outras dicas

The problem most probably is that your language level is set below java 1.7. It is not the same as JRE version used by project. try to find language level in your project settings and set it to Java or Java 7 for this switch on String to be working

according to this answer switching for strings is implemented since JDK7 so switching to 1.7 compliance should do it. As I understand this is not 'switching back' your java version instead it enables you to use those 1.7 features.

You must set the default JDK version for the eclipse, to take all the projects to be created in default JDK version eclipse.

to know how to set the default JDK version in eclipse click here

Strings in switch statements are available only post JDK 1.7

https://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

You need to check your java compiler level and change it to JDK 1.8 or 1.7.

(Right Click on project, go to preferences then look for "Java Compiler" and change "Compiler Compliance Level")

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top