Znav |
---|
next | Java Mapping for Structures |
---|
prev | Java Mapping for Built-In Types |
---|
|
A Slice enumeration maps to the corresponding enumeration in Java. For example:
Wiki Markup |
---|
{zcode:slice}
enum Fruit { Apple, Pear, Orange };
{zcode} |
The Java mapping for Fruit
is shown below:
Wiki Markup |
---|
{zcode:java}
public enum Fruit implements java.io.Serializable {
Apple,
Pear,
Orange;
public int value();
public static Fruit valueOf(int v);
// ...
}
{zcode} |
Given the above definitions, we can use enumerated values as follows:
Wiki Markup |
---|
{zcode:java}
Fruit f1 = Fruit.Apple;
Fruit f2 = Fruit.Orange;
if (f1 == Fruit.Apple) // Compare with constant
// ...
if (f1 == f2) // Compare two enums
// ...
switch (f2) { // Switch on enum
case Fruit.Apple:
// ...
break;
case Fruit.Pear
// ...
break;
case Fruit.Orange
// ...
break;
}
{zcode} |
The Java mapping includes two methods of interest. The value
method returns the Slice value of an enumerator, which is not necessarily the same as its ordinal value. The valueOf
method translates a Slice value into its corresponding enumerator, or returns null
if no match is found. Note that the generated class contains a number of other members, which we have not shown. These members are internal to the Ice run time and you must not use them in your application code (because they may change from release to release).
In the Fruit
definition above, the Slice value of each enumerator matches its ordinal value. This will not be true if we modify the definition to include a custom enumerator value:
Wiki Markup |
---|
{zcode:slice}
enum Fruit { Apple, Pear = 3, Orange };
{zcode} |
The table below shows the new relationship between ordinal value and Slice value:
Enumerator | Ordinal | Slice |
---|
Apple | 0 | 0 |
Pear | 1 | 3 |
Orange | 2 | 4 |
Tip |
---|
Java enumerated types inherit implicitly from java.lang.Enum , which defines methods such as ordinal and compareTo that operate on the ordinal value of an enumerator, not its Slice value. |
See Also
Znav |
---|
next | Java Mapping for Structures |
---|
prev | Java Mapping for Built-In Types |
---|
|