• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Switch Statement

 
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How the switch statments is executed internally, the case takes only constants or constant expressions why??
If switch is if else only, then why only constants with the case?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


How the switch statments is executed internally


I presume that differs with the JVM.

The facecious answer to your question is: because that is how the specification defines it. It is designed to work only with integrals for performance reasons I believe.
[ May 20, 2004: Message edited by: Paul Sturrock ]
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The logic for execution is determined at compile time.
If variables were allowed this would not be possible.
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx a lot for ur responses

Can any one plz clarify how this is handled by JVM , with any specific JVM Example
 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi.
back in the old days, using assembly, the implementation of a switch statement used multiple jumps (or breaks in asm/370) that branched on the return codes. This is why return codes where multiples of 4 - the length of a branch statements in bytes.
the jvms will actually convert switch statements to a few if statements. this is why the type of the checked value must be known beforehand. I am not so sure why it is specifically an int
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Andy Smith:
Thanx a lot for ur responses

Can any one plz clarify how this is handled by JVM



The JVM instruction set has two special opcodes, "tableswitch" and "lookupswitch" which implement switch statements. I believe (although I might have this backwards) that "tableswitch" handles non-contiguous cases; "lookupswitch" handles contiguous cases. Head over to java.sun.com and look up the Java VM reference manual to get the details.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
c. nimo:
I read about the implementation of a switch-statement in c++ by good old Bjarne Stroustrup, who told something of a lookup-table used for switch-statements, which leads to faster code than if/ else constructs.

Therefore I cannot imagine that java went backwards and uses some if/ else internally. And how does the jvm tranlate missing break-statements into a few if/ else statements:
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'd love some syntatic sugar to replace elseif tho. Rexx has:

and COBOL has an extraordinarily rich "evaluate" that I won't even try to explain. Imagine being jealous of COBOL in the 21st century.
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Switch statement has static binding rather then runtime as in the case of if-else...
What actually happes in static binding and how actually it helps to make the process faster.. (looking for deeper insight to this issue..)
 
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have no particular experience with typical swich statement compilers, but if you consider the mathematical aspect it becomes clear that they should have advantages over a linear collection of if statements.

If I have a set of N numbers and I can do preprocessing on them, I can sort them in advance so that checking if a number is in the set is a simple logrithmic time binary search. For a finite set of numbers that you know in advance you can hardcode that entire binary search as a set of if statements with no looping or recursive calls. That's considerably more efficient than a simple list of if statements testing each possible outcome seperately which might require you to check all of the cases in the worst case.

This is handy when we know the numbers in advance. If we don't know the numbers in advance we have to sort them. The cost of sorting them outweighs the benifits of the fast search. We'd be better off doing a linear test of all possibilties.
 
Andy Smith
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nice responses especially I liked the one from "David Weitzman " a lot.
Could anyone go ahead and find out some more interesting stuffs for switch execution .. lke one explained by David in a nice manner...
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This brief explanation suggests three techniques (jump tables, linear search, and binary search) and supplies some code that suggests how an assembler version would look.
 
reply
    Bookmark Topic Watch Topic
  • New Topic