-2

When you say enums are constant in java, what do you actually mean?

What type of constant(integer constant , char constant, string constant) are they?

Why Java system is not complaining when I use enums as switch cases value?

As for as i know prior to 1.7 switch statement accepts byte, short, int or char as case value. So when someone say enum are constant and they can be used in switch cases, what type of constant they mean because switch accepts only byte, short, int or char

For example String constants, they are added in java 1.7 but enum are there since 1.5 and they are being used in switch cases value and now from Java 1.7 on words I can use String as switch case value?

How does Java handles enum in switch cases, does it use ordinal() value of enums?

geecatc
  • 1
  • 1
  • 2
  • See also: http://stackoverflow.com/questions/10387329/using-string-representations-of-enum-values-in-switch-case and http://stackoverflow.com/questions/338206/switch-statement-with-strings-in-java – Flight Odyssey Nov 01 '13 at 00:30
  • http://docs.oracle.com/javase/tutorial/java/javaOO/enum.html That should help –  Nov 01 '13 at 00:31
  • @FlightOdyssey Actually what you have pointed is exactly opposite of what i asked? but that is good, probably i would have thought that after clearing this current doubt? – geecatc Nov 01 '13 at 00:35
  • Specifically the enum is a class that extends java.lang.Enum and the constants are static instances of that class. It is just a special syntax to do basically normal Java stuff. They are probably compared in a switch as an integer. Somebody who knows more can correct me if I'm wrong since I am inferring a bit but that's basically how it works. – Radiodef Nov 01 '13 at 00:37
  • @aliasm2k I have gone through that link at least five time and that link did not answer my question? – geecatc Nov 01 '13 at 00:38
  • What *is* your question? It's impossible to understand what it is you don't understand. The language you have quoted is perfectly clear. – user207421 Nov 01 '13 at 00:42
  • @Radiodef When you say **They are probably compared as an integer** what do you mean? How can i get that integer value? – geecatc Nov 01 '13 at 00:43
  • The private constructor for the enum class you declare (that you can't call in code but is called when you declare the enum constants) takes a String and the ordinal int as arguments. Enum implements Comparable and I *assume* they are compared on the ordinal value. I *assume* they are allowed in a switch as special syntax similar to the way that implementing Iterable allows for-each iteration. I can't find a resource to back that up exactly but I think it's a safe assumption. – Radiodef Nov 01 '13 at 00:49
  • @EJP As for as i know prior to 1.7 switch statement accepts byte, short, int or char as case value. So when someone say enum are constant and they can be used in switch cases, what type of constant they mean because switch accepts only byte, short, int or char? – geecatc Nov 01 '13 at 00:56
  • Here's the language spec for enums: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.9 I basically think you shouldn't worry about the switch comparison. It is almost certainly special syntax. Under the hood an enum is just a class with static instances of itself so switching on one could only be allowed out of some specialness. – Radiodef Nov 01 '13 at 00:58
  • @Radiodef i also assume the same. If i don't get any different answer then i will move on with the same assumption. Thank you for the help. – geecatc Nov 01 '13 at 01:03
  • 1
    It's best to think of them with a concrete example, like colors. Imagine switching between red, green, blue, yellow, etc. Trying to convert them to strings or integers is meaningless. Under the covers I believe it is actually an identify comparison (`==`), since enums are guaranteed to have one and only one copy of each instance per JVM. –  Nov 01 '13 at 01:10
  • @geecatc You're wrong. It's as simple as that. Switch statements have accepted enums as well, since enums were introduced. Your question is self-contradictory. – user207421 Nov 01 '13 at 01:41
  • @EJP ok i get it. Thank you for the help!! I did not knew that switch statements have accepted enum and also i read the concept from a book and it was not mentioned there clearly. Now i get it. May be my understanding was wrong. – geecatc Nov 01 '13 at 02:26

1 Answers1

0

I think I know what your asking. Enums are constants because they cannot be changed during run time.

you can use a switch statement on them because there is a predefined number of elements so the compiler knows when all the cases have been covered.

Todoy
  • 1,146
  • 1
  • 9
  • 17
  • so does that mean if I create a arraylist of 10 constant object i can use that list in switch statement? – geecatc Nov 01 '13 at 01:00
  • no because the arraylist itself is not constant – Todoy Nov 01 '13 at 01:02
  • yes arraylist is not constant but i was thinking about immutable List. http://stackoverflow.com/questions/3700971/immutable-array-in-java. Ok now after reading about immutable List, i think now understand why i can not use list. but actual question remains **What type of constant are enum constant?** – geecatc Nov 01 '13 at 01:19
  • I dont really know what you mean by that – Todoy Nov 01 '13 at 01:20
  • I think i am trying to learn in more detail than required, Probably i should move on. Thank you for the help. i will wait some more and if there no different answer than yours, then i will accept your answer. – geecatc Nov 01 '13 at 01:21