Skip to content
Advertisement

Does a finally block always get executed in Java?

Considering this code, can I be absolutely sure that the finally block always executes, no matter what something() is? Answer Yes, finally will be called after the execution of the try or catch code blocks. The only times finally won’t be called are: If you invoke System.exit() If you invoke Runtime.getRuntime().halt(exitStatus) If the JVM crashes first If the JVM reaches

Should I avoid using Java Label Statements?

Today I had a coworker suggest I refactor my code to use a label statement to control flow through 2 nested for loops I had created. I’ve never used them before because personally I think they …

How to convert a date String to a Date or Calendar object?

I have a String representation of a date that I need to create a Date or Calendar object from. I’ve looked through Date and Calendar APIs but haven’t found anything that can do this other than creating my own ugly parse method. I know there must be a way, does anyone know of a solution? Answer In brief: See SimpleDateFormat

How to generate a random alpha-numeric string

I’ve been looking for a simple Java algorithm to generate a pseudo-random alpha-numeric string. In my situation it would be used as a unique session/key identifier that would “likely” be unique over 500K+ generation (my needs don’t really require anything much more sophisticated). Ideally, I would be able to specify a length depending on my uniqueness needs. For example, a

Checking the results of a Factory in a unit test

I have developed some classes with similar behavior, they all implement the same interface. I implemented a factory that creates the appropriate object and returns the interface. I am writing a unit test for the factory. All you get back is an interface to the object. What is the best way to test that the factory has worked correctly? I

Detach an entity from JPA/EJB3 persistence context

What would be the easiest way to detach a specific JPA Entity Bean that was acquired through an EntityManager. Alternatively, could I have a query return detached objects in the first place so they …

How do I use 3DES encryption/decryption in Java?

Every method I write to encode a string in Java using 3DES can’t be decrypted back to the original string. Does anyone have a simple code snippet that can just encode and then decode the string back to the original string? I know I’m making a very silly mistake somewhere in this code. Here’s what I’ve been working with so

Advertisement