We have a URL object in one of our Java classes that we want to mock, but it’s a final class so we cannot. We do not want to go a level above, and mock the InputStream because that will still leave us with untested code (we have draconian test coverage standards). I’ve tried jMockIt’s reflec…
UTF-8 text is garbled when form is posted as multipart/form-data
I’m uploading a file to the server. The file upload HTML form has 2 fields: File name – A HTML text box where the user can give a name in any language. File upload – A HTMl ‘file’ where user can specify a file from disk to upload. When the form is submitted, the file contents are…
How to convert java.util.Date to java.sql.Date?
I am trying to use a java.util.Date as input and then creating a query with it – so I need a java.sql.Date. I was surprised to find that it couldn’t do the conversion implicitly or explicitly – but I don’t even know how I would do this, as the Java API is still fairly new to me. Answer…
A Java collection of value pairs? (tuples?)
I like how Java has a Map where you can define the types of each entry in the map, for example <String, Integer>. What I’m looking for is a type of collection where each element in the collection is a pair of values. Each value in the pair can have its own type (like the String and Integer example…
When should I use the “strictfp” keyword in java?
I’ve looked up what this does, but does anyone actually have an example of when you would use the strictfp keyword in Java? Has anyone actually found a use for this? Would there be any side-effects of just putting it on all my floating point operations? Answer Strictfp ensures that you get exactly the s…
What’s the difference between ConcurrentHashMap and Collections.synchronizedMap(Map)?
I have a Map which is to be modified by several threads concurrently. There seem to be three different synchronized Map implementations in the Java API: Hashtable Collections.synchronizedMap(Map) ConcurrentHashMap From what I understand, Hashtable is an old implementation (extending the obsolete Dictionary cl…
How to simplify a null-safe compareTo() implementation?
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…
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…