Core Java

Java Performance Monitoring & Tuning

Getting head dump
2 ways:
1. Use jmap (which is also part of the Sun JDK 6) to dump the heap of a running application to a file using a command line like
jmap -dump:format=b,file=myheap.hprof <pid> (more…)

Advertisements

Generics

Q&A
http://www.baeldung.com/java-generics-interview-questions

Tutorials
https://docs.oracle.com/javase/tutorial/extra/generics/index.html

Learnings
In generic type declaration the wildcard ‘?’ and the ‘super’ keyword are NOT allowed; following will NOT compile:
public <? super T> void go(List<T> a) {  }

However, this is perfectly valid:
public static <T extends Comparable<? super T>> void sort(List<T> list)

The type has to implement comparable of itself or its superclass. So consider java.util.Date. It implements Comparable<Date>. But what about java.sql.Date? It implements Comparable<java.util.Date> as well (java.sql.Date extends java.util.Date).
Without the super signature, SortedList would not be able accept the type of java.sql.Date, because it doesn’t implement a Comparable of itself, but rather of a super class of itself.

————-
Extends – tells you what you can get out of a class (you get at least this, perhaps a subclass). e.g.,
public static <T> void copy(List<T> dest, List<? extends T> src)

Super – tells you what you can put into the class (at most this, perhaps a superclass).

————-

Resource Loading

Use either of below way given a) directory is on the classpath. b) class loading the resource is loaded by the same classloader.

1.    From ClassLoader perspective, all paths are “absolute” already – there’s no context from which they could be relative. Therefore, no need for a leading slash.
InputStream in = this.getClass().getClassLoader().getResourceAsStream(“SomeTextFile.txt”);

2.    From Class perspective, the path is relative to the package of the class unless a leading slash is included, so if we don’t want to use the current package, include a slash like this:
InputStream in = this.getClass().getResourceAsStream(“/SomeTextFile.txt”);