Related Topics

JAVA Programming
enum Season {
WINTER, SPRING, SUMMER, FALL
}
public class Main {
public static void main(String[] args) {
Season season1 = Season.SUMMER;
Season season2 = Season.SUMMER;
if (season1 == season2) {
System.out.println("The seasons are the same");
} else {
System.out.println("The seasons are different");
}
}
}
In this example, we define an enum called Season
with four constants representing the seasons of the year. We create two variables called season1
and season2
, both set to Season.SUMMER
. We then use an if
statement to compare the two variables using the ==
operator.
When we run this program, the output will be:
The seasons are the same
As you can see, the ==
operator is used to compare the references of the two Season
objects. If the two variables refer to the same object (in this case, they do), then the comparison evaluates to true. If the two variables refer to different objects with the same value, the comparison would evaluate to false.
import java.util.EnumSet;
enum Color {
RED, GREEN, BLUE, YELLOW
}
public class Main {
public static void main(String[] args) {
EnumSet<Color> primaryColors = EnumSet.of(Color.RED, Color.BLUE, Color.GREEN);
for (Color color : primaryColors) {
System.out.println(color);
}
}
}
In this example, we define an enum called Color
with four constants representing primary colors. We then create an EnumSet
called primaryColors
, which is initialized with the of
method to include Color.RED
, Color.BLUE
, and Color.GREEN
. We then use a for
loop to iterate over the elements of the primaryColors
set and print them out.
Here is an example of how to use EnumMap
:
import java.util.EnumMap;
enum Color {
RED, GREEN, BLUE, YELLOW
}
public class Main {
public static void main(String[] args) {
EnumMap<Color, Integer> colorCounts = new EnumMap<>(Color.class);
colorCounts.put(Color.RED, 1);
colorCounts.put(Color.GREEN, 2);
colorCounts.put(Color.BLUE, 3);
System.out.println("Number of red objects: " + colorCounts.get(Color.RED));
}
}
In this example, we define an enum called Color
with four constants representing primary colors. We then create an EnumMap
called colorCounts
, which is initialized with the Color
class. We add some key-value pairs to the map and then print out the value associated with the Color.RED
key.
The EnumSet
and EnumMap
classes are useful because they provide efficient implementations that are specifically designed to work with enum types. By using these classes, you can write more concise and efficient code when working with enum types.
EnumType.valueOf(String name)
where EnumType
is the name of the enumeration class and name
is the name of the enumeration constant to be returned.
If the specified name does not match any of the enumeration constants, the valueOf()
method will throw an IllegalArgumentException
.
Here’s an example of using the valueOf()
method with an enumeration:
enum Day {
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}
// Using valueOf() method
Day day = Day.valueOf("MONDAY");
System.out.println(day);
In this example, we declare an enumeration called Day
that represents the days of the week. We then use the valueOf()
method to convert the String “MONDAY” to an enumeration constant of type Day
. The valueOf()
method returns the Day
constant MONDAY
, which is then printed to the console.
interface Vehicle {
void start();
void stop();
}
enum Car implements Vehicle {
SEDAN("Sedan"), HATCHBACK("Hatchback"), SUV("SUV");
private String type;
Car(String type) {
this.type = type;
}
public String getType() {
return type;
}
@Override
public void start() {
System.out.println("Starting the " + getType() + " car");
}
@Override
public void stop() {
System.out.println("Stopping the " + getType() + " car");
}
}
public class Main {
public static void main(String[] args) {
Car car1 = Car.SEDAN;
Car car2 = Car.SUV;
car1.start();
car1.stop();
car2.start();
car2.stop();
}
}
In this example, we define an interface called Vehicle
with two methods: start()
and stop()
. We then define an enumeration called Car
that implements the Vehicle
interface. The Car
enumeration has three constants: SEDAN
, HATCHBACK
, and SUV
.
The Car
enumeration has a private field called type
, which is set through the constructor for each of the enumeration constants. The Car
enumeration also provides implementations for the start()
and stop()
methods of the Vehicle
interface.
In the main
method, we create two instances of the Car
enumeration (SEDAN
and SUV
) and call the start()
and stop()
methods on each instance. The output of the program will be:
Starting the Sedan car
Stopping the Sedan car
Starting the SUV car
Stopping the SUV car
As you can see, the Car
enumeration is able to provide its own implementation for the start()
and stop()
methods defined in the Vehicle
interface.
import java.io.*;
enum MyEnum {
VALUE1, VALUE2, VALUE3;
}
public class EnumSerializationExample {
public static void main(String[] args) throws IOException, ClassNotFoundException {
MyEnum value = MyEnum.VALUE2;
// Serialize the enum value to a byte array
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(out);
oos.writeObject(value);
byte[] bytes = out.toByteArray();
// Deserialize the byte array back into an enum value
ByteArrayInputStream in = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(in);
MyEnum newValue = (MyEnum) ois.readObject();
// Print the original and deserialized values
System.out.println("Original value: " + value);
System.out.println("Deserialized value: " + newValue);
System.out.println("Are they equal? " + (value == newValue));
}
}
In this example, the MyEnum
enumeration is defined with three constant values. The main
method creates an instance of the MyEnum.VALUE2
constant and then serializes it to a byte array using an ObjectOutputStream
. The byte array is then deserialized back into an enum value using an ObjectInputStream
, and the original and deserialized values are compared. The output of this program is:
Original value: VALUE2
Deserialized value: VALUE2
Are they equal? true
This shows that the deserialized value is the same as the original value, and that they are equal to each other.
enum Color {
RED, GREEN, BLUE
}
public class EnumExample {
public static void main(String[] args) {
Color c = Color.RED;
System.out.println("The name of the color is " + c.name());
System.out.println("The ordinal of the color is " + c.ordinal());
System.out.println("The color values are:");
for (Color color : Color.values()) {
System.out.println(color);
}
}
}
In this example, we declare an enumeration Color
with three values: RED
, GREEN
, and BLUE
. We then create an instance of the Color
enumeration with the RED
value and use the name()
method to get the name of the color. We also use the ordinal()
method to get the position of the color in the enumeration. Finally, we use the values()
method to get an array of all the values in the Color
enumeration and print them out.
The output of this program would be:
The name of the color is RED
The ordinal of the color is 0
The color values are:
RED
GREEN
BLUE
As you can see, the Enum
class provides a number of useful methods for working with enumerations in Java.
int i = 10;
Integer j = i; // autoboxing
In this example, the value of the primitive int
variable i
is automatically boxed into an Integer
wrapper class object, which is then assigned to the Integer
variable j
. This simplifies the process of working with int
values, as the Integer
class provides additional methods that can be used to manipulate the value.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.