16

I am using Eclipse to develop a Java program. I had to downgrade JRE and JDK from 1.7x to 1.6. Now everything is pointing to 1.6.x (including the installed JRE and JDK compliance).

But now Eclipse still gives me an error on the switch statement, indicating:

Cannot switch on a value of type String for source level below 1.7. Only convertible int values or enum constants are permitted

on the code below:

Switch("test") // Which is fine with 1.7.x

I removed 1.7.x from computer, not sure why it is still looking for 1.7 instead of 1.6?

0

7 Answers 7

21

Switching on strings was introduced in Java 1.7!

The error message is expected when you downgrade to Java 1.6. In that version you can only switch on primitive types and enums.

Related question:

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

Comments

18

Right click on your project, go to Properties. Select Java Compiler from the left menu. Select your compliance level (1.7 or 1.6). 1.7 will stop that message. 1.6, as others said before, won't let you use strings.

Comments

3

Actually, your code is not valid on 1.6. You can't do a switch on a String.

Comments

2

switch(String) is syntax applicable from Java 7 onwards. Because you have 1.6 which doesn't support switch(String), eclipse giving compilation error.

Change switch(String) to switch(int)

Comments

2

In Java 4 and before you could only use switch on boolean, char, short, byte, int types.

In Java 5 and 6 you could use switch on Enums in addition to previous types.

And only Java 7 supports switch on Strings.

Comments

2

change both your jdk and compiler compliance level: enter image description here

Comments

1

I faced the same issue when I tried to deploy using the Ant tool.

The solution that worked for me was:

  • Right click on project, and then click Properties
  • Go to Java Build Path
  • Go to JRE System Library
  • Execution Environment was selected; I instead selected Alternate JRE and then jre7 (as shown below)

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.