Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (108)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2
  ignore  |  Print  
  Enums !  (Read 3852 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2011-10-09 00:43:18 »

Enums. Are you using them ? If so for what, if not why not ?

Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #1 - Posted 2011-10-09 00:50:49 »

I use them for constants that don't need a value, as they should be used for.

EDIT: For example, I'm working on a text-based interactive fiction for school, and I used Enums for NORTH, SOUTH, EAST, and WEST.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2011-10-09 01:00:02 »

I use them for constants that don't need a value, as they should be used for.
java enums can hold (multiple) values.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Cero
« Reply #3 - Posted 2011-10-09 01:00:36 »

I use them for constants that don't need a value, as they should be used for.

EDIT: For example, I'm working on a text-based interactive fiction for school, and I used Enums for NORTH, SOUTH, EAST, and WEST.

Yeah, I have started to do this too, so there are less potential errors.
I mean if you have Direction myDirection = NORTH, its safer than just using an int, because someone could just mix it up and do like myDirection = 7;
But some of my team hate enums, so I was just interested in the general consensus.

Offline ra4king

JGO Kernel


Medals: 341
Projects: 2
Exp: 5 years


I'm the King!


« Reply #4 - Posted 2011-10-09 01:35:53 »

I use them for constants that don't need a value, as they should be used for.
java enums can hold (multiple) values.
Yeah, enums could be quite valuable for storing info about each constant, too.

I really like Enums and use them whenever I see them being useful.

Offline Eli Delventhal

JGO Kernel


Medals: 42
Projects: 11
Exp: 10 years


Game Engineer


« Reply #5 - Posted 2011-10-09 01:48:47 »

I don't like enums in Java, because I think they missed the point. I just want a numerical constant, man. Java's is basically just a class, it's too overly complicated. I usually just use constant integers or strings.

1  
2  
public static final String TYPE_DOG = "TYPE_DOG";
public static final String TYPE_MUSKRAT = "TYPE_MUSKRAT";

Lawl yes I've done that.

See my work:
OTC Software
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #6 - Posted 2011-10-09 01:54:28 »

Java's is basically just a class, it's too overly complicated.
In the Hotspot JVM (not the bytecode, so this is implementation dependent) it's all turned into ints, and if you have more than 32 enum values, it's (obviously) converted to a long. Beyond that, it uses byte[]. This is done to be able to transform the functionality of EnumSet to binary operators.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #7 - Posted 2011-10-09 03:44:01 »

Java's is basically just a class, it's too overly complicated.
In the Hotspot JVM (not the bytecode, so this is implementation dependent) it's all turned into ints, and if you have more than 32 enum values, it's (obviously) converted to a long. Beyond that, it uses byte[]. This is done to be able to transform the functionality of EnumSet to binary operators.
Dang. I feel for you, man. Having your ints shrink to 5 bits must be harsh... Even worse, your bytes are 1.25 bits each, and that's before rounding errors!
...
Why can you only fit 32 different values in an int? Does only use powers of 2?

Myomyomyo.
Offline sproingie

JGO Kernel


Medals: 202



« Reply #8 - Posted 2011-10-09 04:02:44 »

I don't like enums in Java, because I think they missed the point. I just want a numerical constant, man.

The whole point of enums is that they're NOT numerical constants and that each set of enums is a distinct type from each other.  This is why I can't set my direction to COLOR_RED or my color to NORTH.  And if you're going to introduce distinct types in Java, you're going to do it with classes, unless you're keen on creating a parallel type system that only works for enums.
Offline JL235

JGO Coder


Medals: 10



« Reply #9 - Posted 2011-10-09 04:15:04 »

I've used enums from time to time, but mostly find them too verbose, and too powerful.

I'd have been much happier with something that closer related to 'atoms' or 'symbols' in other languages.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline theagentd
« Reply #10 - Posted 2011-10-09 04:28:48 »

I use them mainly to eliminate invalid values when only a (very) limited range of values is expected. Sure, I agree that they are a little bit too powerful, but I'd rather have them too powerful than too weak, as long as that doesn't come with a too big performance hit of course.

Myomyomyo.
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #11 - Posted 2011-10-09 05:35:29 »

1  
2  
3  
4  
5  
   static public final int CENTER = 1 << 0;
   static public final int TOP = 1 << 1;
   static public final int BOTTOM = 1 << 2;
   static public final int LEFT = 1 << 3;
   static public final int RIGHT = 1 << 4;

Any elegant solutions to use an enum and still be able to do the equivalent of "CENTER | LEFT"?

Offline JL235

JGO Coder


Medals: 10



« Reply #12 - Posted 2011-10-09 06:19:53 »

Even before I started using dynamic and functional languages more heavily, I always felt there was a lot of cruft around enums. But being able to just do 'blah', 'Blah', or ':blah' for denoting an atom/symbol, is so much easier then the whole 'EnumClass::FOO_BAR' + it's declaration.

This is especially if you have pattern matching.

Offline ReBirth
« Reply #13 - Posted 2011-10-09 06:21:29 »

I started to use enum when I found a good sound player class using it. But only that, no more.

Offline divxdede

Junior Member





« Reply #14 - Posted 2011-10-09 12:09:47 »

Another case i use enum is for avoid a maximum of boolean parameters. Why ? because they are too low significant when i read a call usage and i never remember what this boolean is for when reading code.
A small enum in theses cases make the code more readable and more extendable if in a long time the 2-values parameters (boolean) became a 2+ values...

Another case i use it is when i need to store values for a serie of predefined constants. In this case, i use an EnumMap that is really efficient.
   

Offline nsigma
« Reply #15 - Posted 2011-10-09 14:05:03 »

I use enums all the time, and really like them - probably the best addition in Java 5 - not that that's saying much!  Smiley

I've used enums from time to time, but mostly find them too verbose, and too powerful.

I'm surprised by the number of people who seem to have this opinion on this thread.  Too verbose?  How?  Using them at their simplest surely enums are less verbose, because you don't have to assign them a value at creation or check for invalid values in use.  And as for too powerful, it's not like you have to use it or worry about it, but at times it's useful.  Eg. I sometimes use fields to map Java enums to native enums in a type safe way, such as here

Any elegant solutions to use an enum and still be able to do the equivalent of "CENTER | LEFT"?

Yes, flags = EnumSet.of(CENTER, LEFT) along with flags.contains(CENTER), etc.

The elegant bit is how this maps down underneath to (practically) identical code.  And you get type-safety, both for individual flags, as well as a separation between a set of flags and the flags themselves.

Dang. I feel for you, man. Having your ints shrink to 5 bits must be harsh... Even worse, your bytes are 1.25 bits each, and that's before rounding errors!
...
Why can you only fit 32 different values in an int? Does only use powers of 2?

Dang, I think I heard the whooosh as you missed the point!  Tongue  Bitsets means one bit per value - so yes, in powers of 2.

Praxis LIVE - open-source intermedia toolkit and live interactive visual editor
Digital Prisoners - interactive spaces and projections
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #16 - Posted 2011-10-09 14:23:50 »

In the Hotspot JVM (not the bytecode, so this is implementation dependent) it's all turned into ints, and if you have more than 32 enum values, it's (obviously) converted to a long. Beyond that, it uses byte[]. This is done to be able to transform the functionality of EnumSet to binary operators.
Dang. I feel for you, man. Having your ints shrink to 5 bits must be harsh... Even worse, your bytes are 1.25 bits each, and that's before rounding errors!
...
Why can you only fit 32 different values in an int? Does only use powers of 2?
Maybe take your condescending attitude and give it some thought first. I even explained it in that very message.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Kernel


Medals: 202



« Reply #17 - Posted 2011-10-09 19:42:21 »

Any elegant solutions to use an enum and still be able to do the equivalent of "CENTER | LEFT"?

CENTER_LEFT.  Or as explained above, a Set<Direction> containing CENTER and LEFT, which there's some library support for already. 

The point of enums is that they fully enumerate the possible values, and that no new values of that type can be synthesized.   If you need them to act like bits, you don't really have an enum.  Now for compatibility's sake, such as with OpenGL's many many constants, you can assign values to the enums, but for sake of your particular example, it wouldn't gain you any benefit in terms of syntax.

Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #18 - Posted 2011-10-09 19:52:20 »

Any elegant solutions to use an enum and still be able to do the equivalent of "CENTER | LEFT"?

Yes, flags = EnumSet.of(CENTER, LEFT) along with flags.contains(CENTER), etc.

The elegant bit is how this maps down underneath to (practically) identical code.  And you get type-safety, both for individual flags, as well as a separation between a set of flags and the flags themselves.
Hmm. It's ok, if a bit clunky. Does this allocate? Seems to.

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #19 - Posted 2011-10-09 19:53:29 »

Any elegant solutions to use an enum and still be able to do the equivalent of "CENTER | LEFT"?

Yes, flags = EnumSet.of(CENTER, LEFT) along with flags.contains(CENTER), etc.

The elegant bit is how this maps down underneath to (practically) identical code.  And you get type-safety, both for individual flags, as well as a separation between a set of flags and the flags themselves.
Hmm. It's ok, if a bit clunky. Does this allocate? Seems to.
If it's all converted to ints in the JVM, why would it allocate? (it might allocate in bytecode, but that's not the point)

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline sproingie

JGO Kernel


Medals: 202



« Reply #20 - Posted 2011-10-09 21:02:48 »

It allocates an EnumSet instance at the very least.  People obsessed with bit-level performance should stick to C.
Offline theagentd
« Reply #21 - Posted 2011-10-09 21:05:00 »

In the Hotspot JVM (not the bytecode, so this is implementation dependent) it's all turned into ints, and if you have more than 32 enum values, it's (obviously) converted to a long. Beyond that, it uses byte[]. This is done to be able to transform the functionality of EnumSet to binary operators.
Dang. I feel for you, man. Having your ints shrink to 5 bits must be harsh... Even worse, your bytes are 1.25 bits each, and that's before rounding errors!
...
Why can you only fit 32 different values in an int? Does only use powers of 2?
Maybe take your condescending attitude and give it some thought first. I even explained it in that very message.
Okay, I Google'd EnumSet. Happy? ._.

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #22 - Posted 2011-10-09 21:53:28 »

It allocates an EnumSet instance at the very least.
Ehm... why? As said, maybe in bytecode, but not in HotSpot... it's just an int.


People obsessed with bit-level performance should stick to C.
If that's directed at me... I'm just answering a question, explaining what happens underneath.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Nate

JGO Kernel


Medals: 145
Projects: 4
Exp: 14 years


