0

So, all variables in the conditions are static strings. type itself is a string in fact.

         switch(type) {
             case (INT || TINYINT):
                 preparedStatement = setInteger(preparedStatement, value, index);  
                 break;
             case (BIGINT || LONG):
                 preparedStatement = setLong(preparedStatement, value, index);  
                 break;
             case (DATETIME || TIMESTAMP):
                 preparedStatement = setTimestamp(preparedStatement, value, index);  
                 break;
             case (MEDIUMTEXT || ENUM || TEXT || LONGTEXT || VARCHAR):
                 preparedStatement = setString(preparedStatement, value, index);  
                 break;
         }
gran_profaci
  • 8,087
  • 15
  • 66
  • 99

6 Answers6

4

First, switch statements on strings are supported in Java 7+, but not in Java 6 and before.

Next, the || operator (the logical-OR operator) only works on boolean values, not String values. But you can get the same code to be run on multiple cases by listing the cases and not breaking until past the relevant code:

switch(type) {
    case INT:
    case TINYINT:
        // This code will run for INT and TINYINT only.
        preparedStatement = setInteger(preparedStatement, value, index);  
        break;
    case BIGINT:
    case LONG:
        // This code will run for BIGINT and LONG only.
        preparedStatement = setLong(preparedStatement, value, index);  
        break;
    // etc.
rgettman
  • 176,041
  • 30
  • 275
  • 357
  • "First, you can only switch on strings in Java 7" this can't possibly be true. switch on int and Enums aren't allowed anymore? – Steve Kuo Jul 16 '13 at 00:24
  • 3
    @SteveKuo No, switches on strings are allowed only in Java 7+. I didn't mean to say that other datatypes (int, enum) are no longer allowed in Java 7. – rgettman Jul 16 '13 at 00:27
2

Java 7 example:

public String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {
     String typeOfDay;
     switch (dayOfWeekArg) {
         case "Monday":
             typeOfDay = "Start of work week";
             break;
         case "Tuesday":
         case "Wednesday":
         case "Thursday":
             typeOfDay = "Midweek";
             break;
         case "Friday":
             typeOfDay = "End of work week";
             break;
         case "Saturday":
         case "Sunday":
             typeOfDay = "Weekend";
             break;
         default:
             throw new IllegalArgumentException("Invalid day of the week: " + dayOfWeekArg);
     }
     return typeOfDay;
}

Further I have never seen an OR statement inside of a switch like that. I would highly recommend not doing that.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
1

Assuming you are using Java SE 7 (or later) and the constants are static final Strings, then the syntax is not Java.

         case INT: case TINYINT:
Tom Hawtin - tackline
  • 145,806
  • 30
  • 211
  • 305
1

What does this expression evaluate to?

INT || TINYINT

What are the datatypes for INT and TINYINT

I've only ever seen switch used with some primitives (and new in Java 7, string) literals or variables declared as final.

If this isn't throwing a compile error, then the || operator must be defined for whatever datatype those are. But unless that's somehow being resolved at compile time, that operator is not going to be allowed. (Again, this might be something new in Java 7 I'm not aware of.)


If you are trying to do "or" logic, the normative pattern (in pre-7 versions of Java at least), is:

    switch(type) {
        case INT:
        case TINYINT:
             preparedStatement = setInteger(preparedStatement, value, index);  
             break;
        case BIGINT:
        case LONG:
             preparedStatement = 
             break;
spencer7593
  • 106,611
  • 15
  • 112
  • 140
0

It is supported on and after java 7

jmj
  • 237,923
  • 42
  • 401
  • 438
0

You cannot use logical operators in switch statements, even with Strings. You can only test one case at a time.

Kon
  • 10,702
  • 6
  • 41
  • 58