8

I'm trying to switch a string type value which I have made static. However I can't figure out how to put this into a switch statement I was previously using an 'if else' statement but due to the number of items I want to switch this doesn't work.

For the if else I was using `

if (item.ActivityFeedType.equals("Comment"))

for switch I;m trying to use

case (item.ActivityFeedType.equals("Comment")):

Is there something I'm missing?

1
  • 4
    If you use a Java version below 7, is impossible to switch a String Commented Feb 24, 2015 at 11:10

7 Answers 7

20
  1. switch for Strings exists, but it's only available starting from Java 7. The syntax actually is just as with an Integer switch:

    String test = "test";
    switch (test) { 
        case "testt": 
            System.out.println("Wrong"); 
            break; 
        case "test": 
            System.out.println("Got it"); 
            break; 
    }
    
  2. There is no situation that a switch can handle, but a simple if-else could not. A switch simply is more convenient.

  3. I'd recommend selecting lower-case starting letters for a Class' attributes.

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

2 Comments

I think its probably better for me to use an if else but on the last else I'm getting an error '; expected'
Ok, that sounds like a syntax error. It does not mean if-else does not work.
3

Thouhg if you are working with Java 1.7,
Java's switch-case allows only constant variable in case.

Comments

1

It is impossible to switch strings below jdk 7.

You should get an error message similiar to:

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

Comments

1

The java 6 compiler does not allow you to use switch with reference types (String, Object, etc.). You can only do this with the following value types - byte, short, char, and int. If you use a higher version of the JVM, you can do a switch over String values too.

Now to your question. The android build tools use JVM 6 so you cannot use the new switch abilities.

Comments

1

Note : Below java 7 it is not possible to use strings in switch.

if you are using java 7 or above you can use as below

String item="mango";

switch(item){
case "mango" :
      System.out.println("this is mango");
 break;
 ............
 ............
}

Comments

0

if you are using java version 7+

switch(item.ActivityFeedType){
case "Comment":
//do stuff
break;
}

Comments

0

Add Java 1.7 in build gradle

android {
    compileSdkVersion 23
    buildToolsVersion '26.0.2'

    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }
}

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.