Thursday Mar 13, 2008

Boxing and unboxing

What's boxing anyway?

Every type in Java is either a reference type or a primitive type. A reference type is any class, instance, or array type. All reference types are subtypes of class Object, and any variable of reference type may be set to the value null. There are eight primitive types, and each of these has a corresponding library class of reference type. The library classes are located in the package java.lang.

Primitive : Reference Mapping
byte : Byte
short : Short
int : Integer
long : Long
float : Float
double : Double
bool : Boolean
char : Character

Conversion of a primitive type to the corresponding reference type is called boxing and conversion of the reference type to the corresponding primitive type is called unboxing.

But what's autoboxing?

Autoboxing / unboxing is the automated under the covers conversion between primitive types and their equivalent object types. For example, the conversion between an int primitive and an Integer object or between a boolean primitive and a Boolean object. This was introduced in Java 5.

// Assigning primitive type to wrapper type : Boxing
   Integer iWrapper = 10;

 // Assigning object to primitive : Unboxing
   public void intMethod(Integer iWrapper){
      int iPrimitive = iWrapper;

The basic differences between primitive and reference types

  • Primitive types are typically fast and lightweight - they have a small memory footprint. Object equivalents are heavyweight and can burden the VM if created in large numbers.
  • Primitive types are not objects - they are not polymorphic and cannot exhibit any object behavior. Object equivalents behave polymorphically (the numeric ones form an inheritance hierarchy derived from java.lang.Number) and exhibit useful behaviour - e.g. Integer.compareTo(Integer).
  • Primitive types are passed by value into methods. Object equivalents are passed by reference.
  • Primitive types are not reference counted or garbage collected. Object equivalents are.
  • Primitive types are mutable. Their object equivalent classes are all immutable.

Saturday Mar 08, 2008

On Generics

What is Generics?

Generics provides a way for you to communicate the type of a collection to the compiler, so that it can be checked. Once the compiler knows the element type of the collection, the compiler can check that you have used the collection consistently and can insert the correct casts on values being taken out of the collection.

When we declare c to be of type Collection, this tells us something about the variable c that holds true wherever and whenever it is used, and the compiler guarantees it (assuming the program compiles without warnings). A cast, on the other hand, tells us something the programmer thinks is true at a single point in the code, and the VM checks whether the programmer is right only at run time.

When was Java Generics introduced?

JDK 1.5

How is this different from C++'s template mechanism?

You might think that generics are similar, but the similarity is superficial. Generics do not generate a new class for each specialization, nor do they permit “template metaprogramming.”

Generics and subtyping

Is the following code snippet legal?

List<String> ls = new ArrayList<String>(); //1
List<Object> lo = ls; //2

Line 1 is definitely legal. But line 2 will give a compile time error.

Well, take a look at the next few lines:

lo.add(new Object()); // 3
String s = ls.get(0); // 4: attempts to assign an Object to a String!

In general, if Foo is a subtype (subclass or subinterface) of Bar, and G is some generic type declaration, it is not the case that G is a subtype of G. This is probably the hardest thing you need to learn about generics, because it goes against our deeply held intuitions.

What is the supertype of all kinds of collections?

It’s written as

(pronounced “collection of unknown”) , that is, a collection whose element type matches anything.

Is this valid?

Is the following code valid?
Collection c = new ArrayList(); //1
c.add(new Object()); //2
Line 2 will give a compile time error since we don't know what element type of c stands for, we cannot add arbitrary data to it.

Tuesday Mar 04, 2008

Java Serialization

What is serialization?

The process of saving an object's state to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.

What can I use?

The Java Serialization API.

The Rules

Rule #1: The object to be persisted must implement the Serializable interface or inherit that implementation from its object hierarchy.

To persist an object in Java, we must have a persistent object. An object is marked serializable by implementing the interface, which signifies to the underlying API that the object can be flattened into bytes (and subsequently inflated in the future).

public class PersistedClass implements Serializable

Rule #2: The object to be persisted must mark all nonserializable fields transient

On the other hand, certain system-level classes such as Thread, OutputStream and its subclasses, and Socket are not serializable. These should be marked transient since it doesn't make sense to serialize them.

transient private Thread notserialthread;

Version control Gotcha!

Imagine you have a serialized flattened object sitting in your file system for sometime. Meanwhile, you update the class file, perhaps adding a new field. What happens when you try to read in the flattened object?

An will be thrown! -- because all persistent-capable classes are automatically given a unique identifier. If the identifier of the class does not equal the identifier of the flattened object, the exception will be thrown.

If you wish to control versioning, you simply have to provide the serialVersionUID field manually and ensure it is always the same, no matter what changes you make to the classfile.

How? JDK distribution comes with a utility called serialver which returns the generated serialVersionUID. (it is just the hash code of the object by default).

Simply copy the returned line with the version ID and paste it into your code.

The version control works great as long as the changes are compatible. Compatible changes include adding or removing a method or a field. Incompatible changes include changing an object's hierarchy or removing the implementation of the Serializable interface.

Final keyword in Java

The Final keyword is used in Java on an entity that cannot be changed.

Here are a few different contexts in which final can be used:

Final class:

Meaning: A final class cannot be subclassed. Advantage: Security or efficiency. Example: java.util.System

Final method:

Meaning: The method cannot be overridden in a subclass. Advantage: Preventing unexpected behavior crucial to the functionality of the class.

Final variable:

Meaning: The variable is immutable, meaning once assigned, it cannot be reassigned. This is different from constant keyword since constant should be known at compile time and final need not be. Advantage: Optimization.

Tuesday Feb 26, 2008

JPA Query with wildcards

Here is a short tip on using LIKE expression with JPA .

What we are trying to do is get all the items that matches a pattern anywhere in their name.

In simple SQL, what I want to do is:

SELECT userName FROM Profile p WHERE p.userName LIKE %pattern%;

I'm using annotations to create a named query.

query="SELECT p FROM Profile p WHERE p.userName LIKE ?1 ORDER BY p.userName ASC")

(This will even sort the result!!)

Here's the code that demonstrates using this query:

 Query query = strategy.getNamedQuery("Profile.getUsernameWithPattern");
 query.setParameter(1, "%" + pattern + "%");        

Hope you found this tip helpful!




« February 2017