Skip to main content

Posts

regex

Regular expressions are a kind of language within a language, designed to help programmers with their searching task. in general, a regex search runs from left to right, and once a source's character has been used in match, it can't be reused. metacharacter: \d  digit \s  white space  \w word character (letter, digit or "_") [a-f] range from a to f  [a-fA-F]  looking for the occurrences of a-f or A-f not fA combination ^ to negate the character specified nested brackets  to create a union set && to specify intersection of a set quantifiers "allows to specify number of occurrences to match against " +  one or more *  zero or more ?  zero or one greedy: read first the whole string, if no match, move backward character by character till a match is found. reluctant , however, take the opposite approach: They start at the beginning of the input string,...

including Jars

we have two tools for creating a jar within eclipse framework, one named jar and the other named runnable jar for including a jar library, "to be used within my project" copy it in the project "within the root context" right click on it --> build path -->include in build path runnable jar tool will include it automatic  jar tool include description for mainfist in mainfist.txt include it "libJar.jar" within the same context as my jar ex: Manifest-Version: 1.0 Implementation-Version: 1.0.1 Class-Path: libJar.jar Main-Class: pkg.MainClass

Collection as my bag

When a person decide to travel, he keeps thinking ! ! what will i take, which bag will suits my luggage ?? Collection is our bag, container that we staff our objects in. So which ? for an arrayList removing an element from the middle of an array is expensive "require to move up all elements beyond the removed one". linked list saved the day, since array stores object reference in consecutive memory location, linked list stores each object in a separate link, each link stores reference to next link. however, for linkedList.get(index) result in starting from the first till reach the index "much overhead with each look up, starts again from the beginning ". have to trade off However, when looking for a particular element "Search: require visiting all elements till find match" use hashMap faster but have no control over the order. 

exploring black box

Once loved watching a comic old movie, where a clause was often said:"what's in this ring box ?" accompanied with a surprising answer:"an elephant is in this ring box " :o Reflection discover whats in the black box "an object", could know what is in an object as if a mirror that reflects it content "ability to examine the run-time properties of the object " small ex wanted to return a generic list containing result of a sql query "as if hibernate" List<T>list=new ArrayList<T>(); ResultSetMetaData metadata=resultSet.getMetaData(); String[] columnName=new String[metadata.getColumnCount()]; for(int i=0;i<columnName.length;i++){//column name equivalent to object instance variable      columnName[i]=metadata.getColumnLabel(i+1); } int index=0; T t; while (resultSet.next()) {   list.add((T)cls.newInstance()); t=list.get(index); for(int i=0;i<columnName.length;i++){//initializati...

wait / notify in Object not Thread

Within object oriented, an object behaves as if a human behaving so assume you want to sleep and you don't want to trigger an alarm as it annoys you, so you ask your brother to awake you. simple its you are a thread, when telling your brother that you will get asleep "brother.wait( )", an when he awakes you "brother.notify( )".

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.