0

I read in an article on Switch vs If that if we use String as switch parameter,the compiler will not make a jump table. Is this correct and if yes, pleas explain why?

Thanks

PS

I'm more interested in knowing this regarding java. And the part I read about this is from an answer here in Stackoverflow. So here's the link stackoverflow.com/a/395965/1043937

It says about c#, but since java also has the same feature since 1.7, Can someone please explain.

Community
  • 1
  • 1
Pradeep
  • 607
  • 7
  • 17

3 Answers3

3

Switch statements in C cannot accept strings as parameters.

Switch statements on strings in java are available since java 1.7. They weren't before as this block was modeled from the c feature. The fact that java uses, or not, a jump table is implementation dependent and the result also depends on the values.

Edit : for details about the implementation, refer to the link provided by Jon.

Community
  • 1
  • 1
Denys Séguret
  • 355,860
  • 83
  • 755
  • 726
0

Prior to java 1.7 you can't use string as switch parameter. This is a feature in java 1.7 that you can also use String as switch parameter .

Daniel Kamil Kozar
  • 17,278
  • 5
  • 48
  • 61
Pramod Kumar
  • 7,696
  • 5
  • 27
  • 37
0

Switch statements work either with primitive types or enumerated types. Java 7 introduced another type that we can use in Switch statements: the String type.

public void process(Trade t) {

        String status = t.getStatus();



        switch (status) {

        case NEW:

              newTrade(t);

              break;

        case EXECUTE:

              executeTrade(t);

              break;

        case PENDING:

              pendingTrade(t);

              break;



        default:

              break;

        }

  }
bNd
  • 7,342
  • 6
  • 37
  • 70