Skip to main content

Posts

Showing posts from 2013

Pipe is not always for smoking

pipes with IO & Networking Is a way of communication, connecting two ends "program to a file , program to a program ...etc".as if a person calling another between those two person there exist a pipe. For IO one end is a file, where as for networking both ends are programs basic operation open():inputStream, for opening a connection. A person dailing a number, the other end pick up close(), for closing this connection, one person hang up write(byte[] bufferToWrite,int numOfByteToWrite):numOfActuallyWriten. A person talking read(byte[] bufferToReadIn,int numOfByteToRead):numOfBytesRead. A person listening read() operation is a blocking one, so can use available() to check if in the input stream there exist any data to read before do a blocking read. can also synchronize between the two ends by adding a Buffer for file: FileInputStream --> BufferedInputStream --> DataInputStream BufferedInputStream is used to read stream of bytes, if want to distinguish &

my tailor uses Custom tag

Custom tags to avoid placing java code inside the JSP page "scripting", so that the JSP page is able to concentrate only on the presentation logic. old "classic" where a developer has to adjust the flow, from start go to ?? , will evaluate the body or not, all done depending on the return value of overridden methods . instance of tag handler is reused, can't rely on constructor to do initialization, better to use setPageContext( ). now everything is done overriding just one method. simple tag model never reuse tag handler instances. body content: in practice content is not processed at all JspWriter out=getJspContext().getOut(); out.print(" ... "); getJSPBody.invoke(null); out.print(" ... "); Modify to process body content StringWriter writer=new StringWriter(); getJSPBody.invoke( writer ); String bodyContent=writer.toString(); .tld addresses to custom tag, so how to know about explicit "within web.xml"

scattered

Blocks anonymous inner class is for overriding methods of super class. static blocks are called just once "say at class loading time". anonymous blocks are called every time a constructor is called but before it. static{ // block executed at class load time just once } { //anonymous block within class scope called before each instantiation call } Enumeration is an interface that provide an abstract mechanism for visiting elements. enum Meal{ BREAKFAST(7,30),LUNCH(1,30); private int h,m; Meal(int h, int m){ this.h=h;this.m=m}.... Polymorphism is that you declare, say, a method argument of a particular type and at run-time be able to have that argument refer to any sub-type.  void methodName( ArrayList <Animal>  list ){ list.add( new Dog() ) } what if i passed a cat list, i'll then end up having a dog in my cat list, so since at run-time JVM knows the type of arrays, but not the type of collection. < ? extends  > can't add to the

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, then move forward searching for match The last thing to try is the entire i

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++){//initialization

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( )".