• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt in Switch Statement  RSS feed

 
Rajiv Pai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why does this code compile (Though we get no result)



and the below code doesn't compile and gives error??


 
Carey Brown
Bartender
Posts: 3011
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Switch takes an int as its argument (more recent versions of Java will also take Strings).

Any argument that can automatically be passed as an int will work.
 
Ron McLeod
Saloon Keeper
Posts: 1601
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rajiv Pai wrote:... and the below code doesn't compile and gives error??

The Java Language Specification says that for a switch statement:  "The type of the Expression must be char, byte, short, int, Character, Byte, Short, Integer, String, or an enum type, or a compile-time error occurs."
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Switch takes an int as its argument (more recent versions of Java will also take Strings).


I believe the implementation of Strings is done via the String's hashcode. And I believe the formula is actually assumed by the compiler. This means that the compiler can take a string (that is a compile time constant), and convert it to an int (that is a compile time constant) in order to use it as a case target.

Henry
 
Paul Clapham
Sheriff
Posts: 22520
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:I believe the implementation of Strings is done via the String's hashcode. And I believe the formula is actually assumed by the compiler. This means that the compiler can take a string (that is a compile time constant), and convert it to an int (that is a compile time constant) in order to use it as a case target.


I'd be interested to see how that works, because if you pick any integer, there are zillions of different Strings which have that integer as their hashcode.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:I'd be interested to see how that works, because if you pick any integer, there are zillions of different Strings which have that integer as their hashcode.


Because I was curious, too. I did some Googling. On the surface:
The switch statement compares the String object in its expression with the expressions associated with each case label as if it were using the String.equals method; consequently, the comparison of String objects in switch statements is case sensitive. The Java compiler generates generally more efficient bytecode from switch statements that use String objects than from chained if-then-else statements.
For reference: http://docs.oracle.com/javase/7/docs/technotes/guides/language/strings-switch.html

At the nuts & bolts (JVM) level:
The general approach used is to translate a single string switch statement into a series of two chained switch statements: the first a synthesized statement switching on the argument string's hash value and computing a string's position in the list of original case labels, if any, followed by a second switch on the computed integer value.  The second switch has the same code structure as the original string switch statement except that the string case labels are replaced with positional integer constants starting at 0. The first switch statement can be thought of as an inlined map from strings to their position in the case label list.  An alternate implementation would use an actual Map for this purpose, as done for enum switches. ... The generated code assumes that the hashing algorithm of String is the same in the compilation environment as in the environment the code will run in.  The string hashing algorithm in the SE JDK has been unchanged since at least JDK 1.2.  Since the algorithm has been specified since that release as well, it is very unlikely to be changed in the future.  Different hashing algorithms, such as the length of the strings or a perfect hashing algorithm over the particular set of case labels, could potentially be used instead of String.hashCode.
For reference: http://hg.openjdk.java.net/jdk7u/jdk7u/langtools/file/41b81b3e37cd/src/share/classes/com/sun/tools/javac/comp/Lower.java#l3397
 
Rajiv Pai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you all.. its been helpful .... 
 
Campbell Ritchie
Marshal
Posts: 55772
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suggest you write a switch taking Strings and print the bytecode with
javap -c MyClass
I did try that once, but have forgotten what happened.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!