Tuesday, December 11, 2012

Where are variables stored in Java, Stack or Heap?

A primitive variable “holds” directly his value itself. Examples of a primitive variable :  int i, long l; A reference variable “holds” the reference (4 bytes on 32-bit JVMs, 8 bytes on 64-bit JVMs) who refers to an object. Examples of a reference variable : Student s, Date d; local variable...

Wednesday, December 5, 2012

Why nonfair lock is faster than fair lock (java.util.concurrent.locks.Lock) in Java Concurrency

This is the nonfair scenario: When a thread B asks to hold a lock, if the lock is already taken by another thread A. Then the thread B will be suspended. After thread A finished his job, it releases the lock, then thread B is resumed. However, B needs a period of time to be able to actually run it's task from where it was suspended. And this period of time could be relatively long compared to another...

Monday, July 23, 2012

How object in Java is initialized

1. A class is loaded when it is used for the first time, it is loaded from the class file (Ex. Student.class). If this class has a base class, the base class will be loaded before this class.  2. Next, the static initialization in the base class is performed which initialize the static fields of the base class, and then the static initialization of this (derived) class kicks in, which makes...

Monday, July 9, 2012

Sunday, July 8, 2012

Java Generics Wildcards explained

Given a generic class, like List<T>, or any generic class: When using this generic class, for example: List<Student> students = StudentFactory.getStudents(); or public class StudentList implements List<Student> {...} or List<? extends Student> students = StudentFactory.getStudents(); or List<?...