I’m implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform): I want the natural ordering for these objects to be: 1) sorted by name and 2) sorted by value if name is the same; both comparisons should be case-in…
Tag: java
How can “while (i == i) ;” be a non-infinite loop in a single threaded application?
I just got a question that I can’t answer. Suppose you have this loop definition in Java: What is the type of i and the value of i if the loop is not an infinite loop and the program is using only one thread? Answer The API for Double.equals() spells out the answer: “Double.NaN==Double.NaN has the…
Java ReentrantReadWriteLocks – how to safely acquire write lock when in a read lock?
I am using in my code at the moment a ReentrantReadWriteLock to synchronize access over a tree-like structure. This structure is large, and read by many threads at once with occasional modifications to small parts of it – so it seems to fit the read-write idiom well. I understand that with this particul…
Thread safety of static blocks in Java
Let’s say I have some Java code: If a thread is initializing SomeClass’s Class object and is in the middle of initializing the values in the static block when a second thread wants to load SomeClass’s Class again, what happens to the static block? Does the second thread ignore it assuming it…
Highest Performance Database in Java
I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism in Java. In the range of storing 20,000+ java objects, updated every 5 or so seconds. Some options I am open to: Pure JDBC/database combination JDO JPA/ORM/database combination An Object Database Other Storage Mechanism…
StackOverflowError when serializing an object in Java
I am writing an application in Java using Swing. I am trying to implement functionality to save and load simulation states for at simulation i am running. The entire simulation is kept as an object, disconnected from Swing. I am trying to serialize my Simulation class with this code: But i get the following e…
Does Java have native support for events, similar to that of C#?
I’m a bit confused from what I’ve heard Java doesn’t do events. But I know that it does GUI events. Am I missing something? Does java have an event handling mechanism? I’m aware that I can implement a publisher subscriber pattern, but I’m looking for native support within Java. I…
Is there any way to do n-level nested loops in Java?
In other words, can I do something like Except N times? In other words, when the method creating the loops is called, it is given some parameter N, and the method would then create N of these loops nested one in another? Of course, the idea is that there should be an “easy” or “the usualR…
How can I generate an MD5 hash in Java?
Is there any method to generate MD5 hash of a string in Java? Answer You need java.security.MessageDigest. Call MessageDigest.getInstance(“MD5”) to get a MD5 instance of MessageDigest you can use. The compute the hash by doing one of: Feed the entire input as a byte[] and calculate the hash in one…
Variable naming conventions in Java
In PHP, we (at least the good programmers) always start general variable names with a lower-case letter, but class variables/objects with an upper-case letter to distinguish them. In the same way we start general file names with a lower case letter, but files containing Classes with an upper case letter. E.g.…