Esoteric Software


« Reply #23 - Posted 2011-10-09 22:03:42 »

If it's all converted to ints in the JVM, why would it allocate? (it might allocate in bytecode, but that's not the point)

I see it allocates in bytecode / the JDK source, so it makes me wonder what magic happens and also, what Android does (ie, how Android fscks this up).

Offline sproingie

JGO Kernel


Medals: 202



« Reply #24 - Posted 2011-10-10 00:22:31 »

Ehm... why? As said, maybe in bytecode, but not in HotSpot... it's just an int.

There is more to an EnumSet than an int, such as the Set interface.  What may be the case is after inlining that the relevant membership tests become int operations. but HotSpot doesn't do anything without it being bytecode first.  JIT does a lot, but it's not magic

Quote
If that's directed at me... I'm just answering a question, explaining what happens underneath.

No, it's directed at people who are actually fretting about Enums having more overhead than ints.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #25 - Posted 2011-10-10 00:47:57 »

For the sake of it, I benchmarked java.util.RegularEnumSet, and unfortunately even in 6u26 it suffers from the interface overhead.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
   static enum Test
   {
      A, B, C, D, E, F, G, H, I, J, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z;
   }

   private static void benchmark(Set<Test> tests, Test value)
   {
      for (int i = 0; i < 16 * 1024 * 1024; i++)
      {
         tests.contains(value);
      }
   }


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
         Set<Test> enumSet = EnumSet.of(Test.A, Test.B);
         Set<Test> hashSet = new HashSet<Test>();
         Set<Test> treeSet = new TreeSet<Test>();
         for (Test test : enumSet)
         {
            hashSet.add(test);
            treeSet.add(test);
         }

         {
            long t0 = System.nanoTime();
            benchmark(enumSet, Test.A);
            long t1 = System.nanoTime();
            System.out.println("enum took: " + (t1 - t0) / 1000000 + "ms");
         }

         {
            long t0 = System.nanoTime();
            benchmark(hashSet, Test.A);
            long t1 = System.nanoTime();
            System.out.println("hash took: " + (t1 - t0) / 1000000 + "ms");
         }

         {
            long t0 = System.nanoTime();
            benchmark(treeSet, Test.A);
            long t1 = System.nanoTime();
            System.out.println("tree took: " + (t1 - t0) / 1000000 + "ms");

         }


