For example if I have an Enum with two cases, does it make take more memory than a boolean? Languages: Java, C++
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In Java, an
enumis a full-blown class:In order to see the actual size of each
enum, let’s make an actualenumand examine the contents of theclassfile it creates.Let’s say we have the following
Constantsenum class:Compiling the above
enumand disassembling resultingclassfile withjavapgives the following:The disassembly shows that that each field of an
enumis an instance of theConstantsenumclass. (Further analysis withjavapwill reveal that each field is initialized by creating a new object by calling thenew Constants(String)constructor in the static initialization block.)Therefore, we can tell that each
enumfield that we create will be at least as much as the overhead of creating an object in the JVM.