aspose file tools*
The moose likes Features new in Java 7 and the fly likes switch statement with Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "switch statement with Object" Watch "switch statement with Object" New topic
Author

switch statement with Object

Nitesh Sawant
Greenhorn

Joined: Jun 07, 2010
Posts: 3
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" ?
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

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?


PM Me If You Want to get Urgent Help on Java Programming
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
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.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3015
    
  10
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.
Praveen Kumar M K
Ranch Hand

Joined: Jul 03, 2011
Posts: 256
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
Sheriff

Joined: Oct 13, 2005
Posts: 38896
    
  23
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.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7820
    
  21

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

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18847
    
  40

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
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3610
    
  60

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

Joined: Mar 17, 2011
Posts: 7820
    
  21

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 agree. Here's the link: http://aspose.com/file-tools
 
subject: switch statement with Object