Program 8 : Enum & Autoboxing
Develop a Java program that uses enumerations and demonstrates autoboxing with type wrappers.
The
Day
enum now has a constructor and a private field (dayValue
) to store an integer for each day.New methods have been added to the enum:
getDayValue()
: A getter to retrieve the day's number.isWeekend()
: An operation that returnstrue
if the day is a weekend.
The
main
method has been updated to demonstrate these new capabilities.
java
public class EnumAndAutoboxingDemo {
// The enum with constructor, field, and methods.
public enum Day {
// Each constant now calls the constructor to set its integer value.
SUNDAY(1),
MONDAY(2),
TUESDAY(3),
WEDNESDAY(4),
THURSDAY(5),
FRIDAY(6),
SATURDAY(7);
// A private final field for the day's value.
private final int dayValue;
// Enum constructors are implicitly private.
Day(int value) {
this.dayValue = value;
}
// A getter method to access the day's value.
public int getDayValue() {
return this.dayValue;
}
// An operation based on the enum constant.
public boolean isWeekend() {
return (this == SATURDAY || this == SUNDAY);
// 'this' refers to constant on which method is called.
}
}
public static void main(String[] args) {
System.out.println("--- Demonstrating Enumerations with Values and Methods ---");
Day today = Day.WEDNESDAY;
System.out.println("Today is: " + today);
System.out.println("Day number for " + today
+ " is: " + today.getDayValue());
System.out.println("Is " + today
+ " a weekend? " + today.isWeekend());
System.out.println("\n--- Iterating through all days ---");
for (Day day : Day.values()) {
System.out.printf("Day: %-10s | Value: %d | Is Weekend? %s\n",
day, day.getDayValue(), day.isWeekend());
}
System.out.println("\n--- Demonstrating Autoboxing ---");
new AutoBox();
}
static class AutoBox {
public AutoBox() {
Integer iOb = 99;
System.out.println("Original value of iOb: " + iOb);
++iOb; // unboxed, incremented, reboxed
System.out.println("After ++iOb: " + iOb);
iOb += 10; // unboxed, added, reboxed
System.out.println("After iOb += 10: " + iOb);
Integer iOb2 = iOb + (iOb / 3);
// unboxed, computed, reboxed
System.out.println("iOb2 after expression: " + iOb2);
int i = iOb + (iOb / 3);
// unboxed, result stored as primitive
System.out.println("i after expression: " + i);
}
}
}
Output
--- Demonstrating Enumerations with Values and Methods ---
Today is: WEDNESDAY
Day number for WEDNESDAY is: 4
Is WEDNESDAY a weekend? false
--- Iterating through all days ---
Day: SUNDAY | Value: 1 | Is Weekend? true
Day: MONDAY | Value: 2 | Is Weekend? false
Day: TUESDAY | Value: 3 | Is Weekend? false
Day: WEDNESDAY | Value: 4 | Is Weekend? false
Day: THURSDAY | Value: 5 | Is Weekend? false
Day: FRIDAY | Value: 6 | Is Weekend? false
Day: SATURDAY | Value: 7 | Is Weekend? true
--- Demonstrating Autoboxing ---
Original value of iOb: 99
After ++iOb: 100
After iOb += 10: 110
iOb2 after expression: 146
i after expression: 146
java
public class EnumAndAutoboxingDemo {
public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY
}
public static void main(String[] args) {
System.out.println("--- Demonstrating Enumerations ---");
// Assigning a value from the enum
Day today = Day.WEDNESDAY;
System.out.println("Today is: " + today);
System.out.println("\nIterating through all days of the week:");
for (Day day : Day.values()) {
System.out.println(day);
}
// --- Autoboxing and Auto-unboxing Demonstration ---
System.out.println("\n--- Demonstrating Autoboxing ---");
Integer integerObject = 100;
System.out.println("Value of Integer object: "
+ integerObject);
new AutoBox();
//AutoBox unbox = new AutoBox();
//unbox.boxUnbox();
}
static class AutoBox {
public AutoBox() {
Integer iOb = 99;
System.out.println("Original value of iOb: " + iOb);
++iOb; // unboxed, incremented, reboxed
System.out.println("After ++iOb: " + iOb);
iOb += 10; // unboxed, added, reboxed
System.out.println("After iOb += 10: " + iOb);
Integer iOb2 = iOb + (iOb / 3);
// unboxed, computed, reboxed
System.out.println("iOb2 after expression: " + iOb2);
int i = iOb + (iOb / 3);
// unboxed, result stored as primitive
System.out.println("i after expression: " + i);
}
}
}
Output
bash
--- Demonstrating Enumerations ---
Today is: WEDNESDAY
Iterating through all days of the week:
SUNDAY
MONDAY
TUESDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
--- Demonstrating Autoboxing ---
Value of Integer object: 100
Original value of iOb: 99
After ++iOb: 100
After iOb += 10: 110
iOb2 after expression: 146
i after expression: 146