Skip to content

Tag: java

Eclipse release heap back to system

I’m using Eclipse 3.6 with latest Sun Java 6 on Linux (64 bit) with a larger number of large projects. In some special circumstances (SVN updates for example) Eclipse needs up to 1 GB heap. But most of the time it only needs 350 MB. When I enable the heap status panel then I see this most of the time:

When to use a Map instead of a List in Java?

I didn’t get the sense of Maps in Java. When is it recommended to use a Map instead of a List? Answer Java map: An object that maps keys to values. A map cannot contain duplicate keys; each key can map to at most one value. Java list: An ordered collection (also known as a sequence). The user of this

Why do we need immutable class?

I am unable to get what are the scenarios where we need an immutable class. Have you ever faced any such requirement? or can you please give us any real example where we should use this pattern. Answer The other answers seem too focused on explaining why immutability is good. It is very good and I use it when…

Working with Zip and GZip files in Java

It’s been a while since I’ve done Java I/O, and I’m not aware of the latest “right” ways to work with Zip and GZip files. I don’t necessarily need a full working demo – I’m primarily looking for the right interfaces and methods to be using. Yes, I could look up …

C# DateTime.Ticks equivalent in Java

What is the Java equivalent of DateTime.Ticks in C#? What will be the equivalent of above mentioned code in Java? Answer Well, java.util.Date/Calendar only have precision down to the millisecond: That’s the nearest effective equivalent. If you need to convert between a .NET ticks value and a Date/Calend…

Java: reference escape

Read that the following code is an example of “unsafe construction” as it allows this reference to escape. I couldn’t quite get how ‘this’ escapes. I am pretty new to the java world. Can any one help me understand this. Answer The example you have posted in your question comes fr…

How to write unit test for “InterruptedException”

In attempts of 100% code coverage, I came across a situation where I need to unit test block of code that catches an InterruptedException. How does one correctly unit test this? (JUnit 4 syntax please) Answer Right before invoking addMessage(), call Thread.currentThread().interrupt(). This will set the &#8220…

Immutable array in Java

Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn’t actually prevent one from doing something like I want the elements of the array to be unchangeable. Answer Not with primitive arrays. You’ll need to use a List or some other data structure: