Learn Java interactively using bluej

java -classpath . myClass

Java profiling:

if Java handles large data it can get to a memory leak called "out of memory exception" to handle that you can use a heapdump and use some profiling tools to see where big data are accumulating.
$ sudo apt-get install visualvm
$ jvisualvm # just run your java program and visualvm will capture it!

To profile a remote java program: Create the file jstatd.all.policy in $JAVA_HOME/bin
grant codebase "file:${java.home}/../lib/tools.jar" {

$ cd $JAVA_HOME/bin/; jstatd

$ netstat -nlp | grep jstatd   make sure jstatd is running
tcp 0 0 :::39779 :::* LISTEN 28661/jstatd
2 tcp 0 0 :::1099 :::* LISTEN 28661/jstatd
The 4th column indicates the ports that are open. In this case, the ports that need to be opened in the firewall are 1099 and 39779.

Open VisualVM, right click on Remote and select Add Remote Host, and finally type the IP address of the remote host.

To increase the memory given to visualvm:
call it like $ jvisualvm -': jvisualvm -J-Xmx1G
or modify in either of the following that matches your system
To install plugins
In VisualVM main menu choose Tools | Plugins, Plugins dialog is opened
Switch to Available Plugins tab and select the plugin you want to install
Click the Install button, review and accept plugin license and wait for the plugin to be downloaded and installed
Note: plugins for offline VisualVM installations can be manually downloaded from the Plugins Centers page.

Strings Not GarbageCollected

Strings are immutable, they are not garbage collectable!  They are stored as char[] with start, end indexes. a substring() call just returns the same whole array with new indexes.

To avoid Strings use StringBuilder! Especially if you do a lot of string manipulation (concat, modify, substring, delete,...)
StringBuffer is the thread-safe version of StringBuilder

Wrappers of Primitive Types are Immutable! you can't change the content of a wrapped primitive.

Debug: System.out.println()

They will show in the console all the time, Either when you debug or not.


Will be ignored for normal execution (and won't slow down the program when you don't want to debug) unless explicitly specified that you are debugging - then they will be executed.
Assert(height > 0); //passes if the statement is true, throws exception otherwise
Assert(height > 0) : "height= "+ height;  //statement after colon can be any java expression that results in a non-null value

java -ea MyProgram

static nested classes have access to private and static fields of the parent class.
Anonymous class is for example when as a parameter of a method you just implement an abstract class or interface.

protected is just like default, EXCEPT that subclasses outside the package inherit the protected thing

Enum class

public class Myclass{

enum Types {

BAND_TYPE1("my title 1."){
public String getMusic(){
return "jazz";

BAND_TYPE4("another title here."){
public String getMusic(){
return "Classical";


private String title;

Types(String someTitle){
this.title = someTitle;

public String getTitle(){
return this.title;

public String getMusic(){
return "This is a kind of static method, vs the specific type getMusic()";

public static void main(String[] args){
for(Types t: Types.values(){

BAND_TYPE1 my title 1. jazz
BAND_TYPE4 another title here. Classical
BAND_TYPEx BandWithoutBody  This is a kind of static method, vs the specific type getMusic()

** The less two classes know about each other the more loosely coupled they are, they easier they are replaceable
Remote Proxy: is an object local to the client object that pretends to be a remote object. - stub

java.lang.reflect.Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods. Link
   A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below.
   A proxy interface is such an interface that is implemented by a proxy class.
   Each proxy instance has an associated invocation handler object, which implements the interface InvocationHandler. A method invocation on a proxy instance through one of its proxy interfaces will be dispatched to the invoke method of the instance's invocation handler, passing the proxy instance, a java.lang.reflect.Method object identifying the method that was invoked, and an array of type Object containing the arguments. The invocation handler processes the encoded method invocation as appropriate and the result that it returns will be returned as the result of the method invocation on the proxy instance.