Skip to main content

Posts

Showing posts from September, 2012

Erasure

Generic programming means to write code that can be reused for objects of many different types. Before javaSE 5 generics was achieved with inheritance, afterwards to implement generics the compiler applies type Erasure E rasure: "as if a correction mad by erasing" is the process of translating or rewriting code that uses generics into non-generic one, all info between angle bracket is erased "Type variables are erased & replaced by their bonding types 'Object for variables without bounds' ". NB: -bridge method to preserve polymorphism . "assume  a class or interface that extends a parameterized class, and I want to override a method that exist in the parameterized class, after erasure , the method signatures do not not match "the one in the parameterized class is replaced by their bounding type" public void setXXX(Object x) //one for parameterized class "generic class" after erasure public void setXXX(ClassType x) // m

String literal pool

I'm used to use String .format() when constructing a SQL statement, and a friend I knew likes to concatenate saying :"it's more readable to me". So String Objects are immutable, meaning that once they are created, they can't be altered. Concatenating 2 strings doesn't modify either Strings instead, it creates a new String "old ones are added to the string pool". String literals always have a reference to them in String Literal Pool, therefore not eligible for garbage collection. to concatenate use StringBuffer (Synchronized) or StringBuilder (not Synchronized) As both uses an internal array "so that new String Objects are not created". String literal Pool : String are stored in pool and before creating a new string  literals, compiler checks if such string already defined "used to optimize and save space". String literals : a sequence of characters between quotation marks.