If you change the order of execution of the different types of Set<Test> you can see massive performance differences.

Results for Server VM 6u26:
1  
2  
3  
enum took:   7ms <-- first
hash took: 125ms
tree took: 133ms

1  
2  
3  
hash took: 150ms
tree took: 104ms
enum took: 130ms <-- last


Just so you know. And yes, and instance is allocated.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline theagentd
« Reply #26 - Posted 2011-10-10 02:39:15 »

1  
Set<Test> enumSet = EnumSet.of(Test.A, Test.B);

That doesn't look right... Only two values? Why not EnumSet.allOf(Test)?

Myomyomyo.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 749
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #27 - Posted 2011-10-10 02:55:07 »

1  
Set<Test> enumSet = EnumSet.of(Test.A, Test.B);

That doesn't look right... Only two values? Why not EnumSet.allOf(Test)?
How does that not look right...

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline JL235

JGO Coder


Medals: 10



« Reply #28 - Posted 2011-10-10 03:39:37 »

I use enums all the time, and really like them - probably the best addition in Java 5 - not that that's saying much!  Smiley

I've used enums from time to time, but mostly find them too verbose, and too powerful.

I'm surprised by the number of people who seem to have this opinion on this thread.  Too verbose?  How?  Using them at their simplest surely enums are less verbose, because you don't have to assign them a value at creation or check for invalid values in use.  And as for too powerful, it's not like you have to use it or worry about it, but at times it's useful.  Eg. I sometimes use fields to map Java enums to native enums in a type safe way, such as here
The main issue I have is that if you want to use enums heavily, then it requires _lots_ of enum declarations. That is a scenario where symbols/atoms work much better, because you are essentially declaring the enums within their usage, making the code much more terse.

You could then also add guards or pattern matching, for automatic verifying parameters. These would then be verified using type inference, or at runtime. But I don't think Java could really take guards and symbols, or should really get them. Both would be moving away from Java's static type system, which wouldn't fit the philosophy of the language.

So I think Java enums work well for Java, but aren't so great when compared to what you can do in other languages.

Offline theagentd
« Reply #29 - Posted 2011-10-10 03:42:16 »

1  
Set<Test> enumSet = EnumSet.of(Test.A, Test.B);

That doesn't look right... Only two values? Why not EnumSet.allOf(Test)?
How does that not look right...
Ah, the whole test only uses those two enums. Carry on, dear sir. But I do wonder why you only use A and B instead of all...

Myomyomyo.
Pages: [1] 2
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (16 views)
2014-07-30 21:08:39

Riven (22 views)
2014-07-29 18:09:19

Riven (15 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (33 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (43 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
List of Learning Resources
by SilverTiger
2014-07-31 13:54:12

HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!