<?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:blogger='http://schemas.google.com/blogger/2008' 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-7703735018951306032</id><updated>2025-02-12T01:03:20.856+05:30</updated><category term="java"/><category term="Collections"/><category term="spring"/><category term="core-java"/><category term="concurrency"/><category term="design pattern"/><category term="IO / Input Output"/><category term="exception"/><category term="spring-di/ioc"/><category term="list"/><category term="OOP / Object Oriented Programming"/><category term="String"/><category term="map"/><category term="Arrays"/><category term="orm"/><category term="toc2"/><category term="java.io"/><category term="Generics"/><category term="inheritance"/><category term="File handling"/><category term="hibernate"/><category term="syntax and grammar"/><category term="Reflection"/><category term="Arrays-tutorial"/><category term="set"/><category term="Threads"/><category term="eclipse"/><category term="example-code"/><category term="difference"/><category term="java.io.File"/><category term="spring-aop"/><category term="ArrayList"/><category term="operators"/><category term="static"/><category term="xml"/><category term="cpp vs java"/><category term="jdbc"/><category term="jpa / java persistence api"/><category term="1D arrays"/><category term="Annotation"/><category term="Constructor"/><category term="conversion"/><category term="initialization"/><category term="collection-interface"/><category term="directory"/><category term="regex / regular expression"/><category term="serialization"/><category term="Enum"/><category term="Primary / primitive data types"/><category term="java.lang.Annotations"/><category term="programs"/><category term="Stream"/><category term="download-source-code"/><category term="equals()"/><category term="jaxb"/><category term="Class"/><category term="classloader"/><category term="java5Concurrency"/><category term="map-implementation"/><category term="web-service"/><category term="aop-advice"/><category term="gc / garbage collection"/><category term="jvm"/><category term="spring-bean-lifecycle"/><category term="Aspect"/><category term="Interface"/><category term="UML"/><category term="Vector"/><category term="ant"/><category term="jdom"/><category term="naming convention"/><category term="scjp"/><category term="wildcard"/><category term="2D arrays"/><category term="Comparison"/><category term="Creational Pattern"/><category term="Date"/><category term="Nested Class"/><category term="aop-pointcut"/><category term="collection-implementation"/><category term="coltoc"/><category term="firstProgram / helloworld"/><category term="interview questions"/><category term="java7 / jdk7"/><category term="sorting"/><category term="Inner Class"/><category term="LinkedList"/><category term="collection-operation"/><category term="composition"/><category term="design principle"/><category term="equality"/><category term="final"/><category term="spring-jdbc"/><category term="swing"/><category term="synchronizer"/><category term="Page"/><category term="concurrent datastructures"/><category term="jar"/><category term="java-practises"/><category term="locking"/><category term="logging"/><category term="properties"/><category term="sample code"/><category term="structural pattern"/><category term="synchronized"/><category term="HashTable"/><category term="Method (computer science)"/><category term="byte array"/><category term="coding style"/><category term="compareTo()"/><category term="covariance"/><category term="development tools"/><category term="iterator"/><category term="memory"/><category term="native method"/><category term="set-implementation"/><category term="sorted set"/><category term="source code"/><category term="spring-annotations"/><category term="spring-example"/><category term="Apache"/><category term="Collections-base"/><category term="Comparable"/><category term="Fundamental Prog. Structure"/><category term="HashSet"/><category term="Java5 / tiger"/><category term="abstract"/><category term="behavioral pattern"/><category term="blocking concurrent ds"/><category term="blocking queue"/><category term="books"/><category term="c++ / cpp"/><category term="classpath"/><category term="clone"/><category term="compilers"/><category term="concurrent interfaces"/><category term="constant / literal pool"/><category term="control flow"/><category term="dtoc / design toc"/><category term="eclipse-plugin"/><category term="encapsulation_classes"/><category term="executor"/><category term="externalization"/><category term="hashMap"/><category term="integer"/><category term="itext"/><category term="java.lang"/><category term="java.util.zip"/><category term="list-implementation"/><category term="shallow copy"/><category term="static initialization"/><category term="today"/><category term="udf"/><category term="xml parsing"/><category term="Bitwise"/><category term="Comparator"/><category term="File"/><category term="ThreadGroup / thread group"/><category term="bits"/><category term="byte IO"/><category term="deep copy"/><category term="design"/><category term="effective java"/><category term="factory pattern"/><category term="iterate"/><category term="java6"/><category term="jsp"/><category term="null"/><category term="parsing"/><category term="pdf-generation"/><category term="reference"/><category term="rmi"/><category term="server"/><category term="shortcut"/><category term="sort"/><category term="spring-loading-beans"/><category term="try-catch"/><category term="value classes"/><category term="weak-reference"/><category term=".net"/><category term="AtomicInteger"/><category term="Calendar"/><category term="Copying"/><category term="LinkedHashMap"/><category term="aop"/><category term="boundedness"/><category term="cache"/><category term="compression"/><category term="contoc"/><category term="eclipse shortcuts"/><category term="file IO"/><category term="generic-method"/><category term="hashcode"/><category term="heap-memory"/><category term="hibernate-inheritance"/><category term="immutable objects"/><category term="instanceof"/><category term="java.net"/><category term="list-operation"/><category term="output"/><category term="pdf"/><category term="programming language / PL"/><category term="rest"/><category term="runtime"/><category term="semaphore"/><category term="spring-api"/><category term="spring-context"/><category term="stringBuffer"/><category term="stringBuilder"/><category term="substring"/><category term="super"/><category term="swap"/><category term="throw"/><category term="throws"/><category term="toString"/><category term="treeMap"/><category term="treeSet"/><category term="variables"/><category term="volatile"/><category term="web development"/><category term="wrapper"/><category term="wsdl"/><category term="Algorithms"/><category term="Bean"/><category term="ByteArrayInputStream"/><category term="DDD / Domain Driven Design"/><category term="NPE / Null Pointer Exception"/><category term="Object class"/><category term="SOA / service oriented architecture"/><category term="Switch-Case"/><category term="URL"/><category term="WeakHashMap"/><category term="advantage"/><category term="aop-pointcut-expressions"/><category term="batch processing"/><category term="bitwise operators"/><category term="builder pattern"/><category term="bulk operations"/><category term="collection-interoperability"/><category term="collection-view"/><category term="command line arguments"/><category term="commons-lang"/><category term="contravariance"/><category term="custom exception"/><category term="cxf"/><category term="data types"/><category term="decompile"/><category term="dom"/><category term="finally"/><category term="for"/><category term="frame"/><category term="generics faciliy"/><category term="image"/><category term="incomplete"/><category term="init block"/><category term="intern"/><category term="introduction"/><category term="java.nio"/><category term="javamail"/><category term="jdbcTemplate"/><category term="keyword"/><category term="link list"/><category term="linkedHashSet"/><category term="memory model"/><category term="named exception"/><category term="object"/><category term="optimization"/><category term="overloading"/><category term="overriding"/><category term="performance"/><category term="polymorphism"/><category term="random"/><category term="read/write"/><category term="recursion"/><category term="restful-webservice"/><category term="restrictions"/><category term="sax"/><category term="soap"/><category term="spring scope"/><category term="sql"/><category term="stax"/><category term="strategy pattern"/><category term="string tokenizer"/><category term="template method pattern"/><category term="thread pool"/><category term="types"/><category term="varargs"/><category term="Access Control"/><category term="Autoboxing / Unboxing"/><category term="BufferedOutputStream"/><category term="BufferedWriter"/><category term="Collections-methods"/><category term="Copy Constructor"/><category term="Decorator pattern"/><category term="EnumSet"/><category term="Filtered Stream"/><category term="Input"/><category term="Latches"/><category term="Marker Interfaces"/><category term="Principle of least knowledge"/><category term="Setter"/><category term="Singleton"/><category term="String functions"/><category term="adapter pattern"/><category term="advice"/><category term="anti-pattern"/><category term="application-server"/><category term="atom"/><category term="atomic operation"/><category term="avoid null"/><category term="bigdecimal"/><category term="blob"/><category term="boolean"/><category term="casting"/><category term="char"/><category term="clob"/><category term="collection-diagrams"/><category term="collection-iteration"/><category term="commons-logging"/><category term="compare()"/><category term="composite pattern"/><category term="conditions"/><category term="dao / data access object"/><category term="database key"/><category term="deque / double ended queue"/><category term="design pattern example"/><category term="destructor"/><category term="di / dependency injection"/><category term="disadvantage"/><category term="document"/><category term="double"/><category term="encapsulation"/><category term="event"/><category term="feed"/><category term="field"/><category term="fileFilter"/><category term="finalize"/><category term="forEach"/><category term="fun"/><category term="google"/><category term="guice"/><category term="h2 database"/><category term="httpconnection"/><category term="if-else"/><category term="iterable"/><category term="j2ee"/><category term="java.text"/><category term="java.util.logging"/><category term="java4"/><category term="javac options"/><category term="javadoc"/><category term="junit"/><category term="lexical structure"/><category term="lifecycle"/><category term="limitations"/><category term="list-interface"/><category term="locale"/><category term="main-method"/><category term="map-interface"/><category term="memory leak"/><category term="method overriding / hiding"/><category term="new"/><category term="non-static"/><category term="openjdk"/><category term="os / operating system"/><category term="package"/><category term="parametric type"/><category term="path"/><category term="printf"/><category term="private constructor"/><category term="process"/><category term="programming"/><category term="proxy"/><category term="quartz"/><category term="read file"/><category term="rss"/><category term="ruby"/><category term="runnable"/><category term="scheduler"/><category term="scoping"/><category term="security manager"/><category term="spring-beanfactory"/><category term="stack"/><category term="stack memory"/><category term="standard IO"/><category term="state pattern"/><category term="string IO"/><category term="strong-reference"/><category term="system properties"/><category term="tdd / test driven development"/><category term="this"/><category term="timer"/><category term="type"/><category term="type safety"/><category term="using reflection"/><category term="utility class"/><category term="web-server"/><category term="writing xml"/><category term="xml-encoder"/><category term="API"/><category term="Apache POI"/><category term="ArrayDeque"/><category term="BigInteger"/><category term="BufferedInputStream"/><category term="BufferedReader"/><category term="Collections static methods"/><category term="Console"/><category term="DateFormat"/><category term="Dynamic Programming / DP"/><category term="EnumMap"/><category term="Enumeration"/><category term="Execution time"/><category term="FAQ"/><category term="FileFiltername"/><category term="Function parameter"/><category term="GUI"/><category term="Getter"/><category term="GoF"/><category term="Java API"/><category term="ListIterator"/><category term="Microsoft Office"/><category term="Mutable objects"/><category term="Navigable Map"/><category term="Navigable Set"/><category term="NumberFormat"/><category term="Observable"/><category term="Pass by value"/><category term="Principles"/><category term="Property (map)"/><category term="Pros and cos"/><category term="Run time (computing)"/><category term="SimpleDateFormat"/><category term="String utility"/><category term="StringUtils"/><category term="TOPLink"/><category term="ThreadModel"/><category term="Time"/><category term="TimerTask"/><category term="Usage"/><category term="access"/><category term="accessor"/><category term="activity diagram"/><category term="address"/><category term="address of"/><category term="aggregation"/><category term="android"/><category term="anonymous"/><category term="apache commons"/><category term="assert"/><category term="association"/><category term="atomicity"/><category term="awt / abstract window toolkit"/><category term="batchUpdate"/><category term="behavioral design pattern"/><category term="behavioural"/><category term="benchmark"/><category term="binarysearch"/><category term="binarystream"/><category term="binding"/><category term="blocks"/><category term="bpm/business process management"/><category term="bugs"/><category term="build"/><category term="c cpp"/><category term="cascade"/><category term="catalyst"/><category term="cavaj"/><category term="character IO"/><category term="charting"/><category term="checked exception"/><category term="class diagram"/><category term="class file"/><category term="classname"/><category term="cloud computing"/><category term="coarse grained"/><category term="command prompt"/><category term="commons.beanutils"/><category term="comparing languages"/><category term="concatenate"/><category term="connection"/><category term="connection-pooling"/><category term="console input"/><category term="const"/><category term="controversy"/><category term="conventions"/><category term="csv"/><category term="cyclic dependency"/><category term="daemon threads"/><category term="databases"/><category term="datastructues"/><category term="datastructures"/><category term="dbutils"/><category term="debug"/><category term="decompiler"/><category term="deeptoString"/><category term="delete"/><category term="dependency"/><category term="deterministic"/><category term="dispatch"/><category term="divide-by-zero-exception"/><category term="do-while"/><category term="dynamic"/><category term="dynamic / weak typing"/><category term="dynamic arrays"/><category term="ear"/><category term="early binding"/><category term="eclipse errors"/><category term="eclipse how to"/><category term="eclipseLink"/><category term="ejb"/><category term="email"/><category term="empty"/><category term="environment variables"/><category term="equality operator"/><category term="evil constructor"/><category term="excel"/><category term="exe"/><category term="executable"/><category term="executor service"/><category term="expand"/><category term="facade pattern"/><category term="factory-method"/><category term="failsafe"/><category term="file reading"/><category term="filter"/><category term="final classes"/><category term="find bugs"/><category term="fine grained"/><category term="fixImage"/><category term="fizz"/><category term="float"/><category term="formatting output"/><category term="function pointers"/><category term="functions"/><category term="future"/><category term="generator"/><category term="getopt"/><category term="gwt"/><category term="gzip"/><category term="hell"/><category term="helper"/><category term="hibernate fetching strategies"/><category term="hibernate object"/><category term="hibernate-annotation"/><category term="hibernate-xml"/><category term="hiding"/><category term="hollywood princinple"/><category term="hotspot"/><category term="how to"/><category term="how-to"/><category term="hsqldb"/><category term="http"/><category term="http client"/><category term="implementing datastructure using array"/><category term="import"/><category term="in-memory"/><category term="index access"/><category term="inheritance type"/><category term="ini file"/><category term="inputstream"/><category term="internalization"/><category term="interrupt"/><category term="invariance"/><category term="ipaddress"/><category term="ireport"/><category term="isEmpty"/><category term="iterator pattern"/><category term="jad"/><category term="jasper"/><category term="java console"/><category term="java secret"/><category term="java-secrets"/><category term="java-tip"/><category term="java.math"/><category term="java.util.concurrent"/><category term="javac"/><category term="javax"/><category term="javax.tools"/><category term="jax-ws"/><category term="jaxp"/><category term="jdbc drivers"/><category term="jexcelapi"/><category term="jfreechart"/><category term="jms"/><category term="joshuaBloch"/><category term="jsf"/><category term="jsp compiler"/><category term="just-in-time (JIT) compilers"/><category term="jvm options"/><category term="jython"/><category term="keyset"/><category term="late binding"/><category term="lazy initialization"/><category term="libraries"/><category term="literals"/><category term="local variable"/><category term="logical"/><category term="loops"/><category term="lucene"/><category term="macaddress"/><category term="mail"/><category term="mapping"/><category term="matter of style"/><category term="maven"/><category term="memory frame"/><category term="message-format"/><category term="metadata"/><category term="microbechmark"/><category term="mixin"/><category term="mock test"/><category term="mq / message queue"/><category term="multiple inheritance"/><category term="mumbay"/><category term="mutator"/><category term="mvc"/><category term="nanotime"/><category term="natural key"/><category term="natural langauge"/><category term="natural ordering"/><category term="need"/><category term="networking"/><category term="newInstance"/><category term="no to constructor"/><category term="notifyAll"/><category term="number"/><category term="numbers"/><category term="observer pattern"/><category term="online"/><category term="paradigm"/><category term="parallel programming"/><category term="password breaking"/><category term="pointer"/><category term="pooling"/><category term="precompilation"/><category term="prerequisite"/><category term="preventing inheritance"/><category term="primary key"/><category term="priority"/><category term="privilege"/><category term="producer consumer"/><category term="programmatic / runtime compilation"/><category term="proxy pattern"/><category term="pull-parsing"/><category term="rac/restricted-Access-Container"/><category term="random stuff"/><category term="range"/><category term="raw types"/><category term="read"/><category term="read byte"/><category term="read-only"/><category term="repository"/><category term="resource"/><category term="resultset-extractor"/><category term="return"/><category term="rowmapper"/><category term="rtf"/><category term="rtti"/><category term="saaj"/><category term="scope"/><category term="screenshot"/><category term="scripting"/><category term="search"/><category term="sequence diagram"/><category term="sleep"/><category term="slf4j"/><category term="soa/service oriented architecture"/><category term="soft-reference"/><category term="sorted map"/><category term="split"/><category term="spring-configuration-file"/><category term="spring-library"/><category term="spring-mvc"/><category term="spring-web"/><category term="spring3"/><category term="sql date"/><category term="static / strong typing"/><category term="static block"/><category term="static methods"/><category term="stored procedure"/><category term="string abnormal"/><category term="string equality"/><category term="stripes"/><category term="subscript"/><category term="subsequence"/><category term="subtyping"/><category term="sun.misc"/><category term="surrogate key"/><category term="synchronous queue"/><category term="templates"/><category term="testing"/><category term="texture"/><category term="thread factory"/><category term="thread local"/><category term="transient"/><category term="traversing"/><category term="tutorial"/><category term="tutorial-References"/><category term="twitter"/><category term="type parameter"/><category term="type substitution"/><category term="typecast"/><category term="underscore"/><category term="unicode"/><category term="unique"/><category term="unit test"/><category term="unit testing"/><category term="unix / linux"/><category term="url shortener"/><category term="use cases"/><category term="using annotations"/><category term="utf-8"/><category term="validation"/><category term="variance"/><category term="version"/><category term="vm / virtual memory"/><category term="wait"/><category term="war"/><category term="weaving"/><category term="web frameworks"/><category term="while"/><category term="wicket"/><category term="windows os"/><category term="wink"/><category term="without Main"/><category term="workspace"/><category term="write file"/><category term="xml-element"/><category term="xml-output"/><category term="xml-read"/><category term="xpath"/><category term="xsd"/><category term="yield"/><title type='text'>Java Jazzle</title><subtitle type='html'>A java blog with a collection of examples and tutorials on Java and related technologies. (Under maintenance with continuous updates) &#xa;Be in touch with java jazzle or k2java.blogspot.com.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1306</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-5167424506284710513</id><published>2013-10-14T16:14:00.001+05:30</published><updated>2013-10-14T16:15:59.097+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="effective java"/><title type='text'>Notes of “Effective Java” (Chapter 7-11)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Item 37: Use marker interfaces to define types&lt;/b&gt;&lt;br /&gt;
Marker interface contains no method declarations, which indicates 
some property or features. The typical marker interface is Serialize 
interface.&lt;br /&gt;
&lt;b&gt;Item 38: Check parameters for validity&lt;/b&gt;&lt;br /&gt;
Check and restrict the parameters of functions.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Throw Exception&lt;/li&gt;
&lt;li&gt;Assert&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 39: Make defensive copies when needed&lt;/b&gt;&lt;br /&gt;
In general, if a class has mutable components that it gets from or 
returns to&amp;nbsp;its clients, the class must defensively copy these 
components.&lt;br /&gt;
Defensive copy usually implemented by:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;clone() method&lt;/li&gt;
&lt;li&gt;copy constructor&lt;/li&gt;
&lt;li&gt;static factory&lt;/li&gt;
&lt;/ul&gt;
This is not&amp;nbsp;mandatory. If the class trusts the clients, then 
defensive copy could be replaced by documentation explanation to save 
the cost.&lt;br /&gt;
&lt;span id=&quot;more-154&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Item 40: Design method signatures carefully&lt;/b&gt;&lt;br /&gt;
Avoid long parameter lists. &amp;nbsp;Divide them to several methods or use helper method and helper class.&lt;br /&gt;
Favor interfaces over classes.&lt;br /&gt;
Prefer two-element enum types to boolean parameters for clearness and extendability.&lt;br /&gt;
&lt;b&gt;Item 41: Use overloading judiciously&lt;/b&gt;&lt;br /&gt;
Selection among&amp;nbsp;overloaded methods is static (at the compile time), while selection among overridden methods is&amp;nbsp;dynamic.&lt;br /&gt;
Refrain using of overloading methods with same number of parameters.&lt;br /&gt;
If you can not avoid that, try avoiding parameters that can be&amp;nbsp;passed to different overloadings by casts.&lt;br /&gt;
&lt;b&gt;Item 42: Use varargs judiciously&lt;/b&gt;&lt;br /&gt;
Do not use varargs unless you really need it and benefit from it.&lt;br /&gt;
Varargs is expensive in resources.&lt;br /&gt;
Arrays.asList() is tricky. It returns List&amp;lt;Integer&amp;gt; when the 
argument is Integer[] and returns List&amp;lt;int[]&amp;gt; when the argument is
 int[].&lt;br /&gt;
&lt;b&gt;Item 43: Return empty arrays or collections, not nulls&lt;/b&gt;&lt;br /&gt;
Returning null requires extra judgement in the client side.&lt;br /&gt;
Use new Object1[0] for arrays and emptyList(), emptySet() and emptyMap() for containers.&lt;br /&gt;
&lt;b&gt;Item 44: Write doc comments for all exposed API elements&lt;/b&gt;&lt;br /&gt;
@param tag for the parameters. @return for return values. @throws for exception may throw.&lt;br /&gt;
Care the HTML metacharacters in doc comments.&lt;br /&gt;
Write&amp;nbsp;succinct&amp;nbsp;and nice comment as the summary description in the beginning.&lt;br /&gt;
For methods and constructors: verb phrase .&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Returns the number of elements in this collection.&lt;/li&gt;
&lt;/ul&gt;
For&amp;nbsp;classes, interfaces, and fields: noun phrase.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;A task that can be scheduled for one-time or repeated execution&amp;nbsp;by a Timer.&lt;/li&gt;
&lt;/ul&gt;
Document generic types and constants.&lt;br /&gt;
&lt;b&gt;Item 45: Minimize the scope of local variables&lt;/b&gt;&lt;br /&gt;
Minimize the scope of a local variable&amp;nbsp;by declaring it where it is first used.&lt;br /&gt;
Prefer &amp;nbsp;for loops to &amp;nbsp;while loops.&lt;br /&gt;
&lt;b&gt;Item 46: Prefer for-each loops to traditional for loops&lt;/b&gt;&lt;br /&gt;
Use the for each loops to iterate the arrays.&lt;br /&gt;
Exception:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;you need to remove the elements.&lt;/li&gt;
&lt;li&gt;you need to change the value of elements.&lt;/li&gt;
&lt;li&gt;special control for the pace of iteration.&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Item 47: Know and use the libraries&lt;/b&gt;&lt;br /&gt;
Be familiar with the&amp;nbsp;contents of java.lang, java.util, and, to a lesser extent, java.io.&lt;br /&gt;
&lt;b&gt;Item 48: Avoid &amp;nbsp;float and double &amp;nbsp;if exact answers are required&lt;/b&gt;&lt;br /&gt;
Use the BigDecimal or int or long instead.&lt;br /&gt;
int for number with less than 9 digits and long for 19 digits.&lt;br /&gt;
&lt;b&gt;Item 49: Prefer primitive types to boxed primitives&lt;/b&gt;&lt;br /&gt;
“==” operation to mixture of primitive and boxed primitive compares the reference identity but not value.&lt;br /&gt;
Use autoboxing carefully.&lt;br /&gt;
&lt;b&gt;Item 50: Avoid strings where other types are more appropriate&lt;/b&gt;&lt;br /&gt;
Do not use string type in everywhere.&lt;br /&gt;
&lt;b&gt;Item 51: Beware the performance of string concatenation&lt;/b&gt;&lt;br /&gt;
Use append() of StringBuilder instead.&lt;br /&gt;
&lt;b&gt;Item 52: Refer to objects by their interfaces&lt;/b&gt;&lt;br /&gt;
Using interface as parameter type make the program more flexible.&lt;br /&gt;
List&amp;lt;Subscriber&amp;gt; subscribers = new Vector&amp;lt;Subscriber&amp;gt;();&lt;br /&gt;
List&amp;lt;Subscriber&amp;gt; subscribers = new ArrayList&amp;lt;Subscriber&amp;gt;();&lt;br /&gt;
&lt;b&gt;Item 53: Prefer interfaces to reflection&lt;/b&gt;&lt;br /&gt;
Reflection loses all information of complile-time check.&lt;br /&gt;
Reflection is only used at design time.&lt;br /&gt;
When using the class unknown at compile time, try to only instantiate
 them by reflection. Access fields and call methods using interfaces or 
superclasses.&lt;br /&gt;
&lt;b&gt;Item 54: Use native methods judiciously&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
Avoid using native code unless you really need it.&lt;br /&gt;
&lt;b&gt;Item 55: Optimize judiciously&lt;/b&gt;&lt;br /&gt;
Weshould &amp;nbsp;forget about small efficiencies, say about 97% of the time:
 prema-ture optimization is the root of all evil. &amp;nbsp;–Donald E. Knuth.&lt;br /&gt;
Write good programs but not fast programs.&lt;br /&gt;
&lt;b&gt;Item 56: Adhere to generally accepted naming conventions&lt;/b&gt;&lt;br /&gt;
Generally, follow The Java Language Specification as conventions.&lt;br /&gt;
&lt;i&gt;Package &amp;nbsp;&lt;/i&gt; &amp;nbsp; &amp;nbsp;com.google.inject, org.joda.time.format&lt;br /&gt;
&lt;i&gt;Class or Interface&lt;/i&gt; &amp;nbsp; &amp;nbsp; Timer, FutureTask, LinkedHashMap, HttpServlet&lt;br /&gt;
&lt;i&gt;Method or Field&lt;/i&gt; &amp;nbsp; &amp;nbsp;remove, ensureCapacity, getCrc&lt;br /&gt;
&lt;i&gt;Constant Field&lt;/i&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp;MIN_VALUE, NEGATIVE_INFINITY&lt;br /&gt;
&lt;i&gt;Local Variable &amp;nbsp;&lt;/i&gt;&amp;nbsp; &amp;nbsp; i, xref, houseNumber&lt;br /&gt;
&lt;i&gt;Type Parameter&lt;/i&gt; &amp;nbsp; &amp;nbsp;T, E, K, V, X, T1, T2&lt;br /&gt;
&lt;b&gt;Item 57: Use exceptions only for exceptional conditions&lt;/b&gt;&lt;br /&gt;
Do not use the exception for control flow, exception is designed for exceptional condition.&lt;br /&gt;
&lt;b&gt;Item 58: Use checked exceptions for recoverable conditions and&amp;nbsp;runtime exceptions for programming errors&lt;/b&gt;&lt;br /&gt;
Checked exception is used in recoverable case. For that, it need to 
provide information or methods to help the caller to recover.&lt;br /&gt;
&lt;b&gt;Item 59: Avoid unnecessary use of checked exceptions&lt;/b&gt;&lt;br /&gt;
Avoid using the checked exception unless:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;the exception can not avoided by proper use of API.&lt;/li&gt;
&lt;li&gt;Handling exception brings benefits.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 60: Favor the use of standard exceptions&lt;/b&gt;&lt;br /&gt;
Some commonly used exceptions:&lt;br /&gt;
IllegalArgumentException: Non-null parameter value is inappropriate&lt;br /&gt;
IllegalStateException: Object state is inappropriate for method invocation&lt;br /&gt;
NullPointerException: Parameter value is null where prohibited&lt;br /&gt;
IndexOutOfBoundsException: Index parameter value is out of range&lt;br /&gt;
ConcurrentModificationException: Concurrent modification of an object has been detected where it is prohibited&lt;br /&gt;
UnsupportedOperationException: Object does not support method&lt;br /&gt;
&lt;b&gt;Item 61: Throw exceptions appropriate to the abstraction&lt;/b&gt;&lt;br /&gt;
If it is impossible to prevent the occurring of exception in lower 
level, propagate it to higher level by catching and rethrowing.&lt;br /&gt;
Try to log the exception information if detailed information is needed.&lt;br /&gt;
&lt;b&gt;Item 62: Document all exceptions thrown by each method&lt;/b&gt;&lt;br /&gt;
Document the exceptions by Javadoc @throws tag. Do not “throws” unchecked exceptions.&lt;br /&gt;
&lt;b&gt;Item 63: Include failure-capture information in detail messages&lt;/b&gt;&lt;br /&gt;
Include failure-capture information in checked exceptions. Those information is useful in recovering from failure.&lt;br /&gt;
&lt;b&gt;Item 64: Strive for failure atomicity&lt;/b&gt;&lt;br /&gt;
Any generated exception should leave the object in the same state it was in prior to the method invocation.&lt;br /&gt;
Example:&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_66213&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Object pop() {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;if&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(size == &lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;0&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;throw&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;EmptyStackException();&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;Object result = elements[--size];&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;elements[size] = &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;null&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;; &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;// Eliminate obsolete reference&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;result;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Do not try to maintain the failure atomicity when throwing errors.&lt;br /&gt;
&lt;b&gt;Item 65: Don’t ignore exceptions&lt;/b&gt;&lt;br /&gt;
Do not use empty catch block to ignore the exception. Throw it outward at least if you do not know how to handle it.&lt;br /&gt;
&lt;b&gt;Item 66: Synchronize access to shared mutable data&lt;/b&gt;&lt;br /&gt;
Synchronization includes two parts: mutual exclusion and 
visibility.&amp;nbsp;Without synchronization, one&amp;nbsp;thread’s changes might not be 
visible to other threads.&lt;br /&gt;
Do not use &amp;nbsp;Thread.stop as it is depreciated.&lt;br /&gt;
Synchronization has no effect unless both read and write&amp;nbsp;operations are synchronized.&lt;br /&gt;
Volatile and Atomic types should be carefully used.&lt;br /&gt;
&lt;b&gt;Item 67: Avoid excessive synchronization&lt;/b&gt;&lt;br /&gt;
Do not call alien methods in synchronized block. Doing this is uncontrollable and may cause exceptions and deadlock.&lt;br /&gt;
Use concurrent containers like&amp;nbsp;CopyOnWriteArrayList to&amp;nbsp;separate&amp;nbsp;the 
data writing and reading. This is particularly useful to the situation 
in which writing data is happened occasionally.&lt;br /&gt;
In a multicore&amp;nbsp;world, the real cost of excessive synchronization is 
not the CPU time spent obtaining locks; it is the lost opportunities for
 parallelism and the delays imposed by the&amp;nbsp;need to ensure that every 
core has a consistent view of memory.&lt;br /&gt;
Do not synchronize the class internally unless you have good reason.&lt;br /&gt;
&lt;b&gt;Item 68: Prefer executors and tasks to threads&lt;/b&gt;&lt;br /&gt;
Executor framework separate the task and mechanism of executing. So use executors&amp;nbsp;prior&amp;nbsp;to &amp;nbsp;Thread.&lt;br /&gt;
Using Executors.newFixedThreadPool under heavy loading situations.&lt;br /&gt;
Using ScheduledThreadPoolExecutor prior to Timer when&amp;nbsp;multiple&amp;nbsp;timing&amp;nbsp;tasked are required.&lt;br /&gt;
&lt;b&gt;Item 68: Prefer executors and tasks to threads&lt;/b&gt;&lt;br /&gt;
The higher-level utilities in java.util.concurrent fall into three 
categories:the Executor Framework, concurrent&amp;nbsp;collections; and 
synchronizers.&lt;br /&gt;
Utilities in the concurrent library should be considered first before wait() and notify().&lt;br /&gt;
Use &amp;nbsp;ConcurrentHashMap in preference to &amp;nbsp;Collections.synchronizedMap &amp;nbsp;or &amp;nbsp;Hashtable.&lt;br /&gt;
Common synchronizers include CyclicBarrier, CountdownLatch and Semaphore.&lt;br /&gt;
&lt;b&gt;Item 70: Document thread safety&lt;/b&gt;&lt;br /&gt;
The presence of the synchronized modifierin a method declaration is an implementation detail, not a part of its exported&amp;nbsp;API.&lt;br /&gt;
To&amp;nbsp;enable safe concurrent use, a class must clearly document what level of&amp;nbsp;thread safety it supports.&lt;br /&gt;
levels of thread safety:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;immutable&lt;/li&gt;
&lt;li&gt;unconditionally thread-safe&lt;/li&gt;
&lt;li&gt;conditionally thread-safe&lt;/li&gt;
&lt;li&gt;not thread-safe&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 71: Use lazy initialization judiciously&lt;/b&gt;&lt;br /&gt;
Lazy&amp;nbsp;initialization&amp;nbsp;decreases the cost of initializing a class or 
creating an instance, at the&amp;nbsp;expense of increasing the cost of accessing
 the lazily initialized field. It may be worthwhile when only part of 
instances will be initialized in practice.&lt;br /&gt;
Use a&amp;nbsp;synchronized accessor to the getfield method to ensure the concurrency.&lt;br /&gt;
Use&amp;nbsp;the lazy initialization holder class idiom for static field.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_886758&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Lazy initialization holder class idiom for static fields&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;private&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;class&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;FieldHolder {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;final&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;FieldType field = computeFieldValue();&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;FieldType getField() { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;FieldHolder.field; }&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Use the double-check idiom for instance field.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_669857&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
8&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
9&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
10&lt;/div&gt;
&lt;div class=&quot;line number11 index10 alt2&quot;&gt;
11&lt;/div&gt;
&lt;div class=&quot;line number12 index11 alt1&quot;&gt;
12&lt;/div&gt;
&lt;div class=&quot;line number13 index12 alt2&quot;&gt;
13&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Double-check idiom for lazy initialization of instance fields&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;private&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;volatile&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;FieldType field;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;FieldType getField() {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;FieldType result = field;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;if&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(result == &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;null&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;) { &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;// First check (no locking)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;synchronized&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;this&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;) {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;result = field;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;if&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(result == &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;null&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;) &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;// Second check (with locking)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;field = result = computeFieldValue();&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number11 index10 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number12 index11 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;result;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number13 index12 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 72: Don’t depend on the thread scheduler&lt;/b&gt;&lt;br /&gt;
Do not rely on scheduler for correctness of program.&lt;br /&gt;
Do not let the thread busy-wait.&lt;br /&gt;
Use Thread.sleep(1) instead of Thread.yield() for increasing concurrency.&lt;br /&gt;
&lt;b&gt;Item 73: Avoid thread groups&lt;/b&gt;&lt;br /&gt;
Thread groups are obsolete.&lt;br /&gt;
&lt;b&gt;Item 74: Implement &amp;nbsp;Serializable judiciously&lt;/b&gt;&lt;br /&gt;
Long term cost of implementing Serializable include:&lt;br /&gt;
Decreases the flexi-bility to change a class’s implementation once it has been released.&lt;br /&gt;
Increase the likelihood of bug and security holes.&lt;br /&gt;
Increase the burden of testing.&lt;br /&gt;
Classes designed for inheritance &amp;nbsp;should rarely implement&amp;nbsp;Serializable, and interfaces should rarely extend it.&lt;br /&gt;
Exceptions:&amp;nbsp;Throwable, Component, and HttpServlet, etc.&lt;br /&gt;
&lt;b&gt;Item 75: Consider using a custom serialized form&lt;/b&gt;&lt;br /&gt;
Use the default serialized form only when an object’s phys-ical representation is identical to its logical content.&lt;br /&gt;
Disadvantages of inappropriate default serialized form:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;permanently ties the exported API to the &amp;nbsp;current internal representation.&lt;/li&gt;
&lt;li&gt;consume excessive space.&lt;/li&gt;
&lt;li&gt;consume excessive time (graph traversal).&lt;/li&gt;
&lt;li&gt;stack overflow.&lt;/li&gt;
&lt;/ul&gt;
Provide writeObject and readOb-ject methods implementing this 
serialized form. &amp;nbsp;The transient&amp;nbsp;modifier indicates that an instance 
field is to be omitted from a class’s default serialized form.&lt;br /&gt;
Before deciding to make a field nontransient,&amp;nbsp;convince yourself that its value is part of the logical state of the object.&lt;br /&gt;
Declare an explicit serial&amp;nbsp;version UID in every serializable class you write, to tackle the version problem.&lt;br /&gt;
&lt;b&gt;Item 76: Write &amp;nbsp;readObject methods defensively&lt;/b&gt;&lt;br /&gt;
Make defensive copy of fields in readObject() method is necessary like in the constructor.&lt;br /&gt;
For classes with object reference fields that must remain private, defensively&amp;nbsp;copy each object in such a field.&lt;br /&gt;
Check any invariants and throw an &amp;nbsp;InvalidObjectException if a check fails.&lt;br /&gt;
Do not invoke any overridable methods in the constructor or readObject() method.&lt;br /&gt;
&lt;b&gt;Item 77: For instance control, prefer enum types to readResolve&lt;/b&gt;&lt;br /&gt;
The instance control (e.g, Singleton) is violated without readResolve() method after&amp;nbsp;serialization.&lt;br /&gt;
All instance fields with object reference types must be 
declared&amp;nbsp;transient if using the readResolve() to do the instance 
control.&lt;br /&gt;
Instance control through Enum is&amp;nbsp;preferred&amp;nbsp;to the readResolve().&lt;br /&gt;
&lt;b&gt;Item 78: Consider serialization proxies instead of serialized instances&lt;/b&gt;&lt;br /&gt;
Serialization proxy pattern is implemented based on an inner static class with a single constructor.&lt;br /&gt;
Use writeReplace() of enclosing class to write a proxy instance instead of the instance of enclosing class.&lt;br /&gt;
Use readResolve() method to return a instance of enclosing class at 
the time of deserialization, since the method in the inner class is able
 to call the constructor of enclosing class.&lt;br /&gt;
Two limitations of the serialization proxy pattern:&lt;br /&gt;
It is not compatible with&amp;nbsp;classes that are extendable by their 
clients (in that time, the enclosing class has not been initialized).&lt;br /&gt;
It is not compatible&amp;nbsp;with some classes whose object graphs contain circularities.&lt;br /&gt;
Serialization proxy pattern is more secure, but with higher expense.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://raysear.wordpress.com/2012/02/28/notes-of-effective-java-chapter-7-11/&quot;&gt;&lt;b&gt;Source&lt;/b&gt;&lt;/a&gt; &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/5167424506284710513/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2013/10/notes-of-effective-java-chapter-7-11.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/5167424506284710513'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/5167424506284710513'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2013/10/notes-of-effective-java-chapter-7-11.html' title='Notes of “Effective Java” (Chapter 7-11)'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-4807755865520418279</id><published>2013-10-14T16:13:00.001+05:30</published><updated>2013-10-14T16:15:35.597+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="effective java"/><title type='text'>Notes of “Effective Java” (Chapter 1-6)</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Item 1: Consider static factory methods instead of constructors&lt;/b&gt;&lt;br /&gt;
advantage:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Customized meaningful names better than constructors.&lt;/li&gt;
&lt;li&gt;Associated with class instead of creating new objects.&lt;/li&gt;
&lt;li&gt;Able to return subtypes.&lt;/li&gt;
&lt;/ol&gt;
For the 2nd advantage, static factory methods form the basis of 
Service provider Framework including service interface, provider 
interface, provider registration API and service access API.&lt;br /&gt;
&lt;b&gt;Item 2:&amp;nbsp;Consider a builder when faced with many constructor &lt;/b&gt;&lt;b&gt;parameters&lt;/b&gt;&lt;br /&gt;
Compare with telescoping constructors and JavaBean Style.&lt;br /&gt;
Builder is set as a static inner class. Builder makes the object initialized immutable.&lt;br /&gt;
&lt;span id=&quot;more-82&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;Item 3:&amp;nbsp;Enforce the singleton property with a private constructor or an enum type&lt;/b&gt;&lt;br /&gt;
Three ways to implement Singleton patter:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Public field&lt;/li&gt;
&lt;li&gt;Static factory method&lt;/li&gt;
&lt;li&gt;Enum Singleton&lt;/li&gt;
&lt;/ul&gt;
Without Enum, readResovle() function needs to be added to avoid creating spurious instances when doing serialization.&lt;br /&gt;
&lt;b&gt;Item 4: Enforce noninstantiability with a private constructor&lt;/b&gt;&lt;br /&gt;
Some utility classes like java.lang.Math or java.util.Arrays are not 
designed to be instantiated. For preventing them from instantiation and 
subclassing, make the constructor to be private.&lt;br /&gt;
&lt;b&gt;Item 5: Avoid creating unnecessary objects&lt;/b&gt;&lt;br /&gt;
Creating new object especially heavy objects is expensive. Therefore, reusing object always increases efficiency.&lt;br /&gt;
Example: String appending, Boxed primitives.&lt;br /&gt;
&lt;b&gt;Item 6: Eliminate obsolete object references&lt;/b&gt;&lt;br /&gt;
Although Java has garbage collector, memory leak can still happen. GC
 reclaims the memory by inspecting the references of objects.&lt;br /&gt;
Therefore, we need to null out the obsolete object references to release the unused object memory.&lt;br /&gt;
Common memory leak scenarios: Objects constructed inside classes, Cache, Callbacks.&lt;br /&gt;
Notice the Weak references and weakHashmap.&lt;br /&gt;
&lt;b&gt;Item 7: Avoid finalizers&lt;/b&gt;&lt;br /&gt;
In summary, don’t use finalizers except as a safety net or to 
terminate&amp;nbsp;noncritical native resources. In those rare instances where 
you do use a finalizer,&amp;nbsp;remember to invoke super.finalize.&lt;br /&gt;
Explicit termination inside a “try finally” is preferable and reliable.&lt;br /&gt;
&lt;b&gt;Item 8: Obey the general contract when overriding equals&lt;/b&gt;&lt;br /&gt;
Conditions do not need overriding equals():&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Instances are distinguished by reference.&lt;/li&gt;
&lt;li&gt;Equals() defined in superclass works.&lt;/li&gt;
&lt;li&gt;You do not care the equals() function.&lt;/li&gt;
&lt;/ol&gt;
Contract when overriding equals():&lt;br /&gt;
Reflexive, Symmetric, Transitive, Consistent.&lt;br /&gt;
There is no perfect way to override equals() between the subclasses with additional value components and superclasses.&lt;br /&gt;
Notice the @Override annotation.&lt;br /&gt;
&lt;b&gt;Item 9: Always override hashCode when you override equals&lt;/b&gt;&lt;br /&gt;
Principle: equal objects according the equals() function must produce the same hashcode.&lt;br /&gt;
General recipe for hashCode():&lt;br /&gt;
result = 31 * result + c;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;boolean: (f?1:0).&lt;/li&gt;
&lt;li&gt;&amp;nbsp;byte, char, short, or int: (int) f&lt;/li&gt;
&lt;li&gt;long: (int) (f ^ (f &amp;gt;&amp;gt;&amp;gt; 32))&lt;/li&gt;
&lt;li&gt;float: Float.floatToIntBits(f)&lt;/li&gt;
&lt;li&gt;double: Double.doubleToLongBits(f)&lt;/li&gt;
&lt;li&gt;&amp;nbsp;null: 0&lt;/li&gt;
&lt;/ul&gt;
Note we could lazily initialize the hashCode value.&lt;br /&gt;
&lt;b&gt;Item 10: Always override toString&lt;/b&gt;&lt;br /&gt;
Faciliate the class client user by giving useful information in string.&lt;br /&gt;
&lt;b&gt;Item 11: Override clone judiciously&lt;/b&gt;&lt;br /&gt;
Once one class implements the Cloneable interface and overrides the 
clone() method, invoking super.clone() will return a field-to-field copy
 of this class object in which the method is called. If a class contains
 only primitive fields or references to immutable objects, then it is 
usually the case that no fields in the object returned by&amp;nbsp;&lt;tt&gt;super.clone&lt;/tt&gt;&amp;nbsp;need to be modified. Otherwise, deep copy is needed.&lt;br /&gt;
Be careful to use clone except for arrays. Usually it is better to use copy constructor and copy factory method.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;public Yum(Yum yum);&lt;/li&gt;
&lt;li&gt;public static Yum newInstance(Yum yum);&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 12: Consider implementing Comparable&lt;/b&gt;&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_525905&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;interface&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Comparable&amp;lt;T&amp;gt; {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;int&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;compareTo(T t);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Classes that depend on comparison include&amp;nbsp;the sorted collections 
TreeSet and TreeMap, and the utility classes Collections&amp;nbsp;and Arrays, 
which contain searching and sorting algorithms. General interfaces like 
Set, Map, and Collection depend on equals() method.&lt;br /&gt;
Principle:&lt;br /&gt;
If the most significant fields are equal, go on to&amp;nbsp;compare the 
next-most-significant fields, and so on. If all fields are equal, 
the&amp;nbsp;objects are equal; return zero.&lt;br /&gt;
&lt;b&gt;Item 13: Minimize the accessibility of classes and members&lt;/b&gt;&lt;br /&gt;
The rule of thumb is simple: make each class or member as inaccessible as&amp;nbsp;possible.&lt;br /&gt;
Exported API: public, protected.&lt;br /&gt;
If a method overrides a superclass method, it is not permitted to 
have a lower&amp;nbsp;access level in the subclass than it does in the 
superclass. This is&amp;nbsp;necessary to ensure that an instance of the subclass
 is usable anywhere that an&amp;nbsp;instance of the superclass is usable. &amp;nbsp;A 
special case of this&amp;nbsp;rule is that if a class implements an interface, 
all of the class methods that are also&amp;nbsp;present in the interface must be 
declared public. This is so because all members of&amp;nbsp;an interface are 
implicitly public.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Instance fields should never be public&lt;/li&gt;
&lt;li&gt;Classes&amp;nbsp;with public mutable fields are not thread-safe&lt;/li&gt;
&lt;li&gt;Public static final fields should be only used for constants&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 14: In public classes, use accessor methods, not public fields&lt;/b&gt;&lt;br /&gt;
Class is package-private or is a private nested class, there is&amp;nbsp;nothing inherently wrong with exposing its data fields.&lt;br /&gt;
&lt;b&gt;Item 15: Minimize mutability&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Make the immutable class to be final.&lt;/li&gt;
&lt;li&gt;Immutable objects are inherently thread-safe; they require no synchroni-zation.&lt;/li&gt;
&lt;li&gt;Use companion class such as StringBuilder replacing String for performance.&lt;/li&gt;
&lt;li&gt;Static factory is a good way for immutable class.&lt;/li&gt;
&lt;li&gt;Classes should be immutable unless there’s a very good reason to make them&amp;nbsp;mutable.&lt;/li&gt;
&lt;li&gt;Make every field final&amp;nbsp;unless there is a compelling reason to make it nonfinal.&lt;/li&gt;
&lt;li&gt;Don’t provide a public initialization method separate from the 
constructor or static factory unless there is a compelling reason to do 
so.&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Item 16: Favor composition over inheritance&lt;/b&gt;&lt;br /&gt;
For most cases, composition is better than inheritance.&lt;br /&gt;
The keyword “super” is for calling overrided methods&amp;nbsp;or 
constructors&amp;nbsp;in superclass. The invocation in super.method() still calls
 the override methods but not overriden methods.&lt;br /&gt;
&lt;b&gt;Item 17: Design and document for inheritance or else prohibit it&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The class must document or simply prohibit its &amp;nbsp;self-use of overridable methods.&lt;/li&gt;
&lt;li&gt;Constructors must not invoke overridable methods.&lt;/li&gt;
&lt;li&gt;Neither clone() nor readObject() may invoke an overridable method, directly or&amp;nbsp;indirectly, as they behave like constructors.&lt;/li&gt;
&lt;li&gt;Prohibit subclassing in classes that&amp;nbsp;are not designed and documented
 to be safely subclassed by “final” or making the constructor “private”.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 18: Prefer interfaces to abstract classes&lt;/b&gt;&lt;br /&gt;
Interface is generally the better way to define a type that&amp;nbsp;permits multiple implementations.&lt;br /&gt;
Abstract class and Interface can be combined to generate skeletal 
implementation, such as&amp;nbsp;AbstractCollection, AbstractSet, AbstractList.&lt;br /&gt;
&lt;b&gt;Item 19: Use interfaces only to define types&lt;/b&gt;&lt;br /&gt;
Interfaces should only used to define type, representing some classes in&amp;nbsp;hierarchy.&lt;br /&gt;
Enum and Utility class are more appropriate for defining constants.&lt;br /&gt;
&lt;b&gt;Item 20: Prefer class hierarchies to tagged classes&lt;/b&gt;&lt;br /&gt;
Tagged classes&amp;nbsp;are cluttered with&amp;nbsp;tag fields, and switch 
statements,&amp;nbsp;messing the encapsulation and &amp;nbsp;being prone to run-time 
errors.&lt;br /&gt;
Tagged class should be abandoned and replaced by abstract classes.&lt;br /&gt;
&lt;b&gt;Item 21: Use function objects to represent strategies&lt;/b&gt;&lt;br /&gt;
Features like function pointers, delegates, lambda expressions call functions in functions.&lt;br /&gt;
Strategy pattern is built on these features typically on function pointers.&lt;br /&gt;
Function pointers is not supported in java. However, it can be 
emulated by a concrete class with only one method, as a “concrete 
strategy class”.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Strategy ==&amp;gt; Interface&lt;/li&gt;
&lt;li&gt;Concrete strategy ==&amp;gt; Concrete class implementing the interface&lt;/li&gt;
&lt;/ul&gt;
Usually the concrete class is used as anonymous class. For class used
 repeatedly, singleton pattern is applied to the concrete class with one
 static and final instance.&lt;br /&gt;
&lt;b&gt;Item 22: Favor static member classes over nonstatic&lt;/b&gt;&lt;br /&gt;
Four kinds of nested classes:&amp;nbsp;static member classes, nonstatic member classes, anonymous classes, and local&amp;nbsp;classes.&lt;br /&gt;
It is impossible to create&amp;nbsp;an instance of a nonstatic member class without an enclosing instance.&lt;br /&gt;
Declare a member class that does not require access to an enclosing&amp;nbsp;instance, always make it to be static.&lt;br /&gt;
Three common uses of anonymous classes:&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;function objects&lt;/li&gt;
&lt;li&gt;process objects, such asRunnable, Thread&lt;/li&gt;
&lt;li&gt;static&amp;nbsp;factory methods&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Item 23: Don’t use raw types in new code&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Do not use raw type to define variable, as the compiler will not 
check the type of parameters and it loses the type safety and may 
generate run-time error.&lt;/li&gt;
&lt;li&gt;Generics like List&amp;lt;Object&amp;gt; can contain any arbitrary types but still with type safety.&lt;/li&gt;
&lt;li&gt;Unbounded wildcard types like List&amp;lt;?&amp;gt; is capable to hold one of any type of element with type safety.&lt;/li&gt;
&lt;li&gt;Generic type information is&amp;nbsp;erased at runtime&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Item 24: Eliminate unchecked warnings&lt;/b&gt;&lt;br /&gt;
Try to eliminate the unchecked warning, including:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;unchecked cast warnings&lt;/li&gt;
&lt;li&gt;unchecked method invocation warnings&lt;/li&gt;
&lt;li&gt;unchecked&amp;nbsp;generic array creation warnings&lt;/li&gt;
&lt;li&gt;unchecked conversion warnings&lt;/li&gt;
&lt;/ul&gt;
Suppress the warning&amp;nbsp;with an @SuppressWarnings(“unchecked”) annotation in narrowest scope if you are sure about the type safety.&lt;br /&gt;
&lt;b&gt;Item 25: Prefer lists to arrays&lt;/b&gt;&lt;br /&gt;
Arrays are covariant and reifiable.&amp;nbsp;Generics are invariant and erased in run-time.&lt;br /&gt;
Arrays&amp;nbsp;provide runtime type safety but not compile-time type safety and vice versa for&amp;nbsp;generics.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_909565&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Fails at runtime!&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;Object[] objectArray = &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Long[&lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;1&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;];&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;objectArray[&lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;0&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;] = &lt;/code&gt;&lt;code class=&quot;java string&quot;&gt;&quot;I don&#39;t fit in&quot;&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;; &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;// Throws ArrayStoreException&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_555985&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Won&#39;t compile!&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;List&amp;lt;Object&amp;gt; ol = &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;ArrayList&amp;lt;Long&amp;gt;(); &lt;/code&gt;&lt;code class=&quot;java comments&quot;&gt;// Incompatible types&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;ol.add(&lt;/code&gt;&lt;code class=&quot;java string&quot;&gt;&quot;I don&#39;t fit in&quot;&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;);&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Generics enforce their type constraints only at compile&amp;nbsp;time and discard (or erase) their element type information at runtime.&lt;br /&gt;
it is illegal to create an array of a generic type such as “new List&amp;lt;String&amp;gt;[]” and “new List&amp;lt;E&amp;gt;[]“.&lt;br /&gt;
Non-reifiable type is one whose&amp;nbsp;runtime representation contains less information than its compile-time representa-tion.&lt;br /&gt;
Casts to arrays&amp;nbsp;of non-reifiable types should be used only under special circumstances. A better solution is to use list.&lt;br /&gt;
&lt;b&gt;Item 26: Favor generic types&lt;/b&gt;&lt;br /&gt;
Generics are safer. Suppress the warning when casting.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_560389&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java color1&quot;&gt;@SuppressWarnings&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;(&lt;/code&gt;&lt;code class=&quot;java string&quot;&gt;&quot;unchecked&quot;&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Stack() {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;elements = (E[]) &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Object[DEFAULT_INITIAL_CAPACITY];&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 27: Favor generic methods&lt;/b&gt;&lt;br /&gt;
Generic singleton factory:&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_302006&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
8&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
9&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
10&lt;/div&gt;
&lt;div class=&quot;line number11 index10 alt2&quot;&gt;
11&lt;/div&gt;
&lt;div class=&quot;line number12 index11 alt1&quot;&gt;
12&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Generic singleton factory pattern&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;private&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;UnaryFunction&amp;lt;Object&amp;gt; IDENTITY_FUNCTION =&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;UnaryFunction&amp;lt;Object&amp;gt;()&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;{&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Object apply(Object arg) { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;arg; }&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;};&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// IDENTITY_FUNCTION is stateless and its type parameter is&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// unbounded so it&#39;s safe to share one instance across all types.&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
&lt;code class=&quot;java color1&quot;&gt;@SuppressWarnings&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;(&lt;/code&gt;&lt;code class=&quot;java string&quot;&gt;&quot;unchecked&quot;&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;&amp;lt;T&amp;gt; UnaryFunction&amp;lt;T&amp;gt; identityFunction() {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number11 index10 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(UnaryFunction&amp;lt;T&amp;gt;) IDENTITY_FUNCTION;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number12 index11 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Recursive type bound is usually used for Comparable&amp;lt;T&amp;gt;.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_982658&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;interface&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Comparable&amp;lt;T&amp;gt;; {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;int&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;compareTo(T o);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Using a recursive type bound to express mutual comparability&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;static&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;&amp;lt;T &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;extends&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Comparable&amp;lt;T&amp;gt;&amp;gt; T max(List&amp;lt;T&amp;gt; list) {...}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 28: Use bounded wildcards to increase API flexibility&lt;/b&gt;&lt;br /&gt;
Producer-extends, consumer-super(PECS)&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_303671&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
8&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
9&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
10&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Wildcard type for parameter that serves as an E producer&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;void&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;pushAll(Iterable&amp;lt;? &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;extends&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;E&amp;gt; src) {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;for&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(E e : src)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;push(e);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Wildcard type for parameter that serves as an E consumer&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;void&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;popAll(Collection&amp;lt;? &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;super&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;E&amp;gt; dst) {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;while&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(!isEmpty())&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;dst.add(pop());&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
Do not use wildcard types as return&amp;nbsp;types. Rather than providing 
additional flexibility for your users, it would force&amp;nbsp;them to use 
wildcard types in client code.&lt;br /&gt;
Always use Comparable&amp;lt;? super T&amp;gt; in preference to 
Comparable&amp;lt;T&amp;gt;, since the type may implements the Comparable 
interface as an subinterface.&lt;br /&gt;
If a type parameter appears only once in a method declaration, replace it with&amp;nbsp;a wildcard.&lt;br /&gt;
Can’t put&amp;nbsp;any value except null into a List&amp;lt;?&amp;gt;, which means List of some&amp;nbsp;particular&amp;nbsp;type.&lt;br /&gt;
&lt;b&gt;Item 29: Consider typesafe heterogeneous containers&lt;/b&gt;&lt;br /&gt;
Place the type parameter on the key rather than the&amp;nbsp;container to enable the container to include different types.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_461400&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
8&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
9&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
10&lt;/div&gt;
&lt;div class=&quot;line number11 index10 alt2&quot;&gt;
11&lt;/div&gt;
&lt;div class=&quot;line number12 index11 alt1&quot;&gt;
12&lt;/div&gt;
&lt;div class=&quot;line number13 index12 alt2&quot;&gt;
13&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Typesafe heterogeneous container pattern - implementation&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;class&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Favorites {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;private&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Map&amp;lt;Class&amp;lt;?&amp;gt;, Object&amp;gt; favorites =&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;HashMap&amp;lt;Class&amp;lt;?&amp;gt;, Object&amp;gt;();&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;&amp;lt;T&amp;gt; &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;void&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;putFavorite(Class&amp;lt;T&amp;gt; type, T instance) {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;if&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(type == &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;null&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;throw&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;NullPointerException(&lt;/code&gt;&lt;code class=&quot;java string&quot;&gt;&quot;Type is null&quot;&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;favorites.put(type, instance);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number9 index8 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number10 index9 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;&amp;lt;T&amp;gt; T getFavorite(Class&amp;lt;T&amp;gt; type) {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number11 index10 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;type.cast(favorites.get(type));&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number12 index11 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number13 index12 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 30: Use enums instead of int constants&lt;/b&gt;&lt;br /&gt;
Java’s enum types are&amp;nbsp;full-fledged classes.&lt;br /&gt;
Enums provide&amp;nbsp;high-quality implementations of all the Object methods, Comparable and Serializable.&lt;br /&gt;
Enums can also add methods or fields.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_255856&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
8&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Enum type with constant-specific method implementations&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;enum&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Operation {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;PLUS { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;apply(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x, &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;y){&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x + y;} },&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;MINUS { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;apply(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x, &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;y){&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x - y;} },&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;TIMES { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;apply(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x, &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;y){&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x * y;} },&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;DIVIDE { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;apply(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x, &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;y){&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x / y;} };&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;abstract&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;apply(&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;x, &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;double&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;y);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
ValueOf(String) method can&amp;nbsp;translate a constant’s name into the constant itself.&lt;br /&gt;
Consider the strategy enum pattern if multiple enum&amp;nbsp;constants share common behaviors.&lt;br /&gt;
&lt;b&gt;Item 31: Use instance fields instead of ordinals&lt;/b&gt;&lt;br /&gt;
Avoid using ordinal() method of enum.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_941402&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Abuse of ordinal to derive an associated value - DON&#39;T DO THIS&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;enum&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Ensemble {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;SOLO, DUET, TRIO, QUARTET, QUINTET,&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;SEXTET, SEPTET, OCTET, NONET, DECTET;&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;int&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;numberOfMusicians() { &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;return&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;ordinal() + &lt;/code&gt;&lt;code class=&quot;java value&quot;&gt;1&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;; }&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 32: Use EnumSet instead of bit fields&lt;/b&gt;&lt;br /&gt;
Use general Set interface for input type.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_93951&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// EnumSet - a modern replacement for bit fields&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;class&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Text {&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;enum&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Any Set could be passed in, but EnumSet is clearly best&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;public&lt;/code&gt; &lt;code class=&quot;java keyword&quot;&gt;void&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;applyStyles(Set&amp;lt;Style&amp;gt; styles) { ... }&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;}&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 33: Use EnumMap instead of ordinal indexing&lt;/b&gt;&lt;br /&gt;
It is better to use EnumMap to catalog relationship related with Enum type.&lt;br /&gt;
&lt;div&gt;
&lt;div class=&quot;syntaxhighlighter  java&quot; id=&quot;highlighter_347353&quot;&gt;
&lt;table border=&quot;0&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;gutter&quot;&gt;&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
1&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
2&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
3&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
4&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
5&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
6&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
7&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
8&lt;/div&gt;
&lt;/td&gt;&lt;td class=&quot;code&quot;&gt;&lt;div class=&quot;container&quot;&gt;
&lt;div class=&quot;line number1 index0 alt2&quot;&gt;
&lt;code class=&quot;java comments&quot;&gt;// Using an EnumMap to associate data with an enum&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number2 index1 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;Map&amp;lt;Herb.Type, Set&amp;lt;Herb&amp;gt;&amp;gt; herbsByType =&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number3 index2 alt2&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;EnumMap&amp;lt;Herb.Type, Set&amp;lt;Herb&amp;gt;&amp;gt;(Herb.Type.&lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;class&lt;/code&gt;&lt;code class=&quot;java plain&quot;&gt;);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number4 index3 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;for&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(Herb.Type t : Herb.Type.values())&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number5 index4 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;herbsByType.put(t, &lt;/code&gt;&lt;code class=&quot;java keyword&quot;&gt;new&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;HashSet&amp;lt;Herb&amp;gt;());&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number6 index5 alt1&quot;&gt;
&lt;code class=&quot;java keyword&quot;&gt;for&lt;/code&gt; &lt;code class=&quot;java plain&quot;&gt;(Herb h : garden)&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number7 index6 alt2&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;herbsByType.get(h.type).add(h);&lt;/code&gt;&lt;/div&gt;
&lt;div class=&quot;line number8 index7 alt1&quot;&gt;
&lt;code class=&quot;java plain&quot;&gt;System.out.println(herbsByType);&lt;/code&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;b&gt;Item 34: Emulate extensible enums with interfaces&lt;/b&gt;&lt;br /&gt;
In general, extending enums is&amp;nbsp;a bad idea. If you really want to do that, define an interface for expected behaviors.&lt;br /&gt;
&lt;b&gt;Item 35: Prefer annotations to naming patterns&lt;/b&gt;&lt;br /&gt;
Annotations do not directly affect program semantics, but they do 
affect the way programs are treated by tools and libraries, which can in
 turn affect the semantics of the running program. Annotations can be 
read from source files, class files, or reflectively at run time.&lt;br /&gt;
Annotations complement javadoc tags. In general, if the markup is 
intended to affect or produce documentation, it should probably be a 
javadoc tag; otherwise, it should be an annotation.&lt;br /&gt;
&lt;b&gt;Item 36: Consistently use the Override annotation&lt;/b&gt;&lt;br /&gt;
Use&amp;nbsp;@Override annotation except for the abstract methods or methods of interfaces.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://raysear.wordpress.com/2012/01/30/note-of/&quot;&gt;&lt;b&gt;Source&lt;/b&gt;&lt;/a&gt; &lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/4807755865520418279/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2013/10/notes-of-effective-java-chapter-1-6.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/4807755865520418279'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/4807755865520418279'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2013/10/notes-of-effective-java-chapter-1-6.html' title='Notes of “Effective Java” (Chapter 1-6)'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-703377821129517500</id><published>2013-07-28T18:07:00.002+05:30</published><updated>2013-07-28T18:07:55.544+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="fun"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Javapocalypse video </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;iframe allowfullscreen=&#39;allowfullscreen&#39; webkitallowfullscreen=&#39;webkitallowfullscreen&#39; mozallowfullscreen=&#39;mozallowfullscreen&#39; width=&#39;320&#39; height=&#39;266&#39; src=&#39;https://www.youtube.com/embed/E3418SeWZfQ?feature=player_embedded&#39; frameborder=&#39;0&#39;&gt;&lt;/iframe&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/703377821129517500/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2013/07/javapocalypse-video.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/703377821129517500'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/703377821129517500'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2013/07/javapocalypse-video.html' title='Javapocalypse video '/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-2064715726057831630</id><published>2011-12-27T06:19:00.001+05:30</published><updated>2014-04-12T01:15:01.847+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="design"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="runnable"/><category scheme="http://www.blogger.com/atom/ns#" term="thread factory"/><category scheme="http://www.blogger.com/atom/ns#" term="Threads"/><title type='text'>Extend Thread vs implement Runnable</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
There are two ways to create your own thread type: subclass &lt;code&gt;java.lang.Thread&lt;/code&gt; class, or implementing &lt;code&gt;java.lang.Runnable&lt;/code&gt; and pass it to &lt;code&gt;Thread&lt;/code&gt; constructor or &lt;code&gt;java.util.concurrent.ThreadFactory&lt;/code&gt;. What is the difference, and which one is better?&lt;br /&gt;
&lt;ol style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;The practical reason is, a Java class can have only one superclass. So if your thread class extends &lt;code&gt;java.lang.Thread&lt;/code&gt;, it cannot inherit from any other classes. This limits how you can reuse your application logic.&lt;/li&gt;
&lt;li&gt;From a design point of view, there should be a clean separation between how a task is identified and defined, between how it is executed. The former is the responsibility of a &lt;code&gt;Runnalbe&lt;/code&gt; impl, and the latter is job of the &lt;code&gt;Thread&lt;/code&gt; class.&lt;/li&gt;
&lt;li&gt;A &lt;code&gt;Runnable&lt;/code&gt; instance can be passed to other libraries that accept task submission, e.g., &lt;code&gt;java.util.concurrent.Executors&lt;/code&gt;. A Thread subclass inherits all the overhead of thread management and is hard to reuse.&lt;/li&gt;
&lt;li&gt;Their instances also have different lifecycle. Once a thread is started and completed its work, it&#39;s subject to garbage collection. An instance of &lt;code&gt;Runnalbe&lt;/code&gt; task can be resubmitted or retried multiple times, though usually new tasks are instantiated for each submission to ease state management.&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/2064715726057831630/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/extend-thread-vs-implement-runnable.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/2064715726057831630'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/2064715726057831630'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/extend-thread-vs-implement-runnable.html' title='Extend Thread vs implement Runnable'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-6806815672543905290</id><published>2011-12-23T00:40:00.000+05:30</published><updated>2011-12-31T11:02:11.629+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="collection-diagrams"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>O(n) for operations on Java collections</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2WYoxr8QN_nmp9etMoubf3cbVvAvlmQ-ac7e9imcqWXDu7ARIkyaOUF62nEDNdib6m0vojYaKAINKDVu5WxXoEm79CKLT6eAUCrt8BuZqMyOsHxSx9BMhSLLdl03GK2N1e_qK3ioIbkmj/s1600/collectiononotaion1.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2WYoxr8QN_nmp9etMoubf3cbVvAvlmQ-ac7e9imcqWXDu7ARIkyaOUF62nEDNdib6m0vojYaKAINKDVu5WxXoEm79CKLT6eAUCrt8BuZqMyOsHxSx9BMhSLLdl03GK2N1e_qK3ioIbkmj/s320/collectiononotaion1.png&quot; width=&quot;287&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/6806815672543905290/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/on-for-operations-on-java-collections.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6806815672543905290'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6806815672543905290'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/on-for-operations-on-java-collections.html' title='O(n) for operations on Java collections'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2WYoxr8QN_nmp9etMoubf3cbVvAvlmQ-ac7e9imcqWXDu7ARIkyaOUF62nEDNdib6m0vojYaKAINKDVu5WxXoEm79CKLT6eAUCrt8BuZqMyOsHxSx9BMhSLLdl03GK2N1e_qK3ioIbkmj/s72-c/collectiononotaion1.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-6829303733904801042</id><published>2011-12-23T00:35:00.000+05:30</published><updated>2011-12-31T11:02:11.651+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="collection-diagrams"/><category scheme="http://www.blogger.com/atom/ns#" term="collection-interface"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Collection Interfaces - Java Collection Diagrams</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVjAm-WxdA5h3boaCf_tiBOpiIN-nDhwq1eRXvN2oosjcSupCRZm5hX-e8218DXnyHlyi_mVAzlwKsFRbl9lpCTcdk3WU5PSQmdrrkjQu72zRJCMfh8WEnk2zS02skg8JgkWCZ4bZtkWBt/s1600/java-collection+without+map.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;496&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVjAm-WxdA5h3boaCf_tiBOpiIN-nDhwq1eRXvN2oosjcSupCRZm5hX-e8218DXnyHlyi_mVAzlwKsFRbl9lpCTcdk3WU5PSQmdrrkjQu72zRJCMfh8WEnk2zS02skg8JgkWCZ4bZtkWBt/s640/java-collection+without+map.jpg&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
And when we include maps:&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCbsiuYFqUqt05UH7ZwBPkZE6MujIbgbkao2bPvSuUaTttRfe5tRDg1h8SQ-UzYDcjrnH5MUBSyHmZ4NJHxQQXZl0Q5-GYnf1F03f1m76BVzXDYa22usDfrIcIoX5TawzomEYC0KWXL4g/s1600/collections-map.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;242&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgCbsiuYFqUqt05UH7ZwBPkZE6MujIbgbkao2bPvSuUaTttRfe5tRDg1h8SQ-UzYDcjrnH5MUBSyHmZ4NJHxQQXZl0Q5-GYnf1F03f1m76BVzXDYa22usDfrIcIoX5TawzomEYC0KWXL4g/s400/collections-map.jpg&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/6829303733904801042/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/collection-interfaces-java-collection.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6829303733904801042'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6829303733904801042'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/collection-interfaces-java-collection.html' title='Collection Interfaces - Java Collection Diagrams'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgVjAm-WxdA5h3boaCf_tiBOpiIN-nDhwq1eRXvN2oosjcSupCRZm5hX-e8218DXnyHlyi_mVAzlwKsFRbl9lpCTcdk3WU5PSQmdrrkjQu72zRJCMfh8WEnk2zS02skg8JgkWCZ4bZtkWBt/s72-c/java-collection+without+map.jpg" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-6956297955009930118</id><published>2011-12-23T00:21:00.001+05:30</published><updated>2011-12-31T11:02:11.675+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="reference"/><category scheme="http://www.blogger.com/atom/ns#" term="soft-reference"/><category scheme="http://www.blogger.com/atom/ns#" term="weak-reference"/><title type='text'>Avoid memory leaks using Weak&amp;amp;Soft references</title><content type='html'>&lt;p&gt;Some Java developers believe that there is no such a thing as memory leak in Java (thanks to the fabulous automatic Garbage Collection concept) &lt;p&gt;Some others had met the OutOfMemoryError and understood that the JVM has encountered some memory issue but they are not sure if it’s all about the code or maybe even an OS issue… &lt;p&gt;The&lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/api/java/lang/OutOfMemoryError.html&quot;&gt; OutOfMemoryError API docs&lt;/a&gt; reveals that it “Thrown when the Java Virtual Machine cannot allocate an object because it is out of memory, and no more memory could be made available by the garbage collector. “ &lt;p&gt;As we know, the JVM has a parameter that represents the maximum heap size(-Xmx), hence we can defiantly try to increase the heap size. yet some code can generate new instances all the time, if those instances are accessible(being referenced by the main program – in a recursive manner) for the entire program life span, then the GC won’t reclaim those instances. hence the heap will keep increasing and eventually a OutOfMemoryError will be thrown &amp;lt;- we call that memory leak. &lt;p&gt;Our job as Java developers is to release references (that are accessible by the main program) that we won’t use in the future. by doing that we are making sure that the GC will reclaim those instances (free the memory that those instances occupying in the heap). &lt;p&gt;In some cases we reference an instance from 2 different roots. one root represent a fast-retrieval space(e.g. HashMap) and the other manages the real lifespan of that instance. Sometimes we would like to remove the reference of that instance from one root and get the other root(fast retrieval) reference removed automatically. &lt;p&gt;We wouldn’t want to do it manually due to the fact that we are not C++ developers and we wouldn’t like to manage the memory manually.. &lt;p&gt;&lt;strong&gt;Weak references&lt;/strong&gt; &lt;p&gt;In order to solve that we can use &lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/WeakReference.html&quot;&gt;WeakReference.&lt;/a&gt; &lt;p&gt;Instances that are being referenced by &lt;strong&gt;only&lt;/strong&gt; Weak references will get collected on the next collection! (&lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/package-summary.html#reachability&quot;&gt;Weakly reachable&lt;/a&gt;), in other words those references don’t protect their value from the garbage collector. &lt;p&gt;Hence if we would like to manage the life span of an instance by one reference only, we will use the WeakReference object to create all the other references. &lt;p&gt; ( usage: WeakReference wr = new WeakReference(someObject);) &lt;p&gt;In some apps we would like to add all our existing references to some static list, those references should not be strong, otherwise we would have to clean those references manually, we would add those references to the list using this code. &lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; addWeakReference(Object o){&lt;br&gt;   refList.add(&lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; WeakReference(o));&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;since most of the WeakReferences use cases needs a Map data structure, there is an implementation of Map that add a WeakReference automatically for you – &lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/api/java/util/WeakHashMap.html&quot;&gt;WeakHashMap&lt;/a&gt;&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Soft References&lt;/strong&gt;&lt;br /&gt;&lt;p&gt;I saw few implementations of Cache using weak references (e.g. the cache is just a WeakHashMap =&amp;gt; the GC is cleaning old objects in the cahce), without WeakReferences naive cache can easily cause memory leaks and therefor weak references might be a solution for that.&lt;br /&gt;&lt;p&gt;The main problem is that the GC will clean the cached-object probably and most-likely faster then you need.&lt;br /&gt;&lt;p&gt;Soft references solve that, those references are exactly like weak references, yet the GC won’t claim them as fast. we can be sure that the JVM won’t throw an OutOfMemory before it will claim all the soft and weak references!&lt;br /&gt;&lt;p&gt;using a&lt;a href=&quot;http://java.sun.com/j2se/1.4.2/docs/api/java/lang/ref/SoftReference.html&quot;&gt; soft references&lt;/a&gt; in order to cache considered the naive generic cache solution. (poor’s men cache)&lt;br /&gt;&lt;p&gt;( usage:SoftReference sr = new SoftReference(someObject);)&lt;br /&gt;  </content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/6956297955009930118/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/avoid-memory-leaks-using-weak.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6956297955009930118'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6956297955009930118'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/avoid-memory-leaks-using-weak.html' title='Avoid memory leaks using Weak&amp;amp;Soft references'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-8173453844979020348</id><published>2011-12-23T00:11:00.001+05:30</published><updated>2013-07-28T18:01:05.034+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="fun"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Java Forever - Official Video </title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;315&quot; src=&quot;http://www.youtube.com/embed/kLO1djacsfg&quot; width=&quot;560&quot;&gt;&lt;/iframe&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/8173453844979020348/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/java-forever.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/8173453844979020348'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/8173453844979020348'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/java-forever.html' title='Java Forever - Official Video '/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/kLO1djacsfg/default.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-2803900421849722349</id><published>2011-12-22T23:58:00.001+05:30</published><updated>2011-12-31T11:02:11.718+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>One java - 2 compilers (Javac and JIT)</title><content type='html'>&lt;p&gt;It seems like that for students there is a lot of confusion regarding how Java/The JVM works because there are TWO compilers involve, so when someone mentions a compiler or the Just In Time compiler some of them would imagine it’s the same one, the Java Compiler.. &lt;p&gt;&lt;strong&gt;So how does it really works?&lt;/strong&gt; &lt;p&gt;It’s simple.. &lt;p&gt;1) You write Java code (file.java) which compiles to “&lt;a href=&quot;http://en.wikipedia.org/wiki/Java_bytecode&quot;&gt;bytecode&lt;/a&gt;“, this is done using the &lt;strong&gt;‘&lt;a href=&quot;http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html&quot;&gt;javac&lt;/a&gt;” &lt;/strong&gt;the 1st compiler. &lt;p&gt;It’s well known fact that Java can be written once get compiled and run anywhere (on any platform) which mean that different types of JVM can get installed over any type of platform and read the same good old &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_bytecode&quot;&gt;byte code&lt;/a&gt; &lt;p&gt;2) Upon execution of a Java program (the class file or Jar file that consists of some classes and other resources) the JVM should somehow execute the program and somehow translate it to the specific platform &lt;a href=&quot;http://en.wikipedia.org/wiki/Machine_code&quot;&gt;machine code&lt;/a&gt;. &lt;p&gt;In the first versions of Java, the JVM was a “stupid” interprater that executes byte-code line by line….that was extremely slow…people got mad, there were a lot of “lame-java, awesome c” talks…and the JVM guys got irratated and reinvented the JVM. &lt;p&gt;the “new” JVM initially was available as an add-on for Java 1.2 later it became the default Sun JVM (1.3+). &lt;p&gt;So what did they do? they added a second compiler.. Just In Time compiler(aka JIT).. &lt;p&gt;Instead of interpreting line by line, the &lt;a href=&quot;http://en.wikipedia.org/wiki/Just-in-time_compilation&quot;&gt;JIT&lt;/a&gt; compiler compiles the byte-code to machine-code right after the execution.. &lt;p&gt;Moreover, the &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_Virtual_Machine&quot;&gt;JVM &lt;/a&gt;is getting smarter upon every release, it “knows” when it should interpat the code line-by-line and what parts of the code should get compiled beforehand (still on runtime). &lt;p&gt;It does that by taking real-usage statistics, and a long-list of super-awesome heuristics.. &lt;p&gt;The &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_Virtual_Machine&quot;&gt;JVM &lt;/a&gt;can get configured by the user in order to disable/enable some of those heuristics.. &lt;p&gt;&lt;strong&gt;To summarize&lt;/strong&gt;, In order to execute java code, you use two different compilers, the first one(&lt;a href=&quot;http://download.oracle.com/javase/1.4.2/docs/tooldocs/windows/javac.html&quot;&gt;javac&lt;/a&gt;) is generic and compiles java to &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_bytecode&quot;&gt;bytecode&lt;/a&gt;, the second(&lt;a href=&quot;http://en.wikipedia.org/wiki/Just-in-time_compilation&quot;&gt;jit&lt;/a&gt;) is platform-dependent and compiles some portions of the &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_bytecode&quot;&gt;bytecode &lt;/a&gt;to machine-code in runtime!   </content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/2803900421849722349/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/one-java-2-compilers-javac-and-jit.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/2803900421849722349'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/2803900421849722349'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/one-java-2-compilers-javac-and-jit.html' title='One java - 2 compilers (Javac and JIT)'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-3309122013041730484</id><published>2011-12-22T23:55:00.001+05:30</published><updated>2011-12-31T11:02:11.739+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="optimization"/><title type='text'>Optimizing and Speeding up the code in Java</title><content type='html'>&lt;p&gt;&lt;strong&gt;Finalizers: &lt;/strong&gt;object that overwrites the finalize() method (“Called by the garbage collector on an object when garbage collection determines that there are no more references to the object.”) is slower!(for both allocation and collection) if it’s not a must, do clean ups in other ways ( e.g. in JDBC close the connection using the try-catch-finally block instead) &lt;p&gt;&lt;strong&gt;New is expensive&lt;/strong&gt;: creating new heavy object() on the heap is expensive!, it’s recommended to recycle old objects (by changing their fields) or use the &lt;a href=&quot;http://k2java.blogspot.com/2011/02/flyweight-pattern.html&quot;&gt;flyweight design pattern&lt;/a&gt;. &lt;p&gt;&lt;strong&gt;Strings : &lt;/strong&gt;(1) Strings are immutable which mean that upon usage of the + operator between 2 strings the JVM will generate a new String(s1+s2) on the heap (expensive as I just mentioned), in order to avoid that, it’s recommended to use the StringBuffer.(Update) since JDK 1.5 was introduced&amp;nbsp; &lt;a href=&quot;http://java.sun.com/j2se/1.5.0/docs/api/java/lang/StringBuilder.html&quot;&gt;StringBuilder &lt;/a&gt;is a better option than Stringbuffer in a single-threaded environment. &lt;p&gt;(2) Don’t convert your strings to lower case in order to compare them, use String.equalIgnoreCase() instead. &lt;p&gt;(3) String.startswith() is more expensive than String.charat(0) for the first character. &lt;p&gt;&lt;strong&gt;Inline method: &lt;/strong&gt;inline is a compiler feature, when you call a method from anywhere in your code, the compiler copies the content of the inline method and replace the line that calls the method with it. &lt;p&gt;Obviously,It saves runtime time: (1) there is no need to call a method (2) no dynamic dispatch. &lt;p&gt;In some languages you can annotate a method to be inline, yet in Java it’s impossible, it’s the compiler decision. &lt;p&gt;the compiler will consider inline a method if the method is &lt;strong&gt;private&lt;/strong&gt;. &lt;p&gt;My recommendation is to search in your code for methods that are heavily used(mostly in loops) and annotate those method as private if possible. &lt;p&gt;&lt;strong&gt;Don’t invent the wheel: &lt;/strong&gt;the java api is smart and sophisticated and in some cases use native implementation, code that you probably can’t compete with. unless you know what you are doing (performance wise) don’t rewrite methods that already exists in the java API. e.g. benchmarks showed that coping and array using a for loop is at least n/4 times slower than using System.arraycopy() &lt;p&gt;&lt;strong&gt;Reflection:&lt;/strong&gt; reflection became much faster for those of you who use the most recent JVMs, yet using reflection is most certainly slower than not using it. which mean that you better avoid reflection if there is no need. &lt;p&gt;&lt;strong&gt;Synchronization:&lt;/strong&gt; Some data structures auto-support concurrency, in case of a single thread application don’t use those to avoid overhead e.g. use ArrayList instead of a Vector &lt;p&gt;&lt;strong&gt;Multithreads:&lt;/strong&gt; in case of a multi processor use threads, it will defiantly speed up your code, if you are not a thread expert some &lt;a href=&quot;http://www.aartbik.com/JAVAR/index.html&quot;&gt;compilers &lt;/a&gt;know how to restructure your code to thread automatically for you. you can always read a &lt;a href=&quot;http://java.sun.com/docs/books/tutorial/essential/concurrency/&quot;&gt;java threads tutorial&lt;/a&gt; as well &lt;p&gt;&lt;strong&gt;Get familiar with the standard data structures&lt;/strong&gt;:&amp;nbsp;&amp;nbsp; e.g. if you need a dast for puting and retriving objects use HashMap and not an ArrayList. (O(1)) &lt;p&gt;&lt;strong&gt;Add an id field, for faster equals(): &lt;/strong&gt;object that contains many fields are hard to compare ( equals() wise), to avoid that add an id(unique) field to your object and overwrite the equals() method to compare ids only. &lt;p&gt;&lt;strong&gt;Be careful, In case&amp;nbsp; your code already works, optimizing it is a sure way to create new bugs and make your code less maintainable!&lt;/strong&gt; &lt;p&gt;it’s highly recommended to time your method before and after an optimization.   </content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/3309122013041730484/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/12/optimizing-and-speeding-up-code-in-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/3309122013041730484'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/3309122013041730484'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/12/optimizing-and-speeding-up-code-in-java.html' title='Optimizing and Speeding up the code in Java'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-248595473609276049</id><published>2011-10-09T16:13:00.000+05:30</published><updated>2011-12-31T11:02:11.761+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term=".net"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>101 reasos why java is better than .net</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;I was once reading the interesting article and so I decided to share on this blog:&lt;br /&gt;
&lt;a href=&quot;http://www.helpdesk-software.ws/it/29-04-2004.htm&quot;&gt;&lt;b&gt;link&lt;/b&gt;&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/248595473609276049/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/10/101-reasos-why-java-is-better-than-net.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/248595473609276049'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/248595473609276049'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/10/101-reasos-why-java-is-better-than-net.html' title='101 reasos why java is better than .net'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-551934688843419735</id><published>2011-10-09T16:08:00.000+05:30</published><updated>2011-12-31T11:02:11.783+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="divide-by-zero-exception"/><category scheme="http://www.blogger.com/atom/ns#" term="double"/><category scheme="http://www.blogger.com/atom/ns#" term="exception"/><category scheme="http://www.blogger.com/atom/ns#" term="interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Divide by Zero in case of Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;One of the good interview questions I found was related to divide by zero.&lt;br /&gt;
Lets take the case:&lt;br /&gt;
&lt;br /&gt;
What will be the output of this code snippet?&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f6f8ff; color: #000020;&quot;&gt;&lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;class&lt;/span&gt; NaN &lt;span style=&quot;color: #406080;&quot;&gt;{&lt;/span&gt;
 
    &lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #7779bb;&quot;&gt;void&lt;/span&gt; main&lt;span style=&quot;color: #308080;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6679aa; font-weight: bold;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #308080;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #308080;&quot;&gt;]&lt;/span&gt; args&lt;span style=&quot;color: #308080;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #406080;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #7779bb;&quot;&gt;double&lt;/span&gt; d &lt;span style=&quot;color: #308080;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;2.0&lt;/span&gt; &lt;span style=&quot;color: #308080;&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: #406080;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #6679aa; font-weight: bold;&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080;&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080;&quot;&gt;.&lt;/span&gt;println&lt;span style=&quot;color: #308080;&quot;&gt;(&lt;/span&gt;d&lt;span style=&quot;color: #308080;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #406080;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #406080;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;br /&gt;
What&#39;s the answer: The code will not compile or will it throw a DivideByZero error? Both are wrong. The code compiles fine and the output is,&lt;br /&gt;
&lt;div style=&quot;background-color: #cfe2f3; font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;Infinity&lt;/div&gt;&lt;br /&gt;
Let&#39;s check another code snippet,&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background: #f6f8ff; color: #000020;&quot;&gt;&lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;class&lt;/span&gt; NaN &lt;span style=&quot;color: #406080;&quot;&gt;{&lt;/span&gt;
 
    &lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold;&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #7779bb;&quot;&gt;void&lt;/span&gt; main&lt;span style=&quot;color: #308080;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6679aa; font-weight: bold;&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #308080;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #308080;&quot;&gt;]&lt;/span&gt; args&lt;span style=&quot;color: #308080;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: #406080;&quot;&gt;{&lt;/span&gt;
        &lt;span style=&quot;color: #7779bb;&quot;&gt;double&lt;/span&gt; d &lt;span style=&quot;color: #308080;&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;0.0&lt;/span&gt; &lt;span style=&quot;color: #308080;&quot;&gt;/&lt;/span&gt; &lt;span style=&quot;color: green;&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: #406080;&quot;&gt;;&lt;/span&gt;
        &lt;span style=&quot;color: #6679aa; font-weight: bold;&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080;&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080;&quot;&gt;.&lt;/span&gt;println&lt;span style=&quot;color: #308080;&quot;&gt;(&lt;/span&gt;d&lt;span style=&quot;color: #308080;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080;&quot;&gt;;&lt;/span&gt;
    &lt;span style=&quot;color: #406080;&quot;&gt;}&lt;/span&gt;
&lt;span style=&quot;color: #406080;&quot;&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;br /&gt;
The output in this case is,&lt;br /&gt;
&lt;pre style=&quot;background-color: #cfe2f3;&quot;&gt;NaN&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/551934688843419735/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/10/divide-by-zero-in-case-of-java.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/551934688843419735'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/551934688843419735'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/10/divide-by-zero-in-case-of-java.html' title='Divide by Zero in case of Java'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-2434901413522124270</id><published>2011-10-08T23:49:00.000+05:30</published><updated>2011-12-31T11:02:11.806+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="ruby"/><title type='text'>Hello World in Ruby using Netbeans</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG0G2ZewpwuFR2HJZn_dB8izUNv8jOv2dbTBCpq3t3jr4zW77JBEAzuVSBFgbUtytQIZXqfEx3hyxlEGl-INqM6Gy_ae2zc6XyeX_Dbfg3npDbH-UeDHAYLTZkGQVMER_cg3xHegyralxD/s1600/Netbeans%252Band%252BRuby.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;267&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG0G2ZewpwuFR2HJZn_dB8izUNv8jOv2dbTBCpq3t3jr4zW77JBEAzuVSBFgbUtytQIZXqfEx3hyxlEGl-INqM6Gy_ae2zc6XyeX_Dbfg3npDbH-UeDHAYLTZkGQVMER_cg3xHegyralxD/s320/Netbeans%252Band%252BRuby.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;After getting a decent knowledge of &lt;a href=&quot;http://www.technicalypto.com/2010/05/lets-ruby.html&quot;&gt;what Ruby is&lt;/a&gt; and the &lt;a href=&quot;http://www.technicalypto.com/2010/06/useful-books-on-ruby-and-ruby-on-rails.html&quot;&gt;useful resources&lt;/a&gt; that are available, lets get into action by creating our first hello world program.&lt;br /&gt;
&lt;br /&gt;
Ruby and Ruby on Rails development becomes amazingly easy using Netbeans  IDE where you can perform almost all the tasks in one single IDE. &lt;a href=&quot;http://netbeans.org/downloads/index.html&quot;&gt;Download&lt;/a&gt;  and install the Netbeans-Ruby version. You should be having JDK  installed in your box, thats pretty much the pre-requisite for Netbeans  to install.&lt;br /&gt;
&lt;br /&gt;
OR you can simply download the complete version of netbeans and install ruby plugins.&lt;br /&gt;
&lt;br /&gt;
Download the ruby from &lt;a href=&quot;http://www.ruby-lang.org/en/downloads/&quot;&gt;&lt;b&gt;here&lt;/b&gt;&lt;/a&gt;. Extract it to some location, Say C:\\ruby&lt;br /&gt;
Now create new project.&lt;br /&gt;
Launch Netbeans go to File --&amp;gt; New  Project. A new project wizard should come up. Select Ruby Application i.e. C:ruby\bin.,  click Next and click Finish. Thats it!!!!&lt;br /&gt;
You dont even have to type a  Hello World. The new project template does it for you! So sweet isnt it?  Now, click on the big green Run button and the console should print out  Hello World without a hitch. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG0G2ZewpwuFR2HJZn_dB8izUNv8jOv2dbTBCpq3t3jr4zW77JBEAzuVSBFgbUtytQIZXqfEx3hyxlEGl-INqM6Gy_ae2zc6XyeX_Dbfg3npDbH-UeDHAYLTZkGQVMER_cg3xHegyralxD/s1600/Netbeans%252Band%252BRuby.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;267&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG0G2ZewpwuFR2HJZn_dB8izUNv8jOv2dbTBCpq3t3jr4zW77JBEAzuVSBFgbUtytQIZXqfEx3hyxlEGl-INqM6Gy_ae2zc6XyeX_Dbfg3npDbH-UeDHAYLTZkGQVMER_cg3xHegyralxD/s320/Netbeans%252Band%252BRuby.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;Now just run the program :)&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/2434901413522124270/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/10/hello-world-in-ruby-using-netbeans.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/2434901413522124270'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/2434901413522124270'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/10/hello-world-in-ruby-using-netbeans.html' title='Hello World in Ruby using Netbeans'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgG0G2ZewpwuFR2HJZn_dB8izUNv8jOv2dbTBCpq3t3jr4zW77JBEAzuVSBFgbUtytQIZXqfEx3hyxlEGl-INqM6Gy_ae2zc6XyeX_Dbfg3npDbH-UeDHAYLTZkGQVMER_cg3xHegyralxD/s72-c/Netbeans%252Band%252BRuby.png" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-8545279629895644450</id><published>2011-10-08T23:34:00.000+05:30</published><updated>2011-12-31T11:02:11.828+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="ruby"/><title type='text'>installing RadRails on Eclipse</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;entry&quot;&gt; I could not find a proper answer when i wanted to install RadRails on  eclipse . This is how it is done  , .... in the Eclipse menu go to&lt;br /&gt;
&lt;span style=&quot;color: #3333ff;&quot;&gt;Help--&amp;gt;Software Updates --&amp;gt; Find and Install....&lt;/span&gt;&lt;br /&gt;
then in the pop - up window which appears ,..&lt;br /&gt;
&lt;span style=&quot;color: #3333ff;&quot;&gt;Search for new features to install&lt;/span&gt;&lt;br /&gt;
and  then click next and then u will have to add 2 new remote sites, .. the  details for the sites are the ones which were very difficult to obtain ,  they are&lt;br /&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;site 1 :&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3333ff;&quot;&gt;Name :RadRails&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3333ff;&quot;&gt;URL : http://radrails.sourceforge.net/update&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #000066;&quot;&gt;site 2 :&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3333ff;&quot;&gt;Name :RDT&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #3333ff;&quot;&gt;URL : http://updatesite.rubypeople.org/release&lt;/span&gt;&lt;br /&gt;
then click  on &lt;span style=&quot;color: #3333ff;&quot;&gt;finish&lt;/span&gt; , u are almost done with the installation , u have to just follow the instructions from here on to finish the installation .&lt;br /&gt;
&lt;/div&gt;&lt;div class=&quot;ping-track&quot;&gt;       This entry was posted              on Friday, May 16, 2008        at 8:37 PM        and is filed under          &lt;a href=&quot;http://satisheerpini.blogspot.com/search/label/eclipse&quot; rel=&quot;tag&quot;&gt;eclipse&lt;/a&gt;, &lt;a href=&quot;http://satisheerpini.blogspot.com/search/label/linux&quot; rel=&quot;tag&quot;&gt;linux&lt;/a&gt;, &lt;a href=&quot;http://satisheerpini.blogspot.com/search/label/ruby&quot; rel=&quot;tag&quot;&gt;ruby&lt;/a&gt;        . You can follow any responses to this entry through the  &lt;a href=&quot;http://satisheerpini.blogspot.com/feeds/9055556815792700955/comments/default&quot; target=&quot;_blank&quot; type=&quot;application/atom+xml&quot;&gt;comments feed&lt;/a&gt; .      &lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/8545279629895644450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/10/installing-radrails-on-eclipse.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/8545279629895644450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/8545279629895644450'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/10/installing-radrails-on-eclipse.html' title='installing RadRails on Eclipse'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-7342223817894738680</id><published>2011-10-03T17:57:00.000+05:30</published><updated>2011-12-31T11:02:11.850+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collections"/><category scheme="http://www.blogger.com/atom/ns#" term="final"/><category scheme="http://www.blogger.com/atom/ns#" term="interview questions"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="list"/><title type='text'>Making collections final</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Once we write final&lt;br /&gt;
&lt;div style=&quot;background-color: #cfe2f3;&quot;&gt;ArrayList list = new ArrayList();&lt;/div&gt;&lt;br /&gt;
We can  add, delete objects from this list, but I can not&lt;br /&gt;
&lt;div style=&quot;background-color: #cfe2f3;&quot;&gt;list = new ArrayList()  or list = list1.&lt;/div&gt;&lt;br /&gt;
So declaring the &lt;code&gt;list&lt;/code&gt; final means that you cannot reassign the &lt;code&gt;list&lt;/code&gt; variable to another object.&lt;br /&gt;
&lt;br /&gt;
There are two situations in which this can be useful.  &lt;br /&gt;
&lt;ol&gt;&lt;li&gt;You want to make sure that no-one &lt;b&gt;reassigns&lt;/b&gt; your &lt;code&gt;list&lt;/code&gt;  variable once it has received its value. This can reduce complexity and  helps in understanding the semantics of your class/method. In this case  you are usually better off by using good naming conventions and  reducing method length (the class/method is already too complex to be  easily understood).&lt;/li&gt;
&lt;li&gt;When using &lt;b&gt;inner classes&lt;/b&gt; you need to declare variables as &lt;code&gt;final&lt;/code&gt; in an enclosing scope so that you can access them in the inner class. This way, Java can copy your &lt;code&gt;final&lt;/code&gt;  variable into the inner class object (it will never change its value)  and the inner class object does not need to worry what happens to the  outer class object while the inner class object is alive and needs to  access the value of that variable.&lt;/li&gt;
&lt;/ol&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/7342223817894738680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/10/making-collections-final.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7342223817894738680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7342223817894738680'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/10/making-collections-final.html' title='Making collections final'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-3512163522884933911</id><published>2011-09-27T23:55:00.000+05:30</published><updated>2011-12-31T11:02:11.872+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="difference"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="structural pattern"/><title type='text'>Difference between Proxy, Decorator, Adaptor, and Bridge Patterns ?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;post-text&quot;&gt;Proxy, Decorator, Adapter, and Bridge are all variations on &quot;wrapping&quot; a class.  But their uses are different.&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;&lt;b&gt;Proxy&lt;/b&gt; could be used when you want to  lazy-instantiate an object, or hide the fact that you&#39;re calling a  remote service, or control access to the object.&lt;br /&gt;
See &lt;a href=&quot;http://k2java.blogspot.com/2011/03/proxy-pattern.html&quot;&gt;&lt;b&gt;proxy pattern&lt;/b&gt;&lt;/a&gt; with details.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Decorator&lt;/b&gt; is also called &quot;Smart Proxy.&quot;  This is  used when you want to add functionality to an object, but not by  extending that object&#39;s type.  This allows you to do so at runtime.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Adapter / Wrapper&lt;/b&gt; is used when you have an abstract  interface, and you want to map that interface to another object which  has similar functional role, but a different interface.&lt;br /&gt;
In particular Decorator looks very close to Adapter but still there is basic difference:&lt;br /&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;1&quot; cellspacing=&quot;1&quot; style=&quot;text-align: center; width: 500px;&quot;&gt;&lt;thead&gt;
&lt;tr&gt;&lt;th scope=&quot;col&quot;&gt;       &amp;nbsp;&lt;/th&gt;      &lt;th scope=&quot;col&quot;&gt;       Adapter / Wrapper&lt;/th&gt;      &lt;th scope=&quot;col&quot;&gt;       Decorator&lt;/th&gt;     &lt;/tr&gt;
&lt;/thead&gt;    &lt;tbody&gt;
&lt;tr&gt;      &lt;td&gt;       &lt;span style=&quot;color: blue;&quot;&gt;Composes &lt;/span&gt;&quot;origin&quot; class&lt;/td&gt;      &lt;td&gt;       True&lt;/td&gt;      &lt;td&gt;       True&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;      &lt;td&gt;       &lt;span style=&quot;color: blue;&quot;&gt;Modifies &lt;/span&gt;original interface&lt;/td&gt;      &lt;td&gt;       &lt;span style=&quot;color: blue;&quot;&gt;True&lt;/span&gt;&lt;/td&gt;      &lt;td&gt;       False&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;      &lt;td&gt;       &lt;div&gt;        Modifies &lt;span style=&quot;color: blue;&quot;&gt;behavior &lt;/span&gt;of interface&lt;/div&gt;&lt;/td&gt;      &lt;td&gt;       False&lt;/td&gt;      &lt;td&gt;       &lt;span style=&quot;color: blue;&quot;&gt;True&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;      &lt;td&gt;       Proxies method calls&lt;/td&gt;      &lt;td&gt;       True&lt;/td&gt;      &lt;td&gt;       True&lt;/td&gt;     &lt;/tr&gt;
&lt;/tbody&gt;   &lt;/table&gt;&lt;div&gt;&amp;nbsp;   &lt;/div&gt;See &lt;a href=&quot;http://k2java.blogspot.com/2011/03/adapter-pattern-wrapper-pattern.html&quot;&gt;&lt;b&gt;Adapter pattern&lt;/b&gt;&lt;/a&gt; and &lt;a href=&quot;http://k2java.blogspot.com/2011/02/decorator-pattern.html&quot;&gt;&lt;b&gt;Decorator Pattern&lt;/b&gt;&lt;/a&gt; with examples.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Bridge&lt;/b&gt; is very similar to Adapter, but we call  it Bridge when you define both the abstract interface and the underlying  implementation.  I.e. you&#39;re not adapting to some legacy or third-party  code, you&#39;re the designer of all the code but you need to be able to  swap out different implementations.&lt;br /&gt;
See &lt;a href=&quot;http://k2java.blogspot.com/2011/05/bridge-pattern.html&quot;&gt;&lt;b&gt;bridge pattern&lt;/b&gt;&lt;/a&gt; with example.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Facade&lt;/b&gt; is a higher-level (read: simpler)  interface to a subsystem of one or more classes.  Think of Facade as a  sort of container for other objects, as opposed to simply a wrapper. So you don&#39;t have to worry about so many things, just call the facade to do all the stuff.&lt;br /&gt;
See &lt;a href=&quot;http://k2java.blogspot.com/2011/03/facade-pattern.html&quot;&gt;&lt;b&gt;Facade pattern&lt;/b&gt;&lt;/a&gt; with example.&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/3512163522884933911/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/09/difference-between-proxy-decorator.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/3512163522884933911'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/3512163522884933911'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/09/difference-between-proxy-decorator.html' title='Difference between Proxy, Decorator, Adaptor, and Bridge Patterns ?'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-4861077407351174112</id><published>2011-09-27T19:17:00.000+05:30</published><updated>2011-12-31T11:02:11.893+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="abstract"/><category scheme="http://www.blogger.com/atom/ns#" term="fizz"/><category scheme="http://www.blogger.com/atom/ns#" term="Interface"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Interface And Abstraction – Spiritual Explanation</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;“What is the difference between interface and abstract class?”, a typical interview question   . Answer is so obvious, and we can list at least 3 differences. Here is  an attempt to correlate Object Oriented world with spiritual world, and  explain abstraction and interface.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;“Soul is an abstract notion realized by  concrete living being”. So as per OO world, if you got “Soul” as  abstract class, you got to have a concrete class “Life”. Soul can not  operate without Life.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Just like I said object is an entity  with an objective to live, abstract class can live through concrete  class. Inheritance is the only mechanism for an abstract class to live.  Spiritually, Soul operates through life.&amp;nbsp;Life is an object with a  process thread running in it.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;In Spiritual world, it is said that  “Soul carries the karma (good or bad actions) from previous life, which  has influences in the new life”. If you consider this statement, karma  is the action log recorded into the abstract class.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;Body is an interface to life (and hence  interface to Soul) and real world. so IBody can be implemented by body  of human, bird or any living creature. Once the objective is complete in  this real world, destructor is called on life object. However actions  are kept recorded (like log files) in the abstract class through out  this new life.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;In every new life actions recorded in abstract class is not revealed, as it is private declaration   . Only the ultimate object builder (God) &amp;nbsp;has access to it. Object  builder reads this private variable, and makes the decision of choosing a  new interface for Soul. Dispose method is programmed to be called in a  timer function. When timer event fires, dispose is called, and the Life  object is disposed terminating the process thread.&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;text-align: justify;&quot;&gt;After thread is terminated and Life is  disposed, Object builder reads the record log from Soul class. Based on  the karma recorded in the log, a new object with the suitable interface  (body) is created, and Life is induced into it with a process thread.  And a timer is started in the Life class, which when fires Thread Abort  is called. &lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/4861077407351174112/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/09/interface-and-abstraction-spiritual.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/4861077407351174112'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/4861077407351174112'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/09/interface-and-abstraction-spiritual.html' title='Interface And Abstraction – Spiritual Explanation'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-7408972765458933242</id><published>2011-09-22T18:20:00.001+05:30</published><updated>2011-12-31T11:02:11.914+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="deep copy"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="shallow copy"/><title type='text'>Shallow Copy and Deep Copy</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;There are two ways to make a copy of an object called shallow copy and deep copy.&lt;br /&gt;
Here is what they mean:&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Shallow Copy&lt;/span&gt;&lt;br /&gt;
Shallow  copy is a bit-wise copy of an object. A new object is created that has  an exact copy of the values in the original object. If any of the fields  of the object are references to other objects, just the references are  copied. Thus, if the object you are copying contains references to yet  other objects, a shallow copy refers to the same subobjects.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: large;&quot;&gt;Deep Copy&lt;/span&gt;&lt;br /&gt;
Deep  copy is a complete duplicate copy of an object. If an object has  references to other objects, complete new copies of those objects are  also made. A deep copy generates a copy not only of the primitive values  of the original object, but copies of all subobjects as well, all the  way to the bottom. If you need a true, complete copy of the original  object, then you will need to implement a full deep copy for the object.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;img align=&quot;center&quot; alt=&quot;Shallow Copy&quot; height=&quot;132&quot; src=&quot;http://www.jusfortechies.com/images/core-java/shallow_copy.png&quot; style=&quot;border-style: none; margin: 0px auto; padding: 0px;&quot; width=&quot;400&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;color: #0b5394;&quot;&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Shallow Copy&lt;/span&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;img align=&quot;center&quot; alt=&quot;Deep Copy&quot; height=&quot;139&quot; src=&quot;http://www.jusfortechies.com/images/core-java/deep_copy.png&quot; style=&quot;border-style: none; margin: 0px auto; padding: 0px;&quot; width=&quot;400&quot; /&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr style=&quot;color: #0b5394;&quot;&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: small;&quot;&gt;Deep Copy&lt;/span&gt;&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;br /&gt;
&lt;b&gt;You may like :&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;a href=&quot;http://k2java.blogspot.com/2011/05/prototype-pattern.html&quot;&gt;Design patterns using shallow copy and deep copy - Prototype pattern&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://k2java.blogspot.com/2011/04/java-clone-shallow-copy-and-deep-copy.html&quot;&gt;Shallow and deep copy using clone&lt;/a&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/7408972765458933242/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/09/shallow-copy-and-deep-copy.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7408972765458933242'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7408972765458933242'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/09/shallow-copy-and-deep-copy.html' title='Shallow Copy and Deep Copy'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-359398050844593300</id><published>2011-09-22T18:06:00.000+05:30</published><updated>2011-09-22T18:06:08.753+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="design pattern"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="mvc"/><title type='text'>MVC Pattern Basics</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Definition&lt;/span&gt;&lt;br /&gt;
The Model-View-Controller (MVC) architectural pattern is used in  software engineering to allow for the separation of three common  features in GUI applications: &lt;br /&gt;
&lt;ul class=&quot;smallmargin notes&quot;&gt;&lt;li&gt;the data access (typically via a database)&lt;/li&gt;
&lt;li&gt;the business logic (how the data will be used)&lt;/li&gt;
&lt;li&gt;user interaction (how the data and actions will be visually presented)&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
MVC pattern differenciates all the 3 aspects of application in 3 difft tiers.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Modal&lt;/span&gt;&lt;br /&gt;
It incorporates following point&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&amp;nbsp;Holds the business logic of the application.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;holds data sent between different. tiers like JSP to servlet and handler classes.&lt;/li&gt;
&lt;li&gt;One of the goals of the model is to give a presentation which does not directly refer to any specific DBMS.&amp;nbsp; &lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;View&lt;/span&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;This represents the visible user interface, so handles presentation tier of the application.&lt;/li&gt;
&lt;li&gt; it knows enough about the  data to create a coherent presentation, but does not actually do the  work of presenting the data. Instead, it provides the ability to  manipulate data &lt;b&gt;outside&lt;/b&gt; the View through event handlers defined within.&amp;nbsp; &lt;/li&gt;
&lt;li&gt;handles presentation logic, client side validation.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;e.g.&lt;/b&gt; HTML, JSP&lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Controller&lt;/span&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;handles the flow of the application&lt;/li&gt;
&lt;li&gt; Joins the Model with the View and is the heart of the business logic. &lt;/li&gt;
&lt;li&gt;It is the source of activity when an event occurs and defines the event  handling actions which access data (from the Model) and are presented to  the user (in the View). &lt;/li&gt;
&lt;li&gt;e.g. Servlets, Action Class in Struts, ActionServlet is using  struts-config.xml as front controller to handle each request and  redirect it to appropriate destination.&lt;/li&gt;
&lt;/ul&gt;&lt;div style=&quot;text-align: left;&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;Advantages&lt;/span&gt;&lt;/div&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&amp;nbsp;Increases modularity of the application.&lt;/li&gt;
&lt;li&gt;&amp;nbsp;Increases Maintainability of the application, i.e.&amp;nbsp; the data presentation can be changed without any notion  of how the data is obtained and conversely, the data access can be  changed without any knowledge of how it is to be presented.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Example&lt;/span&gt;&lt;br /&gt;
to be added soon&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;/ul&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/359398050844593300/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/09/mvc-pattern-basics.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/359398050844593300'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/359398050844593300'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/09/mvc-pattern-basics.html' title='MVC Pattern Basics'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-6899247350335806148</id><published>2011-09-22T16:47:00.000+05:30</published><updated>2011-12-31T11:02:11.935+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="random stuff"/><title type='text'>Feelings today</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlWQ8lnIbK9Z8z2jwcNjd6CLRdSO2p-j-04HWctZ5OyDtwN_leJKWbfnYY-GvnwafAarCTqb5a7dx9ucaU18wLe49zKxA3jmcaEh1-P0UO-3RxkIr-N8bS5xXZO_tX0UQaf8M_LOmZ7XSy/s1600/feelings-poster-2.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlWQ8lnIbK9Z8z2jwcNjd6CLRdSO2p-j-04HWctZ5OyDtwN_leJKWbfnYY-GvnwafAarCTqb5a7dx9ucaU18wLe49zKxA3jmcaEh1-P0UO-3RxkIr-N8bS5xXZO_tX0UQaf8M_LOmZ7XSy/s640/feelings-poster-2.jpg&quot; width=&quot;420&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/6899247350335806148/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/09/feelings-today.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6899247350335806148'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/6899247350335806148'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/09/feelings-today.html' title='Feelings today'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlWQ8lnIbK9Z8z2jwcNjd6CLRdSO2p-j-04HWctZ5OyDtwN_leJKWbfnYY-GvnwafAarCTqb5a7dx9ucaU18wLe49zKxA3jmcaEh1-P0UO-3RxkIr-N8bS5xXZO_tX0UQaf8M_LOmZ7XSy/s72-c/feelings-poster-2.jpg" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-7354429667903061127</id><published>2011-09-22T16:34:00.000+05:30</published><updated>2011-12-31T11:02:11.959+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="core-java"/><category scheme="http://www.blogger.com/atom/ns#" term="double"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Java: Rounding off to 2 decimal places</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Seeking the simplest way to round off a float value to 2 decimal places, i found these:&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 1:&lt;/strong&gt;&lt;br /&gt;
x = (double)int((x+0.005)*100.0)/100.0;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 2:&lt;/strong&gt;&lt;br /&gt;
x = Math.round(x*100.0) / 100.0;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 3:&lt;/strong&gt;&lt;br /&gt;
DecimalFormat df2 = new DecimalFormat( &quot;#,###,###,##0.00&quot; );&lt;br /&gt;
double dd = 100.2397;&lt;br /&gt;
double dd2dec = new Double(df2.format(dd)).doubleValue();&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 4:&lt;/strong&gt;&lt;br /&gt;
f = (float) (Math.round(n*100.0f)/100.0f);&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 5:&lt;/strong&gt;&lt;br /&gt;
double r = 5.1234;&lt;br /&gt;
System.out.println(r); // r is 5.1234&lt;br /&gt;
int decimalPlaces = 2;&lt;br /&gt;
BigDecimal bd = new BigDecimal(r);&lt;br /&gt;
bd = bd.setScale(decimalPlaces, BigDecimal.ROUND_HALF_UP); // setScale is immutable&lt;br /&gt;
r = bd.doubleValue();&lt;br /&gt;
System.out.println(r); // r is 5.12&lt;br /&gt;
&lt;br /&gt;
[source:&lt;a href=&quot;http://www.thescripts.com/&quot;&gt; www.thescripts.com&lt;/a&gt;, accuracy unchecked]&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;How I did it:&lt;/strong&gt;&lt;br /&gt;
float percentage = score.floatValue()/(qapairs.length*10)*100; //my float value&lt;br /&gt;
percentage = Float.valueOf((new DecimalFormat(&quot;###.00&quot;).format(percentage))); &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/7354429667903061127/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/09/java-rounding-off-to-2-decimal-places.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7354429667903061127'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7354429667903061127'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/09/java-rounding-off-to-2-decimal-places.html' title='Java: Rounding off to 2 decimal places'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-7453603572275455457</id><published>2011-07-15T04:37:00.000+05:30</published><updated>2011-12-31T11:02:11.983+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="NPE / Null Pointer Exception"/><category scheme="http://www.blogger.com/atom/ns#" term="null"/><title type='text'>The Value of String.valueOf</title><content type='html'>&lt;p&gt; Most Java developers have probably had their &lt;a href=&quot;http://jeremy.zawodny.com/blog/archives/000422.html&quot;&gt;fill&lt;/a&gt; of &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/NullPointerException.html&quot;&gt;NullPointerException&lt;/a&gt;. &lt;a href=&quot;http://runtime32.blogspot.com/2009/02/javalangnullpointerexception.html&quot;&gt;Most of us&lt;/a&gt; have learned the value of doing &lt;a href=&quot;http://ozgwei.blogspot.com/2008/12/preventing-nullpointerexception.html&quot;&gt;certain things&lt;/a&gt; to reduce our &quot;&lt;a href=&quot;http://www.urbandictionary.com/define.php?term=builds%20character&quot;&gt;opportunities&lt;/a&gt;&quot; of &lt;a href=&quot;http://blog.reindel.com/2007/12/19/preventing-the-nullpointerexception-in-java/&quot;&gt;encountering the NullPointerException&lt;/a&gt;. Indeed, there is a Wiki page dedicated to &lt;a href=&quot;http://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException&quot;&gt;preventing or reducing NullPointerExceptions&lt;/a&gt;.&lt;br&gt;&lt;br&gt;Several people have argued for additional language support for improved and easier handling of potential null. These include &lt;a href=&quot;http://www.jroller.com/scolebourne/entry/java_7_null_default_and&quot;&gt;Java SE 7 proposals&lt;/a&gt;, &lt;a href=&quot;http://jonasboner.com/2006/04/04/the-optimized-null-check-pattern.html&quot;&gt;Optimized Null Check&lt;/a&gt;, and &lt;a href=&quot;http://www.cs.virginia.edu/%7Eweimer/students/kinga-ms-pres.pdf&quot;&gt;Kinga Dobolyi&lt;/a&gt;&#39;s thesis &lt;a href=&quot;http://www.cs.virginia.edu/%7Eweimer/students/kinga-ms-paper.pdf&quot;&gt;Changing Java’s Semantics for Handling Null Pointer Exceptions&lt;/a&gt;.&lt;br&gt;&lt;br&gt;Among the &lt;a href=&quot;http://www.codemonkeyism.com/archives/2009/02/04/better-null-handling-strategies-for-java/&quot;&gt;many things we can already do&lt;/a&gt; rather easily to reduce our encounters with NullPointerException, one particular easy thing to do is to apply &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29&quot;&gt;String.valueOf(Object)&lt;/a&gt; when appropriate. The &lt;code&gt;String.valueOf(Object)&lt;/code&gt; method, as its &lt;a href=&quot;http://java.sun.com/j2se/javadoc/&quot;&gt;Javadoc&lt;/a&gt;-generated &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29&quot;&gt;documentation&lt;/a&gt; states, returns &quot;null&quot; if the passed in object is &lt;code&gt;null&lt;/code&gt; and returns the results on the passed-in &lt;code&gt;Object&lt;/code&gt;&#39;s &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/Object.html#toString%28%29&quot;&gt;toString()&lt;/a&gt; call if the passed-in &lt;code&gt;Object&lt;/code&gt; is not null. In other words, &lt;code&gt;String.valueOf(String)&lt;/code&gt; does the null checking for you.&lt;br&gt;&lt;br&gt;The use of &lt;code&gt;String.valueOf(Object)&lt;/code&gt; is particularly useful when implementing &lt;code&gt;toString&lt;/code&gt; methods on custom classes. Because most &lt;code&gt;toString&lt;/code&gt; implementations provide the class&#39;s data members in String format, &lt;code&gt;String.valueOf(Object)&lt;/code&gt; is a natural fit. All Java objects based on classes that extend &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/Object.html&quot;&gt;Object&lt;/a&gt; provide a &lt;code&gt;toString()&lt;/code&gt; implementation even if it is simply their parent&#39;s (or even &lt;code&gt;Object&lt;/code&gt;&#39;s) implementation of &lt;code&gt;toString()&lt;/code&gt;. However, if a member class implements &lt;code&gt;toString&lt;/code&gt; but the member itself is null rather than an instance of the class, then the &lt;code&gt;toString()&lt;/code&gt; does no good (and actually leads to a &lt;code&gt;NullPointerException&lt;/code&gt; when called).&lt;br&gt;&lt;br&gt;This is demonstrated with the following example code.&lt;/p&gt; &lt;p&gt;Consider a class called PersonName, which holds last name and first name of the person:&lt;/p&gt; &lt;p id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;package&lt;/span&gt; com.vaani.string;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; * Class upon which to call toString.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; */&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;  &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; PersonName&lt;br&gt;{&lt;br&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; String lastName;&lt;br&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; String firstName;&lt;br&gt;&lt;br&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; PersonName(&lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String newLastName, &lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String newFirstName)&lt;br&gt;    {&lt;br&gt;        lastName = newLastName;&lt;br&gt;        firstName = newFirstName;&lt;br&gt;    }&lt;br&gt;&lt;br&gt;    &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;     * Provide String representation of me.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;     *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;     * @return My String representation.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;     */&lt;/span&gt;&lt;br&gt;    @Override&lt;br&gt;    &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; String toString()&lt;br&gt;    {&lt;br&gt;        &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; firstName + &lt;span style=&quot;color: #006080&quot;&gt;&quot; &quot;&lt;/span&gt; + lastName;&lt;br&gt;    }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Person.java&lt;br&gt;&lt;/p&gt;&lt;br /&gt;&lt;p id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;package&lt;/span&gt; com.vaani.string;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;  &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Person&lt;br&gt;{&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; PersonName name;&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Person( PersonName newName)&lt;br&gt;   {&lt;br&gt;      name = newName;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Provide String representation of me.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * @return My String representation.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; String toString()&lt;br&gt;   {&lt;br&gt;          &lt;span style=&quot;color: #008000&quot;&gt;//to be implemented&lt;/span&gt;&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Now this person.java has toString() which is to be implemented, but we will see how it throws NPE. Consider the main method:&lt;/p&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;PersonName personName = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; PersonName(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Flintstone&quot;&lt;/span&gt;, null);&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;//print personname as string&lt;/span&gt;&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person&#39;s Name         [DIRECT]: &quot;&lt;/span&gt; + personName);&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person&#39;s Name       [TOSTRING]: &quot;&lt;/span&gt; + personName.toString());&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person&#39;s Name [STRING.VALUEOF]: &quot;&lt;/span&gt; + String.valueOf(personName));&lt;br&gt;      &lt;/pre&gt;Output will be:&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;Person&lt;span style=&quot;color: #006080&quot;&gt;&#39;s Name         [DIRECT]: null Flintstone&lt;br&gt;Person&#39;&lt;/span&gt;s Name       [TOSTRING]: null Flintstone&lt;br&gt;Person&#39;s Name [STRING.VALUEOF]: null Flintstone&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;Now consider the toString() func is represented as &lt;br&gt;&lt;br&gt;Also if we call Person&#39;s constructor with personName(instance created above), everything will still be fine:&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;PersonName name;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;//code skipped&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; String toString()&lt;br&gt;{&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;// Don&#39;t use -- can lead to runtime error (NullPointerException)&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; name.toString();&lt;br&gt;}&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;Now as we did earlier:&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;PersonName personName = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; PersonName(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Flintstone&quot;&lt;/span&gt;, null);&lt;br&gt;      &lt;br&gt;      &lt;span style=&quot;color: #008000&quot;&gt;//create next object from personName&lt;/span&gt;&lt;br&gt;Person personOne = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Person(personName);&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person One         [DIRECT]: &quot;&lt;/span&gt; + personOne);&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person One       [TOSTRING]: &quot;&lt;/span&gt; + personOne.toString());&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person One [STRING.VALUEOF]: &quot;&lt;/span&gt; + String.valueOf(personOne));&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;This will work fine because of personName will return its toString() func, and dont throw any NPE. But this will result in bang:&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;Person personTwo = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Person(null);&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person Two         [DIRECT]: &quot;&lt;/span&gt; + personTwo);&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person Two       [TOSTRING]: &quot;&lt;/span&gt; + personTwo.toString());&lt;br&gt;System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person Two [STRING.VALUEOF]: &quot;&lt;/span&gt; + String.valueOf(personTwo));&lt;br&gt;      &lt;/pre&gt;Each of this above line throws exception. So please be aware of this. To avoid null pointer exception you can implement toString() func of Person like this:&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; String toString()&lt;br&gt;{&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;// It&#39;s all good&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; String.valueOf(name);&lt;br&gt;}&lt;/pre&gt;Now the personTwo will be printed as null.&lt;br&gt;Full code listing for Person.java(personName.java is already covered):&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;package&lt;/span&gt; com.vaani.string;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt;  &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Person&lt;br&gt;{&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; PersonName name;&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; Person( PersonName newName)&lt;br&gt;   {&lt;br&gt;      name = newName;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Provide String representation of me.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * @return My String representation.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; String toString()&lt;br&gt;   {&lt;br&gt;      &lt;span style=&quot;color: #008000&quot;&gt;// Don&#39;t use -- leads to compiler time error (incompatible types)&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #008000&quot;&gt;//return name;&lt;/span&gt;&lt;br&gt;&lt;br&gt;      &lt;span style=&quot;color: #008000&quot;&gt;// Don&#39;t use -- can lead to runtime error (NullPointerException)&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #008000&quot;&gt;//return name.toString();&lt;/span&gt;&lt;br&gt;&lt;br&gt;      &lt;span style=&quot;color: #008000&quot;&gt;// It&#39;s all good&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; String.valueOf(name);&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;main method to run this code:&lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;package&lt;/span&gt; com.vaani.string;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;import&lt;/span&gt; java.io.IOException;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;import&lt;/span&gt; java.io.OutputStream;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;import&lt;/span&gt; java.util.logging.Logger;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; * Example class demonstrating use of String representations available through&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; * implicit String, toString(), and String.valueOf().&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; */&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; StringValueFunc&lt;br&gt;{&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String NEW_LINE = System.getProperty(&lt;span style=&quot;color: #006080&quot;&gt;&quot;line.separator&quot;&lt;/span&gt;);&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/** Using java.util.logging. */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; Logger LOGGER = Logger.getLogger(&lt;br&gt;           StringValueFunc.&lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt;.getName());&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Main function for running tests/demonstrations.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * @param arguments Command-line arguments; none anticipated.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String[] arguments)&lt;br&gt;   {&lt;br&gt;       &lt;span style=&quot;color: #008000&quot;&gt;//takes last name and first name&lt;/span&gt;&lt;br&gt;       PersonName personName = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; PersonName(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Flintstone&quot;&lt;/span&gt;, null);&lt;br&gt;       &lt;br&gt;       &lt;span style=&quot;color: #008000&quot;&gt;//create next object from personName&lt;/span&gt;&lt;br&gt;       Person personOne = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Person(personName);&lt;br&gt;       &lt;span style=&quot;color: #008000&quot;&gt;//just initiate to null&lt;/span&gt;&lt;br&gt;       Person personTwo = &lt;span style=&quot;color: #0000ff&quot;&gt;new&lt;/span&gt; Person(null);&lt;br&gt;      printHeader(&lt;span style=&quot;color: #006080&quot;&gt;&quot;String representation of direct Strings&quot;&lt;/span&gt;, System.out);&lt;br&gt;       &lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person&#39;s Name         [DIRECT]: &quot;&lt;/span&gt; + personName);&lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person&#39;s Name       [TOSTRING]: &quot;&lt;/span&gt; + personName.toString());&lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person&#39;s Name [STRING.VALUEOF]: &quot;&lt;/span&gt; + String.valueOf(personName));&lt;br&gt;      printBlankLine(System.out);&lt;br&gt;&lt;br&gt;      printHeader(&lt;span style=&quot;color: #006080&quot;&gt;&quot;String representation of non-null complex object&quot;&lt;/span&gt;, System.out);&lt;br&gt;       &lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person One         [DIRECT]: &quot;&lt;/span&gt; + personOne);&lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person One       [TOSTRING]: &quot;&lt;/span&gt; + personOne.toString());&lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person One [STRING.VALUEOF]: &quot;&lt;/span&gt; + String.valueOf(personOne));&lt;br&gt;      printBlankLine(System.out);&lt;br&gt;&lt;br&gt;      printHeader(&lt;span style=&quot;color: #006080&quot;&gt;&quot;String representation of null complex object&quot;&lt;/span&gt;, System.out);&lt;br&gt;      &lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person Two         [DIRECT]: &quot;&lt;/span&gt; + personTwo);&lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person Two       [TOSTRING]: &quot;&lt;/span&gt; + personTwo.toString());&lt;br&gt;      System.out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Person Two [STRING.VALUEOF]: &quot;&lt;/span&gt; + String.valueOf(personTwo));&lt;br&gt;      printBlankLine(System.out);&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; printHeader(&lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String message, &lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; OutputStream out)&lt;br&gt;   {&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String headerSeparator =&lt;br&gt;         &lt;span style=&quot;color: #006080&quot;&gt;&quot;====================================================================&quot;&lt;/span&gt;;&lt;br&gt;&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;try&lt;/span&gt;&lt;br&gt;      {&lt;br&gt;         out.write((headerSeparator + NEW_LINE + message + NEW_LINE).getBytes());&lt;br&gt;         out.write((headerSeparator + NEW_LINE).getBytes());&lt;br&gt;      }&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;catch&lt;/span&gt; (IOException ioEx)&lt;br&gt;      {&lt;br&gt;         System.out.println(headerSeparator);&lt;br&gt;         System.out.println(message);&lt;br&gt;         System.out.println(headerSeparator);&lt;br&gt;         LOGGER.warning(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Could not write header information to provided OutputStream.&quot;&lt;/span&gt;);&lt;br&gt;      }&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; printBlankLine(&lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; OutputStream out)&lt;br&gt;   {&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;try&lt;/span&gt;&lt;br&gt;      {&lt;br&gt;         out.write(NEW_LINE.getBytes());&lt;br&gt;      }&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;catch&lt;/span&gt; (IOException ioEx)&lt;br&gt;      {&lt;br&gt;         System.out.println(NEW_LINE);&lt;br&gt;         LOGGER.warning(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Could not write blank line to provided OutputStream.&quot;&lt;/span&gt;);&lt;br&gt;      }&lt;br&gt;   }&lt;br&gt;&lt;br&gt;&lt;br&gt; }&lt;br&gt;&lt;/pre&gt;&lt;br&gt;The above code can be used to demonstrate building of a &lt;code&gt;toString&lt;/code&gt; method on a complex object and how its behaves when called by an owning class. The method of most interest is at the bottom of the code shown above. Two return values are commented out because of problems associated with them. The final example, using &lt;code&gt;String.valueOf(Object)&lt;/code&gt; is NOT commented out because it works the best each time it is run whether or not the complex &lt;code&gt;PersonName&lt;/code&gt; object is null. The next three images show the output for each of these presentations of the Person objects&#39; String representations.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;p&gt;Finally, the &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/String.html&quot;&gt;String&lt;/a&gt; class provides many overloaded valueOf methods. In addition to the version that was the focus of this blog post (&lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29&quot;&gt;accepts an Object&lt;/a&gt;), the other overloaded versions of valueOf accept primitive data types and arrays of primitive data types.&lt;br&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;Regardless of what the future brings in terms of improved null handling in Java, there are many tactics we can take today to reduce the unwanted (sometimes we actually do want them thrown!) &lt;a href=&quot;http://www.java-tips.org/java-se-tips/java.lang/a-small-tip-on-string-to-avoid-nullpointerexce.html&quot;&gt;occurrences&lt;/a&gt; of &lt;a href=&quot;http://c2.com/cgi/wiki?NullPointerException&quot;&gt;NullPointerException&lt;/a&gt;. One of these is to use &lt;code&gt;String.valueOf(Object)&lt;/code&gt; when appropriate.&lt;br /&gt;&lt;p&gt;&lt;strong&gt;Additional Resources&lt;/strong&gt;&lt;br /&gt;&lt;ul&gt;&lt;br /&gt;&lt;li&gt;&lt;a href=&quot;http://www.devx.com/tips/Tip/16234&quot;&gt;String.valueOf or Integer.toString()?&lt;/a&gt;&lt;br /&gt;&lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/328661/explicit-vs-implicit-call-of-tostring&quot;&gt;Explicit versus Implicit Call of toString&lt;/a&gt;&lt;br /&gt;&lt;li&gt;&lt;a href=&quot;http://www.java2s.com/Code/Java/Data-Type/TheValueofaStringwithStringvalueOfmethod.htm&quot;&gt;The Value of a String with String.valueOf() Method&lt;/a&gt;&lt;br /&gt;&lt;li&gt;&lt;a href=&quot;http://www.roseindia.net/java/java-conversion/convert-number-to-string.shtml&quot;&gt;Convert Number to String&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;  </content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/7453603572275455457/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/07/value-of-stringvalueof.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7453603572275455457'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/7453603572275455457'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/07/value-of-stringvalueof.html' title='The Value of String.valueOf'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-4108133917632191112</id><published>2011-07-15T04:10:00.000+05:30</published><updated>2011-09-18T01:59:37.539+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="effective java"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="NPE / Null Pointer Exception"/><category scheme="http://www.blogger.com/atom/ns#" term="null"/><title type='text'>Effective Java : How to Avoid NPE / Null Pointer Exception in java?</title><content type='html'>It doesn&#39;t take much Java development experience to learn firsthand what the NullPointerException is about. In fact, one person has highlighted dealing with this as the &lt;a href=&quot;http://www.javacoffeebreak.com/articles/toptenerrors.html&quot;&gt;number one mistake Java developers make&lt;/a&gt;. I &lt;a href=&quot;http://k2java.blogspot.com/2011/07/value-of-stringvalueof.html&quot;&gt;blogged previously&lt;/a&gt; on use of &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29&quot;&gt;String.value(Object)&lt;/a&gt; to reduce unwanted &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/NullPointerException.html&quot;&gt;NullPointerException&lt;/a&gt;s. There are &lt;a href=&quot;http://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException&quot;&gt;several other&lt;/a&gt; simple &lt;a href=&quot;http://blog.reindel.com/2007/12/19/preventing-the-nullpointerexception-in-java/&quot;&gt;techniques&lt;/a&gt; one can use to reduce or eliminate the &lt;a href=&quot;http://www.roseindia.net/software-tutorials/detail/22289&quot;&gt;occurrences&lt;/a&gt; of this common type of &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/RuntimeException.html&quot;&gt;RuntimeException&lt;/a&gt; that has been with us since &lt;a href=&quot;http://user.cs.tu-berlin.de/%7Ejutta/ht/JDK-1.0-quickref.html&quot;&gt;JDK 1.0&lt;/a&gt;. This blog post collects and summarizes some of the most popular of these techniques.&lt;br&gt;&lt;br&gt;&lt;b&gt;Check Each Object For Null Before Using&lt;/b&gt;&lt;br&gt;&lt;br&gt;The most sure way to avoid a &lt;a href=&quot;http://basicsuncovered.blogspot.com/2005/11/null-pointer-exception.html&quot;&gt;NullPointerException&lt;/a&gt; is to check all object references to ensure that they are not null before accessing one of the object&#39;s fields or methods. As the following example indicates, this is a very simple technique.&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;background: rgb(246,248,255); color: #000020&quot;&gt;&lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; String causeStr = &quot;adding String to Deque that is set to null.&quot;&lt;span style=&quot;color: #308080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; String elementStr = &quot;Fudd&quot;&lt;span style=&quot;color: #308080&quot;&gt;;&lt;/span&gt;&lt;br&gt;Deque&amp;lt;String&amp;gt; deque = null&lt;span style=&quot;color: #308080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;try&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   deque&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;push&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;elementStr&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;   log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;Successful at &quot;&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;+&lt;/span&gt; causeStr&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;catch (NullPointerException nullPointer)&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;causeStr&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; nullPointer&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;try&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;deque &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;      deque &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;new&lt;/span&gt; LinkedList&lt;span style=&quot;color: #308080&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;String&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;   deque&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;push&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;elementStr&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;   log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;  &lt;span style=&quot;color: #1060b6&quot;&gt;&quot;Successful at &quot;&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;+&lt;/span&gt; causeStr&lt;br&gt;       &lt;span style=&quot;color: #308080&quot;&gt;+&lt;/span&gt; &lt;span style=&quot;color: #1060b6&quot;&gt;&quot; (by checking first for null and instantiating Deque implementation)&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt;&lt;br&gt;       &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;catch (NullPointerException nullPointer)&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;causeStr&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; nullPointer&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;In the code above, the &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/util/Deque.html&quot;&gt;Deque&lt;/a&gt; used is intentionally initialized to null to facilitate the example. The code in the first &lt;code&gt;try&lt;/code&gt; block does not check for null before trying to access a &lt;a href=&quot;http://marxsoftware.blogspot.com/2008/10/java-se-6-deque.html&quot;&gt;Deque&lt;/a&gt; method. The code in the second &lt;code&gt;try&lt;/code&gt; block does check for null and instantiates an implementation of the &lt;code&gt;Deque&lt;/code&gt; (&lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html&quot;&gt;LinkedList&lt;/a&gt;) if it is null. The output from both examples looks like this:&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;font-size: 9pt; overflow: auto&quot; name=&quot;code&quot;&gt;ERROR: NullPointerException encountered while trying to adding &lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt; overflow: auto&quot; name=&quot;code&quot;&gt;String to Deque that is set to null.&lt;br&gt;java.lang.NullPointerException&lt;br&gt;INFO: Successful at adding String to Deque that is set to null. &lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt; overflow: auto&quot; name=&quot;code&quot;&gt;(by checking first for null and instantiating Deque implementation)&lt;/pre&gt;&lt;br&gt;The message following ERROR in the output above indicates that a &lt;code&gt;NullPointerException&lt;/code&gt; is thrown when a method call is attempted on the null &lt;code&gt;Deque&lt;/code&gt;. The message following INFO in the output above indicates that by checking &lt;code&gt;Deque&lt;/code&gt; for null first and then instantiating a new implementation for it when it is null, the exception was avoided altogether.&lt;br&gt;&lt;br&gt;This approach is often used and, as shown above, can be very useful in avoiding unwanted (unexpected) &lt;code&gt;NullPointerException&lt;/code&gt; instances. However, it is not without its costs. Checking for null before using every object can bloat the code, can be tedious to write, and opens more room for problems with development and maintenance of the additional code. For this reason, there has been talk of introducing Java language support for &lt;a href=&quot;http://tech.puredanger.com/java7/#null&quot;&gt;built-in null detection&lt;/a&gt;, &lt;a href=&quot;http://www.cs.virginia.edu/colloquia/event730.html&quot;&gt;automatic adding&lt;/a&gt; of these checks for null after the initial coding, &lt;a href=&quot;http://www.jroller.com/scolebourne/entry/java_7_null_safe_types&quot;&gt;null-safe types&lt;/a&gt;, use of &lt;a href=&quot;http://www.developer.com/design/article.php/3308941&quot;&gt;Aspect-Oriented Programming&lt;/a&gt; (&lt;a href=&quot;http://www.onjava.com/pub/a/onjava/2004/01/14/aop.html&quot;&gt;AOP&lt;/a&gt;) to add &lt;a href=&quot;http://www.artima.com/weblogs/viewpost.jsp?thread=43091&quot;&gt;null checking to byte code&lt;/a&gt;, and other &lt;a href=&quot;http://www.dcs.gla.ac.uk/%7Ehuttona/npd/&quot;&gt;null-detection tools&lt;/a&gt;.&lt;br&gt;&lt;br&gt;&lt;a href=&quot;http://groovy.codehaus.org/&quot;&gt;Groovy&lt;/a&gt; already provides a &lt;a href=&quot;http://jlorenzen.blogspot.com/2007/10/using-groovy-to-easily-avoid-nasty.html&quot;&gt;convenient mechanism&lt;/a&gt; for dealing with object references that are potentially null. Groovy&#39;s &lt;a href=&quot;http://groovy.codehaus.org/Operators#Operators-SafeNavigationOperator%28%3F.%29&quot;&gt;safe navigation operator&lt;/a&gt; (&lt;code&gt;?.&lt;/code&gt;) returns null rather than throwing a &lt;code&gt;NullPointerException&lt;/code&gt; when a null object reference is accessed.&lt;br&gt;&lt;br&gt;Because checking null for every object reference can be tedious and does bloat the code, many developers choose to judiciously select which objects to check for null. This typically leads to checking of null on all objects of &lt;a href=&quot;http://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException#Check_all_references_obtained_from_.27untrusted.27_methods&quot;&gt;potentially unknown origins&lt;/a&gt;. The idea here is that objects can be checked at exposed interfaces and then be assumed to be safe after the initial check.&lt;br&gt;&lt;br&gt;This is a situation where the &lt;a href=&quot;http://www.devdaily.com/java/edu/pj/pj010018/&quot;&gt;ternary operator&lt;/a&gt; can be particularly useful. Instead of&lt;br&gt;&lt;pre style=&quot;background: rgb(246,248,255); color: #000020&quot;&gt;&lt;span style=&quot;color: #595979&quot;&gt;// retrieved a BigDecimal called someObject&lt;/span&gt;&lt;br&gt;String returnString&lt;span style=&quot;color: #308080&quot;&gt;;&lt;/span&gt;&lt;br&gt;if (someObject != null)&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   returnString &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; someObject&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;toEngineeringString&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;else&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   returnString &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #1060b6&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;the ternary operator supports this more concise syntax&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;background: rgb(246,248,255); color: #000020&quot;&gt;&lt;span style=&quot;color: #595979&quot;&gt;// retrieved a BigDecimal called someObject&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; String returnString =  (someObject != null)&lt;br&gt;                           ? someObject.toEngineeringString()&lt;br&gt;                           : &quot;&quot;&lt;span style=&quot;color: #308080&quot;&gt;;&lt;/span&gt;&lt;br&gt;}&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;b&gt;Check Method Arguments for Null&lt;/b&gt;&lt;br&gt;&lt;br&gt;The technique just discussed can be used on all objects. As stated in that technique&#39;s description, many developers choose to only check objects for null when they come from &quot;untrusted&quot; sources. This often means testing for null first thing in methods exposed to external callers. For example, in a particular class, the developer might choose to check for null on all objects passed to &lt;code&gt;public&lt;/code&gt; methods, but not check for null in &lt;code&gt;private&lt;/code&gt; methods.&lt;br&gt;&lt;br&gt;The following code demonstrates this checking for null on method entry. It includes a single method as the demonstrative method that turns around and calls two methods, passing each method a single null argument. One of the methods receiving a null argument checks that argument for null first, but the other just assumes the passed-in parameter is not null.&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;background: rgb(246,248,255); color: #000020&quot;&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; Append predefined text String to the provided StringBuilder.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;@param&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; builder The StringBuilder that will have text appended to it&lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; should&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;    be non&lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;null.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;@throws&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; IllegalArgumentException Thrown if the provided StringBuilder is&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;    null.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;private&lt;/span&gt; void appendPredefinedTextToProvidedBuilderCheckForNull(&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; StringBuilder builder)&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;builder &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;         &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;IllegalArgumentException&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;br&gt;            &lt;span style=&quot;color: #1060b6&quot;&gt;&quot;The provided StringBuilder was null; non-null value must be provided.&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;      builder&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;append&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;Thanks for supplying a StringBuilder.&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #3f7f8f&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; Append predefined text String to the provided StringBuilder.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;@param&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; builder The StringBuilder that will have text appended to it&lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; should&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;    be non&lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;null.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;private&lt;/span&gt; void appendPredefinedTextToProvidedBuilderNoCheckForNull(&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; StringBuilder builder)&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;      builder&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;append&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;Thanks for supplying a StringBuilder.&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #3f7f8f&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; Demonstrate effect of checking parameters for null before trying to use&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt; passed&lt;/span&gt;&lt;span style=&quot;color: #7f9fbf; font-weight: bold&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;in parameters that are potentially null.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #3f7f8f&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;public&lt;/span&gt; void demonstrateCheckingArgumentsForNull()&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;String&lt;/span&gt; causeStr &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #1060b6&quot;&gt;&quot;provide null to method as argument.&quot;&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;      logHeader&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;DEMONSTRATING CHECKING METHOD PARAMETERS FOR NULL&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;try&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;         appendPredefinedTextToProvidedBuilderNoCheckForNull&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;NullPointerException&lt;/span&gt; nullPointer&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;         log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;causeStr&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; nullPointer&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;try&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;         appendPredefinedTextToProvidedBuilderCheckForNull&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;catch&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;IllegalArgumentException&lt;/span&gt; illegalArgument&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;         log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;causeStr&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; illegalArgument&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;When the above code is executed, the output appears as shown next.&lt;br&gt;&lt;pre style=&quot;font-size: 9pt&quot; name=&quot;code&quot;&gt;ERROR: NullPointerException encountered while trying to provide null to &lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt&quot; name=&quot;code&quot;&gt;method as argument.&lt;br&gt;java.lang.NullPointerException&lt;br&gt;ERROR: IllegalArgumentException encountered while trying to provide null &lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt&quot; name=&quot;code&quot;&gt;to method as argument.&lt;br&gt;java.lang.IllegalArgumentException: The provided StringBuilder was null;&lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt&quot; name=&quot;code&quot;&gt;non-null value must be provided.&lt;/pre&gt;&lt;br&gt;&lt;br&gt;In both cases, an error message was logged. However, the case in which a null was checked for threw an advertised &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/IllegalArgumentException.html&quot;&gt;IllegalArgumentException&lt;/a&gt; that included additional context information about when the null was encountered. Alternatively, this null parameter could have been handled in a variety of ways. For the case in which a null parameter was not handled, there were no options for how to handle it. Many people prefer to throw a &lt;code&gt;NullPolinterException&lt;/code&gt; with the additional context information when a null is explicitly discovered (see Item #60 in the Second Edition of &lt;i&gt;Effective Java&lt;/i&gt; or Item #42 in First Edition), but I have a slight preference for &lt;code&gt;IllegalArgumentException&lt;/code&gt; when it is explicitly a method argument that is null because I think the very exception adds context details and it is easy to include &quot;null&quot; in the subject.&lt;br&gt;&lt;br&gt;The technique of checking method arguments for null is really a subset of the more general technique of checking all objects for null. However, as outlined above, arguments to publicly exposed methods are often the least trusted in an application and so checking them may be more important than checking the average object for null.&lt;br&gt;&lt;br&gt;Checking method parameters for null is also a subset of the more general practice of checking method parameters for general validity as discussed in Item #38 of the Second Edition of &lt;i&gt;Effective Java&lt;/i&gt; (Item 23 in First Edition).&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Consider Primitives Rather than Objects&lt;/b&gt;&lt;br&gt;&lt;br&gt;I don&#39;t think it is a good idea to select a primitive data type (such as &lt;code&gt;int&lt;/code&gt;) over its corresponding object reference type (such as &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/Integer.html&quot;&gt;Integer&lt;/a&gt;) simply to avoid the possibility of a &lt;code&gt;NullPointerException&lt;/code&gt;, but there is no denying that one of the advantages of primitive types is that they do not lead to &lt;code&gt;NullPointerException&lt;/code&gt;s. However, primitives still must be checked for validity (a month cannot be a negative integer) and so this benefit may be small. On the other hand, primitives cannot be used in &lt;a href=&quot;http://java.sun.com/docs/books/tutorial/collections/index.html&quot;&gt;Java Collections&lt;/a&gt; and there are times one wants the ability to set a value to null.&lt;br&gt;&lt;br&gt;The most important thing is to be very cautious about the combination of primitives, reference types, and autoboxing. There is a warning in &lt;i&gt;Effective Java&lt;/i&gt; (Second Edition, Item #49) regarding the dangers, including throwing of &lt;code&gt;NullPointerException&lt;/code&gt;, related to careless mixing of primitive and reference types.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Carefully Consider Chained Method Calls&lt;/b&gt;&lt;br&gt;&lt;br&gt;A &lt;code&gt;NullPointerException&lt;/code&gt; can be very easy to find because a line number will state where it occurred. For example, a stack trace might look like that shown next:&lt;br&gt;&lt;br&gt;&lt;pre class=&quot;text&quot; name=&quot;code&quot; 9pt;=&quot;&quot;&gt;java.lang.NullPointerException&lt;br&gt;     at dustin.examples.AvoidingNullPointerExamples.&lt;/pre&gt;&lt;pre class=&quot;text&quot; name=&quot;code&quot; 9pt;=&quot;&quot;&gt;demonstrateNullPointerExceptionStackTrace(AvoidingNullPointerExamples.java:222)&lt;br&gt;     at dustin.examples.&lt;/pre&gt;&lt;pre class=&quot;text&quot; name=&quot;code&quot; 9pt;=&quot;&quot;&gt;AvoidingNullPointerExamples.main(AvoidingNullPointerExamples.java:247)&lt;/pre&gt;&lt;br&gt;The stack trace makes it obvious that the &lt;code&gt;NullPointerException&lt;/code&gt; was thrown as a result of code executed on line 222 of &lt;code&gt;AvoidingNullPointerExamples.java&lt;/code&gt;. Even with the line number provided, it can still be difficult to narrow down which object is null if there are multiple objects with methods or fields accessed on the same line.&lt;br&gt;&lt;br&gt;For example, a statement like &lt;code&gt;someObject.getObjectA().getObjectB().getObjectC().toString();&lt;/code&gt; has four possible calls that might have thrown the &lt;code&gt;NullPointerException&lt;/code&gt; attributed to the same line of code. Using a debugger can help with this, but there may be situations when it is preferable to simply break the above code up so that each call is performed on a separate line. This allows the line number contained in a stack trace to easily indicate which exact call was the problem. Furthermore, it facilitates explicit checking each object for null. However, on the downside, breaking up the code increases the line of code count (&lt;a href=&quot;http://marxsoftware.blogspot.com/2008/09/lines-of-code-and-unintended.html&quot;&gt;to some that&#39;s a positive!&lt;/a&gt;) and may not always be desirable, especially if one is certain none of the methods in question will ever be null.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Make NullPointerExceptions More Informative&lt;/b&gt;&lt;br&gt;&lt;br&gt;In the above recommendation, the warning was to consider carefully use of method call chaining primarily because it made having the line number in the stack trace for a &lt;code&gt;NullPointerException&lt;/code&gt; less helpful than it otherwise might be. However, the line number is only shown in a stack trace when the code was compiled with the debug flag turned on. If it was compiled without debug, the stack trace looks like that shown next:&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;font-size: 9pt&quot; class=&quot;text&quot; name=&quot;code&quot;&gt;java.lang.NullPointerException&lt;br&gt;     at dustin.examples.AvoidingNullPointerExamples.&lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt&quot; class=&quot;text&quot; name=&quot;code&quot;&gt;demonstrateNullPointerExceptionStackTrace(Unknown Source)&lt;br&gt;    at dustin.examples.AvoidingNullPointerExamples.main(Unknown Source)&lt;/pre&gt;&lt;br&gt;As the above output demonstrates, there is a method name, but not no line number for the &lt;code&gt;NullPointerException&lt;/code&gt;. This makes it more difficult to immediately identify what in the code led to the exception. One way to address this is to provide context information in any thrown &lt;code&gt;NullPointerException&lt;/code&gt;. This idea was demonstrated earlier when a &lt;code&gt;NullPointerException&lt;/code&gt; was caught and re-thrown with additional context information as a &lt;code&gt;IllegalArgumentException&lt;/code&gt;. However, even if the exception is simply re-thrown as another &lt;code&gt;NullPointerException&lt;/code&gt; with context information, it is still helpful. The context information helps the person debugging the code to more quickly identify the true cause of the problem.&lt;br&gt;&lt;br&gt;The following example demonstrates this principle.&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;background: rgb(246,248,255); color: #000020&quot;&gt;&lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; Calendar nullCalendar = null&lt;span style=&quot;color: #308080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;br&gt;try&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;Date&lt;/span&gt; date &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; nullCalendar&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;getTime&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;catch (NullPointerException nullPointer)&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;NullPointerException with useful data&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; nullPointer&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;br&gt;try&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;if&lt;/span&gt; &lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;nullCalendar &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;null&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;      &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;throw&lt;/span&gt; &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;new&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;NullPointerException&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;Could not extract Date from provided Calendar&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #200080; font-weight: bold&quot;&gt;final&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;Date&lt;/span&gt; date &lt;span style=&quot;color: #308080&quot;&gt;=&lt;/span&gt; nullCalendar&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;getTime&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;catch (NullPointerException nullPointer)&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;{&lt;/span&gt;&lt;br&gt;   log&lt;span style=&quot;color: #308080&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #1060b6&quot;&gt;&quot;NullPointerException with useful data&quot;&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; nullPointer&lt;span style=&quot;color: #308080&quot;&gt;,&lt;/span&gt; &lt;span style=&quot;color: #6679aa; font-weight: bold&quot;&gt;System&lt;/span&gt;&lt;span style=&quot;color: #308080&quot;&gt;.&lt;/span&gt;out&lt;span style=&quot;color: #308080&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #406080&quot;&gt;;&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #406080&quot;&gt;}&lt;/span&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;The output from running the above code looks as follows.&lt;br&gt;&lt;br&gt;&lt;pre style=&quot;font-size: 9pt&quot; class=&quot;text&quot; name=&quot;code&quot;&gt;ERROR: NullPointerException encountered while trying to NullPointerException &lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt&quot; class=&quot;text&quot; name=&quot;code&quot;&gt;with useful data&lt;br&gt;java.lang.NullPointerException&lt;br&gt;ERROR: NullPointerException encountered while trying to&lt;/pre&gt;&lt;pre style=&quot;font-size: 9pt&quot; class=&quot;text&quot; name=&quot;code&quot;&gt;NullPointerException with useful data&lt;br&gt;java.lang.NullPointerException: Could not extract Date from provided Calendar&lt;/pre&gt;&lt;br&gt;The first error does not provide any context information and only conveys that it is a &lt;code&gt;NullPointerException&lt;/code&gt;. The second error, however, had explicit context information added to it which would go a long way in helping identify the source of the exception.&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;b&gt;Use String.valueOf Rather than toString&lt;/b&gt;&lt;br&gt;&lt;br&gt;As described previously, one of the surest methods for avoiding &lt;code&gt;NullPointerException&lt;/code&gt; is to check the object being referenced for null first. The &lt;a href=&quot;http://java.sun.com/javase/6/docs/api/java/lang/String.html#valueOf%28java.lang.Object%29&quot;&gt;String.valueOf(Object)&lt;/a&gt; method is a good example of a case where this check for null can be done implicitly without any additional effort on the developer&#39;s part. I &lt;a href=&quot;http://marxsoftware.blogspot.com/2009/04/value-of-stringvalueof.html&quot;&gt;blogged on this previously&lt;/a&gt;, but include a brief example of its use here.&lt;br /&gt;&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;br /&gt;&lt;p&gt;Source : &lt;a href=&quot;http://marxsoftware.blogspot.com/2009/04/effective-java-nullpointerexception.html&quot;&gt;Inspired by actual events&lt;/a&gt;&lt;/p&gt;  </content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/4108133917632191112/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/07/effective-java-how-to-avoid-npe-null.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/4108133917632191112'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/4108133917632191112'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/07/effective-java-how-to-avoid-npe-null.html' title='Effective Java : How to Avoid NPE / Null Pointer Exception in java?'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-281201766593949980</id><published>2011-07-15T04:07:00.000+05:30</published><updated>2011-07-15T04:13:27.264+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="Arrays"/><category scheme="http://www.blogger.com/atom/ns#" term="deep copy"/><category scheme="http://www.blogger.com/atom/ns#" term="deeptoString"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="toString"/><title type='text'>Stringifying Java Arrays</title><content type='html'>&lt;a href=&quot;http://www.oracle.com/technetwork/java/javase/index-jsp-135232.html&quot;&gt;J2SE 5&lt;/a&gt; provided &lt;a href=&quot;http://java.sun.com/developer/technicalArticles/releases/j2se15/&quot;&gt;significant new language features&lt;/a&gt; that made Java significantly easier to use and more expressive than it had ever been. Although &quot;big&quot; &lt;a href=&quot;http://download.oracle.com/javase/1.5.0/docs/relnotes/features.html&quot;&gt;new J2SE 5 features&lt;/a&gt; such as &lt;a href=&quot;http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html&quot;&gt;enums&lt;/a&gt;, &lt;a href=&quot;http://download.oracle.com/javase/1.5.0/docs/guide/language/generics.html&quot;&gt;generics&lt;/a&gt;, and &lt;a href=&quot;http://download.oracle.com/javase/1.5.0/docs/guide/language/annotations.html&quot;&gt;annotations&lt;/a&gt;, there was a plethora of new APIs and methods that have made our lives easier ever since. In this post, I briefly look at two of my favorites when &lt;a href=&quot;http://www.devarticles.com/c/a/Java/Whats-New-in-Java-15-Tiger/&quot;&gt;dealing with Java arrays&lt;/a&gt;: the &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#toString%28java.lang.Object[]%29&quot;&gt;Arrays.toString(Object[])&lt;/a&gt; method and the &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepToString%28java.lang.Object[]%29&quot;&gt;Arrays.deepToString(Object[])&lt;/a&gt; method.&lt;br&gt;&lt;br&gt;The &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html&quot;&gt;Arrays class&lt;/a&gt; has been around since JDK 1.2, but the methods for conveniently and simply converting arrays to a readable String including relevant array content without using &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#asList%28T...%29&quot;&gt;Arrays.asList()&lt;/a&gt; were added with J2SE 5. Both &lt;code&gt;Arrays.toString(Object[])&lt;/code&gt; and &lt;code&gt;Arrays.deepToString(Object[])&lt;/code&gt; are static methods that act upon the arrays provided to them. The former, &lt;code&gt;Arrays.toString(Object[])&lt;/code&gt;, is intended for single-dimension arrays while the latter, &lt;code&gt;Arrays.deepToString(Object[])&lt;/code&gt;, is intended for multi-dimensional arrays. As my example later in this post will demonstrate, the multi-dimension &lt;code&gt;deepToString&lt;/code&gt; will produce expected results even for single-dimension arrays. The two methods also provide easy and safe null handling, &lt;a href=&quot;http://k2java.blogspot.com/2011/07/effective-java-how-to-avoid-npe-null.html&quot;&gt;something that I appreciate&lt;/a&gt;.&lt;br&gt;&lt;br&gt;The next code listing is for a simple demonstration class that demonstrates trying to put the contents of various types of Java arrays into a String format. The types of arrays demonstrated are a single dimension array, a double dimensional array representing multi-dimensional arrays of various sizes, and an array that is really just null. The three methods demonstrated for getting a String out of these three types of arrays are (1) simple &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/lang/Object.html#toString%28%29&quot;&gt;Object.toString()&lt;/a&gt; on each array (implicitly in case of null array to avoid the dreaded &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/lang/NullPointerException.html&quot;&gt;NullPointerException&lt;/a&gt;), (2) &lt;code&gt;Arrays.toString(Object[])&lt;/code&gt;, and (3) &lt;code&gt;Arrays.deepToString(Object[])&lt;/code&gt;.&lt;br&gt; &lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;package&lt;/span&gt; com.vaani.arraytest;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;import&lt;/span&gt; java.util.Arrays;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;import&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; java.lang.System.out;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; * Simple demonstration of Arrays.toString(Object[]) method and the&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; * Arrays.deepToString(Object[]) method.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt; */&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;class&lt;/span&gt; Array2String&lt;br&gt;{&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Demonstrate usage and behavior of Arrays.toString(Object[]).&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; demonstrateArraysToString()&lt;br&gt;   {&lt;br&gt;      out.println(&lt;br&gt;           &lt;span style=&quot;color: #006080&quot;&gt;&quot;Single Dimension Arrays.toString: &quot;&lt;/span&gt;&lt;br&gt;         + Arrays.toString(prepareSingleDimensionArray()));&lt;br&gt;      out.println(&lt;br&gt;           &lt;span style=&quot;color: #006080&quot;&gt;&quot;Double Dimension Arrays.toString: &quot;&lt;/span&gt;&lt;br&gt;         + Arrays.toString(prepareDoubleDimensionArray()));&lt;br&gt;      out.println(&lt;br&gt;           &lt;span style=&quot;color: #006080&quot;&gt;&quot;Null Array Arrays.toString: &quot;&lt;/span&gt;&lt;br&gt;         + Arrays.toString(prepareNullArray()));&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Demonstrate usage and behavior of Arrays.deepToString(Object[]).&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; demonstrateArraysDeepToString()&lt;br&gt;   {&lt;br&gt;       out.println(&lt;br&gt;           &lt;span style=&quot;color: #006080&quot;&gt;&quot;Single Dimension Arrays.deepToString: &quot;&lt;/span&gt;&lt;br&gt;         + Arrays.deepToString(prepareSingleDimensionArray()));&lt;br&gt;      out.println(&lt;br&gt;           &lt;span style=&quot;color: #006080&quot;&gt;&quot;Double Dimension Arrays.deepToString: &quot;&lt;/span&gt;&lt;br&gt;         + Arrays.deepToString(prepareDoubleDimensionArray()));&lt;br&gt;      out.println(&lt;br&gt;           &lt;span style=&quot;color: #006080&quot;&gt;&quot;Null Array Arrays.deepToString: &quot;&lt;/span&gt;&lt;br&gt;         + Arrays.deepToString(prepareNullArray()));&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Demonstrate attempting to get String version of array with simple toString()&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * call (not using Arrays class).&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; demonstrateDirectArrayString()&lt;br&gt;   {&lt;br&gt;      out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Single Dimension toString(): &quot;&lt;/span&gt; + prepareSingleDimensionArray().toString());&lt;br&gt;      out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Double Dimension toString(): &quot;&lt;/span&gt; + prepareDoubleDimensionArray());&lt;br&gt;      out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;Null Array toString(): &quot;&lt;/span&gt; + prepareNullArray());&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Prepare a single-dimensional array to be used in demonstrations.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * @return Single-dimensional array.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; Object[] prepareSingleDimensionArray()&lt;br&gt;   {&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String[] names = {&lt;span style=&quot;color: #006080&quot;&gt;&quot;Aaron&quot;&lt;/span&gt;, &lt;span style=&quot;color: #006080&quot;&gt;&quot;Bianca&quot;&lt;/span&gt;, &lt;span style=&quot;color: #006080&quot;&gt;&quot;Charles&quot;&lt;/span&gt;, &lt;span style=&quot;color: #006080&quot;&gt;&quot;Denise&quot;&lt;/span&gt;, &lt;span style=&quot;color: #006080&quot;&gt;&quot;Elmer&quot;&lt;/span&gt;};&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; names;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Prepare a double-dimension array to be used in demonstrations.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * @return Double-dimensional array.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; Object[] prepareDoubleDimensionArray()&lt;br&gt;   {&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; Object[][] namesAndAges = {&lt;br&gt;         {&lt;span style=&quot;color: #006080&quot;&gt;&quot;Aaron&quot;&lt;/span&gt;, 10}, {&lt;span style=&quot;color: #006080&quot;&gt;&quot;Bianca&quot;&lt;/span&gt;, 25}, {&lt;span style=&quot;color: #006080&quot;&gt;&quot;Charles&quot;&lt;/span&gt;, 32}, {&lt;span style=&quot;color: #006080&quot;&gt;&quot;Denise&quot;&lt;/span&gt;, 29}, {&lt;span style=&quot;color: #006080&quot;&gt;&quot;Elmer&quot;&lt;/span&gt;, 67}};&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; namesAndAges;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Prepare a null array.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    *&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * @return Array that is really null.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;private&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; Object[] prepareNullArray()&lt;br&gt;   {&lt;br&gt;      &lt;span style=&quot;color: #0000ff&quot;&gt;return&lt;/span&gt; null;&lt;br&gt;   }&lt;br&gt;&lt;br&gt;   &lt;span style=&quot;color: #008000&quot;&gt;/**&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Main executable function for demonstrating Arrays.toString(Object[]) and&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    * Arrays.deepToString(Object[]) methods.&lt;/span&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;    */&lt;/span&gt;&lt;br&gt;   &lt;span style=&quot;color: #0000ff&quot;&gt;public&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;static&lt;/span&gt; &lt;span style=&quot;color: #0000ff&quot;&gt;void&lt;/span&gt; main(&lt;span style=&quot;color: #0000ff&quot;&gt;final&lt;/span&gt; String[] arguments)&lt;br&gt;   {&lt;br&gt;       out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;\n\n\nDemonstrating direct array to string using toString():&quot;&lt;/span&gt;);&lt;br&gt;      demonstrateDirectArrayString();&lt;br&gt;     &lt;br&gt;      out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;\n\n\nDemonstrating Arrays.toString() to get arrays as String:&quot;&lt;/span&gt;);&lt;br&gt;      demonstrateArraysToString();&lt;br&gt;      &lt;br&gt;      out.println(&lt;span style=&quot;color: #006080&quot;&gt;&quot;\n\n\nDemonstrating Arrays.deepToString() to get the String:&quot;&lt;/span&gt;);&lt;br&gt;      demonstrateArraysDeepToString();&lt;br&gt;   }&lt;br&gt;}&lt;br&gt;&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;The above code exercises the three mentioned approaches for getting a String out of an array on the three different types of arrays: single dimension, multi dimension, and null array. The output from running this code demonstrates the utility of the different approaches. That output is shown next. &lt;br&gt;&lt;br /&gt;&lt;div id=&quot;codeSnippetWrapper&quot;&gt;&lt;pre style=&quot;border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px&quot; id=&quot;codeSnippet&quot;&gt;Demonstrating direct array to string using toString():&lt;br&gt;Single Dimension toString(): [Ljava.lang.String;@3e25a5&lt;br&gt;Double Dimension toString(): [[Ljava.lang.Object;@19821f&lt;br&gt;Null Array toString(): null&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;///////////&lt;/span&gt;&lt;br&gt;Demonstrating Arrays.toString() to get arrays as String:&lt;br&gt;Single Dimension Arrays.toString: [Aaron, Bianca, Charles, Denise, Elmer]&lt;br&gt;Double Dimension Arrays.toString: [[Ljava.lang.Object;@addbf1, [Ljava.lang.Object;@42e816, [Ljava.lang.Object;@9304b1, [Ljava.lang.Object;@190d11, [Ljava.lang.Object;@a90653]&lt;br&gt;Null Array Arrays.toString: null&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;span style=&quot;color: #008000&quot;&gt;///////////&lt;/span&gt;&lt;br&gt;Demonstrating Arrays.deepToString() to get the String:&lt;br&gt;Single Dimension Arrays.deepToString: [Aaron, Bianca, Charles, Denise, Elmer]&lt;br&gt;Double Dimension Arrays.deepToString: [[Aaron, 10], [Bianca, 25], [Charles, 32], [Denise, 29], [Elmer, 67]]&lt;br&gt;Null Array Arrays.deepToString: null&lt;br&gt;&lt;/pre&gt;&lt;br&gt;&lt;/div&gt;The code above and its corresponding output lead to several observations:&lt;br&gt;&lt;br&gt;&lt;br /&gt;&lt;ol&gt;&lt;br /&gt;&lt;li&gt;Simple &lt;code&gt;Object.toString()&lt;/code&gt; on arrays is seldom what we want as it only prints the String representation of the array itself and not of its contents.&lt;br /&gt;&lt;li&gt;&lt;code&gt;Arrays.toString(Object[])&lt;/code&gt; will print a String representation for multi-dimensional arrays, but this representation suffers the same drawbacks as &lt;code&gt;Object.toString()&lt;/code&gt; after the first dimension. The first dimension (and only dimension for a single dimension array) gets put into an expected String, but deeper dimensions have the same &lt;code&gt;Object.toString()&lt;/code&gt; treatment.&lt;br /&gt;&lt;li&gt;&lt;code&gt;Arrays.deepToString(Object[])&lt;/code&gt;, while intended for multi-dimensional arrays, produces the expected results for both single and multi-dimensional arrays.&lt;br /&gt;&lt;li&gt;Both &lt;code&gt;Arrays.toString(Object[])&lt;/code&gt; and &lt;code&gt;Arrays.deepToString(Object[])&lt;/code&gt; handle null array gracefully, simply returning a String &quot;null&quot;.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;div&gt;I tend to use &lt;a href=&quot;http://download.oracle.com/javase/tutorial/collections/index.html&quot;&gt;Java Collections&lt;/a&gt; far more than I use &lt;a href=&quot;http://download.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html&quot;&gt;Java arrays&lt;/a&gt;. However, when I do need to work with arrays, it is nice to have the many useful features of the &lt;code&gt;java.util.Arrays&lt;/code&gt; class. As this post has demonstrated, &lt;code&gt;Arrays.toString(Object[])&lt;/code&gt; and &lt;code&gt;Arrays.deepToString(Object[])&lt;/code&gt; are particularly valuable in obtaining a useful String representation of an array&#39;s contents. The &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html&quot;&gt;java.util.Arrays&lt;/a&gt; class provides similar &quot;deep&quot; methods for performing equals and hashCode functionality on multi-dimensional arrays: &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepEquals%28java.lang.Object[],%20java.lang.Object[]%29&quot;&gt;Arrays.deepEquals&lt;/a&gt; and &lt;a href=&quot;http://download.oracle.com/javase/6/docs/api/java/util/Arrays.html#deepHashCode%28java.lang.Object[]%29&quot;&gt;Arrays.deepHashCode&lt;/a&gt;.&lt;/div&gt;  </content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/281201766593949980/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/07/stringifying-java-arrays.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/281201766593949980'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/281201766593949980'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/07/stringifying-java-arrays.html' title='Stringifying Java Arrays'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7703735018951306032.post-3825688406283068713</id><published>2011-07-15T03:32:00.000+05:30</published><updated>2011-07-15T03:32:04.108+05:30</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="javac options"/><title type='text'>javac&#39;s -Xprint Option</title><content type='html'></content><link rel='replies' type='application/atom+xml' href='http://k2java.blogspot.com/feeds/3825688406283068713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://k2java.blogspot.com/2011/07/javacs-xprint-option.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/3825688406283068713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7703735018951306032/posts/default/3825688406283068713'/><link rel='alternate' type='text/html' href='http://k2java.blogspot.com/2011/07/javacs-xprint-option.html' title='javac&#39;s -Xprint Option'/><author><name>Kinshuk Chandra</name><uri>http://www.blogger.com/profile/01344610750518430564</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>