Content deleted Content added
→Type system > Enumerations: new section |
|||
Line 424:
I reverted an edit claiming that the Java <code>char</code> type is an "unsigned 16 bit integer" type. According to the language spec it is an ''integral type'' which as soon as it is used with a numeric operator (such as bitwise shift, -and and -or ) it gets ''promoted'' to an integer. So the <code>char</code> type is not an integer type, neither formally nor practically. It is a separate integral type which is ''convertible'' to integer but is itself not an integer. Please read chapter 5 of the Java Language Specification if in doubt. <span style="font-size: smaller;" class="autosigned">— Preceding [[Wikipedia:Signatures|unsigned]] comment added by [[Special:Contributions/87.50.3.197|87.50.3.197]] ([[User talk:87.50.3.197|talk]]) 21:23, 12 June 2011 (UTC)</span><!-- Template:UnsignedIP --> <!--Autosigned by SineBot-->
== Type system > Enumerations ==
I am a C# programmer and have been since version 1.0 of the language, but my intimate knowledge of the language may be incomplete. With that said, my understanding of enumerations is that they are type-safe. Also, as far as I know, C# is completely type-safe. It impossible, or near, to do something in C# that is not type-safe.
Enumerations do not derive from integer types (8, 16, 32, or 64-bit), but instead derive from System.Enum which in turn derives from System.ValueType, System.IComparable, System.IFormattable, and System.IConvertible. System.ValueType implicitly derives from System.Object. So, enumerations are value types, but do not derive from any explicit value type. When specifying an integral type for an enumeration, you're merely telling the compiler and runtime what the enumeration's numerical limits are; not deriving from the integral type. The idea of deriving from an integral type is an erroneous one because integral types are value types. Value types are implicitly sealed which means you cannot derive from them.
Enumerations only allow you to assign the enumeration's defined values. Integral values can be assigned to an enumerated type through casting, but this can lead to having values outside the implied range (the defined values) of the enumeration. An exception to this rule is the value of zero, which can be assigned to an enumerated type without casting.
It is true that enumerations don't allow anything other than the enumeration values. But, the Enum class defines four ToString methods which will convert any enumeration to a string without the need to define an explicit ToString method on an enumeration. The Enum class also defines several other methods which makes working with any enumeration easy.
So in short, I propose that the section on Enumerations be revised. I don't know enough about Java to do it in a non-biased manner.
|