Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Doubt in Switch Statement

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??


 
Saloon Keeper
Posts: 8738
71
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 3713
523
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
author
Posts: 23909
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 26910
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 322
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.. its been helpful ....  
 
Marshal
Posts: 74354
334
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic