<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-895041349172269996</id><updated>2012-02-18T12:52:53.678-08:00</updated><title type='text'>Java+ Tech Shares</title><subtitle type='html'>Blog for Java + Other technical shares. Started by Deepak Singhvi.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>25</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-3687545876616947027</id><published>2009-02-15T03:31:00.000-08:00</published><updated>2009-04-19T01:45:08.563-07:00</updated><title type='text'>Normalization : 1NF, 2NF, 3NF, BCNF</title><content type='html'>&lt;span style="font-weight: bold;"&gt;Normalization:&lt;/span&gt;&lt;br /&gt;Database normalization is a process by which an existing schema is modified to bring its component tables into compliance with a series of progressive normal forms.&lt;br /&gt;&lt;br /&gt;The goal of database normalization is to ensure that every non-key column in every table is directly dependent on the key, the whole key and nothing but the key and with this goal come benefits in the form of reduced redundancies, fewer anomalies, and improved efficiencies (While normalization tends to increase the duplication of data, it does not introduce redundancy, which is unnecessary duplication.).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;About the Key&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Column(s) C is primary key for table T if:&lt;br /&gt; Property 1: All columns in T are functionally dependent on C&lt;br /&gt; Property 2: No subcollection of columns in C (assuming C is a collection of&lt;br /&gt;                            columns and not just a single column) also has Property 1&lt;br /&gt;Candidate Keys -&lt;br /&gt;    Column(s) on which all other columns in table are functionally dependent&lt;br /&gt;Alternate Keys -&lt;br /&gt;    Candidate keys not chosen as primary keys&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;First Normal Form&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The first normal form (or 1NF) requires that the values in each column of a table are atomic. By atomic we mean that there are no sets of values within a column.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Second Normal Form&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Where the First Normal Form deals with atomicity of data, the Second Normal Form (or 2NF) deals with relationships between composite key columns and non-key columns. The normal forms are progressive, so to achieve Second Normal Form, your tables must already be in First Normal Form.&lt;br /&gt;The second normal form (or 2NF) any non-key columns must depend on the entire primary key. In the case of a composite primary key, this means that a non-key column cannot depend on only part of the composite key.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Third Normal Form&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Third Normal Form (3NF) requires that all columns depend directly on the primary key. Tables violate the Third Normal Form when one column depends on another column, which in turn depends on the primary key (a transitive dependency).&lt;br /&gt;One way to identify transitive dependencies is to look at your table and see if any columns would require updating if another column in the table was updated. If such a column exists, it probably violates 3NF.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;Boyce-Codd Normal Form (BCNF)&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;When a relation has more than one candidate key, anomalies may result even though the relation is in 3NF. 3NF does not deal satisfactorily with the case of a relation with overlapping candidate keys i.e. composite candidate keys with at least one attribute in common.&lt;br /&gt;BCNF is based on the concept of a determinant. A determinant is any attribute (simple or composite) on which some other attribute is fully functionally dependent. A relation is in BCNF is, and only if, every determinant is a candidate key.&lt;br /&gt;&lt;br /&gt;Consider the following relation and determinants.&lt;br /&gt;R(a,b,c,d)&lt;br /&gt;a,c -&gt; b,d&lt;br /&gt;a,d -&gt; b&lt;br /&gt;&lt;br /&gt;To be in BCNF, all valid determinants must be a candidate key. In the relation R,   a,c-&gt;b,d is the determinate used, so the first determinate is fine.&lt;br /&gt;a,d-&gt;b suggests that a,d can be the primary key, which would determine b. However this would not determine c. This is not a candidate key, and thus R is not in BCNF.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;• Unnormalised&lt;br /&gt;Grade_report(StudNo,StudName,(Major,Advisor(CourseNo,Ctitle,InstrucName,InstructLocn,Grade)))&lt;br /&gt;&lt;br /&gt;• 1NF Remove repeating groups&lt;br /&gt;Student(StudNo,StudName)&lt;br /&gt;StudMajor(StudNo,Major,Advisor)&lt;br /&gt;StudCourse(StudNo,Major,CourseNo,&lt;br /&gt;       Ctitle,InstrucName,InstructLocn,Grade)&lt;br /&gt;&lt;br /&gt;• 2NF Remove partial key dependencies&lt;br /&gt;       Student(StudNo,StudName)&lt;br /&gt;       StudMajor(StudNo,Major,Advisor)&lt;br /&gt;       StudCourse(StudNo,Major,CourseNo,Grade)&lt;br /&gt;       Course(CourseNo,Ctitle,InstrucName,InstructLocn)&lt;br /&gt;&lt;br /&gt;• 3NF Remove transitive dependencies&lt;br /&gt;       Student(StudNo,StudName)&lt;br /&gt;       StudMajor(StudNo,Major,Advisor)&lt;br /&gt;       StudCourse(StudNo,Major,CourseNo,Grade)&lt;br /&gt;       Course(CourseNo,Ctitle,InstrucName)&lt;br /&gt;       Instructor(InstructName,InstructLocn)&lt;br /&gt;&lt;br /&gt;• BCNF Every determinant is a candidate key&lt;br /&gt;– Student : only determinant is StudNo&lt;br /&gt;– StudCourse: only determinant is StudNo,Major&lt;br /&gt;– Course: only determinant is CourseNo&lt;br /&gt;– Instructor: only determinant is InstrucName&lt;br /&gt;– StudMajor: the determinants are&lt;br /&gt;                        StudNo,Major, or&lt;br /&gt;                        Advisor&lt;br /&gt;Only StudNo,Major is a candidate key.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;• BCNF&lt;br /&gt;&lt;br /&gt;       Student(StudNo,StudName)&lt;br /&gt;       StudCourse(StudNo,Major,CourseNo,Grade)&lt;br /&gt;       Course(CourseNo,Ctitle,InstrucName)&lt;br /&gt;       Instructor(InstructName,InstructLocn)&lt;br /&gt;       StudMajor(StudNo,Advisor)&lt;br /&gt;       Adviser(Adviser,Major)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;A complete normalization of tables is desirable, but you may find that in practice that full normalization can introduce complexity to your design and application. More tables often means more JOIN operations, and in most database management systems (DBMSs) such JOIN operations can be costly, leading to decreased performance. The key lies in finding a balance where the first three normal forms are generally met without creating an exceedingly complicated schema.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=13&amp;l=st1&amp;mode=software&amp;search=Java&amp;fc1=000000&amp;lt1=&amp;lc1=3366FF&amp;bg1=FFFFFF&amp;f=ifr" marginwidth="0" marginheight="0" width="468" height="60" border="0" frameborder="0" style="border:none;" scrolling="no"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-3687545876616947027?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/3687545876616947027/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=3687545876616947027' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3687545876616947027'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3687545876616947027'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2009/02/normalization-about-key-columns-c-is.html' title='Normalization : 1NF, 2NF, 3NF, BCNF'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-316705155623830481</id><published>2008-02-21T05:43:00.000-08:00</published><updated>2009-01-26T07:46:44.444-08:00</updated><title type='text'>Difference between Java Beans and Enterprise Java Beans</title><content type='html'>A &lt;strong&gt;Java Bean &lt;/strong&gt;is a software component written in the Java programming language that conforms to the JavaBeans component specification. The JavaBeans APIs became part of the "core" Java APIs as of the 1.1 release of the JDK. The JavaBeans specification defines a Java-based software component model that adds a number of features to the Java programming language. Some of these features include:&lt;br /&gt;&lt;br /&gt;    introspection &lt;br /&gt;    customization &lt;br /&gt;    events &lt;br /&gt;    properties &lt;br /&gt;    persistence &lt;br /&gt;&lt;br /&gt;Java Beans is a specification developed by Sun Microsystems that defines how Java objects interact. An object that conforms to this specification is called a JavaBean, and is similar to an ActiveX control. It can be used by any application that understands the JavaBeans format. &lt;br /&gt;The principal difference between ActiveX controls and JavaBeans are that ActiveX controls can be developed in any programming language but executed only on a Windows platform, whereas JavaBeans can be developed only in Java, but can run on any platform. &lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;Enterprise JavaBeans (EJBs)&lt;/strong&gt; are Java-based software components that are built to comply with Java's EJB specification and run inside of an EJB container supplied by a J2EE provider. An EJB container provides distributed application functionality such as transaction support, persistence and lifecycle management for the EJBs.&lt;br /&gt;&lt;br /&gt;An Enterprise JavaBeansTM (EJB) component, or enterprise bean, is a body of code having fields and methods to implement modules of business logic. You can think of an enterprise bean as a building block that can be used alone or with other enterprise beans to execute business logic on the Java EE server.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=B00007BGV7&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-316705155623830481?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/316705155623830481/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=316705155623830481' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/316705155623830481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/316705155623830481'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2008/02/difference-between-java-beans-and.html' title='Difference between Java Beans and Enterprise Java Beans'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-5722849779480032635</id><published>2008-02-16T22:13:00.001-08:00</published><updated>2008-02-16T23:20:08.292-08:00</updated><title type='text'>Aspect-Oriented Programming and Introduction to AspectJ</title><content type='html'>&lt;div align="left"&gt;&lt;br /&gt;&lt;strong&gt;Aspect-Oriented ProgrammingIntroduction to AspectJ&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;Programming paradigms&lt;br /&gt;• Procedural programming&lt;br /&gt;– Executing a set of commands in a given sequence&lt;br /&gt;– Fortran, C, Cobol&lt;br /&gt;• Functional programming&lt;br /&gt;– Evaluating a function defined in terms of other functions&lt;br /&gt;– Lisp, ML, OCaml&lt;br /&gt;• Logic programming&lt;br /&gt;– Proving a theorem by finding values for the free variables&lt;br /&gt;– Prolog&lt;br /&gt;• Object-oriented programming (OOP)&lt;br /&gt;– Organizing a set of objects, each with its own set of responsibilities&lt;br /&gt;– Smalltalk, Java, C++ (to some extent)&lt;br /&gt;• Aspect-oriented programming (AOP)&lt;br /&gt;– Executing code whenever a program shows certain behaviors&lt;br /&gt;– AspectJ (a Java extension)&lt;br /&gt;– Does not replace O-O programming, but rather complements it&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="left"&gt;&lt;strong&gt;Introduction&lt;br /&gt;&lt;/strong&gt;• Currently, the dominant programming paradigm is object-oriented programming that:&lt;br /&gt;• Object orientation is a clever idea, but has certain limitations&lt;br /&gt;• has been presented as a technology that can fundamentally aid software engineering&lt;br /&gt;• is reflected in the entire spectrum of current software development methodologies and tools&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Introduction AOP&lt;/strong&gt;&lt;br /&gt;• A new programming technique called aspect-oriented programming (AOP):&lt;br /&gt;- makes it possible to clearly express those programs that OOP fail to support&lt;br /&gt;- enables the modularization of crosscutting concerns by supporting a new unit of &lt;/div&gt;&lt;br /&gt;&lt;div align="left"&gt;software modularity – aspects – that provide encapsulation for crosscutting concerns&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;What are aspects?&lt;br /&gt;&lt;/strong&gt;• The current working definition is (May 99, Gregor Kiczales):&lt;br /&gt;- modular units that cross-cut the structure of other modular units&lt;br /&gt;- units that is defined in terms of partial information from other units&lt;br /&gt;- exist in both design and implementation&lt;br /&gt;&lt;br /&gt;Aspect: A distinct feature or element in a problem&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Concerns&lt;br /&gt;&lt;/strong&gt;• AOP is based on the idea that computer systems are better programmed by separately specifying the various concerns of a system&lt;br /&gt;• Separation of concerns is an important software engineering principle guiding all stage of a software development methodology&lt;br /&gt;• Concerns:&lt;br /&gt;are properties or areas of interest&lt;br /&gt;can range from high-level notion to low level-notion&lt;br /&gt;can be functional or nonfunctional (systemic)&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The problem&lt;/strong&gt;&lt;br /&gt;• Some programming tasks cannot be neatly encapsulated in objects, but must be scattered throughout the code&lt;br /&gt;• Examples:&lt;br /&gt;– Logging (tracking program behavior to a file)&lt;br /&gt;– Profiling (determining where a program spends its time)&lt;br /&gt;– Tracing (determining what methods are called when)&lt;br /&gt;– Session tracking, session expiration&lt;br /&gt;– Special security management&lt;br /&gt;• The result is crosscuting code--the necessary code “cuts across” many different classes and methods&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Example&lt;br /&gt;class Fraction { &lt;/div&gt;&lt;div align="left"&gt;int numerator; &lt;/div&gt;&lt;div align="left"&gt;int denominator; &lt;/div&gt;&lt;div align="left"&gt;... &lt;/div&gt;&lt;div align="left"&gt;public Fraction multiply(Fraction that) { &lt;/div&gt;&lt;div align="left"&gt;traceEnter("multiply", new Object[] {that}); &lt;/div&gt;&lt;div align="left"&gt;Fraction result = new Fraction( this.numerator * that.numerator, this.denominator * that.denominator); &lt;/div&gt;&lt;div align="left"&gt;result = result.reduceToLowestTerms(); &lt;/div&gt;&lt;div align="left"&gt;traceExit("multiply", result); &lt;/div&gt;&lt;div align="left"&gt;return result; &lt;/div&gt;&lt;div align="left"&gt;} &lt;/div&gt;&lt;div align="left"&gt;&lt;/div&gt;&lt;div align="left"&gt;...&lt;/div&gt;&lt;div align="left"&gt;}&lt;br /&gt;Now imagine similar code in every method you might want to trace&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="left"&gt;&lt;strong&gt;Consequences of crosscutting code&lt;br /&gt;&lt;/strong&gt;• Redundant code&lt;br /&gt;– Same fragment of code in many places&lt;br /&gt;• Difficult to reason about&lt;br /&gt;– Non-explicit structure&lt;br /&gt;– The big picture of the tangling isn’t clear&lt;br /&gt;• Difficult to change&lt;br /&gt;– Have to find all the code involved...&lt;br /&gt;– ...and be sure to change it consistently&lt;br /&gt;– ...and be sure not to break it by accident&lt;br /&gt;• Inefficient when crosscuting code is not needed&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;AspectJTM&lt;/strong&gt;&lt;br /&gt;• AspectJ is a small, well-integrated extension to Java&lt;br /&gt;– Based on the 1997 PhD thesis by Christina Lopes, A Language Framework for Distributed Programming&lt;br /&gt;• AspectJ modularizes crosscutting concerns&lt;br /&gt;– That is, code for one aspect of the program (such as tracing) is collected together in one place&lt;br /&gt;• The AspectJ compiler is free and open source&lt;br /&gt;• AspectJ works with JBuilder, Forté, Eclipse, probably others&lt;br /&gt;• Best online writeup: http://www.eclipse.org/aspectj/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Terminology&lt;br /&gt;&lt;/strong&gt;• A join point is a well-defined point in the program flow&lt;br /&gt;• A pointcut is a group of join points&lt;br /&gt;• Advice is code that is executed at a pointcut&lt;br /&gt;• Introduction modifies the members of a class and the relationships between classes&lt;br /&gt;• An aspect is a module for handling crosscutting concerns&lt;br /&gt;– Aspects are defined in terms of pointcuts, advice, and introduction&lt;br /&gt;– Aspects are reusable and inheritable&lt;br /&gt;• Each of these terms will be discussed in greater detail&lt;br /&gt;&lt;strong&gt;The Figure Element example&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_JLUn9JWaGS8/R7farFIP4qI/AAAAAAAAABs/l1ry9TM4Mss/s1600-h/Picture3.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5167839531076936354" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_JLUn9JWaGS8/R7farFIP4qI/AAAAAAAAABs/l1ry9TM4Mss/s400/Picture3.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using Objects&lt;br /&gt;&lt;/strong&gt;• Expressive&lt;br /&gt;– What is going on its clear&lt;br /&gt;• Abstraction&lt;br /&gt;– Focus on more or less detail&lt;br /&gt;• Structure &amp;amp; Modularity&lt;br /&gt;– What goes where&lt;br /&gt;– How parts fit together&lt;br /&gt;–Things are pretty clear, you know every thing about it. If you wanna add new shapes(FigureElement) that also you can add.&lt;br /&gt;–So now lets make it probelamatic… how… Simple Observer Pattern&lt;br /&gt;–If there is any change in the shape…. Drawing Object should be notified.&lt;br /&gt;–So what is this , good design but a worst code.&lt;br /&gt;–So what can we do , we can use aspects , we can have a aspect for observer pattern and which will be weaved with the existing codess and proviides use the way to work with new requirements.&lt;br /&gt;–So we have Good Design and Good Code.&lt;br /&gt;–Lets see how to do that with the help of aspects.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Other Aspects&lt;/strong&gt;&lt;br /&gt;• Security&lt;br /&gt;– Pointcut for when checking happens&lt;br /&gt;• Optimization&lt;br /&gt;• Distribution&lt;br /&gt;• Synchronization&lt;br /&gt;• Persistance&lt;br /&gt;• And ofcourse Logging/Tracing&lt;br /&gt;• And very many application –specific aspects&lt;br /&gt;- i.e EnsureLiveness&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Example I&lt;br /&gt;&lt;/strong&gt;• A pointcut named move that chooses various method calls:&lt;/div&gt;&lt;div align="left"&gt;pointcut move(): call(void FigureElement.setXY(int,int))&amp;#124;&amp;#124;&lt;br /&gt;call(void Point.setX(int))&amp;#124;&amp;#124;&lt;br /&gt;call(void Point.setY(int))&amp;#124;&amp;#124;&lt;br /&gt;&lt;/div&gt;&lt;div align="left"&gt;call(void Line.setP1(Point))&amp;#124;&amp;#124;&lt;br /&gt;&lt;/div&gt;&lt;div align="left"&gt;call(void Line.setP2(Point));&amp;#124;&amp;#124;&lt;br /&gt;&lt;br /&gt;• Advice (code) that runs before the move pointcut:&lt;br /&gt;before(): move() { &lt;/div&gt;&lt;div align="left"&gt;System.out.println("About to move");&lt;/div&gt;&lt;div align="left"&gt;}&lt;br /&gt;• Advice that runs after the move pointcut:&lt;br /&gt;&lt;/div&gt;&lt;div align="left"&gt;after(): move() { &lt;/div&gt;&lt;div align="left"&gt;System.out.println("Just successfully moved");&lt;/div&gt;&lt;div align="left"&gt;}&lt;br /&gt;Join points&lt;br /&gt;• A join point is a well-defined point in the program flow&lt;br /&gt;– We want to execute some code (“advice”) each time a join point is reached&lt;br /&gt;– AspectJ provides a syntax for indicating these join points “from outside” the actual code&lt;br /&gt;• A join point is a point in the program flow “where something happens”&lt;br /&gt;– Examples:&lt;br /&gt;• When a method is called&lt;br /&gt;• When an exception is thrown&lt;br /&gt;• When a variable is accessed&lt;br /&gt;&lt;br /&gt;AspectJ supports 11 different kinds of join points.These are the method call, method execution, constructor call, constructor execution, field get, field set, pre initialization, initialization, static initialization, handler, and advice execution join points.&lt;br /&gt;Pointcuts&lt;br /&gt;• Pointcut definitions consist of a left-hand side and a right-hand side, separated by a colon&lt;br /&gt;– The left-hand side consists of the pointcut name and the pointcut parameters (i.e. the data available when the events happen)&lt;br /&gt;– The right-hand side consists of the pointcut itself&lt;br /&gt;• Example pointcut:pointcut setter(): call(void setX(int));&lt;br /&gt;– The name of this pointcut is setter&lt;br /&gt;– The pointcut has no parameters&lt;br /&gt;– The pointcut itself is call(void setX(int))&lt;br /&gt;– The pointcut refers to any time the void setX(int) method is called&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;strong&gt;Example pointcut designators I&lt;/strong&gt;&lt;br /&gt;• When a particular method body executes:&lt;br /&gt;– execution(void Point.setX(int))&lt;br /&gt;• When a method is called:&lt;br /&gt;– call(void Point.setX(int))&lt;br /&gt;• When an exception handler executes:&lt;br /&gt;– handler(ArrayOutOfBoundsException)&lt;br /&gt;• When the object currently executing (i.e. this) is of type SomeType:&lt;br /&gt;– this(SomeType)&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="left"&gt;&lt;strong&gt;Example pointcut designators II&lt;br /&gt;&lt;/strong&gt;• When the target object is of type SomeType&lt;br /&gt;– target(SomeType)&lt;br /&gt;• When the executing code belongs to class MyClass&lt;br /&gt;– within(MyClass)&lt;br /&gt;• When the join point is in the control flow of a call to a Test's no-argument main method&lt;br /&gt;– cflow(call(void Test.main()))&lt;br /&gt;Pointcut designator wildcards&lt;br /&gt;• It is possible to use wildcards to declare pointcuts:&lt;br /&gt;– execution(* *(..))&lt;br /&gt;• Chooses the execution of any method regardless of return or parameter types&lt;br /&gt;– call(* set(..))&lt;br /&gt;• Chooses the call to any method named set regardless of return or parameter type&lt;br /&gt;• In case of overloading there may be more than one such set method; this pointcut picks out calls to all of them&lt;br /&gt;Pointcut designators based on types&lt;br /&gt;• You can select elements based on types. For example,&lt;br /&gt;– execution(int *())&lt;br /&gt;• Chooses the execution of any method with no parameters that returns an int&lt;br /&gt;– call(* setY(long))&lt;br /&gt;• Chooses the call to any setY method that takes a long as an argument, regardless of return type or declaring type&lt;br /&gt;– call(* Point.setY(int))&lt;br /&gt;• Chooses the call to any of Point’s setY methods that take an int as an argument, regardless of return type&lt;br /&gt;– call(*.new(int, int))&lt;br /&gt;• Chooses the call to any classes’ constructor, so long as it takes exactly two ints as arguments&lt;br /&gt;Pointcut designator composition&lt;br /&gt;• Pointcuts compose through the operations or (“”), and (“&amp;amp;&amp;amp;”) and not (“!”)&lt;br /&gt;• Examples:&lt;br /&gt;– target(Point) &amp;amp;&amp;amp; call(int *())&lt;br /&gt;• Chooses any call to an int method with no arguments on an instance of Point, regardless of its name&lt;br /&gt;– call(* *(..)) &amp;amp;&amp;amp; (within(Line) within(Point))&lt;br /&gt;• Chooses any call to any method where the call is made from the code in Point’s or Line’s type declaration&lt;br /&gt;– within(*) &amp;amp;&amp;amp; execution(*.new(int))&lt;br /&gt;• Chooses the execution of any constructor taking exactly one int argument, regardless of where the call is made from&lt;br /&gt;– !this(Point) &amp;amp;&amp;amp; call(int *(..))&lt;br /&gt;• Chooses any method call to an int method when the executing object is any type except Point&lt;br /&gt;&lt;strong&gt;Pointcut designators based on modifiers&lt;br /&gt;&lt;/strong&gt;• call(public * *(..))&lt;br /&gt;– Chooses any call to a public method&lt;br /&gt;• execution(!static * *(..))&lt;br /&gt;– Chooses any execution of a non-static method&lt;br /&gt;• execution(public !static * *(..))&lt;br /&gt;– Chooses any execution of a public, non-static method&lt;br /&gt;• Pointcut designators can be based on interfaces as well as on classes&lt;br /&gt;Example I, repeated&lt;br /&gt;• A pointcut named move that chooses various method calls:&lt;br /&gt;pointcut move(): call(void FigureElement.setXY(int,int))&lt;br /&gt;call (void Point.setX(int)) &lt;/div&gt;&lt;div align="left"&gt;call(void Point.setY(int)) &lt;/div&gt;&lt;div align="left"&gt;call(void Line.setP1(Point)) &lt;/div&gt;&lt;div align="left"&gt;call(void Line.setP2(Point));&lt;br /&gt;&lt;br /&gt;• Advice (code) that runs before the move pointcut:&lt;br /&gt;before(): move() { &lt;/div&gt;&lt;div align="left"&gt;System.out.println("About to move");&lt;/div&gt;&lt;div align="left"&gt;}&lt;br /&gt;&lt;br /&gt;• Advice that runs after the move pointcut:&lt;br /&gt;after(): move() { &lt;/div&gt;&lt;div align="left"&gt;System.out.println("Just successfully moved");&lt;/div&gt;&lt;div align="left"&gt;}&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Kinds of advice&lt;/strong&gt;•&lt;br /&gt;AspectJ has several kinds of advice; here are some of them:&lt;br /&gt;– Before advice runs as a join point is reached, before the program proceeds with the join point&lt;br /&gt;– After advice on a particular join point runs after the program proceeds with that join point&lt;br /&gt;• after returning advice is executed after a method returns normally&lt;br /&gt;• after throwing advice is executed after a method returns by throwing an exception&lt;br /&gt;• after advice is executed after a method returns, regardless of whether it returns normally or by throwing an exception&lt;br /&gt;– Around advice on a join point runs as the join point is reached, and has explicit control over whether the program proceeds with the join point&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Example II, with parameters&lt;br /&gt;&lt;/strong&gt;• You can access the context of the join point:&lt;br /&gt;• pointcut setXY(FigureElement fe, int x, int y): call(void FigureElement.setXY(int, int)) &amp;amp;&amp;amp; target(fe) &amp;amp;&amp;amp; args(x, y);&lt;br /&gt;• after(FigureElement fe, int x, int y) returning: setXY(fe, x, y) { System.out.println(fe + " moved to (" + x + ", " + y + ").");}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Introduction &lt;/strong&gt;&lt;br /&gt;• An introduction is a member of an aspect, but it defines or modifies a member of type (class). With introduction we can&lt;br /&gt;– add another methods to an existing class&lt;br /&gt;– add fields to an existing class&lt;br /&gt;– extend an existing class with another&lt;br /&gt;– implement an interface in an existing class&lt;br /&gt;– convert checked exceptions into unchecked exceptions&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Example introduction&lt;br /&gt;&lt;/strong&gt;public aspect CloneSimpleClass {&lt;br /&gt;declare parents: SimpleClass2 implements Cloneable;&lt;br /&gt;declare soft: CloneNotSupportedException: execution(Object clone());&lt;br /&gt;public Object SimpleClass2.clone(){ return super.clone();}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div align="left"&gt;/*&lt;br /&gt;* 1. Class which does not implement CloneNotSupportedException, expected output is&lt;br /&gt;* sucessful running of the method calls because we have aspect for cloning.&lt;br /&gt;*/&lt;br /&gt;public class SimpleClass2 {&lt;br /&gt;private String name=null;&lt;br /&gt;public SimpleClass2(String s){&lt;br /&gt;name = s;&lt;br /&gt;}&lt;br /&gt;&lt;/div&gt;&lt;div align="left"&gt;&lt;/div&gt;&lt;div align="left"&gt;public static void main(String[] args) {&lt;br /&gt;SimpleClass2 ref1 = new SimpleClass2("Demo");&lt;br /&gt;ref1.funtion();&lt;br /&gt;try {&lt;br /&gt;SimpleClass2 ref2 = (SimpleClass2) ref1.clone();&lt;br /&gt;ref2.funtion();&lt;br /&gt;} catch (Exception e) {&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;private void funtion() {&lt;br /&gt;System.out.println("Name is "+name);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;So what this does, it adds a functionality for cloning to a non&lt;br /&gt;Cloneable class. Deciding about the feature enancement&lt;br /&gt;during runtime …. Amazing capability in the product….&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="left"&gt;&lt;strong&gt;Approximate syntax&lt;/strong&gt;&lt;br /&gt;• An aspect is: aspect nameOfAspect { body }&lt;br /&gt;– An aspect contains introductions, pointcuts, and advice&lt;br /&gt;• A pointcut designator is: when(signature)&lt;br /&gt;– The signature includes the return type&lt;br /&gt;– The “when” is call, handler, execution, etc.&lt;br /&gt;• A named pointcut designator is: name(parameters): pointcutDesignator&lt;br /&gt;• Advice is: adviceType(parameters): pointcutDesignator { body }&lt;br /&gt;• Introductions are basically like normal Java code&lt;br /&gt;&lt;strong&gt;Example aspect I&lt;br /&gt;&lt;/strong&gt;• aspect PointWatching {&lt;br /&gt;private Vector Point.watchers = new Vector();&lt;br /&gt;public static void addWatcher(Point p, Screen s) { p.Watchers.add(s); }&lt;br /&gt;public static void removeWatcher(Point p, Screen s) { p.Watchers.remove(s); }&lt;br /&gt;static void updateWatcher(Point p, Screen s) { s.display(p); }&lt;br /&gt;&lt;br /&gt;pointcut changes(Point p): target(p) &amp;amp;&amp;amp; call(void Point.set*(int));&lt;br /&gt;after(Point p): changes(p) { &lt;/div&gt;&lt;div align="left"&gt;Iterator iter = p.Watchers.iterator(); &lt;/div&gt;&lt;div align="left"&gt;while ( iter.hasNext() ) { &lt;/div&gt;&lt;div align="left"&gt;updateWatcher(p, (Screen)iter.next()); &lt;/div&gt;&lt;div align="left"&gt;} }}&lt;br /&gt;&lt;br /&gt;How to identify which aspect is working&lt;br /&gt;&lt;a href="http://bp2.blogger.com/_JLUn9JWaGS8/R7fcUlIP4rI/AAAAAAAAAB0/Z3h0rx091MM/s1600-h/Picture1.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5167841343553135282" style="CURSOR: hand" alt="" src="http://bp2.blogger.com/_JLUn9JWaGS8/R7fcUlIP4rI/AAAAAAAAAB0/Z3h0rx091MM/s400/Picture1.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;• In case of Introduction, which we applied to SimnpleClass2, aspect scope is for the class&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_JLUn9JWaGS8/R7fc3FIP4sI/AAAAAAAAAB8/UM6IfkPpfuw/s1600-h/Picture2.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5167841936258622146" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_JLUn9JWaGS8/R7fc3FIP4sI/AAAAAAAAAB8/UM6IfkPpfuw/s400/Picture2.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;The role of aspects in software design&lt;/strong&gt;&lt;br /&gt;• AOP aims at providing better means of addressing the well-known problem of separation of concerns&lt;br /&gt;• Three basic approaches to addressing the process of separation of concerns:&lt;br /&gt;&lt;u&gt;Language-based approach&lt;br /&gt;&lt;/u&gt;• It is based on the definition of a set of language constructs&lt;br /&gt;• Relevant concerns are identified at the problem domain and translated to aspectual construct&lt;br /&gt;• The final application is obtained by weaving the primary structure with the crosscutting aspects&lt;br /&gt;&lt;u&gt;Framework-based approach&lt;br /&gt;&lt;/u&gt;• Provides more flexible constructs&lt;br /&gt;• Concerns are materialized as aspectual classes at the framework level&lt;br /&gt;• Developers can customize these aspects using the mechanism supported by the framework&lt;br /&gt;• These types of framework are known as AO frameworks (explicitly engineers concerns)&lt;br /&gt;&lt;u&gt;Architecture-oriented approach&lt;br /&gt;&lt;/u&gt;• Early identification of concerns using architectural organizational models&lt;br /&gt;• Architectural view-point involves a higher level of abstraction than the previous approaches&lt;br /&gt;• It typically comprises two stages&lt;br /&gt;Architecture-oriented approach&lt;br /&gt;• First, developers should determine the problem architecture&lt;br /&gt;• Then, the approach enables several kinds of aspect materialization through different frameworks&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;div align="left"&gt;&lt;strong&gt;Concluding remarks&lt;br /&gt;&lt;/strong&gt;• Aspect-oriented programming (AOP) is a new paradigm--a new way to think about programming&lt;br /&gt;• AOP is somewhat similar to event handling, where the “events” are defined outside the code itself&lt;br /&gt;• AspectJ is not itself a complete programming language, but an adjunct to Java&lt;br /&gt;• AspectJ does not add new capabilities to what Java can do, but adds new ways of modularizing the code&lt;br /&gt;• AspectJ is free, open source software&lt;br /&gt;• AspectJ based on Java Features and which includes&lt;br /&gt;– Annotations&lt;br /&gt;– Generics&lt;br /&gt;– Enumerated Types&lt;br /&gt;– AutoBoxing and UnBoxing&lt;br /&gt;– New Reflection Interfaces&lt;br /&gt;– Load Time Weaving &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Will be adding few exampls ............. shortly..........&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-5722849779480032635?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/5722849779480032635/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=5722849779480032635' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/5722849779480032635'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/5722849779480032635'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2008/02/aspect-oriented-programming-and.html' title='Aspect-Oriented Programming and Introduction to AspectJ'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_JLUn9JWaGS8/R7farFIP4qI/AAAAAAAAABs/l1ry9TM4Mss/s72-c/Picture3.gif' height='72' width='72'/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-8494021175769756411</id><published>2008-02-03T00:06:00.000-08:00</published><updated>2009-04-19T01:41:43.538-07:00</updated><title type='text'>Download Plugins for eclipse..... very easy</title><content type='html'>Just select any required plugin and click on download....&lt;br /&gt;&lt;br /&gt;&lt;a href="http://ondemand.yoxos.com/geteclipse/start"&gt;http://ondemand.yoxos.com/geteclipse/start&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=16&amp;l=st1&amp;mode=software&amp;search=Java&amp;fc1=000000&amp;lt1=&amp;lc1=3366FF&amp;bg1=FFFFFF&amp;f=ifr" marginwidth="0" marginheight="0" width="468" height="336" border="0" frameborder="0" style="border:none;" scrolling="no"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-8494021175769756411?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/8494021175769756411/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=8494021175769756411' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8494021175769756411'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8494021175769756411'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2008/02/download-plugins-for-eclipse-very-easy.html' title='Download Plugins for eclipse..... very easy'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-3873351358003188670</id><published>2008-01-19T00:18:00.000-08:00</published><updated>2008-01-20T00:34:23.324-08:00</updated><title type='text'>Java Tips for ...novice programmers...</title><content type='html'>&lt;span style="color:#660000;"&gt;&lt;strong&gt;Object-Oriented Programming&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;• An abstract method cannot (obviously) be final.&lt;br /&gt;• An abstract method cannot be static because static methods cannot be&lt;br /&gt;overridden.&lt;br /&gt;• An instance method can be both protected and abstract. A static method&lt;br /&gt;can be protected.&lt;br /&gt;• The JVM does not call an object’s constructor when you clone the object.&lt;br /&gt;• Classes can be modified from their default state using any of the three&lt;br /&gt;keywords: public, abstract, and final. So, can’t have a static class,&lt;br /&gt;only static methods.&lt;br /&gt;• A final variable is a constant, and a final method cannot be overridden.&lt;br /&gt;• A call to this in a constructor must also be on the first line.&lt;br /&gt;Note: can’t have an explicit call to super followed by a call to this&lt;br /&gt;in a constructor - only one direct call to another constructor is allowed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#660000;"&gt;&lt;strong&gt;Memory and Garbage Collection&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;• Can’t predict when garbage collection will occur, but it does run&lt;br /&gt;whenever memory gets low.&lt;br /&gt;• If you want to perform some task when your object is about to be&lt;br /&gt;garbage collected, you can override the java.lang.Object method called&lt;br /&gt;finalize(). This method is declared as protected, does not return a value,&lt;br /&gt;and throws a Throwable object, i.e. protected void finalize() &lt;span style="font-size:0;"&gt;&lt;/span&gt;throws Throwable.&lt;br /&gt;• Always invoke the superclass’s finalize() method if you ove&lt;span style="font-size:0;"&gt;&lt;/span&gt;rride finalize().&lt;br /&gt;• The JVM only invokes finalize() once per object. Since this is the case,&lt;br /&gt;do not resurrect an object in finalize as when the object is finalized&lt;br /&gt;again its finalize() method will not be called. Instead you should create a&lt;br /&gt;clone of the object if you must bring the object back to life.&lt;br /&gt;• Remember that Java passes method parameters by value and not by&lt;br /&gt;reference. Obviously then, anything that happens to a primitive data&lt;br /&gt;type inside a method does not affect the original value in the calling code.&lt;br /&gt;Also, any reassignment of object references inside a method has no effect&lt;br /&gt;on the objects passed in.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#660000;"&gt;&lt;strong&gt;Exceptions&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;• Invoking a method which declares it throws exceptions is not&lt;br /&gt;possible unless either the code is placed in a try-catch, or the calling&lt;br /&gt;method declares that it throws the exceptions, i.e. checked&lt;br /&gt;exceptions must be caught or rethrown. If the try-catch&lt;br /&gt;approach is used, then the try-catch must cope with all of the&lt;br /&gt;exceptions which a method declares it throws.&lt;br /&gt;• RuntimeException and its subclasses are unchecked exceptions.&lt;br /&gt;• Unchecked exceptions do not have to be caught.&lt;br /&gt;• All Errors are unchecked.&lt;br /&gt;• You should never throw an unchecked exception in your own code, even&lt;br /&gt;though the code will compile.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#993300;"&gt;Methods&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;• A native method does not have a body, or even a set of braces,&lt;br /&gt;e.g. public native void method();&lt;br /&gt;• A native method cannot be abstract.&lt;br /&gt;• A native method can throw exceptions.&lt;br /&gt;• A subclass may make an inherited method synchronized, or it may&lt;br /&gt;leave offthe synchronized keyword so that its version is not synchronized.&lt;br /&gt;If a methodin a subclass is not synchronized but the method in the&lt;br /&gt;superclass is, the threadobtains the monitor for the object when it enters&lt;br /&gt;the superclass’s method.• You cannot make a method in a subclass more&lt;br /&gt;private than it is defined in thesuperclass, ‘though you can make it more public.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#993300;"&gt;Threads&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;• A Java program runs until the only threads left running are daemon threads.&lt;br /&gt;• A Thread can be set as a user or daemon thread when it is created.&lt;br /&gt;• In a standalone program, your class runs until your main() method&lt;br /&gt;exists - unless your main() method creates more threads.&lt;br /&gt;• You can initiate your own thread of execution by creating a Thread&lt;br /&gt;object, invoking its start() method, and providing the behaviour that&lt;br /&gt;tells the thread what to do. The thread will run until its run() method&lt;br /&gt;exists, after which it will come to a halt - thus ending its life cycle.&lt;br /&gt;• The Thread class, by default, doesn’t provide any behaviour for run().&lt;br /&gt;• A thread has a life cycle. Creating a new Thread instance puts the thread&lt;br /&gt;into the "new thread" state. When the start() method is invoked, the&lt;br /&gt;thread is then "alive" and "runnable". A thread at this point will repond&lt;br /&gt;to the method isAlive () by returning true.&lt;br /&gt;• The thread will continue to return true to isAlive() until it is "dead",&lt;br /&gt;no matter whether it is "runnable" or "not runnable".&lt;br /&gt;• There are 3 types of code that can be synchronized: class methods, i&lt;br /&gt;nstance methods, any block of code within a method.&lt;br /&gt;• Variables cannot take the synchronized keyword.&lt;br /&gt;• Synchronization stays in effect if you enter a synchronized method&lt;br /&gt;and call out to a non-synchronized method. The thread only gives&lt;br /&gt;up the monitor after the synchronized method returns.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#993300;"&gt;Inner Class&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;• If you define an inner class at the same level as the enclosing class’&lt;br /&gt;instance variables, the inner class can access those instance variables -&lt;br /&gt;no matter what their access control.&lt;br /&gt;• If you define an inner class within a method, the inner class can&lt;br /&gt;access the enclosing class’ instance variables and also the local&lt;br /&gt;variables and parameter for that method.&lt;br /&gt;• If you do reference local variables or parameters from an inner&lt;br /&gt;class, those variables or parameters must be declared as final to&lt;br /&gt;help guarantee data integrity.&lt;br /&gt;• You can also define an anonymous class - a class without a name.&lt;br /&gt;• If you’d like to refer to the current instance of the enclosing class,&lt;br /&gt;you can write EnclosingClassName.this.&lt;br /&gt;• If you need to refer to the inner class using a fully qualified name,&lt;br /&gt;you can write EnclosingClassName.InnerClassName.&lt;br /&gt;• If your inner class is not defined as static you can only create new&lt;br /&gt;instances of this class from a non-static method.&lt;br /&gt;• Anonymous classes cannot have const&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#993300;"&gt;Serializing Objects&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;•It is now possible to read and write objects as well as primitive data&lt;br /&gt;types, using classes that implement ObjectInput and ObjectOutput.&lt;br /&gt;These two interfaces extend DataInput and DataOutput to read or&lt;br /&gt;write an object. ObjectInputStream and ObjectOutputStream&lt;br /&gt;implement these interfaces.&lt;br /&gt;• If a class implements the Serializable interface, then its public and&lt;br /&gt;protected instance variables will be read from and written to the&lt;br /&gt;stream automatically when you use ObjectInputStream and&lt;br /&gt;ObjectOutputStream.&lt;br /&gt;• If an instance variable refers to another object, it will also be&lt;br /&gt;read or written, and this continues recursively.&lt;br /&gt;• If the referenced object does not implement the Serializable&lt;br /&gt;interface, Java will throw a NotSerializableException.&lt;br /&gt;• The Serializable interface serves only to identify those instances&lt;br /&gt;of a particular class that can be read from and written to a stream.&lt;br /&gt;It does not actually define any methods that you must implement.&lt;br /&gt;• If you create your own class and want to keep certain data from&lt;br /&gt;being read or written, you can declare that instance variable&lt;br /&gt;using the transient keyword. Java skips any variable declared as&lt;br /&gt;transient when it reads and writes the object following the Serializable&lt;br /&gt;protocol.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="color:#993300;"&gt;Reflection&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;• Using the Reflection API, a program can determine a class’ accessible&lt;br /&gt;fields, methods and constructors at runtime.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;will be adding moreeeeeeeeeeeee later, guys....... you are also free to add comments on this and more points........&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-3873351358003188670?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/3873351358003188670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=3873351358003188670' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3873351358003188670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3873351358003188670'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2008/01/java-tips-for-people-new-to-java.html' title='Java Tips for ...novice programmers...'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-2046869519596873275</id><published>2007-12-14T04:59:00.000-08:00</published><updated>2009-04-19T01:47:24.963-07:00</updated><title type='text'>MVC and Swing</title><content type='html'>The Model-View-Controller (MVC) architecture factors function code from the GUI design using a controller module. The controller module ties event listeners in the view module to their actions in the model module. Good programming practice implies private properties with public accessor methods for those needing access from outside their container object. These three object modules can be designed at different times by different programmers. &lt;br /&gt;&lt;br /&gt;The well-known MVC paradigm is generally recommended as the fundamental architecture for GUI development. Many variations of the pattern are available, like MVC++, HMVC (Hierarchical MVC), MVC Model 2, MVC Push, and MVC Pull, with each emphasizing slightly different issues. &lt;br /&gt;&lt;br /&gt;The model object should have methods for run(), about(), help(), and exit() as these are common to most utilities. The view object constructor should accept a string that incorporates the utility title. The view object also requires methods to build the listeners. buttonActionListeners() includes addActionListener() and a setActionCommand(string) which is used to pass a reference of the pressed button. The controller module uses getActionCommand() to call the correct action method in the model. An example of MVC architecture using the MyGUI example is:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt; * @author Deepak Singhvi&lt;br /&gt; * @version v7.4&lt;br /&gt; */&lt;br /&gt;&lt;br /&gt;import java.awt.*;&lt;br /&gt;import java.awt.event.*;&lt;br /&gt;import javax.swing.*;&lt;br /&gt;/* myGUI demonstrates separation of functionality&lt;br /&gt;   from GUI design by using MVC architecture */&lt;br /&gt;&lt;br /&gt;// first comes the root class that builds the architecture&lt;br /&gt;public class MyGUI {&lt;br /&gt;&lt;br /&gt;    public static void main(String args[]) {&lt;br /&gt;        MyGUIModel model = new MyGUIModel();&lt;br /&gt;        MyGUIView view = new MyGUIView("myGUI MVC Demo");&lt;br /&gt;        MyGUIController controller = new MyGUIController(model, view);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// the model is where functionality (ie properties &amp; methods) goes&lt;br /&gt;class MyGUIModel {&lt;br /&gt;&lt;br /&gt;    public void exit() {&lt;br /&gt;        System.exit(0);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    public void run() {&lt;br /&gt;        JOptionPane.showMessageDialog(null, "Deepak hear you!",&lt;br /&gt;                                      "Message Dialog", JOptionPane.PLAIN_MESSAGE);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// the view is where the GUI is built&lt;br /&gt;class MyGUIView extends JFrame {&lt;br /&gt;&lt;br /&gt;    JButton run = new JButton("Run the Utility");&lt;br /&gt;    JButton exit = new JButton("Exit After Save");&lt;br /&gt;    JPanel buttons = new JPanel(new GridLayout(4, 1, 2, 2));&lt;br /&gt;&lt;br /&gt;    MyGUIView(String title) // the constructor&lt;br /&gt;    {&lt;br /&gt;        super(title);&lt;br /&gt;        setBounds(100, 100, 250, 150);&lt;br /&gt;        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);&lt;br /&gt;        buttons.add(run);&lt;br /&gt;        buttons.add(exit);&lt;br /&gt;        this.getContentPane().add("Center", buttons);&lt;br /&gt;        setVisible(true);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    //method to add ActionListener passed by Controller to buttons&lt;br /&gt;    public void buttonActionListeners(ActionListener al) {&lt;br /&gt;        run.setActionCommand("run");&lt;br /&gt;        run.addActionListener(al);&lt;br /&gt;        exit.setActionCommand("exit");&lt;br /&gt;        exit.addActionListener(al);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// the controller listens for actions and reacts&lt;br /&gt;class MyGUIController implements ActionListener {&lt;br /&gt;&lt;br /&gt;    MyGUIModel model;&lt;br /&gt;    MyGUIView view;&lt;br /&gt;&lt;br /&gt;    public MyGUIController(MyGUIModel model, MyGUIView view) {&lt;br /&gt;        // create the model and the GUI view&lt;br /&gt;        this.model = model;&lt;br /&gt;        this.view = view;&lt;br /&gt;        // Add action listener from this class to buttons of the view&lt;br /&gt;        view.buttonActionListeners(this);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    // Provide interactions for actions performed in the view.&lt;br /&gt;    public void actionPerformed(ActionEvent ae) {&lt;br /&gt;        String action_com = ae.getActionCommand();&lt;br /&gt;        char c = action_com.charAt(0);&lt;br /&gt;        switch (c) {&lt;br /&gt;            case'r':&lt;br /&gt;                model.run();&lt;br /&gt;                break;&lt;br /&gt;            case'e':&lt;br /&gt;                model.exit();&lt;br /&gt;                break;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=48&amp;l=st1&amp;mode=software&amp;search=Java&amp;fc1=000000&amp;lt1=&amp;lc1=3366FF&amp;bg1=FFFFFF&amp;f=ifr" marginwidth="0" marginheight="0" width="728" height="90" border="0" frameborder="0" style="border:none;" scrolling="no"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-2046869519596873275?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/2046869519596873275/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=2046869519596873275' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2046869519596873275'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2046869519596873275'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/12/mvc-and-swing.html' title='MVC and Swing'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-149188143599079495</id><published>2007-12-13T21:14:00.000-08:00</published><updated>2008-06-05T20:50:49.685-07:00</updated><title type='text'>Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine</title><content type='html'>Hi Friends,&lt;br /&gt;&lt;br /&gt;I found this white paper on Garbage collection in Sun's website intersting. It discusses on options of choosing the GC.&lt;br /&gt;&lt;br /&gt;In the J2SE platform version 1.4.2 there were four garbage collectors from which to choose but without an explicit choice by the user the serial garbage collector was always chosen. In version 5.0 the choice of the collector is based on the class of the machine on which the application is started.&lt;br /&gt;&lt;br /&gt;This “smarter choice” of the garbage collector is generally better but is not always the best. For the user who wants to make their own choice of garbage collectors, this document will provide information on which to base that choice. This will first include the general features of the garbage collections and tuning options to take the best advantage of those features. The examples are given in the context of the serial, stop-the-world collector. Then specific features of the other collectors will be discussed along with factors that should considered when choosing one of the other collectors.&lt;br /&gt;&lt;br /&gt;When does the choice of a garbage collector matter to the user? For many applications it doesn't. That is, the application can perform within its specifications in the presence of garbage collection with pauses of modest frequency and duration. An example where this is not the case (when the serial collector is used) would be a large application that scales well to large number of threads, processors, sockets, and a large amount of memory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Since the Document is huge I have given the link for the same:&lt;br /&gt;&lt;br /&gt;&lt;a href="http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html"&gt;http://java.sun.com/docs/hotspot/gc5.0/gc_tuning_5.html&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;I hope it will help in optimizing the GC related problems&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-149188143599079495?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/149188143599079495/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=149188143599079495' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/149188143599079495'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/149188143599079495'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/12/tuning-garbage-collection-with-50.html' title='Tuning Garbage Collection with the 5.0 Java[tm] Virtual Machine'/><author><name>Prashanth Shivaram</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-8819078766524896511</id><published>2007-11-25T02:25:00.000-08:00</published><updated>2007-12-09T06:44:25.887-08:00</updated><title type='text'>Inheritance, Dependency, Association, Aggregation, Composition....a comparative study...</title><content type='html'>&lt;div&gt;&lt;div&gt;The ‘is a’ relationship is expressed with inheritance and ‘has a’ relationship is expressed with composition. Both inheritance and composition allow you to place sub-objects inside your new class. Two of the main techniques for code reuse are class inheritance and object composition. &lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt; &lt;/div&gt;&lt;div&gt;&lt;a href="http://bp2.blogger.com/_JLUn9JWaGS8/R0lOHq2lyDI/AAAAAAAAAA4/zHUDD-p-K98/s1600-h/inheriVsComp.GIF"&gt;&lt;img id="BLOGGER_PHOTO_ID_5136722743662069810" style="CURSOR: hand" alt="" src="http://bp2.blogger.com/_JLUn9JWaGS8/R0lOHq2lyDI/AAAAAAAAAA4/zHUDD-p-K98/s400/inheriVsComp.GIF" border="0" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;Inheritance is uni-directional. For example House is a Building. But Building is not a House. Inheritance uses extends key word.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Composition: is used when House has a Bathroom. It is incorrect to say House is a Bathroom. Composition simply means using instance variables that refer to other objects. The class House will have an instance variable, which refers to a Bathroom object.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;strong&gt;Which one to use? &lt;/strong&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Inheritance should be only used when subclass ‘is a’ superclass.&lt;br /&gt;􀂃 Don’t use inheritance just to get code reuse. If there is no ‘is a’ relationship then use composition for code reuse. Overuse of implementation inheritance (uses the “extends” key word) can break all the subclasses, if the superclass is modified.&lt;br /&gt;􀂃 Do not use inheritance just to get polymorphism. If there is no ‘is a’ relationship and all you want is polymorphism then use interface inheritance with composition, which gives you code reuse.&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;span style="color:#663333;"&gt;&lt;strong&gt;Difference between dependency, association,aggregation and composition:&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;Though Java is a true object-oriented language that thoroughly supports inheritance, careful thought should be given to the use of this feature, since in many cases an alternative is to use a more flexible object relationship. The commonly identified object relationships are as follows:&lt;br /&gt;&lt;/div&gt;&lt;div&gt;a) dependency&lt;br /&gt;b) association&lt;br /&gt;c) aggregation&lt;br /&gt;d) composition &lt;/div&gt;&lt;div&gt;&lt;br /&gt;The distinction between these relationships is based on the duration and the nature of the relationship.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The aggregation and composition relationships involve a tighter binding between the related objects. The related objects have a long-term relationship and have some level of mutual dependency, which may be exclusive, that defines their existence.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;strong&gt;Dependency&lt;/strong&gt;&lt;br /&gt;An object dependency exists when there is a short-term relationship between the objects. For instance, the relationship between a shopping cart and a checkout object would be short term, since once the checkout operation is complete, the checkout object would no longer be needed. The same relationship would exist for a stock transfer object that needed to use a stock item object to get the information on the stock item being transferred. The stock transfer object would have a dependency relationship with the stock item object; the stock transfer object would read the transfer information and could then discard the stock item object and continue processing.&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;a href="http://bp2.blogger.com/_JLUn9JWaGS8/R1qTaA8SRFI/AAAAAAAAABY/3RcCL9j_j4o/s1600-h/dependency.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5141583999735579730" style="FLOAT: left; MARGIN: 0px 10px 10px 0px; CURSOR: hand" alt="" src="http://bp2.blogger.com/_JLUn9JWaGS8/R1qTaA8SRFI/AAAAAAAAABY/3RcCL9j_j4o/s400/dependency.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div&gt;&lt;strong&gt;&lt;/strong&gt; &lt;/div&gt;&lt;div&gt;&lt;strong&gt;&lt;/strong&gt; &lt;/div&gt;&lt;div&gt;&lt;strong&gt;Association&lt;br /&gt;&lt;/strong&gt;An object association represents a more long-term association than the dependency relationship. The controlling object will obtain a reference to the association object and then use the reference to call methods on the object. The relationship between a car and a driver is representative of this relationship. The car will have a driver who will be associated with the car for a period of time. &lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;a href="http://bp0.blogger.com/_JLUn9JWaGS8/R1qTLg8SREI/AAAAAAAAABQ/uDCsyd2db-g/s1600-h/association.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5141583750627476546" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_JLUn9JWaGS8/R1qTLg8SREI/AAAAAAAAABQ/uDCsyd2db-g/s400/association.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;div&gt;&lt;strong&gt;&lt;u&gt;Aggregation&lt;/u&gt;&lt;/strong&gt; is an association in which one class belongs to a collection. This is a part of a whole&lt;br /&gt;relationship where a part can exist without a whole. With an &lt;strong&gt;aggregation&lt;/strong&gt; relationship, the contained object is part of the greater whole. There is a mutual dependency between the two objects, but the contained object can participate in other aggregate relationships and may exist independently of the whole. For example, a FileReader object that has been created using a File object represents a mutual dependency where the two objects combine to create a useful mechanism for reading characters from a file. The UML symbols for expressing this relationship as shown in following figure which involve a line connecting the two classes with a diamond at the object that represents the greater whole.&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;a href="http://bp2.blogger.com/_JLUn9JWaGS8/R1qRGA8SRCI/AAAAAAAAABA/_ESOCASbcpk/s1600-h/aggregation.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5141581457114940450" style="CURSOR: hand" alt="" src="http://bp2.blogger.com/_JLUn9JWaGS8/R1qRGA8SRCI/AAAAAAAAABA/_ESOCASbcpk/s400/aggregation.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;strong&gt;&lt;u&gt;Composition&lt;/u&gt;&lt;/strong&gt; is an association in which one class belongs to a collection. This is a part of a whole relationship where a part cannot exist without a whole. If a whole is deleted then all parts are deleted. So composition has a stronger relationship.&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;With the &lt;strong&gt;composition&lt;/strong&gt; relationship, the client object is owned by the greater whole. The contained object cannot participate in more than one compositional relationship. An example of this is a customer object and its related address object; the address object cannot exist without a customer object. This relationship is shown with a darkened diamond at the object, which represents the greater whole, as shown in the follwing figure&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_JLUn9JWaGS8/R1qRVg8SRDI/AAAAAAAAABI/NjljeYPd22w/s1600-h/composition.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5141581723402912818" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_JLUn9JWaGS8/R1qRVg8SRDI/AAAAAAAAABI/NjljeYPd22w/s400/composition.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt; &lt;/div&gt;&lt;div&gt;....will be updating with example code later.....&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-8819078766524896511?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/8819078766524896511/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=8819078766524896511' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8819078766524896511'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8819078766524896511'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/11/inheritance-aggregation-compositiona.html' title='Inheritance, Dependency, Association, Aggregation, Composition....a comparative study...'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp2.blogger.com/_JLUn9JWaGS8/R0lOHq2lyDI/AAAAAAAAAA4/zHUDD-p-K98/s72-c/inheriVsComp.GIF' height='72' width='72'/><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-3362186378537105162</id><published>2007-11-25T02:19:00.000-08:00</published><updated>2009-01-26T07:49:18.067-08:00</updated><title type='text'>Java Tools Collections</title><content type='html'>Users can find all tools realted to java at this link&lt;br /&gt;&lt;a href="http://javatoolbox.com/categories"&gt;http://javatoolbox.com/categories&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;For example, if you are looking for some testing tool,  but not sure which one to use or from where to get, you can check it here&lt;br /&gt;&lt;a href="http://javatoolbox.com/categories/tests"&gt;http://javatoolbox.com/categories/tests&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=8&amp;l=as1&amp;asins=B000055XYI&amp;fc1=000000&amp;IS2=1&amp;lt1=_blank&amp;m=amazon&amp;lc1=0000FF&amp;bc1=000000&amp;bg1=FFFFFF&amp;f=ifr" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-3362186378537105162?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/3362186378537105162/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=3362186378537105162' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3362186378537105162'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3362186378537105162'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/11/java-tools-collections.html' title='Java Tools Collections'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-579180244927799489</id><published>2007-11-02T03:57:00.000-07:00</published><updated>2007-11-02T04:22:19.182-07:00</updated><title type='text'>Common causes for memory leaks in Java applications</title><content type='html'>&lt;strong&gt;1) Unbounded caches&lt;/strong&gt;&lt;br /&gt;A very simple example of a memory leak would be a java.util.Collection object (for example, a HashMap) that is acting as a cache but which is growing without any bounds.&lt;br /&gt;&lt;br /&gt;public class MyClass {&lt;br /&gt;static HashSet myContainer = new HashSet();&lt;br /&gt;public void leak(int numObjects) {&lt;br /&gt;for (int i = 0; i &amp;lt; numObjects; ++i) {&lt;br /&gt;String leakingUnit = new String("this is leaking object: " + i);&lt;br /&gt;myContainer.add(leakingUnit);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public static void main(String[] args) throws Exception {&lt;br /&gt;{&lt;br /&gt;&lt;strong&gt;MyClass myObj = new MyClass();&lt;br /&gt;myObj.leak(100000); // One hundred thousand&lt;/strong&gt; }&lt;br /&gt;System.gc();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In the above program, there is a class with the name MyClass which has a static reference to HashSet by the name of myContainer. In the main method of the class: MyClass, (in bold text) within which an instance of the class: MyClass is instantiated and its member operation: leak is invoked. This results in the addition of a hundred thousand String objects into the container: myContainer. After the program control exits the subscope, the instance of the MyClass object is garbage collected, because there are no references to that instance of the MyClass object outside that subscope. However, the MyClass class object has a static reference to the member variable called myContainer. Due to this static reference, the myContainer HashSet continues to persist in the Java heap even after the sole instance of the MyClass object has been garbage collected and, along with the HashSet, all the String objects inside the HashSet continue to persist, holding up a significant portion of the Java heap until the program exits the main method.&lt;br /&gt;&lt;br /&gt;This program demonstrates a basic memory leaking operation involving an unbounded growth in a cache object. Most caches are implemented using the Singleton pattern involving a static reference to a top level Cache class as shown in this example.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Here is the GC information for you for the above snippet....&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;[GC 512K-&gt;253K(1984K), 0.0018368 secs]&lt;br /&gt;[GC 765K-&gt;467K(1984K), 0.0015165 secs]&lt;br /&gt;[GC 979K-&gt;682K(1984K), 0.0016116 secs]&lt;br /&gt;[GC 1194K-&gt;900K(1984K), 0.0015495 secs]&lt;br /&gt;[GC 1412K-&gt;1112K(1984K), 0.0015553 secs]&lt;br /&gt;[GC 1624K-&gt;1324K(1984K), 0.0014902 secs]&lt;br /&gt;[GC 1836K-&gt;1537K(2112K), 0.0016068 secs]&lt;br /&gt;[Full GC 1537K-&gt;1537K(2112K), 0.0120419 secs]&lt;br /&gt;[GC 2047K-&gt;1824K(3136K), 0.0019275 secs]&lt;br /&gt;[GC 2336K-&gt;2035K(3136K), 0.0016584 secs]&lt;br /&gt;[GC 2547K-&gt;2248K(3136K), 0.0015602 secs]&lt;br /&gt;[GC 2760K-&gt;2461K(3136K), 0.0015517 secs]&lt;br /&gt;[GC 2973K-&gt;2673K(3264K), 0.0015695 secs]&lt;br /&gt;[Full GC 2673K-&gt;2673K(3264K), 0.0144533 secs]&lt;br /&gt;[GC 3185K-&gt;2886K(5036K), 0.0013183 secs]&lt;br /&gt;[GC 3398K-&gt;3098K(5036K), 0.0015822 secs]&lt;br /&gt;[GC 3610K-&gt;3461K(5036K), 0.0028318 secs]&lt;br /&gt;[GC 3973K-&gt;3673K(5036K), 0.0019273 secs]&lt;br /&gt;[GC 4185K-&gt;3885K(5036K), 0.0019377 secs]&lt;br /&gt;[GC 4397K-&gt;4097K(5036K), 0.0012906 secs]&lt;br /&gt;[GC 4609K-&gt;4309K(5036K), 0.0017647 secs]&lt;br /&gt;[GC 4821K-&gt;4521K(5036K), 0.0017731 secs]&lt;br /&gt;[Full GC 4521K-&gt;4521K(5036K), 0.0222485 secs]&lt;br /&gt;[GC 4971K-&gt;4708K(8012K), 0.0042461 secs]&lt;br /&gt;[GC 5220K-&gt;4920K(8012K), 0.0018258 secs]&lt;br /&gt;[GC 5432K-&gt;5133K(8012K), 0.0018648 secs]&lt;br /&gt;[GC 5645K-&gt;5345K(8012K), 0.0018069 secs]&lt;br /&gt;[GC 5857K-&gt;5558K(8012K), 0.0017825 secs]&lt;br /&gt;[GC 6070K-&gt;5771K(8012K), 0.0018911 secs]&lt;br /&gt;[GC 6283K-&gt;5984K(8012K), 0.0016350 secs]&lt;br /&gt;[GC 6496K-&gt;6197K(8012K), 0.0020342 secs]&lt;br /&gt;[GC 6475K-&gt;6312K(8012K), 0.0013560 secs]&lt;br /&gt;[Full GC 6312K-&gt;6118K(8012K), 0.0341375 secs]&lt;br /&gt;[GC 6886K-&gt;6737K(11032K), 0.0045417 secs]&lt;br /&gt;[GC 7505K-&gt;7055K(11032K), 0.0027473 secs]&lt;br /&gt;[GC 7823K-&gt;7374K(11032K), 0.0028045 secs]&lt;br /&gt;[GC 8142K-&gt;7693K(11032K), 0.0029234 secs]&lt;br /&gt;[GC 8461K-&gt;8012K(11032K), 0.0027353 secs]&lt;br /&gt;[GC 8780K-&gt;8331K(11032K), 0.0027790 secs]&lt;br /&gt;[GC 9099K-&gt;8651K(11032K), 0.0028329 secs]&lt;br /&gt;[GC 9419K-&gt;8970K(11032K), 0.0027895 secs]&lt;br /&gt;[GC 9738K-&gt;9289K(11032K), 0.0028037 secs]&lt;br /&gt;[GC 10057K-&gt;9608K(11032K), 0.0028161 secs]&lt;br /&gt;[GC 10376K-&gt;9927K(11032K), 0.0028482 secs]&lt;br /&gt;[GC 10695K-&gt;10246K(11032K), 0.0028858 secs]&lt;br /&gt;[GC 11014K-&gt;10565K(11416K), 0.0029284 secs]&lt;br /&gt;[Full GC 10565K-&gt;10565K(11416K), 0.0506198 secs]&lt;br /&gt;[GC 11781K-&gt;11071K(18956K), 0.0035594 secs]&lt;br /&gt;[GC 12287K-&gt;11577K(18956K), 0.0042315 secs]&lt;br /&gt;[GC 12793K-&gt;12082K(18956K), 0.0043194 secs]&lt;br /&gt;[GC 12843K-&gt;12390K(18956K), 0.0030633 secs]&lt;br /&gt;[GC 13606K-&gt;13494K(18956K), 0.0085937 secs]&lt;br /&gt;[Full GC 13782K-&gt;13613K(18956K), 0.0646513 secs]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;2) Infinite Loops&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Some memory leaks occur due to program errors in which infinite loop in the application code allocates new objects and adds them to a data structure accessible from outside the program loop scope. This type of infinite loops can sometimes occur due to multithreaded access into a shared unsynchronized data structure. These types of memory leaks manifest as fast growing memory leaks, where if the verbose GC data reports a sharp drop in free heap space in a very short time leads to an OutOfMemoryError.&lt;br /&gt;&lt;span style="color:#663366;"&gt;&lt;strong&gt;Friends please add more to this post... dying to see more in this post....&lt;/strong&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-579180244927799489?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/579180244927799489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=579180244927799489' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/579180244927799489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/579180244927799489'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/11/common-causes-for-memory-leaks-in-java.html' title='Common causes for memory leaks in Java applications'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-373976496498508146</id><published>2007-11-02T03:11:00.000-07:00</published><updated>2009-01-29T03:58:35.722-08:00</updated><title type='text'>Simple performance improvement suggestions.</title><content type='html'>I have found the following simple way of making small improvements to performance for projects in the past. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;- Use Boolean.valueOf(b) instead of new Boolean(b) &lt;br /&gt; Note: however, some classes use a flag and rely on new Boolean() object being &lt;br /&gt; different. A bad practice&lt;br /&gt; synchronized(myInRelServFlg) {&lt;br /&gt;   myInRelServFlg = new Boolean(theFlg.booleanValue());&lt;br /&gt; }&lt;br /&gt;&lt;br /&gt;- Avoid the use of new String(String)&lt;br /&gt;- Avoid the use of a single character String in a string concatenation. Or better &lt;br /&gt;  yet use StringBuilder where possible. Most times the synchronization from &lt;br /&gt;  StringBuffer is not required. Sun just released StringBuilder in 1.5, an &lt;br /&gt;  unsynchronized variant of StringBuffer.&lt;br /&gt;&lt;br /&gt;- Avoid the use of StringBuffer().toString() in string concatenation.&lt;br /&gt;- Avoid creating temproary objects to convert to a String or from a String. E.g. new &lt;br /&gt;  Integer(int).toString() and new Integer(String).intValue()&lt;br /&gt;- Use constants for zero length arrays rather than creating the dynamically. Note: &lt;br /&gt;  however, some classes use zero length array objects for locks. A bad practice&lt;br /&gt;  terminationLock = new int[0];&lt;br /&gt;&lt;br /&gt;- Avoid the use of loop to copy arrays, use System.arraycopy() instead.&lt;br /&gt;- Avoid creating an instance of a class just to get the classes name. e.g.&lt;br /&gt;  (new java.sql.Date(123456)).getClass().getName ()&lt;br /&gt;&lt;br /&gt;-About the history of Vector: Vector was in the 1.0 libraries&lt;br /&gt; By the time of 1.2 ArrayList had come along and people were switching to it for &lt;br /&gt; better performance. Vector was synchronised and because of that slower than the &lt;br /&gt; unsynchronised ArrayList.&lt;br /&gt; By the time of 1.4 however the synchronisation mechanism was much much better - &lt;br /&gt; and Vector actually had a slight performance advantage over ArrayList.&lt;br /&gt;&lt;br /&gt;-Performance with inner classes: When considering whether to use an inner class, &lt;br /&gt; keep in mind that application startup time and memory footprint are typically &lt;br /&gt; directly proportional to the number of classes you load. The more classes you &lt;br /&gt; create, the longer your program takes to start up and the more memory it will take. &lt;br /&gt; As an application developer one has to balance this with other design constraints &lt;br /&gt; the person may have. I am not suggesting you turn your application into a single &lt;br /&gt; monolithic class in hopes of cutting down startup time and memory footprint — this &lt;br /&gt; would lead to unnecessary headaches and maintenance burdens. &lt;br /&gt;&lt;br /&gt;Does anyone have any comments or other suggestions?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=13&amp;l=ur1&amp;category=software&amp;banner=19B9W0V74Z9KV3E29MR2&amp;f=ifr" width="468" height="60" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-373976496498508146?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/373976496498508146/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=373976496498508146' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/373976496498508146'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/373976496498508146'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/11/simple-performance-improvement.html' title='Simple performance improvement suggestions.'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-2776030600650235329</id><published>2007-10-29T01:31:00.000-07:00</published><updated>2007-10-29T01:34:22.652-07:00</updated><title type='text'>Google Tip: Use a Colon</title><content type='html'>Search engines have gotten so good and useful syntax for more specific results. here are top three most useful Google search modifiers that use a colon:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;site&lt;/strong&gt;:URL and search term. As in, site:www.pcmag.com "wireless router." Insiders point out that this modifier is even stronger if you drop the www. You can also drop the domain name entirely and search, for example, only .gov sites.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;define&lt;/strong&gt;:word. This brings up definitions, related phrases, and offers to translate the word.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;filetype&lt;/strong&gt;:file extension and search term. It may be obvious, but this lets you search for files with a certain extension, such as PPT for a PowerPoint presentation on your topic.&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-2776030600650235329?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/2776030600650235329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=2776030600650235329' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2776030600650235329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2776030600650235329'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/google-tip-use-colon.html' title='Google Tip: Use a Colon'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-8051799617943850509</id><published>2007-10-24T21:28:00.000-07:00</published><updated>2007-10-24T21:35:27.864-07:00</updated><title type='text'>Insufficient memory problem with StringBuffer</title><content type='html'>Using string buffer without selecting the proper construction can lead to memory leak.&lt;br /&gt;&lt;br /&gt;Lets have a look of the constructor of string buffer&lt;br /&gt;&lt;br /&gt;Constructs a string buffer with no characters in it and an initial capacity of 16 characters.     &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;public StringBuffer() { &lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;     super(16);    &lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Suppose you are creating objects of type StringBuffer in a loop, no of objects may change depnding upon the input. Every time it will create object to store at least 16 characters, you may not need all the 16, in that case remaining space will be unused and cannout be allocated for other purpose.&lt;br /&gt;&lt;br /&gt;At some point these unused memory location may lead to Out of memory problem.&lt;br /&gt;&lt;br /&gt;Instead of that we can use another constructor&lt;br /&gt;&lt;br /&gt;Constructs a string buffer with no characters in it and  the specified initial capacity.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt; public StringBuffer(int capacity) { &lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;     super(capacity);    &lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;}&lt;/strong&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-8051799617943850509?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/8051799617943850509/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=8051799617943850509' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8051799617943850509'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8051799617943850509'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/insufficient-memory-problem-with.html' title='Insufficient memory problem with StringBuffer'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-8703146293489439151</id><published>2007-10-24T04:39:00.000-07:00</published><updated>2009-04-19T01:45:54.418-07:00</updated><title type='text'>Java Class Loaders</title><content type='html'>Class loaders have always been the key component of the Java, loading classes into the JVM at runtime. Class loaders fetch classes into the JVM, so they constitute the first line of defense in the JVM Sandbox, filtering malicious code, guarding trusted libraries, and setting up protection domains.&lt;br /&gt;&lt;br /&gt;Further, the J2EE architecture makes uses of the class loaders to the fullest. Almost every component of J2EE is the dynamically loaded by the application server.&lt;br /&gt;&lt;br /&gt;In a JVM, each and every class is loaded by some instance of a java.lang.ClassLoader. The ClassLoader class is located in the java.lang package and developers are free to subclass it to add their own functionality to class loading.&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_JLUn9JWaGS8/Rx8wU-jwRtI/AAAAAAAAAAU/W806nOv5n_g/s1600-h/loader_02_10_07_11.bmp"&gt;&lt;img id="BLOGGER_PHOTO_ID_5124868037919393490" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_JLUn9JWaGS8/Rx8wU-jwRtI/AAAAAAAAAAU/W806nOv5n_g/s400/loader_02_10_07_11.bmp" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;JVM creates an instance of java.lang.Class for every data type loaded into memory. The instance of this java.lang.Class is also stored on heap like any other object. Every object in Java contains a reference to this Class object of its data type. The getClass() method can be used to obtain the associated Class object. The getClass() method is inherited from the java.lang.Object hence available in every class. &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Lets see an example of Dynamic class loading&lt;/strong&gt;&lt;/p&gt;public class &lt;strong&gt;&lt;span style="color:#003300;"&gt;DynamicLoader&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;{&lt;br /&gt;public static void main(String[] args) throws Exception&lt;br /&gt;{&lt;br /&gt;Class toRun = Class.forName(args[0]);&lt;br /&gt;String[] newArgs = scrubArgs(args);&lt;br /&gt;Method mainMethod = findMain(toRun);&lt;br /&gt;mainMethod.invoke(null, new Object[] { newArgs });&lt;br /&gt;}&lt;br /&gt;private static String[] scrubArgs(String[] args)&lt;br /&gt;{&lt;br /&gt;String[] toReturn = new String[args.length-1];&lt;br /&gt;for (int i=1; i&amp;lt;args.length; i++)&lt;br /&gt;{&lt;br /&gt;toReturn[i-1] = args[i].toLowerCase();&lt;br /&gt;}&lt;br /&gt;return toReturn;&lt;br /&gt;}&lt;br /&gt;private static Method findMain(Class clazz) throws Exception&lt;br /&gt;{&lt;br /&gt;Method[] methods = clazz.getMethods();&lt;br /&gt;for (int i=0; i&amp;lt;methods.length; i++)&lt;br /&gt;{&lt;br /&gt;if (methods[i].getName().equals("main"))&lt;br /&gt;return methods[i];&lt;br /&gt;}&lt;br /&gt;return null;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class &lt;strong&gt;&lt;span style="color:#663300;"&gt;Echo&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;{&lt;br /&gt;public static void main (String args[])&lt;br /&gt;{&lt;br /&gt;for (int i=0; i&amp;lt;args.length; i++)&lt;br /&gt;{&lt;br /&gt;System.out.println("Echo arg"+i+" = "+args[i]);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Running DynamicLoader&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&gt;java DynamicLoader ECHO One Two Three&lt;/p&gt;&lt;p&gt;&lt;strong&gt;And the output is &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;Echo arg0 = One&lt;/p&gt;&lt;p&gt;Echo arg1 = Two&lt;/p&gt;&lt;p&gt;Echo arg2 = Three&lt;/p&gt;&lt;p&gt;As you can see, DynamicLoader created an instance of the Echo class, and called its main method, all without having any direct reference to Echo itself. In O-O parlance, this means that DynamicLoader is completely decoupled from Echo; there are no explicit dependencies between these two classes.&lt;/p&gt;&lt;br /&gt;Class.forName(). In most of these systems, the code to do the run-time loading comes through the method forName on the class java.lang.Class; its use is demonstrated in the DynamicLoader code, above. Class.forName attempts to load the class whose name is given in its only argument, and returns the Class instance representing that class. In the event that the Class could not be found, resolved, verified, or loaded, Class.forName throws one of several different Exceptions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size:0;"&gt;&lt;strong&gt;CLASS LOADER&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now that we know java.lang.Class, it’s easy to comprehend the class loader of Java, which is a type of java.lang.Classloader (or its sub types).&lt;br /&gt;The purpose of a class loader is to load the byte codes in ".class" file, and build the java.lang.Class object corresponding to the loaded type on JVM heap memory.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The java.lang.Class object is used to create objects requested by the programs.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp0.blogger.com/_JLUn9JWaGS8/Rx8yS-jwRvI/AAAAAAAAAAk/g_2cAaluJ8Q/s1600-h/loader_02_10_07_2.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5124870202582910706" style="CURSOR: hand" alt="" src="http://bp0.blogger.com/_JLUn9JWaGS8/Rx8yS-jwRvI/AAAAAAAAAAk/g_2cAaluJ8Q/s400/loader_02_10_07_2.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Java has hierarchy of class loaders linked in a chain of parent-child relationship. Every class loader in Java, except the bootstrap class loader, has a parent class loader. At top of the parent-child chain is the bootstrap class loader.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Most java programs have at least 3 class loaders active behind the scenes. They are as follows&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href="http://bp3.blogger.com/_JLUn9JWaGS8/Rx8ynujwRwI/AAAAAAAAAAs/ZjXx9fkG3iI/s1600-h/loader_02_10_07_3.gif"&gt;&lt;img id="BLOGGER_PHOTO_ID_5124870559065196290" style="CURSOR: hand" alt="" src="http://bp3.blogger.com/_JLUn9JWaGS8/Rx8ynujwRwI/AAAAAAAAAAs/ZjXx9fkG3iI/s400/loader_02_10_07_3.gif" border="0" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Bootstrap or Primordial Class loader:&lt;/strong&gt; Class loader is responsible for loading only the core Java API (e.g. classes files from rt.jar). Since the core classes are required to bootstrap any Java program the class loader is called bootstrap class loader. This is root of all the class loader hierarchy in a java application.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Extension Class loader:&lt;/strong&gt; Class loader responsible for loading classes from the Java extension directory (i.e. classes or jars in jre/lib/ext of the java installation directory). This class loader is responsible for loading the "installed extensions". Bootstrap class loader is the parent of this class loader.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;System class loader or Application class loader:&lt;/strong&gt; Class loader responsible for loading classes from the java class path (i.e. class directories or jars present in CLASSPATH environment variable of the Operating System). Extension class loader is the parent of this class loader. This class loader is by default the parent of all the custom/user defined class loaders in a java application.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The class loader subsystem involves many other parts of the Java virtual machine and several classes from the java.lang library. For example, user-defined class loaders are regular Java objects whose class descends from java.lang.ClassLoader. The methods of class ClassLoader allow Java applications to access the virtual machine's class loading machinery. Also, for every type a Java virtual machine loads, it creates an instance of class java.lang.Class to represent that type. Like all objects, user-defined class loaders and instances of class Class reside on the heap. Data for loaded types resides in the method area.&lt;br /&gt;&lt;br /&gt;This use of dynamic runtime loading is the heart of Java Application Servers like the Java2 Enterprise Edition reference implementation, Enterprise JavaBeans, and the Servlet Specification. In each one of these architectures, at the time the application server is compiled, it knows nothing about the code that will be attached to it.&lt;br /&gt;&lt;br /&gt;Instead, it simply asks the user for a classname to load, loads the class, creates an instance of the class, and starts making method calls on that instance. (It does so either through Reflection, or by requiring that clients implement a particular interface or class, like GenericServlet in the Servlet spec, or EJBObject in the EJB spec.)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The class loader subsystem is responsible for more than just locating and importing the binary data for classes. It must also verify the correctness of imported classes, allocate and initialize memory for class variables, and assist in the resolution of symbolic references. These activities are performed in a strict order:&lt;br /&gt;1.Loading: finding and importing the binary data for a type&lt;br /&gt;2.Linking: performing verification, preparation, and (optionally) resolution&lt;br /&gt;         a. Verification: ensuring the correctness of the imported type&lt;br /&gt;         b. Preparation: allocating memory for class variables and &lt;br /&gt;            initializing the memory to default values&lt;br /&gt;         c. Resolution: transforming symbolic references from the type into direct &lt;br /&gt;            references.&lt;br /&gt;3.Initialization: invoking Java code that initializes class variables to their &lt;br /&gt;  proper starting values.&lt;br /&gt;&lt;br /&gt;&lt;p&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=13&amp;l=st1&amp;mode=software&amp;search=Java&amp;fc1=000000&amp;lt1=&amp;lc1=3366FF&amp;bg1=FFFFFF&amp;f=ifr" marginwidth="0" marginheight="0" width="468" height="60" border="0" frameborder="0" style="border:none;" scrolling="no"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-8703146293489439151?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/8703146293489439151/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=8703146293489439151' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8703146293489439151'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8703146293489439151'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/java-class-loaders.html' title='Java Class Loaders'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://bp0.blogger.com/_JLUn9JWaGS8/Rx8wU-jwRtI/AAAAAAAAAAU/W806nOv5n_g/s72-c/loader_02_10_07_11.bmp' height='72' width='72'/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-7011302909655736430</id><published>2007-10-17T02:53:00.001-07:00</published><updated>2007-10-17T02:54:54.905-07:00</updated><title type='text'>Custom String Values for Enum</title><content type='html'>&lt;p&gt;The default string value for java enum is its face value, or the element name. However, you can customize the string value by overriding toString() method. For example,&lt;br /&gt;public enum MyType {&lt;br /&gt;  ONE {&lt;br /&gt;   public String toString() {&lt;br /&gt;    return "this is one";&lt;br /&gt;   }&lt;br /&gt;  },&lt;br /&gt;&lt;br /&gt;  TWO {&lt;br /&gt;  public String toString() {&lt;br /&gt;   return "this is two";&lt;br /&gt;  }&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;Running the following test code will produce this:&lt;br /&gt;&lt;br /&gt;public class EnumTest {&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt;   System.out.println(MyType.ONE);&lt;br /&gt;   System.out.println(MyType.TWO);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;-------------&lt;br /&gt;this is one&lt;br /&gt;this is two&lt;br /&gt;&lt;br /&gt;Another interesting fact is, once you override toString() method, you in effect turn each element into an anonymous inner class. So after compiling the above enum class, you will see a long list of class files:&lt;br /&gt;&lt;strong&gt;MyType.class&lt;br /&gt;MyType$1.class&lt;br /&gt;MyType$2.class&lt;/strong&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-7011302909655736430?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/7011302909655736430/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=7011302909655736430' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/7011302909655736430'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/7011302909655736430'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/default-string-value-for-java-enum-is.html' title='Custom String Values for Enum'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-3794621971816418213</id><published>2007-10-17T02:47:00.000-07:00</published><updated>2009-01-26T08:21:44.013-08:00</updated><title type='text'>Java Enum and Its Superclass</title><content type='html'>All java enum implicitly extend from java.lang.Enum. Since java doesn't allow multiple inheritance, enum types can't have superclass. They can't even extend from java.lang.Enum, nor java.lang.Object. It also means enum A can't inherit or extend enum B.&lt;br /&gt;&lt;br /&gt;For example, the following is an invalid enum declaration:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#6633ff;"&gt;public enum MyNumENUM extends Object {&lt;br /&gt;ONE, TWO&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;Compiler error:&lt;br /&gt;MyNumENUM.java:3: '{' expectedpublic enum MyNumENUM extends Object { MyNumENUM.java:6: expected&lt;br /&gt;2 errors&lt;br /&gt;&lt;br /&gt;The correct form should be:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;public enum MyNumENUM {&lt;br /&gt;ONE, TWO&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=21&amp;l=ur1&amp;category=books&amp;banner=1PBGJ6MX362TVT8X9P82&amp;f=ifr" width="125" height="125" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-3794621971816418213?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/3794621971816418213/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=3794621971816418213' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3794621971816418213'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3794621971816418213'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/java-enum-and-its-superclass.html' title='Java Enum and Its Superclass'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-407601893098864432</id><published>2007-10-17T00:34:00.000-07:00</published><updated>2007-11-02T03:28:44.173-07:00</updated><title type='text'>Faster Deep Copies of Java Objects ( Shallow Copy and Deep Copy )</title><content type='html'>Faster Deep Copies of Java Objects&lt;br /&gt;&lt;p&gt; The java.lang.Object root superclass defines a clone() method that will, assuming the subclass implements the java.lang.Cloneable interface, return a copy of the object. While Java classes are free to override this method to do more complex kinds of cloning, the default behavior of clone() is to return a shallow copy of the object. This means that the values of all of the origical object’s fields are copied to the fields of the new object.&lt;br /&gt;&lt;br /&gt;A property of shallow copies is that fields that refer to other objects will point to the same objects in both the original and the clone. For fields that contain primitive or immutable values (int, String, float, etc…), there is little chance of this causing problems. For mutable objects, however, cloning can lead to unexpected results. Figure 1 shows an example.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;import java.util.Vector;&lt;br /&gt;public class Example1 {&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;// Make a Vector&lt;br /&gt;Vector original = new Vector();&lt;br /&gt;// Make a StringBuffer and add it to the Vector&lt;br /&gt;StringBuffer text = new StringBuffer(”The quick brown fox”);&lt;br /&gt;original.addElement(text);&lt;br /&gt;// Clone the vector and print out the contents&lt;br /&gt;Vector clone = (Vector) original.clone();&lt;br /&gt;System.out.println(”A. After cloning”);&lt;br /&gt;printVectorContents(original, “original”);&lt;br /&gt;printVectorContents(clone, “clone”);&lt;br /&gt;System.out.println(“——————————————————–”);&lt;br /&gt;System.out.println();&lt;br /&gt;// Add another object (an Integer) to the clone and&lt;br /&gt;// print out the contents&lt;br /&gt;clone.addElement(new Integer(5));&lt;br /&gt;System.out.println(”B. After adding an Integer to the clone”);&lt;br /&gt;printVectorContents(original, “original”);&lt;br /&gt;printVectorContents(clone, “clone”);&lt;br /&gt;System.out.println(“——————————————————–”);&lt;br /&gt;System.out.println();&lt;br /&gt;// Change the StringBuffer contents&lt;br /&gt;text.append(” jumps over the lazy dog.”);&lt;br /&gt;System.out.println(”C. After modifying one of original’s elements”);&lt;br /&gt;printVectorContents(original, “original”);&lt;br /&gt;printVectorContents(clone, “clone”);&lt;br /&gt;System.out.println(“——————————————————–”);&lt;br /&gt;System.out.println();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void printVectorContents(Vector v, String name) {&lt;br /&gt;System.out.println(” Contents of \”" + name + “\”:”);&lt;br /&gt;// For each element in the vector, print out the index, the&lt;br /&gt;// class of the element, and the element itself&lt;br /&gt; for (int i = 0; i &lt; v.size(); i++) {&lt;br /&gt;  Object element = v.elementAt(i);&lt;br /&gt;  System.out.println(” ” + i + ” (” +element.getClass().getName() + “): ” +element);&lt;br /&gt; }&lt;br /&gt;System.out.println();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Figure 1. Modifying Vector contents after cloning&lt;br /&gt;In this example we create a Vector and add a StringBuffer to it. Note that StringBuffer (unlike, for example, String is mutable — it’s contents can be changed after creation. Figure 2 shows the output of the example in Figure 1.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&gt; java Example1&lt;br /&gt;A. After cloning&lt;br /&gt;&lt;br /&gt;Contents of “original”:&lt;br /&gt;&lt;br /&gt;0 (java.lang.StringBuffer): The quick brown fox&lt;br /&gt;Contents of “clone”:&lt;br /&gt;&lt;br /&gt;0 (java.lang.StringBuffer): The quick brown fox&lt;br /&gt;——————————————————–&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;B. After adding an Integer to the clone&lt;br /&gt;&lt;br /&gt;Contents of “original”:&lt;br /&gt;&lt;br /&gt;0 (java.lang.StringBuffer): The quick brown fox&lt;br /&gt;Contents of “clone”:&lt;br /&gt;&lt;br /&gt;0 (java.lang.StringBuffer): The quick brown fox&lt;br /&gt;&lt;br /&gt;1 (java.lang.Integer): 5&lt;br /&gt;——————————————————–&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;C. After modifying one of original’s elements&lt;br /&gt;&lt;br /&gt;Contents of “original”:&lt;br /&gt;&lt;br /&gt;0 (java.lang.StringBuffer): The quick brown fox jumps over the lazy dog.&lt;br /&gt;Contents of “clone”:&lt;br /&gt;&lt;br /&gt;0 (java.lang.StringBuffer): The quick brown fox jumps over the lazy dog.&lt;br /&gt;&lt;br /&gt;1 (java.lang.Integer): 5&lt;br /&gt;——————————————————–&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Figure 2. Output from the example code in Figure 1&lt;br /&gt;In the first block of output (”A”), we see that the clone operation was successful: The original vector and the clone have the same size (1), content types, and values. The second block of output (”B”) shows that the original vector and its clone are distinct objects. If we add another element to the clone, it only appears in the clone, and not in the original. The third block of output (”C”) is, however, a little trickier. Modifying the StringBuffer that was added to the original vector has changed the value of the first element of both the original vector and its clone. The explanation for this lies in the fact that clone made a shallow copy of the vector, so both vectors now point to the exact same StringBuffer instance.&lt;br /&gt;&lt;br /&gt;This is, of course, sometimes exactly the behavior that you need. In other cases, however, it can lead to frustrating and inexplicable errors, as the state of an object seems to change “behind your back”.&lt;br /&gt;&lt;br /&gt;The solution to this problem is to make a deep copy of the object. A deep copy makes a distinct copy of each of the object’s fields, recursing through the entire graph of other objects referenced by the object being copied. The Java API provides no deep-copy equivalent to Object.clone(). One solution is to simply implement your own custom method (e.g., deepCopy()) that returns a deep copy of an instance of one of your classes. This may be the best solution if you need a complex mixture of deep and shallow copies for different fields, but has a few significant drawbacks:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;You must be able to modify the class (i.e., have the source code) or implement a subclass. If you have a third-party class for which you do not have the source and which is marked final, you are out of luck.&lt;br /&gt;&lt;br /&gt;You must be able to access all of the fields of the class’s superclasses. If significant parts of the object’s state are contained in private fields of a superclass, you will not be able to access them.&lt;br /&gt;&lt;br /&gt;You must have a way to make copies of instances of all of the other kinds of objects that the object references. This is particularly problematic if the exact classes of referenced objects cannot be known until runtime.&lt;br /&gt;&lt;br /&gt;Custom deep copy methods are tedious to implement, easy to get wrong, and difficult to maintain. The method must be revisited any time a change is made to the class or to any of its superclasses.&lt;br /&gt;&lt;br /&gt;A common solution to the deep copy problem is to use Java Object Serialization (JOS). The idea is simple: Write the object to an array using JOS’s ObjectOutputStream and then use ObjectInputStream to reconsistute a copy of the object. The result will be a completely distinct object, with completely distinct referenced objects. JOS takes care of all of the details: superclass fields, following object graphs, and handling repeated references to the same object within the graph. Figure 3 shows a first draft of a utility class that uses JOS for making deep copies.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.ByteArrayInputStream;&lt;br /&gt;import java.io.ByteArrayOutputStream;&lt;br /&gt;import java.io.ObjectOutputStream;&lt;br /&gt;import java.io.ObjectInputStream;&lt;br /&gt;/**&lt;br /&gt;* Utility for making deep copies (vs. clone()’s shallow copies) of&lt;br /&gt;* objects. Objects are first serialized and then deserialized. Error&lt;br /&gt;* checking is fairly minimal in this implementation. If an object is&lt;br /&gt;* encountered that cannot be serialized (or that references an object&lt;br /&gt;* that cannot be serialized) an error is printed to System.err and&lt;br /&gt;* null is returned. Depending on your specific application, it might&lt;br /&gt;* make more sense to have copy(…) re-throw the exception.&lt;br /&gt;*&lt;br /&gt;* A later version of this class includes some minor optimizations.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public class UnoptimizedDeepCopy {&lt;br /&gt;/**&lt;br /&gt;* Returns a copy of the object, or null if the object cannot&lt;br /&gt;* be serialized.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public static Object copy(Object orig) {&lt;br /&gt;Object obj = null;&lt;br /&gt;try {&lt;br /&gt;// Write the object out to a byte array&lt;br /&gt;ByteArrayOutputStream bos = new ByteArrayOutputStream();&lt;br /&gt;ObjectOutputStream out = new ObjectOutputStream(bos);&lt;br /&gt;out.writeObject(orig);&lt;br /&gt;out.flush();&lt;br /&gt;out.close();&lt;br /&gt;// Make an input stream from the byte array and read&lt;br /&gt;// a copy of the object back in.&lt;br /&gt;ObjectInputStream in = new ObjectInputStream(&lt;br /&gt;new ByteArrayInputStream(bos.toByteArray()));&lt;br /&gt;obj = in.readObject();&lt;br /&gt;}&lt;br /&gt;catch(IOException e) {&lt;br /&gt;e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;catch(ClassNotFoundException cnfe) {&lt;br /&gt;cnfe.printStackTrace();&lt;br /&gt;}&lt;br /&gt;return obj;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Figure 3. Using Java Object Serialization to make a deep copy&lt;br /&gt;Unfortunately, this approach has some problems, too:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;It will only work when the object being copied, as well as all of the other objects references directly or indirectly by the object, are serializable. (In other words, they must implement java.io.Serializable.) Fortunately it is often sufficient to simply declare that a given class implements java.io.Serializable and let Java’s default serialization mechanisms do their thing.&lt;br /&gt;&lt;br /&gt;Java Object Serialization is slow, and using it to make a deep copy requires both serializing and deserializing. There are ways to speed it up (e.g., by pre-computing serial version ids and defining custom readObject() and writeObject() methods), but this will usually be the primary bottleneck.&lt;br /&gt;&lt;br /&gt;The byte array stream implementations included in the java.io package are designed to be general enough to perform reasonable well for data of different sizes and to be safe to use in a multi-threaded environment. These characteristics, however, slow down ByteArrayOutputStream and (to a lesser extent) ByteArrayInputStream.&lt;br /&gt;&lt;br /&gt;The first two of these problems cannot be addressed in a general way. We can, however, use alternative implementations of ByteArrayOutputStream and ByteArrayInputStream that makes three simple optimizations:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ByteArrayOutputStream, by default, begins with a 32 byte array for the output. As content is written to the stream, the required size of the content is computed and (if necessary), the array is expanded to the greater of the required size or twice the current size. JOS produces output that is somewhat bloated (for example, fully qualifies path names are included in uncompressed string form), so the 32 byte default starting size means that lots of small arrays are created, copied into, and thrown away as data is written. This has an easy fix: construct the array with a larger inital size.&lt;br /&gt;&lt;br /&gt;All of the methods of ByteArrayOutputStream that modify the contents of the byte array are synchronized. In general this is a good idea, but in this case we can be certain that only a single thread will ever be accessing the stream. Removing the synchronization will speed things up a little. ByteArrayInputStream’s methods are also synchronized.&lt;br /&gt;&lt;br /&gt;The toByteArray() method creates and returns a copy of the stream’s byte array. Again, this is usually a good idea: If you retrieve the byte array and then continue writing to the stream, the retrieved byte array should not change. For this case, however, creating another byte array and copying into it merely wastes cycles and makes extra work for the garbage collector.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;An optimized implementation of ByteArrayOutputStream is shown in Figure 4.&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;import java.io.OutputStream;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;import java.io.ByteArrayInputStream;&lt;br /&gt;/**&lt;br /&gt;* ByteArrayOutputStream implementation that doesn’t synchronize methods&lt;br /&gt;* and doesn’t copy the data on toByteArray().&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public class FastByteArrayOutputStream extends OutputStream {&lt;br /&gt;/**&lt;br /&gt;* Buffer and size&lt;br /&gt;*/&lt;br /&gt;protected byte[] buf = null;&lt;br /&gt;protected int size = 0;&lt;br /&gt;/**&lt;br /&gt;* Constructs a stream with buffer capacity size 5K&lt;br /&gt;*/&lt;br /&gt;public FastByteArrayOutputStream() {&lt;br /&gt;this(5 * 1024);&lt;br /&gt;}&lt;br /&gt;/**&lt;br /&gt;* Constructs a stream with the given initial size&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public FastByteArrayOutputStream(int initSize) {&lt;br /&gt;this.size = 0;&lt;br /&gt;this.buf = new byte[initSize];&lt;br /&gt;}&lt;br /&gt;/**&lt;br /&gt;* Ensures that we have a large enough buffer for the given size.&lt;br /&gt;*/&lt;br /&gt;private void verifyBufferSize(int sz) {&lt;br /&gt;if (sz &gt; buf.length) {&lt;br /&gt;byte[] old = buf;&lt;br /&gt;buf = new byte[Math.max(sz, 2 * buf.length )];&lt;br /&gt;System.arraycopy(old, 0, buf, 0, old.length);&lt;br /&gt;old = null;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;public int getSize() {&lt;br /&gt;return size;&lt;br /&gt;}&lt;br /&gt;/**&lt;br /&gt;* Returns the byte array containing the written data. Note that this&lt;br /&gt;* array will almost always be larger than the amount of data actually&lt;br /&gt;* written.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public byte[] getByteArray() {&lt;br /&gt;return buf;&lt;br /&gt;}&lt;br /&gt;public final void write(byte b[]) {&lt;br /&gt;verifyBufferSize(size + b.length);&lt;br /&gt;System.arraycopy(b, 0, buf, size, b.length);&lt;br /&gt;size += b.length;&lt;br /&gt;}&lt;br /&gt;public final void write(byte b[], int off, int len) {&lt;br /&gt;verifyBufferSize(size + len);&lt;br /&gt;System.arraycopy(b, off, buf, size, len);&lt;br /&gt;size += len;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public final void write(int b) {&lt;br /&gt;verifyBufferSize(size + 1);&lt;br /&gt;buf[size++] = (byte) b;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public void reset() {&lt;br /&gt;size = 0;&lt;br /&gt;}&lt;br /&gt;/**&lt;br /&gt;* Returns a ByteArrayInputStream for reading back the written data&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public InputStream getInputStream() {&lt;br /&gt;return new FastByteArrayInputStream(buf, size);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Figure 4. Optimized version of ByteArrayOutputStream&lt;br /&gt;The getInputStream() method returns an instance of an optimized version of ByteArrayInputStream that has unsychronized methods. The implementation of FastByteArrayInputStream is shown in Figure 5.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.io.InputStream;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;/&lt;br /&gt;* ByteArrayInputStream implementation that does not synchronize methods.&lt;br /&gt;*/&lt;br /&gt;public class FastByteArrayInputStream extends InputStream {&lt;br /&gt;/&lt;br /&gt;* Our byte buffer&lt;br /&gt;*/&lt;br /&gt;protected byte[] buf = null;&lt;br /&gt;/&lt;br /&gt;* Number of bytes that we can read from the buffer&lt;br /&gt;*/&lt;br /&gt;protected int count = 0;&lt;br /&gt;/&lt;br /&gt;* Number of bytes that have been read from the buffer&lt;br /&gt;*/&lt;br /&gt;protected int pos = 0;&lt;br /&gt;public FastByteArrayInputStream(byte[] buf, int count) {&lt;br /&gt;   this.buf = buf;&lt;br /&gt;   this.count = count;&lt;br /&gt;}&lt;br /&gt;public final int available() {&lt;br /&gt;   return count - pos;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public final int read() {&lt;br /&gt;   return (pos &lt; count) ? (buf[pos++] &amp; 0xff) : -1;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public final int read(byte[] b, int off, int len) {&lt;br /&gt;   if (pos &gt;= count)&lt;br /&gt;     return -1;&lt;br /&gt;   if ((pos + len) &gt; count)&lt;br /&gt;     len = (count - pos);&lt;br /&gt;   System.arraycopy(buf, pos, b, off, len);&lt;br /&gt;   pos += len;&lt;br /&gt;   return len;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;  public final long skip(long n) {&lt;br /&gt;     if ((pos + n) &gt; count)&lt;br /&gt;       n = count - pos;&lt;br /&gt;     if (n &lt; 0)&lt;br /&gt;       return 0;&lt;br /&gt;     pos += n;&lt;br /&gt;     return n;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Figure 5. Optimized version of ByteArrayInputStream.&lt;br /&gt;Figure 6 shows a version of a deep copy utility that uses these classes:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.io.IOException;&lt;br /&gt;import java.io.ByteArrayInputStream;&lt;br /&gt;import java.io.ByteArrayOutputStream;&lt;br /&gt;import java.io.ObjectOutputStream;&lt;br /&gt;import java.io.ObjectInputStream;&lt;br /&gt;/**&lt;br /&gt;&lt;br /&gt;* Utility for making deep copies (vs. clone()’s shallow copies) of&lt;br /&gt;* objects. Objects are first serialized and then deserialized. Error&lt;br /&gt;* checking is fairly minimal in this implementation. If an object is&lt;br /&gt;* encountered that cannot be serialized (or that references an object&lt;br /&gt;* that cannot be serialized) an error is printed to System.err and&lt;br /&gt;* null is returned. Depending on your specific application, it might&lt;br /&gt;* make more sense to have copy(…) re-throw the exception.&lt;br /&gt;&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public class DeepCopy {&lt;br /&gt;&lt;br /&gt;/**&lt;br /&gt;* Returns a copy of the object, or null if the object cannot&lt;br /&gt;* be serialized.&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;public static Object copy(Object orig) {&lt;br /&gt;Object obj = null;&lt;br /&gt;try {&lt;br /&gt;// Write the object out to a byte array&lt;br /&gt;&lt;strong&gt;FastByteArrayOutputStream fbos =new FastByteArrayOutputStream();&lt;br /&gt;ObjectOutputStream out = new ObjectOutputStream(fbos);&lt;/strong&gt;&lt;br /&gt;out.writeObject(orig);&lt;br /&gt;out.flush();&lt;br /&gt;out.close();&lt;br /&gt;// Retrieve an input stream from the byte array and read&lt;br /&gt;// a copy of the object back in.&lt;br /&gt;ObjectInputStream in =&lt;br /&gt;new ObjectInputStream(&lt;strong&gt;fbos.getInputStream()&lt;/strong&gt;);&lt;br /&gt;obj = in.readObject();&lt;br /&gt;}&lt;br /&gt;catch(IOException e) {&lt;br /&gt;  e.printStackTrace();&lt;br /&gt;}&lt;br /&gt;catch(ClassNotFoundException cnfe) {&lt;br /&gt;  cnfe.printStackTrace();&lt;br /&gt;}&lt;br /&gt;return obj;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;Figure 6. Deep-copy implementation using optimized byte array streams&lt;br /&gt;&lt;br /&gt;The extent of the speed boost will depend on a number of factors in your specific application (more on this later), but the simple class shown in Figure 7 tests the optimized and unoptimized versions of the deep copy utility by repeatedly copying a large object.&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;import java.util.Hashtable;&lt;br /&gt;import java.util.Vector;&lt;br /&gt;import java.util.Date;&lt;br /&gt;&lt;br /&gt;public class SpeedTest {&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;// Make a reasonable large test object. Note that this doesn’t&lt;br /&gt;// do anything useful — it is simply intended to be large, have&lt;br /&gt;// several levels of references, and be somewhat random. We start&lt;br /&gt;// with a hashtable and add vectors to it, where each element in&lt;br /&gt;// the vector is a Date object (initialized to the current time),&lt;br /&gt;// a semi-random string, and a (circular) reference back to the&lt;br /&gt;// object itself. In this case the resulting object produces&lt;br /&gt;// a serialized representation that is approximate 700K.&lt;br /&gt;&lt;br /&gt;Hashtable obj = new Hashtable();&lt;br /&gt;&lt;br /&gt;for (int i = 0; i &lt; 100; i++) {&lt;br /&gt;&lt;br /&gt;   Vector v = new Vector();&lt;br /&gt;&lt;br /&gt;   for (int j = 0; j &lt; 100; j++) {&lt;br /&gt;     v.addElement(new Object[] {&lt;br /&gt;     new Date(),"A random number: " + Math.random(),&lt;br /&gt;     obj});&lt;br /&gt;   }&lt;br /&gt;   obj.put(new Integer(i), v);&lt;br /&gt;} &lt;br /&gt;int iterations = 10;&lt;br /&gt;// Make copies of the object using the unoptimized version&lt;br /&gt;// of the deep copy utility.&lt;br /&gt;long unoptimizedTime = 0L;&lt;br /&gt;for (int i = 0; i &lt; iterations; i++) {&lt;br /&gt;  long start = System.currentTimeMillis();&lt;br /&gt;  Object copy = UnoptimizedDeepCopy.copy(obj);&lt;br /&gt;  unoptimizedTime += (System.currentTimeMillis() - start);&lt;br /&gt;// Avoid having GC run while we are timing...&lt;br /&gt;  copy = null;&lt;br /&gt;  System.gc();&lt;br /&gt;}&lt;br /&gt;// Repeat with the optimized version&lt;br /&gt;&lt;br /&gt;long optimizedTime = 0L;&lt;br /&gt;for (int i = 0; i &lt; iterations; i++) {&lt;br /&gt;long start = System.currentTimeMillis();&lt;br /&gt;Object copy = DeepCopy.copy(obj);&lt;br /&gt;optimizedTime += (System.currentTimeMillis() - start);&lt;br /&gt;// Avoid having GC run while we are timing...&lt;br /&gt;copy = null;&lt;br /&gt;System.gc();&lt;br /&gt;}&lt;br /&gt;System.out.println("Unoptimized time: " + unoptimizedTime);&lt;br /&gt;System.out.println(" Optimized time: " + optimizedTime);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;br /&gt;Figure 7. Testing the two deep copy implementations.&lt;br /&gt;A few notes about this test:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The object that we are copying is large. While somewhat random, it will generally have a serialized size of around 700 Kbytes.&lt;br /&gt;&lt;br /&gt;The most significant speed boost comes from avoid extra copying of data in FastByteArrayOutputStream. This has several implications:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Using the unsynchronized FastByteArrayInputStream speeds things up a little, but the standard java.io.ByteArrayInputStream is nearly as fast.&lt;br /&gt;&lt;br /&gt;Performance is mildly sensitive to the initial buffer size in FastByteArrayOutputStream, but is much more sensitive to the rate at which the buffer grows. If the objects you are copying tend to be of similar size, copying will be much faster if you initialize the buffer size and tweak the rate of growth.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Measuring speed using elapsed time between two calls to System.currentTimeMillis() is problematic, but for single-threaded applications and testing relatively slow operations it is sufficient. A number of commercial tools (such as JProfiler) will give more accurate per-method timing data.&lt;br /&gt;&lt;br /&gt;Testing code in a loop is also problematic, since the first few iterations will be slower until HotSpot decides to compile the code. Testing larger numbers of iterations aleviates this problems.&lt;br /&gt;&lt;br /&gt;Garbage collection further complicates matters, particularly in cases where lots of memory is allocated. In this example, we manually invoke the garbage collector after each copy to try to keep it from running while a copy is in progress.&lt;br /&gt;&lt;br /&gt;These caveats aside, the performance difference is sigificant. For example, the code as shown in Figure 7 (on a 500Mhz G3 Macintosh iBook running OSX 10.3 and Java 1.4.1) reveals that the unoptimized version requires about 1.8 seconds per copy, while the optimized version only requires about 1.3 seconds. Whether or not this difference is signficant will, of course, depend on the frequency with which your application does deep copies and the size of the objects being copied. &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-407601893098864432?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/407601893098864432/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=407601893098864432' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/407601893098864432'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/407601893098864432'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/faster-deep-copies-of-java-objects.html' title='Faster Deep Copies of Java Objects ( Shallow Copy and Deep Copy )'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-2294498237828245685</id><published>2007-10-17T00:20:00.000-07:00</published><updated>2007-10-17T00:32:30.796-07:00</updated><title type='text'>String empty check is more easy now with JDK6</title><content type='html'>Prior to JDK 6, we can check if a string is empty in 2 ways:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;if(s != null &amp;amp;&amp;amp; s.length() == 0)&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#cc33cc;"&gt;if(("").equals(s))&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Checking its length is more readable and may be a little faster. Starting from JDK 6, String class has a new convenience method isEmpty():&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;boolean isEmpty()&lt;/strong&gt;&lt;br /&gt;Returns true if, and only if, length() is 0.&lt;br /&gt;&lt;br /&gt;It is just a shorthand for checking length. Of course, if the String is null, you will still get NullPointerException.&lt;br /&gt;I don't see much value in adding this convenience method. Instead,&lt;br /&gt;I'd like to see a static utility method that also handle null value:&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;public static boolean notEmpty(String s) {&lt;br /&gt;return (s != null &amp;amp;&amp;amp; s.length() &gt; 0);&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Another option, use StringUtils.isEmpty(String str) of Apache commons , can be downloaded from&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;http://commons.apache.org/&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;It checks for null string also and return true for empty&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;public static boolean isEmpty(String str) {&lt;br /&gt;return str == null  str.length() == 0;&lt;br /&gt;}&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-2294498237828245685?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/2294498237828245685/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=2294498237828245685' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2294498237828245685'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2294498237828245685'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/string-empty-check-is-more-easy-now.html' title='String empty check is more easy now with JDK6'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-7144798208804905729</id><published>2007-10-17T00:13:00.000-07:00</published><updated>2007-10-17T00:19:23.523-07:00</updated><title type='text'>6 Common Errors in Setting Java Heap Size</title><content type='html'>Two JVM options are often used to tune JVM heap size: -Xmx for maximum heap size, and -Xms for initial heap size. Here are some common mistakes I have seen when using them:&lt;br /&gt;&lt;br /&gt;• Missing m, M, g or G at the end (they are case insensitive). For example,&lt;br /&gt;&lt;br /&gt;java -Xmx128 BigApp&lt;br /&gt;java.lang.OutOfMemoryError: Java heap space&lt;br /&gt;&lt;br /&gt;The correct command should be: java -Xmx128m BigApp. To be precise, -Xmx128 is a valid setting for very small apps, like HelloWorld. But in real life, I guess you really mean -Xmx128m&lt;br /&gt;&lt;br /&gt;• Extra space in JVM options, or incorrectly use =. For example,&lt;br /&gt;&lt;br /&gt;java -Xmx 128m BigApp&lt;br /&gt;Invalid maximum heap size: -Xmx&lt;br /&gt;Could not create the Java virtual machine.&lt;br /&gt;java -Xmx=512m HelloWorld&lt;br /&gt;Invalid maximum heap size: -Xmx=512m&lt;br /&gt;Could not create the Java virtual machine.&lt;br /&gt;&lt;br /&gt;The correct command should be java -Xmx128m BigApp, with no whitespace nor =. -X options are different than -Dkey=value system properties, where = is used.&lt;br /&gt;&lt;br /&gt;• Only setting -Xms JVM option and its value is greater than the default maximum heap size, which is 64m. The default minimum heap size seems to be 0. For example,&lt;br /&gt;&lt;br /&gt;java -Xms128m BigApp&lt;br /&gt;Error occurred during initialization of VM&lt;br /&gt;Incompatible initial and maximum heap sizes specified&lt;br /&gt;&lt;br /&gt;The correct command should be java -Xms128m -Xmx128m BigApp. It's a good idea to set the minimum and maximum heap size to the same value. In any case, don't let the minimum heap size exceed the maximum heap size.&lt;br /&gt;&lt;br /&gt;• Heap size is larger than your computer's physical memory.For example,&lt;br /&gt;&lt;br /&gt;java -Xmx2g BigApp&lt;br /&gt;Error occurred during initialization of VM&lt;br /&gt;Could not reserve enough space for object heap&lt;br /&gt;Could not create the Java virtual machine.&lt;br /&gt;&lt;br /&gt;The fix is to make it lower than the physical memory: java -Xmx1g BigApp&lt;br /&gt;&lt;br /&gt;• Incorrectly use mb as the unit, where m or M should be used instead.&lt;br /&gt;&lt;br /&gt;java -Xms256mb -Xmx256mb BigApp&lt;br /&gt;Invalid initial heap size: -Xms256mb&lt;br /&gt;Could not create the Java virtual machine.&lt;br /&gt;&lt;br /&gt;• The heap size is larger than JVM thinks you would ever need. For example,&lt;br /&gt;&lt;br /&gt;java -Xmx256g BigApp&lt;br /&gt;Invalid maximum heap size: -Xmx256g&lt;br /&gt;The specified size exceeds the maximum representable size.&lt;br /&gt;Could not create the Java virtual machine.&lt;br /&gt;&lt;br /&gt;The fix is to lower it to a reasonable value: java -Xmx256m BigApp&lt;br /&gt;&lt;br /&gt;• The value is not expressed in whole number. For example,&lt;br /&gt;java -Xmx0.9g BigApp&lt;br /&gt;Invalid maximum heap size: -Xmx0.9g&lt;br /&gt;Could not create the Java virtual machine.&lt;br /&gt;The correct command should be java -Xmx928m BigApp&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;How to set java heap size in Eclipse?&lt;/strong&gt;&lt;br /&gt;You have 2 options:&lt;br /&gt;&lt;br /&gt;1. Edit eclipse-home/eclipse.ini to be something like the following and restart Eclipse.&lt;br /&gt;&lt;br /&gt;-vmargs&lt;br /&gt;-Xms64m&lt;br /&gt;-Xmx256m&lt;br /&gt;&lt;br /&gt;2. Or, you can just run eclipse command with additional options at the very end. Anything after -vmargs will be treated as JVM options and passed directly to the JVM. JVM options specified in the command line this way will always override those in eclipse.ini. For example,&lt;br /&gt;eclipse -vmargs -Xms64m -Xmx256m&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-7144798208804905729?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/7144798208804905729/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=7144798208804905729' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/7144798208804905729'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/7144798208804905729'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/6-common-errors-in-setting-java-heap.html' title='6 Common Errors in Setting Java Heap Size'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-3796811861773403139</id><published>2007-10-17T00:07:00.000-07:00</published><updated>2007-10-17T00:10:54.483-07:00</updated><title type='text'>New IOException Constructors in JDK 6</title><content type='html'>In JDK 1.5 or earlier, IOException only has 2 constructors: &lt;br /&gt;IOException() and IOException(String s). &lt;br /&gt;&lt;br /&gt;So you can't pass a cause exception or throwable to these constructors. To do that, you will need to do something like this:&lt;br /&gt;&lt;br /&gt;ZipException ze = new ZipException("Nested ZipException");&lt;br /&gt;IOException e = new IOException("IOException with nested ZipException");&lt;br /&gt;e.initCause(ze);&lt;br /&gt;throw e;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In JDK 6, 2 additional constructors are added: &lt;br /&gt;&lt;br /&gt;IOException(String s, Throwable cause)&lt;br /&gt;IOException(Throwable cause)&lt;br /&gt;&lt;br /&gt;So the following can compile and run fine in JDK 6 and later:&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;ZipException ze = new ZipException("Nested ZipException");&lt;br /&gt;IOException e = new IOException(ze);&lt;br /&gt;throw e;&lt;br /&gt;&lt;br /&gt;but will fail to compile in JDK 1.5 or earlier:&lt;br /&gt;&lt;br /&gt;c:\tmp &gt; javac -version A.java&lt;br /&gt;javac 1.5.0_06A.java:15: cannot find symbol&lt;br /&gt;symbol  : constructor IOException(java.util.zip.ZipException)&lt;br /&gt;location: class java.io.IOException              &lt;br /&gt;IOException e = new IOException(ze);                              &lt;br /&gt;                ^1 error&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-3796811861773403139?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/3796811861773403139/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=3796811861773403139' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3796811861773403139'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/3796811861773403139'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/new-ioexception-constructors-in-jdk-6.html' title='New IOException Constructors in JDK 6'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-920653650213387535</id><published>2007-10-16T20:35:00.000-07:00</published><updated>2007-10-16T20:40:33.570-07:00</updated><title type='text'>Tiger Provided option for getting various Thread States</title><content type='html'>&lt;span style="font-family:times new roman;"&gt;Prior to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two. Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States. A thread can only be in one of the following states at a given point in time.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;NEW&lt;/strong&gt; A Fresh thread that has not yet started to execute.&lt;br /&gt;&lt;strong&gt;RUNNABLE&lt;/strong&gt; A thread that is executing in the Java virtual machine.&lt;br /&gt;&lt;strong&gt;BLOCKED&lt;/strong&gt; A thread that is blocked waiting for a monitor lock.&lt;br /&gt;&lt;strong&gt;WAITING&lt;/strong&gt; A thread that is wating to be notified by another thread.&lt;br /&gt;&lt;strong&gt;TIMED_WAITING&lt;/strong&gt; A thread that is wating to be notified by another thread for a specific amount of time&lt;br /&gt;&lt;strong&gt;TERMINATED&lt;/strong&gt; A thread whos run method has ended.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The folowing code prints out all thread states.&lt;br /&gt;&lt;br /&gt;public class ThreadStates{&lt;br /&gt;public static void main(String[] args){&lt;br /&gt;Thread t = new Thread();&lt;br /&gt;Thread.State e = t.getState();&lt;br /&gt;Thread.State[] ts = e.values();&lt;br /&gt;for(int i = 0; i &lt; ts.length; i++){&lt;br /&gt;System.out.println(ts[i]);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;span style="font-family:times new roman;"&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-920653650213387535?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/920653650213387535/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=920653650213387535' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/920653650213387535'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/920653650213387535'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/tiger-provided-option-for-getting.html' title='Tiger Provided option for getting various Thread States'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-8033320700788817722</id><published>2007-10-16T20:33:00.000-07:00</published><updated>2007-10-16T20:34:08.121-07:00</updated><title type='text'>Free Java Lectures</title><content type='html'>&lt;p&gt;The &lt;a href="http://curmudgeon99.googlepages.com/"&gt;Free Java Lectures &lt;/a&gt;page bills itself as "two semesters of College-Level Java--for free" offers a comprehensive introduction to Java over the course of 28 sessions, from basic language concepts up through commonly-used libraries like servlets, JSP's, and Struts. Each lecture is a presentation file in .pps format, which can be opened with &lt;a href="http://www.openoffice.org/"&gt;OpenOffice.org. &lt;/a&gt;&lt;/p&gt;&lt;p&gt;Looks good to me...&lt;/p&gt;&lt;p&gt;Lets add few more to wat we know...&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-8033320700788817722?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/8033320700788817722/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=8033320700788817722' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8033320700788817722'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8033320700788817722'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/free-java-lectures.html' title='Free Java Lectures'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-588955960075206329</id><published>2007-10-16T08:34:00.000-07:00</published><updated>2007-10-16T08:45:52.411-07:00</updated><title type='text'>When to join threads</title><content type='html'>Let's say I need to spawn multiple threads to do the work, and continue to the next step only after all of them complete. I will need to tell the main thread to wait. The key point is to use &lt;strong&gt;Thread.join()&lt;/strong&gt; method. For example,&lt;br /&gt;&lt;br /&gt;package foo;&lt;br /&gt;import java.util.Vector;&lt;br /&gt;public class ThreadTest {&lt;br /&gt;     private Vector&lt;string&gt; threadNames = new Vector&lt;string&gt;();&lt;br /&gt;    &lt;br /&gt;     public static void main(String[] args) {&lt;br /&gt;        ThreadTest test = new ThreadTest();&lt;br /&gt;        test.threadTest(Integer.parseInt(args[0]));&lt;br /&gt;        System.out.println(test.threadNames);  &lt;br /&gt;     }&lt;br /&gt;&lt;br /&gt;      private void threadTest(int numOfThreads) {&lt;br /&gt;      Thread[] threads = new Thread[numOfThreads];&lt;br /&gt;      for (int i = 0; i &lt; threads.length; i++) {&lt;br /&gt;          threads[i] = new foo.ThreadTest.MyThread();&lt;br /&gt;          threads[i].start();&lt;br /&gt;      }&lt;br /&gt;      for (int i = 0; i &lt; threads.length; i++) {&lt;br /&gt;           try {&lt;br /&gt;              threads[i].join();&lt;br /&gt;           } catch (InterruptedException ignore) {}&lt;br /&gt;       }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   class MyThread extends Thread {&lt;br /&gt;       public void run() {&lt;br /&gt;            for (int i = 0; i &lt; 1000000; i++) {&lt;br /&gt;                 i = i + 0;&lt;br /&gt;            }&lt;br /&gt;            threadNames.add(getName());&lt;br /&gt;        }&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;The output when running this program with 10 threads:&lt;/span&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;[Thread-1, Thread-3, Thread-0, Thread-5, Thread-7, Thread-6, Thread-9, Thread-2, Thread-8, Thread-4]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;The order in which the threads are executed is random, which is expected.Also note that we use two for-loops, the first to create and start each thread, and the second loop to join each thread. If each thread is joined right after start, the effect is these threads are executed sequentially, without the desired concurrency. For example, the following code snippet results in serial execution:&lt;br /&gt;&lt;br /&gt;private void threadTest(int numOfThreads) {&lt;br /&gt;    Thread[] threads = new Thread[numOfThreads];&lt;br /&gt;    for (int i = 0; i &lt; threads.length; i++) {&lt;br /&gt;         threads[i] = new foo.ThreadTest.MyThread();&lt;br /&gt;         threads[i].start();&lt;br /&gt;         try {&lt;br /&gt;              threads[i].join();&lt;br /&gt;         } catch (InterruptedException ignore) { }&lt;br /&gt;     }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;Output:[Thread-0, Thread-1, Thread-2, Thread-3, Thread-4, Thread-5, Thread-6, Thread-7, Thread-8, Thread-9]&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;If we don't use any join at all, threadNames may be empty, or partially filled, since the main thread will just move on when it gets the chance.&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#cc33cc;"&gt;The output for running 10 threads may be:[Thread-0, Thread-1, Thread-2, Thread-3]&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-588955960075206329?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/588955960075206329/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=588955960075206329' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/588955960075206329'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/588955960075206329'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/when-to-join-threads.html' title='When to join threads'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-2762837657417965061</id><published>2007-10-16T03:39:00.000-07:00</published><updated>2007-10-16T03:48:38.876-07:00</updated><title type='text'>Java HotSpot VM Options</title><content type='html'>&lt;strong&gt;&lt;span style="color:#cc33cc;"&gt;Java HotSpot VM Options&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Standard options recognized by the Java HotSpot VM are described on the Java Application Launcher reference pages for Windows, Solaris and Linux.&lt;br /&gt;&lt;br /&gt;Options that begin with -X are non-standard (not guaranteed to be supported on all VM implementations), and are subject to change without notice in subsequent releases of the JDK.&lt;br /&gt;&lt;br /&gt;Options that are specified with -XX are not stable and are not recommended for casual use. These options are subject to change without notice.&lt;br /&gt;&lt;br /&gt;Some Useful -XX OptionsDefault values are listed for Java SE 6 for Solaris Sparc with -server. Some options may vary per architecture/OS/JVM version. Platforms with a differing default value are listed in the description.&lt;br /&gt;&lt;br /&gt;Boolean options are turned on with -XX:+ and turned off with -XX:-.Numeric options are set with -XX:=. Numbers can include 'm' or 'M' for megabytes, 'k' or 'K' for kilobytes, and 'g' or 'G' for gigabytes (for example, 32k is the same as 32768).String options are set with -XX:=, are usually used to specify a file, a path, or a list of commandsFlags marked as manageable are dynamically writeable through the JDK management interface (com.sun.management.HotSpotDiagnosticMXBean API) and also through JConsole. In Monitoring and Managing Java SE 6 Platform Applications, Figure 3 shows an example. The manageable flags can also be set through jinfo -flag.The options below are loosely grouped into three categories.&lt;br /&gt;&lt;br /&gt;Behavioral options change the basic behavior of the VM.Performance tuning options are knobs which can be used to tune VM performance.Debugging options generally enable tracing, printing, or output of VM information.&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;strong&gt;Behavioral Options&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Option and Default ValueDescription&lt;br /&gt;&lt;br /&gt;-XX:-AllowUserSignalHandlers Do not complain if the application installs signal handlers. (Relevant to Solaris and Linux only.)&lt;br /&gt;-XX:AltStackSize=16384 Alternate signal stack size (in Kbytes). (Relevant to Solaris only, removed from 5.0.)&lt;br /&gt;-XX:-DisableExplicitGC Disable calls to System.gc(), JVM still performs garbage collection when necessary.&lt;br /&gt;-XX:+FailOverToOldVerifier Fail over to old verifier when the new type checker fails. (Introduced in 6.)&lt;br /&gt;-XX:+HandlePromotionFailure The youngest generation collection does not require a guarantee of full promotion of all live objects. (Introduced in 1.4.2 update 11) [5.0 and earlier: false.]-XX:+MaxFDLimit Bump the number of file descriptors to max. (Relevant to Solaris only.)&lt;br /&gt;-XX:PreBlockSpin=10 Spin count variable for use with&lt;br /&gt;-XX:+UseSpinning. Controls the maximum spin iterations allowed before entering operating system thread synchronization code. (Introduced in 1.4.2.)&lt;br /&gt;-XX:-RelaxAccessControlCheck Relax the access control checks in the verifier. (Introduced in 6.)&lt;br /&gt;-XX:+ScavengeBeforeFullGC Do young generation GC prior to a full GC. (Introduced in 1.4.1.)&lt;br /&gt;-XX:+UseAltSigs Use alternate signals instead of SIGUSR1 and SIGUSR2 for VM internal signals. (Introduced in 1.3.1 update 9, 1.4.1. Relevant to Solaris only.)&lt;br /&gt;-XX:+UseBoundThreads Bind user level threads to kernel threads. (Relevant to Solaris only.)&lt;br /&gt;-XX:-UseConcMarkSweepGC Use concurrent mark-sweep collection for the old generation. (Introduced in 1.4.1)&lt;br /&gt;-XX:+UseGCOverheadLimit Use a policy that limits the proportion of the VM's time that is spent in GC before an OutOfMemory error is thrown. (Introduced in 6.)&lt;br /&gt;-XX:+UseLWPSynchronization Use LWP-based instead of thread based synchronization. (Introduced in 1.4.0. Relevant to Solaris only.)&lt;br /&gt;-XX:-UseParallelGC Use parallel garbage collection for scavenges. (Introduced in 1.4.1)&lt;br /&gt;-XX:-UseParallelOldGC Use parallel garbage collection for the full collections. Enabling this option automatically sets&lt;br /&gt;-XX:+UseParallelGC. (Introduced in 5.0 update 6.)&lt;br /&gt;-XX:-UseSerialGC Use serial garbage collection. (Introduced in 5.0.)&lt;br /&gt;-XX:-UseSpinning Enable naive spinning on Java monitor before entering operating system thread synchronizaton code. (Relevant to 1.4.2 and 5.0 only.) [1.4.2, multi-processor Windows platforms: true]&lt;br /&gt;-XX:+UseTLAB Use thread-local object allocation (Introduced in 1.4.0, known as UseTLE prior to that.) [1.4.2 and earlier, x86 or with -client: false]&lt;br /&gt;-XX:+UseSplitVerifier Use the new type checker with StackMapTable attributes. (Introduced in 5.0.)[5.0: false]&lt;br /&gt;-XX:+UseThreadPriorities Use native thread priorities.&lt;br /&gt;-XX:+UseVMInterruptibleIO Thread interrupt before or with EINTR for I/O operations results in OS_INTRPT. (Introduced in 6. Relevant to Solaris only.)&lt;br /&gt;-------------------------------------------------------------------------------&lt;br /&gt;&lt;strong&gt;Performance Options&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Option and Default ValueDescription&lt;br /&gt;&lt;br /&gt;-XX:+AggressiveOpts Turn on point performance compiler optimizations that are expected to be default in upcoming releases. (Introduced in 5.0 update 6.)&lt;br /&gt;-XX:CompileThreshold=10000 Number of method invocations/branches before compiling [-client: 1,500]&lt;br /&gt;-XX:LargePageSizeInBytes=4m Sets the large page size used for the Java heap. (Introduced in 1.4.0 update 1.) [amd64: 2m.]&lt;br /&gt;-XX:MaxHeapFreeRatio=70 Maximum percentage of heap free after GC to avoid shrinking.&lt;br /&gt;-XX:MaxNewSize=size Maximum size of new generation (in bytes). Since 1.4, MaxNewSize is computed as a function of NewRatio. [1.3.1 Sparc: 32m; 1.3.1 x86: 2.5m.]&lt;br /&gt;-XX:MaxPermSize=64m Size of the Permanent Generation. [5.0 and newer: 64 bit VMs are scaled 30% larger; 1.4 amd64: 96m; 1.3.1 -client: 32m.]&lt;br /&gt;-XX:MinHeapFreeRatio=40 Minimum percentage of heap free after GC to avoid expansion.-XX:NewRatio=2 Ratio of new/old generation sizes. [Sparc -client: 8; x86 -server: 8; x86 -client: 12.]-client: 4 (1.3) 8 (1.3.1+), x86: 12]&lt;br /&gt;-XX:NewSize=2.125m Default size of new generation (in bytes) [5.0 and newer: 64 bit VMs are scaled 30% larger; x86: 1m; x86, 5.0 and older: 640k]&lt;br /&gt;-XX:ReservedCodeCacheSize=32m Reserved code cache size (in bytes) - maximum code cache size. [Solaris 64-bit, amd64, and -server x86: 48m; in 1.5.0_06 and earlier, Solaris 64-bit and and64: 1024m.]&lt;br /&gt;-XX:SurvivorRatio=8 Ratio of eden/survivor space size [Solaris amd64: 6; Sparc in 1.3.1: 25; other Solaris platforms in 5.0 and earlier: 32]&lt;br /&gt;-XX:TargetSurvivorRatio=50 Desired percentage of survivor space used after scavenge.&lt;br /&gt;-XX:ThreadStackSize=512 Thread Stack Size (in Kbytes). (0 means use default stack size) [Sparc: 512; Solaris x86: 320 (was 256 prior in 5.0 and earlier); Sparc 64 bit: 1024; Linux amd64: 1024 (was 0 in 5.0 and earlier); all others 0.]&lt;br /&gt;-XX:+UseBiasedLocking Enable biased locking. For more details, see this tuning example. (Introduced in 5.0 update 6.) [5.0: false]&lt;br /&gt;-XX:+UseFastAccessorMethods Use optimized versions of GetField.&lt;br /&gt;-XX:-UseISM Use Intimate Shared Memory. [Not accepted for non-Solaris platforms.] For details, see Intimate Shared Memory.&lt;br /&gt;-XX:+UseLargePages Use large page memory. (Introduced in 5.0 update 5.) For details, see Java Support for Large Memory Pages.&lt;br /&gt;-XX:+UseMPSS Use Multiple Page Size Support w/4mb pages for the heap. Do not use with ISM as this replaces the need for ISM. (Introduced in 1.4.0 update 1, Relevant to Solaris 9 and newer.) [1.4.1 and earlier: false]&lt;br /&gt;--------------------------------------------------------------------------------&lt;br /&gt;&lt;strong&gt;Debugging Options&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Option and Default ValueDescription&lt;br /&gt;-XX:-CITime Prints time spent in JIT Compiler. (Introduced in 1.4.0.)&lt;br /&gt;-XX:ErrorFile=./hs_err_pid.log If an error occurs, save the error data to this file. (Introduced in 6.)&lt;br /&gt;-XX:-ExtendedDTraceProbes Enable performance-impacting dtrace probes. (Introduced in 6. Relevant to Solaris only.)&lt;br /&gt;-XX:HeapDumpPath=./java_pid.hprof Path to directory or filename for heap dump. Manageable. (Introduced in 1.4.2 update 12, 5.0 update 7.)&lt;br /&gt;-XX:-HeapDumpOnOutOfMemoryError Dump heap to file when java.lang.OutOfMemoryError is thrown. Manageable. (Introduced in 1.4.2 update 12, 5.0 update 7.)&lt;br /&gt;-XX:OnError=";" Run user-defined commands on fatal error. (Introduced in 1.4.2 update 9.)&lt;br /&gt;-XX:OnOutOfMemoryError=";" Run user-defined commands when an OutOfMemoryError is first thrown. (Introduced in 1.4.2 update 12, 6)&lt;br /&gt;-XX:-PrintClassHistogram Print a histogram of class instances on Ctrl-Break. Manageable. (Introduced in 1.4.2.) The jmap -histo command provides equivalent functionality.&lt;br /&gt;-XX:-PrintConcurrentLocks Print java.util.concurrent locks in Ctrl-Break thread dump. Manageable. (Introduced in 6.) The jstack -l command provides equivalent functionality.&lt;br /&gt;-XX:-PrintCommandLineFlags Print flags that appeared on the command line. (Introduced in 5.0.)&lt;br /&gt;-XX:-PrintCompilation Print message when a method is compiled.&lt;br /&gt;-XX:-PrintGC Print messages at garbage collection. Manageable.&lt;br /&gt;-XX:-PrintGCDetails Print more details at garbage collection. Manageable. (Introduced in 1.4.0.)&lt;br /&gt;-XX:-PrintGCTimeStamps Print timestamps at garbage collection. Manageable (Introduced in 1.4.0.)&lt;br /&gt;-XX:-PrintTenuringDistribution Print tenuring age information.-XX:-TraceClassLoading Trace loading of classes.&lt;br /&gt;-XX:-TraceClassLoadingPreorder Trace all classes loaded in order referenced (not loaded). (Introduced in 1.4.2.)&lt;br /&gt;-XX:-TraceClassResolution Trace constant pool resolutions. (Introduced in 1.4.2.)&lt;br /&gt;-XX:-TraceClassUnloading Trace unloading of classes.&lt;br /&gt;-XX:-TraceLoaderConstraints Trace recording of loader constraints.&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-2762837657417965061?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/2762837657417965061/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=2762837657417965061' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2762837657417965061'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/2762837657417965061'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/java-hotspot-vm-options.html' title='Java HotSpot VM Options'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-895041349172269996.post-8911992175465459349</id><published>2007-10-16T03:06:00.000-07:00</published><updated>2009-01-26T08:17:40.038-08:00</updated><title type='text'>Cracking password protected excel sheet.</title><content type='html'>&lt;p&gt;If a excel sheet is password protected, &lt;/p&gt;&lt;p&gt;open Tools -&gt; Macros -&gt; Visual Basic Editor -&gt; &lt;/p&gt;&lt;p&gt;Now new sheet will be opened , Click on Insert -&gt; Module.&lt;/p&gt;&lt;p&gt;New module will be added in the VBA Project, double click on module and paste the following :&lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt; &lt;/p&gt;&lt;p&gt;&lt;strong&gt;Sub PasswordBreaker()     &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt; Dim i As Integer, j As Integer, k As Integer  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Dim l As Integer, m As Integer, n As Integer  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Dim i1 As Integer, i2 As Integer, i3 As Integer  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Dim i4 As Integer, i5 As Integer, i6 As Integer  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;On Error Resume Next  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;For i = 65 To 66: For j = 65 To 66: For k = 65 To 66  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;For l = 65 To 66: For m = 65 To 66: For i1 = 65 To 66  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;For i2 = 65 To 66: For i3 = 65 To 66: For i4 = 65 To 66  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;For i5 = 65 To 66: For i6 = 65 To 66: For n = 32 To 126              &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;ActiveSheet.Unprotect Chr(i) &amp;amp; Chr(j) &amp;amp; Chr(k) &amp;amp; _      &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chr(l) &amp;amp; Chr(m) &amp;amp; Chr(i1) &amp;amp; Chr(i2) &amp;amp; Chr(i3) &amp;amp; _      &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chr(i4) &amp;amp; Chr(i5) &amp;amp; Chr(i6) &amp;amp; Chr(n)  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;If ActiveSheet.ProtectContents = False Then      &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;MsgBox "One usable password is " &amp;amp; Chr(i) &amp;amp; Chr(j) &amp;amp; _          &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chr(k) &amp;amp; Chr(l) &amp;amp; Chr(m) &amp;amp; Chr(i1) &amp;amp; Chr(i2) &amp;amp; _          &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chr(i3) &amp;amp; Chr(i4) &amp;amp; Chr(i5) &amp;amp; Chr(i6) &amp;amp; Chr(n)   &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;ActiveWorkbook.Sheets(1).Select   &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Range("a1").FormulaR1C1 = Chr(i) &amp;amp; Chr(j) &amp;amp; _          &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chr(k) &amp;amp; Chr(l) &amp;amp; Chr(m) &amp;amp; Chr(i1) &amp;amp; Chr(i2) &amp;amp; _          &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Chr(i3) &amp;amp; Chr(i4) &amp;amp; Chr(i5) &amp;amp; Chr(i6) &amp;amp; Chr(n)       &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Exit Sub  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;End If  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Next: Next: Next: Next: Next: Next  &lt;/strong&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Next: Next: Next: Next: Next: Next&lt;br /&gt;End Sub&lt;/strong&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;Click on Run Sub from Run Menu. and go back to excel , its open now.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=13&amp;l=ur1&amp;category=books&amp;banner=1N4P1140VP34Z6816KR2&amp;f=ifr" width="468" height="60" scrolling="no" border="0" marginwidth="0" style="border:none;" frameborder="0"&gt;&lt;/iframe&gt;&lt;div class="blogger-post-footer"&gt;&lt;iframe src="http://rcm.amazon.com/e/cm?t=jatesh-20&amp;o=1&amp;p=27&amp;l=qs1&amp;f=ifr" width="180" height="150" frameborder="0" scrolling="no"&gt;&lt;/iframe&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/895041349172269996-8911992175465459349?l=gemsbond.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://gemsbond.blogspot.com/feeds/8911992175465459349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=895041349172269996&amp;postID=8911992175465459349' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8911992175465459349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/895041349172269996/posts/default/8911992175465459349'/><link rel='alternate' type='text/html' href='http://gemsbond.blogspot.com/2007/10/cracking-password-protected-excel-sheet.html' title='Cracking password protected excel sheet.'/><author><name>Gems Bond</name><uri>http://www.blogger.com/profile/04756477205679396988</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='http://bp0.blogger.com/_JLUn9JWaGS8/R1_qPFHP8wI/AAAAAAAAABk/gGpjMVZv9rY/S220/IMG_2070.JPG'/></author><thr:total>0</thr:total></entry></feed>
