Forums Register Login

Doubt in Rules Roundup switch int only (enum works)

+Pie Number of slices to send: Send
You can not only switch on an int or a compatible type (byte or short) but you can now switch on an enumerated type created with the enum keyword.



Kaydell

[ June 07, 2007: Message edited by: Kaydell Leavitt ]
[ June 07, 2007: Message edited by: Kaydell Leavitt ]
+Pie Number of slices to send: Send
I agree!




Thanks,
+Pie Number of slices to send: Send
My guess is that the switch uses the Enum.ordinal() method to get to an int value under the covers. That makes the Enum value an int value and thus valid for the switch-function.

But I might be wrong....

Cor
+Pie Number of slices to send: Send
 


My guess is that the switch uses the Enum.ordinal() method to get to an int value under the covers.



You may be able to convert an Enum to an int, but an Enum is really an instance of a subclass of Enum. An Enum is not compatible with an int and that's a good thing so that you don't accidentally pass an int constant where it isn't appropriate. With enum values, if you try to pass them as parameters where an int is required, you get a compile-time error.

Kaydell
+Pie Number of slices to send: Send
I believe that you can switch on the following types:

byte, short, int, char, and enum

Kaydell
+Pie Number of slices to send: Send


It does not compile!
+Pie Number of slices to send: Send
Hi,
even i have learnt this new thing that Enum works with switch
so far even i thought that only int compatible types work with switch.

Thanks
+Pie Number of slices to send: Send
 

Originally posted by Sergio Tridente:


It does not compile!



Try

Cor
[ June 12, 2007: Message edited by: Cor Takken ]
It's hard to fight evil. The little things, like a nice sandwich, really helps. Right tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 915 times.
Similar Threads
How Can I Try Linux?
How do I get the AUTOINCREMENT value?
hi
Java Round-Up game
In Eclipse, how can I hide empty packages?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 05:40:33.