• 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
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

switch statement with Object

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java 7 has new feature : "switch statement with java.lang.String".
What may be the reason for not adding feature as "switch statement with java.lang.Object" ?
 
Ranch Hand
Posts: 530
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nitesh Sawant wrote:Java 7 has new feature : "switch statement with java.lang.String".
What may be the reason for not adding feature as "switch statement with java.lang.Object" ?


To check for what from the object?
 
Marshal
Posts: 76404
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Object as a switch case is too non‑specific. What people wanted was an alternative toThe switch on Strings was specifically requested by users. There was probably no demand for switching on plain simple Objects. It is worth comparing the bytecode of this sort of code:Now look how much more complicated the bytecode is for Strings. It would be even worse for any kind of Object. But see how much easier the original code is to read with enum elements or Strings.
 
author
Posts: 23928
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

Also, let's not forget that Java requires that the targets for the case statements must be compile time constants. And Java already have it well defined for strings. To switch on anything else, a new concept of object literals will have to be created.

Henry
 
Master Rancher
Posts: 4268
57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Also, let's not forget that Java requires that the targets for the case statements must be compile time constants.


Or enums.

Henry Wong wrote:And Java already have it well defined for strings. To switch on anything else, a new concept of object literals will have to be created.


Or they could just change the rules, as they did when they added enums.

It wouldn't really be difficult to make rules for an Object switch statement that was equivalent to a series of if/else statements. If they wanted to. But there also wouldn't be much point, since such an addition wouldn't really be any better than a series of if/else statements.
 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my opinion(maybe a bit naive here), they probably did not go for Object switch because then they would have to get into instanceof or getClass confusions. It would raise more issues in case there was a situation of Superclass and Subclass cases in the same switch statement(which would potentially violate the IS-A releation).
 
Campbell Ritchie
Marshal
Posts: 76404
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you still find the JSR about that? It may be that people specifically requested switch on Strings (I am pretty sure that is in fact the case) and did not request switch on anything else.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:It wouldn't really be difficult to make rules for an Object switch statement that was equivalent to a series of if/else statements.
If they wanted to.


Hmmm. Not so sure about that; and it would mean re-writing practically all the docs about switch, since right now it uses compile-time constants.
A switchref command perhaps?

But there also wouldn't be much point, since such an addition wouldn't really be any better than a series of if/else statements.


Again, probably a personal preference, but I really like the "look" of switch statements. To me they're visually very clean.

Winston
 
Henry Wong
author
Posts: 23928
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

Mike Simmons wrote:

Henry Wong wrote:Also, let's not forget that Java requires that the targets for the case statements must be compile time constants.


Or enums.

Henry Wong wrote:And Java already have it well defined for strings. To switch on anything else, a new concept of object literals will have to be created.


Or they could just change the rules, as they did when they added enums.



Although, enums are not compile time constants -- as defined by the specification. The possible values of the enum are constants, and can be determined at compile time -- so I guess it is possible to use this same technique to switch on the wrapper objects as well.

Winston Gutkowski wrote:

But there also wouldn't be much point, since such an addition wouldn't really be any better than a series of if/else statements.


Again, probably a personal preference, but I really like the "look" of switch statements. To me they're visually very clean.



Not sure if I agree with this -- when I see a switch statement, I expect a constant dispatch (or almost constant dispatch) .... meaning I don't expect the 1000th case statement to have to go through 1000 condition checks before it can be located.

Henry
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:... I don't expect the 1000th case statement ...


Well, I'm pretty liberal when it comes to adhering to OO design principles, but a switch with thousand cases would make me to start some serious refactoring code-rewriting effort.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:Although, enums are not compile time constants -- as defined by the specification. The possible values of the enum are constants, and can be determined at compile time -- so I guess it is possible to use this same technique to switch on the wrapper objects as well.


I have to admit, I haven't thought this through completely, but it seems to me that the definition for switch could be extended to include references, providing it's clearly understood that a case statement involving a reference is only satisfied by an identity comparison.

That way, it would allow enum values perforce, and also String literal matching via:
switch (s.intern()) { ...
Indeed, I reckon the compiler could probably substitute it automatically for String expressions.

It would also then work for classes, viz:although I'm not really sure if we want to be encouraging dispatch code.

Winston
 
I knew I would regret that burrito. But this tiny ad has never caused regrets:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic