When the enhanced for loop (foreach loop) was added to Java, it was made to work with a target of either an array or Iterable. That works great for Collection classes that only implement one type of iteration, and thus have a single iterator() method. But I find myself incredibly frustrated the odd time I wan…
Tag: java
Search for a word in a String
If I am looking for a particular word inside a string, for example, in the string “how are you” I am looking for “are”. Would a regular indexOf() work faster and better or a Regex match() Which of the two methods above is a better way of looking for a string inside another string? Or i…
Converting from Integer, to BigInteger
I was wondering if there was any way to convert a variable of type Integer, to BigInteger. I tried typecasting the Integer variable, but i get an error that says inconvertible type. Answer The method you want is BigInteger#valueOf(long val). E.g., Making a String first is unnecessary and undesired.
Eclipse Java utility project and multiple web apps
If I create a utility project and multiple dynamic web projects within Eclipse and set it up so that dynamic web projects depend on the utility project, I’m guessing that I will have to redeploy all …
Naming conventions for Java methods that return boolean
I like using question mark at the end of method/function names in other languages. Java doesn’t let me do this. As a workaround how else can I name boolean returning methods in Java? Using an is, has, should, can in the front of a method sound okay for some cases. Is there a better way to name such meth…
How to clean up ThreadLocals
Does any one have an example how to do this? Are they handled by the garbage collector? I’m using Tomcat 6. Answer The javadoc says this: “Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after…
Difference using @Id and @EmbeddedId for a compound key
I’ve created an entity that uses @Id to point to an @Embeddable compound key. Everything I believe works fine as is. However, after switching @Id to @EmbeddedId everything continues to work fine as far as I can tell. Before: After: Is there a difference between using the @Id and @EmbeddedId annotations …
Do you really need the ‘finally’ block
There are 3 permutations of a try…catch…finally block in java. try…catch try…catch…finally try…finally Once the finally block is executed, control goes to the next line after the finally …
How to calculate “time ago” in Java?
In Ruby on Rails, there is a feature that allows you to take any Date and print out how “long ago” it was. For example: Is there an easy way to do this in Java? Answer Take a look at the PrettyTime library. It’s quite simple to use: You can also pass in a locale for internationalized message…
Why does a BufferedImage require so much memory beyond the size of its data array?
I’m trying to determine how much heap any given TYPE_INT_ARGB BufferedImage will use so that, for a program which is doing some image processing, I can set a reasonable max heap based on the size of image we feed it. I wrote the following program as a test, which I then used to determine the least maxim…