Prev: Socket Buffered*putStream Threads and Full-duplex communication
Next: Cross Platform/Browser way to send PNG file to Webserver
From: Thomas Pornin on 16 Mar 2010 12:23 According to <avoidingspam2001(a)yahoo.com>: > I'm told that Java enum types don't have an integer value so there > can't be any Hamming Distance between them. Oh, that's quite something else than what I was talking about. On the Java abstract machine, enumeration constants are instances of a compiler-generated class which extends Enum. So that, when this comes down to the opcodes, the constants are actually pointers to those instances. Since they are allocated in due sequence, chances are that those instances will be close together in RAM, possibly consecutive, and there will be very few differences between those pointers. Moreover, every enumeration constant _contains_ an integer, which is what the Enum.ordinal() method returns, and that's what 'switch' uses when handling enumeration constants. These integer values are automatically allocated, beginning with zero. Of course, nothing prevents you from using your "own" integer constants (defined with a sequence of 'public static final int FOO = ...') with whatever patterns you wish, and use them in a 'switch'. That's what you would do in C, C++, assembly... and Java prior to Java 5. The Java 'enum' construction is only an additional tool which adds better types and reduces the amount of work for the programmer. This does not make it mandatory in any way. > I'm not sure, in my obviously advanced state of Java ignorance, how > one prevents a damaged memory bit from causing an inappropriate > decision path from being taken in a switch/case or if/else satement. As far as I can tell, pretty much everything in a computer assumes that the RAM is "correct". In particular, when the CPU executes instructions, it interprets memory bits into opcodes. A wrong bit at that point transforms a 'if' into something else, regardless of what the programmer was trying to do. Singleing out enumeration constants and trying to give them a "sparse" representation, is like locking a safe onboard the Titanic. Sure, no iceberg will damage the safe contents. It does not mean that all problems, or even the majority of problems, have been addressed. Usually, hardware errors being all-compassing, they are handled with more hardware. You use ECC RAM, which has extra bits, and the machine circuitry uses those extra bits to recover from one-bit errors. Similar schemes are used within the CPU (for cache RAM). --Thomas Pornin
From: Joshua Cranmer on 16 Mar 2010 13:42 On 03/16/2010 10:57 AM, avoidingspam2001(a)yahoo.com wrote: > On Mar 16, 10:52 am, r...(a)zedat.fu-berlin.de (Stefan Ram) wrote: >> avoidingspam2...(a)yahoo.com writes: >>> How does one get a Java enum class to support hamming distance? >> >> By implementing the appropriate methods. > > An ideal insider's answer... Terse, true and absolutely meaningless to > me, an obvious novice. It is an appropriately vague answer for an appropriately vague question. Hamming distance is the difference between two strings... of what? Besides, there can be some differences between what is precisely meant by "support" and also "get a Java enum class": * Write a static utility method to do X for inputs of some type T extends Enum<T>. * Write a method for an enum X that supports this. * Make a method on Enum itself that does this. etc. -- Beware of bugs in the above code; I have only proved it correct, not tried it. -- Donald E. Knuth
From: Roedy Green on 17 Mar 2010 02:15 On Tue, 16 Mar 2010 07:57:12 -0700 (PDT), avoidingspam2001(a)yahoo.com wrote, quoted or indirectly quoted someone who said : >An ideal insider's answer... Terse, true and absolutely meaningless to >me, an obvious novice. He is poking fun at the extremely vague question. -- Roedy Green Canadian Mind Products http://mindprod.com Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, �How would I develop if it were my money?� I�m amazed how many theoretical arguments evaporate when faced with this question. ~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming.
From: Roedy Green on 17 Mar 2010 02:17
On Tue, 16 Mar 2010 07:48:23 -0700 (PDT), avoidingspam2001(a)yahoo.com wrote, quoted or indirectly quoted someone who said : >How does one get a Java enum class to support hamming distance? Hamming distance is a very general term. You will have to be more specific about the definition you are using. -- Roedy Green Canadian Mind Products http://mindprod.com Responsible Development is the style of development I aspire to now. It can be summarized by answering the question, �How would I develop if it were my money?� I�m amazed how many theoretical arguments evaporate when faced with this question. ~ Kent Beck (born: 1961 age: 49) , evangelist for extreme programming. |