Why Java uses Classpath Parameter or Environment Variables?

In a Java class import statements are used to access other classes. You also do a wild card import like org.fromdev.* on your java file. In such cases, It will become very impractical/slow for the Java Virtual Machine to search for classes in every file/folder on a machine, therefore you can provide the Java Virtual … Read more

In Real Applications, how do you know that you have a Memory Leak

If you profile your application, you can notice a graph like a saw tooth. Here is how you can determine this with the help of jconsole for the above bad key class example. All you have to do is while your MemoryLeak is running, get the Java process id by typing. C:\>jps 5808 Jps 4568 … Read more

How will you Fix the Above Memory Leak

By providing proper implentation for the key class as shown below with the equals() and hashCode() methods.   ….. static class Key { private String key;   public Key(String key) { this.key = key; }     @Override public boolean equals(Object obj) {   if (obj instanceof Key) return key.equals(((Key) obj).key); else return false;   … Read more

How will you go About Creating a Memory Leak in Java

In Java, memory leaks are possible under a number of scenarios. Here is a typical example where hashCode( ) and equals( ) methods are not implemented for the Key class that is used to store key/value pairs in a HashMap. This will end up creating a large number of duplicate objects. All memory leaks in … Read more

Can we Use the Constructor, Instead of init(), to Initialize Servlet

Yes, of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still … Read more