Category Archives: java

What's an example of duck typing in Java?

Questions: I just recently heard of duck typing and I read the Wikipedia article about it, but I’m having a hard time translating the examples into Java, which would really help my understanding. Would anyone be able to give a clear example of duck typing in Java and how I might possibly use it? Answers:… Read More »

Understanding max JVM heap size – 32bit vs 64bit

Questions: I’ve read the max heap size on 32bit Windows is ~1.5GB which is due to the fact that the JVM requires contiguous memory. Can someone explain the concept of “contiguous memory” and why you only have max 1.5GB on Windows? Secondly, what then is the max heap size on 64 bit Windows and why… Read More »

How to load a large xlsx file with Apache POI?

Questions: I have a large .xlsx file (141 MB, containing 293413 lines with 62 columns each) I need to perform some operations within. I am having problems with loading this file (OutOfMemoryError), as POI has a large memory footprint on XSSF (xlsx) workbooks. This SO question is similar, and the solution presented is to increase… Read More »

Correct way to synchronize ArrayList in java

Questions: I’m not sure if this is the correct way to synchronize my ArrayList. I have an ArrayList in_queue which is passed in from the registerInQueue function. ArrayList<Record> in_queue = null; public void registerInQueue(ArrayList in_queue) { this.in_queue = in_queue; } Now I’m trying to synchronize it. Is this sychronizing my in_queue object correctly? List<Record> in_queue_list… Read More »

In Java, what is the difference between this.method() and method()?

Questions: Is there any difference between calling this.method() and method() (including performance difference)? Answers: The only time it matters is if you are using OuterClass.this.method() e.g. class OuterClass { void method() { } class InnerClass { void method() { OuterClass.this.method(); // not the same as method(). } } } Questions: Answers: There is absolutely no… Read More »

What is the difference between lowagie and iText?

Questions: What is the difference between lowagie and iText? Is this just version difference or an upgrade to the library. Which one is recommended to be used? Answers: I am Lowagie, the lowagie you refer to. This is what I have to say about this: You should use the latest version of iText. Currently… Read More »

what is the right way to get request's ip

Questions: I find some different ways to get ip in servlet. but i don’t know which one is right and why. 1: request.getHeader( “X-Real-IP” ) 2: String ip = request.getHeader(“X-Forwarded-For”); if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip)) { ip = request.getHeader(“Proxy-Client-IP”); } if (ip == null || ip.length() == 0 || “unknown”.equalsIgnoreCase(ip))… Read More »

Instantiating object of type parameter

Questions: I have got a template class as follows: class MyClass<T> { T field; public void myMethod() { field = new T(); // gives compiler error } } How do I create a new instance of T in my class? Answers: After type erasure, all that is known about T is that it is some… Read More »