What Is the Default Value for Enum Variable

What is the default value for enum variable?

It is whatever member of the enumeration represents the value 0. Specifically, from the documentation:

The default value of an enum E is the value produced by the expression (E)0.

As an example, take the following enum:

enum E
{
Foo, Bar, Baz, Quux
}

Without overriding the default values, printing default(E) returns Foo since it's the first-occurring element.

However, it is not always the case that 0 of an enum is represented by the first member. For example, if you do this:

enum F
{
// Give each element a custom value
Foo = 1, Bar = 2, Baz = 3, Quux = 0
}

Printing default(F) will give you Quux, not Foo.

If none of the elements in an enum G correspond to 0:

enum G
{
Foo = 1, Bar = 2, Baz = 3, Quux = 4
}

default(G) returns literally 0, although its type remains as G (as quoted by the docs above, a cast to the given enum type).

Choosing the default value of an Enum type without having to change values

The default for an enum (in fact, any value type) is 0 -- even if that is not a valid value for that enum. It cannot be changed.

How to get default value of an enum from a type variable

The default value of an enum is 0... Even if there is no value defined for 0. In the end you can always (EnumType)123 for any enum. enum don't check/restrict their "valid" values. Only give some labels to some specific values.

Note that the 0 I spoke before is a "typed" value... So it is (EnumType)0, not a (int)0... You can:

object oDefault = Enum.ToObject(p.PropertyType, 0);

Works even with non-int based enums, like:

enum MyEnum : long
{
}

Clearly you could even:

object oDefault = Activator.CreateInstance(p.PropertyType);

because new SomeEnumType() is 0.

Can Java enum class set default value

The default for one who holds a reference to an enum without setting a value would be null (either automatically in case of a class field, or set by the user explicitly).

Unfortunately you cannot override the method valueOf for your own enum, as it is static.

But you can still create your methods:

public enum PartsOfSpeech2 {

n("noun"),
wp("标点"),
a("adjective"),
d("conjunction");

private String value;

PartsOfSpeech2(String value) {
this.value = value;
}

// declare your defaults with constant values
private final static PartsOfSpeech2 defaultValue = n;
private final static String defaultString = "%";

// `of` as a substitute for `valueOf` handling the default value
public static PartsOfSpeech2 of(String value) {
if(value.equals(defaultString)) return defaultValue;
return PartsOfSpeech2.valueOf(value);
}

// `defaultOr` for handling default value for null
public static PartsOfSpeech2 defaultOr(PartsOfSpeech2 value) {
return value != null ? value : defaultValue;
}

@Override
public String toString() { return value; }

}

Default value of enum declared in class

The default value of the first name in an enum is 0, regardless of the scope of the enum.

There is no guaranteed default value of an automatic local variable like test x; in main here. It has an indeterminate value. And it's Undefined Behavior to use that value.

You can ¹default-initialize it like this:

test x{};


¹ A subtle point is that at top level this gives a “value-initialization”.

Enum variable default value?

The program has Undefined Behavior. The value of enummy is indeterminate. Conceptually there is no difference between your code and the following code:

int main() {
int i; //indeterminate value
std::cout << i; //undefined behavior
};

If you had defined your variable at namespace scope, it would be value initialized to 0.

enum SomeEnum {  
EValue1 = 1,
EValue2 = 4,
};
SomeEnum e; // e is 0
int i; // i is 0

int main()
{
cout << e << " " << i; //prints 0 0
}

Don't be surprised that e can have values different from any of SomeEnum's enumerator values. Each enumeration type has an underlying integral type(such as int, short, or long) and the set of possible values of an object of that enumeration type is the set of values that the underlying integral type has. Enum is just a way to conveniently name some of the values and create a new type, but you don't restrict the values of your enumeration by the set of the enumerators' values.

Update: Some quotes backing me:

To zero-initialize an object of type T means:

— if T is a scalar type (3.9), the object is set to the value of 0
(zero) converted to T;

Note that enumerations are scalar types.

To value-initialize an object of type T means:
— if T is a class type
blah blah
— if T is a non-union class
type blah blah

— if T is an array type, then blah blah —
otherwise, the object is zero-initialized

So, we get into otherwise part. And namespace-scope objects are value-initialized

What is the default value for an Enum type instance variable in Java?

A.A is a static variable. It's a bad idea, but authorized, to access static variable of a class using a variable referring to an instance of that class, even if it's null. That's not limited to enums:

Integer i = null;
System.out.println(i.MAX_VALUE);

runs fine. But it should really be written as

System.out.println(Integer.MAX_VALUE);

Initial Value of an Enum

Default value for enum types is 0 (which is by default, the first element in the enumeration). Fields of a class will be initialized to the default value.

If you need to represent an unknown value in the enum, you can add an element Unknown with value 0. Alternatively, you could declare the field as Nullable<MyEnum> (MyEnum?).

How to set the default value of enum type variable?

You must set it to one of the enum values, like:

Pesna(char *i = "NULL", int min = 0, tip t = pop)
// ^^^^^

Another techique is to use a Default value declared in the enum itself and use that one. This makes it easier if you change your mind later what the default should be:

enum tip {
pop,
rap,
rock,
Default = rap, // Take care not to use default, that's a keyword
};

Pesna(char *i = "NULL", int min = 0, tip t = Default)
// ^^^^^^^^^

Default value for ENUM

You could create a wrapper to stash the original input:

public class FoodInput {

private final Food food;
private final String input;

public FoodInput(String input) {
this.food = Food.fromString(input);
this.input = input;
}

public Food getFood() {
return food;
}

public String getInput() {
return input;
}
}


Related Topics



Leave a reply



Submit