How much memory do Enums take?

后端 未结 12 1281
野性不改
野性不改 2020-12-13 06:13

For example if I have an Enum with two cases, does it make take more memory than a boolean? Languages: Java, C++

12条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-13 06:41

    If your enum will ever have only two cases, indeed using a boolean instead might be a better idea (memory size, performance, usage/logic), even more in Java.
    If you are wondering about memory cost, it might imply you plan to use lot of them. In Java you can use BitSet class, or on a smaller scale, in both languages you can manipulate bits with bitwise operations.

提交回复
热议问题