Skip to main content

Posts

Showing posts from July, 2012

Java references in nutshell

Java developers struggle to use java references and i don't blame them. The topic seldom covered by text books and even code reviews rarely give emphasis on usage of references. Why the topic is important at all? and if everybody (atleast people i have come across) is coding without it why the hell you need it? Since you are reading this, either you are already aware of them or for the interview. Anyways, knowing references helps you manage memory better because you are able to define the behavior of the object when GC (garbage collector)  is run. Type of references: 1. Strong R eference : The normal reference in the java code is strong reference and the object referenced by strong reference is eligible for GC as soon as scope ends or reference started pointing to null in the code. E.g.  Person p = new Person();                 p = null; // will make object initialized above eligible for GC 2. Weak Reference : As name suggest these reference are weaker than the