<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7634848634080023834</id><updated>2012-01-19T14:52:10.327+05:30</updated><category term='VO and Domain Object'/><category term='improve boost performance design pattern'/><category term='struts2 request response session action'/><category term='design patterns in struts2'/><category term='Difference between DTO'/><title type='text'>Java J2ee Typical problems with Solution</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://java-boss.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://java-boss.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>AmitKumarGupta</name><uri>http://www.blogger.com/profile/10897202134878054444</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://3.bp.blogspot.com/_oAk6ii8gAg4/SlNKTpPP0oI/AAAAAAAAAvM/QV7R3XH2z38/S220/Z17zwqbb.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>15</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7634848634080023834.post-3083394613464593813</id><published>2011-07-18T11:20:00.002+05:30</published><updated>2011-07-18T11:34:50.577+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='Difference between DTO'/><category scheme='http://www.blogger.com/atom/ns#' term='VO and Domain Object'/><title type='text'>Difference between DTO, VO and Domain Object</title><content type='html'>&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;b style="font-weight: bold;"&gt;Data Transfer Objects (DTO)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Are simple POJOs for the purpose of transporting data around between software sub-system interfaces. The DTOs do not have any behavior other than storage and retrieval of its data.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;blockquote style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Purpose is for data transfer.&lt;br /&gt;It is a bunch of data, not necessarily coherent&lt;br /&gt;No behavior&lt;/blockquote&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;One of the reasons for DTOs in a J2EE system are that Entity beans are not serializable. Another reason is that the domain model and the data being presented to the view is not necessarily the same structures.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;b style="font-weight: bold;"&gt;Value Objects (VO)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Are domain driven entities that know how to define themselves; they make implicity concepts explicit. Value objects in DDD (Domain Driven Design) do validation on the data being passed in. For instance a phone number might be used in a DTO as a String but in a &amp;nbsp;VO it will be a PhoneNumber type.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;blockquote style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Purpose is for domain representation&lt;br /&gt;High data coherence&lt;br /&gt;Rich behavior&lt;/blockquote&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;b style="font-weight: bold;"&gt;Domain Model (DOM)&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Is the relationships between different entities in the system. In J2EE systems these are the Entity Beans that have a one to one mapping with tables in persistent storage. The domain model documents the key concepts and the vocabulary of the system being modeled. This can be applied at many levels; the DTOs, the value objects, the business objects and the persistent objects. In simpler systems often the domain model can do double duty as both the business objects and the persisted entities. In more complex systems these layers need to be broken into different tiers.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;blockquote style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Purpose is for domain representation of business concepts&lt;/blockquote&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Often in systems domain model for the business objects and the data access layer are one and the same and are defined as Entity Beans or objects in the ORM that directly map to the database schema. In these instances the system has effectively outgrown that architecture and needs two levels added to it; a DTO layer and a Value Object layer.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;blockquote style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;Database&amp;nbsp;&lt;i&gt;to&lt;/i&gt;&amp;nbsp;(Entity Beans)&amp;nbsp;&lt;i&gt;to&lt;/i&gt;&amp;nbsp;Stateless Session Beans&amp;nbsp;&lt;i&gt;to&lt;/i&gt;(VOs)&amp;nbsp;&lt;i&gt;to&lt;/i&gt;&amp;nbsp;Webservices/Remote Interfaces&amp;nbsp;&lt;i&gt;to&lt;/i&gt;&amp;nbsp;(DTOs)&amp;nbsp;&lt;i&gt;to&lt;/i&gt;other systems such as the view&lt;/blockquote&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;b style="font-weight: bold;"&gt;Data Transfer Object Design&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;The DTOs are simple storage and do nothing other than transfer data across sub systems. This means they need to match the data requirements of the requesting and receiving systems exactly. For instance the view may request or respond with Credit Card;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: monospace; font-size: 12px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; padding-top: 10px;"&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;public class CreditCardDTO {&lt;br /&gt;     public String cardType;&lt;br /&gt;    public String cardNumber;&lt;br /&gt;    public String ccv;&lt;br /&gt;    public String nameOnCard;&lt;br /&gt;    public String expirationMonth;&lt;br /&gt;    public String expirationYear;&lt;br /&gt; }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;In this instance they are strings as there is no need to describe the data, or do anything complex with the data in a DTO. This would be passed through either the webservices or the remote interfaces to the business logic tier where it would be converted into a value object.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;b style="font-weight: bold;"&gt;Value Object Design&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;The VOs are representational of the vocabulary in the domain model and carry the assumptions and behavior inherent in that. Continuing with the Credit Card, when it is changed into a Value Object it will look like:&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: monospace; font-size: 12px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; padding-top: 10px;"&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;public class CreditCardVO {&lt;br /&gt;     public CreditCardType creditCardType;&lt;br /&gt;    public CreditCardNumber creditCardNumber;&lt;br /&gt;    public CreditCardCCV creditCardCCV;&lt;br /&gt;    public String nameOnCard;&lt;br /&gt;    public ExpirationDate expirationDate;&lt;br /&gt; }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;The CreditCardVO is composed of other value objects such as the CreditCardNumber. This does validation on the credit card number that is stored in the DTO as a string and then transmits whether it really is a credit card number to the webservice or remote interfaces quickly whether it is or not.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: monospace; font-size: 12px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; padding-top: 10px;"&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;public class CreditCardNumber {&lt;br /&gt;     private String creditCardNumber;&lt;br /&gt;     public CreditCardNumber(CreditCardType type, String creditCardNumber)&lt;br /&gt;        throws ValidationException {&lt;br /&gt;         if (creditCardNumber==null) {&lt;br /&gt;            throw new ValidationException("No credit card number present.");&lt;br /&gt;        }&lt;br /&gt;         if (type==CreditCardType.VISA) {&lt;br /&gt;             if (!Pattern.compile("^4[0-9]{12}(?:[0-9]{3})?$").matcher(this.creditCardNumber).matches()) {&lt;br /&gt;                throw new ValidationException("Not a VISA credit card.");&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;         this.creditCardNumber = creditCardNumber;&lt;br /&gt;    }&lt;br /&gt;     public String getCreditCardNumber() {&lt;br /&gt;        return this.creditCardNumber;&lt;br /&gt;    }&lt;br /&gt; }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;While this is a bit clunky with a lot of logic present in the constructor that can be refactored it is a good example of dealing with the issue. From this structure a credit card value object knows how to be a credit card number.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;b style="font-weight: bold;"&gt;Domain Model Design&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;The business logic tier passes the value objects to the Entities to be transformed into persistent objects in the data store. A credit card value object will be absorbed by a CreditCardProfile Entity Bean and then persisted.&lt;/span&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;pre style="border-bottom-color: rgb(204, 204, 204); border-bottom-style: solid; border-bottom-width: 1px; border-top-color: rgb(204, 204, 204); border-top-style: solid; border-top-width: 1px; font-family: monospace; font-size: 12px; padding-bottom: 10px; padding-left: 20px; padding-right: 10px; padding-top: 10px;"&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;@Entity&lt;br /&gt;public class CreditCardProfile {&lt;br /&gt;     @Id&lt;br /&gt;    private long id;&lt;br /&gt;     @Column&lt;br /&gt;    private String nameOnCard;&lt;br /&gt;     @Column&lt;br /&gt;    private String creditCardNumber;&lt;br /&gt;     @Column&lt;br /&gt;    private Date expirationDate;&lt;br /&gt; }&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;div style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;&lt;/div&gt;&lt;span class="Apple-style-span" style="font-family: sans-serif; font-size: 17px; line-height: 24px;"&gt;At each level of the objects being passed between layers there are different requirements and different vocabularies. Separating the data being moved around the system with DTOs, VOs and Domain Model objects ensures sufficient decoupling that they system is not impervious to future change.&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7634848634080023834-3083394613464593813?l=java-boss.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-boss.blogspot.com/feeds/3083394613464593813/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-boss.blogspot.com/2011/07/difference-between-dto-vo-and-domain.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/3083394613464593813'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/3083394613464593813'/><link rel='alternate' type='text/html' href='http://java-boss.blogspot.com/2011/07/difference-between-dto-vo-and-domain.html' title='Difference between DTO, VO and Domain Object'/><author><name>AmitKumarGupta</name><uri>http://www.blogger.com/profile/10897202134878054444</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://3.bp.blogspot.com/_oAk6ii8gAg4/SlNKTpPP0oI/AAAAAAAAAvM/QV7R3XH2z38/S220/Z17zwqbb.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7634848634080023834.post-8946518361361986928</id><published>2011-07-05T15:11:00.001+05:30</published><updated>2011-07-05T15:11:18.920+05:30</updated><title type='text'>Difference between Dependency Injection and IOC , and the difference between Push Dependency Injection and Push Dependency Injection</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: HelveticaNeue-MediumCond; font-size: 16.0pt; mso-bidi-font-family: HelveticaNeue-MediumCond; mso-bidi-font-size: 11.0pt;"&gt;Dependency Injection&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;It is common for developers to believe that IOC and DI are the same thing. This is incorrect,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;and I want to make it clear right at the outset that they are two different yet related&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;concepts. Just as IOC deals with inverting the control flow in an application, DI describes&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;how one object resolves or finds other objects on which it needs to invoke some methods.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;There are several ways to achieve DI, and one such strategy is IOC. I will explain the&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;different DI strategies one by one in the next few sections.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: Utopia-Bold; font-size: 11.0pt; mso-bidi-font-family: Utopia-Bold; mso-bidi-font-size: 9.5pt;"&gt;Direct Instantiation&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;Direct instantiation is the simplest form of DI. The dependent object is directly instantiated&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;using the new operator, as shown in Listing-1.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: HelveticaNeue-BoldCond; font-size: 11.0pt; mso-bidi-font-family: HelveticaNeue-BoldCond; mso-bidi-font-size: 9.5pt;"&gt;Listing 1. &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;FormulaOneDriver.java&lt;/span&gt;&lt;i&gt;&lt;span style="font-family: Utopia-Italic; font-size: 11.0pt; mso-bidi-font-family: Utopia-Italic; mso-bidi-font-size: 9.5pt;"&gt;:Using Direct Instantiation&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public class FormulaOneDriver{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public Car getCar(){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;Car car = new FerrariCar();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;return car;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;The Formula 1 driver object (&lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;FormulaOneDriver&lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;) needs a car to drive. Hence, it creates&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;an instance of the &lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;Car &lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;object directly and uses it. Direct instantiation increases coupling&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;and scatters object creation code across the application, making it hard to maintain and&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;unit test.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: Utopia-Bold; font-size: 11.0pt; mso-bidi-font-family: Utopia-Bold; mso-bidi-font-size: 9.5pt;"&gt;Factory Helper&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;The factory helper is a common and widely used dependency injection strategy. It is&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;based on the GOF factory method design pattern. The factory method consolidates the&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;use of the new operator and supplies appropriate object instances based on some input.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;This is shown in Listing 2.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: HelveticaNeue-BoldCond; font-size: 11.0pt; mso-bidi-font-family: HelveticaNeue-BoldCond; mso-bidi-font-size: 9.5pt;"&gt;Listing 2. &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;FormulaOneDriver.java&lt;/span&gt;&lt;i&gt;&lt;span style="font-family: Utopia-Italic; font-size: 11.0pt; mso-bidi-font-family: Utopia-Italic; mso-bidi-font-size: 9.5pt;"&gt;:Using Factory Helper&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public class FormulaOneDriver{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public Car getCar(){&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;Car car = CarFactory.getInstance("FERARI");&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;return car;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;Using a factory promotes an object design best practice called &lt;/span&gt;&lt;i&gt;&lt;span style="font-family: Utopia-Italic; font-size: 11.0pt; mso-bidi-font-family: Utopia-Italic; mso-bidi-font-size: 9.5pt;"&gt;program to interface&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;(P2I). This principle states that concrete objects must implement an interface that is used&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;in the caller program rather than the concrete object itself. Therefore, you can easily&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;substitute a different implementation with little impact on client code. In other words,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;there is no direct dependency on the concrete implementation leading to low coupling.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;Listing 2-3 shows the &lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;Car &lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;interface.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: HelveticaNeue-BoldCond; font-size: 11.0pt; mso-bidi-font-family: HelveticaNeue-BoldCond; mso-bidi-font-size: 9.5pt;"&gt;Listing 3. &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;Car.java&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public interface Car{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public Color getColor();&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;//other methods&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;The &lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;FerrariCar &lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;provides a concrete implementation of the &lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;Car &lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;interface, as shown in&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;Listing 4.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: HelveticaNeue-BoldCond; font-size: 11.0pt; mso-bidi-font-family: HelveticaNeue-BoldCond; mso-bidi-font-size: 9.5pt;"&gt;Listing 4. &lt;/span&gt;&lt;/b&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;FerrariCar.java&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;public class FerrariCar implements Car{&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;//...implementation of methods defined in Car&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;// ...implementation of other methods&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;This pattern also consolidates object creation in only a handful of factory classes,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;making it easy to maintain. With a factory helper, it is also possible to make object creation&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;configurable. You can define the concrete implementation that you supply in some&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;properties or XML configuration files, making it swappable on the fly.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: Utopia-Bold; font-size: 11.0pt; mso-bidi-font-family: Utopia-Bold; mso-bidi-font-size: 9.5pt;"&gt;Locate in Registry Service&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;This third method should be familiar with EJB developers. They often need to look up EJB&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;object references on the JNDI registry service. In this case, the EJB objects are already created&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;and registered in JNDI with a specific key. The objects may be located in a remote&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;JVM, but JNDI makes lookup using this key quite similar to Listing 2.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;All these strategies are commonly called &lt;/span&gt;&lt;i&gt;&lt;span style="font-family: Utopia-Italic; font-size: 11.0pt; mso-bidi-font-family: Utopia-Italic; mso-bidi-font-size: 9.5pt;"&gt;pull &lt;/span&gt;&lt;/i&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;dependency injection. This is because&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;the dependent object is pulled in by the object that ultimately uses it. I prefer to classify&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;the pull methods as dependency resolution, rather than dependency injection. This is&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;because the true dependency injection happens with IOC and is called &lt;/span&gt;&lt;i&gt;&lt;span style="font-family: Utopia-Italic; font-size: 11.0pt; mso-bidi-font-family: Utopia-Italic; mso-bidi-font-size: 9.5pt;"&gt;push &lt;/span&gt;&lt;/i&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;DI. In this&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;approach, an external container or application framework creates and passes the&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;dependent object to the object that requires it. The dependent objects are mostly supplied&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;using constructor or setter methods. However, for this the application framework&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;must know which dependent object to provide and which object to notify with the&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;dependent object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;It is interesting to note that EJB containers support not only pull DI (one session&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;bean looking up another session bean, for instance, in the JNDI) but also push DI. This is&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;evident from the &lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;setSessionContext(javax.ejb.SessionContext ctx) &lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;or &lt;/span&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;setEntityContext&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: TheSansMonoCondensed-SemiLight; font-size: 11.0pt; mso-bidi-font-family: TheSansMonoCondensed-SemiLight; mso-bidi-font-size: 9.0pt;"&gt;(javax.ejb.EntityContext ctx) &lt;/span&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;method where the context object is created, initialized,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;and passed to the EJB objects by the container. This is called &lt;/span&gt;&lt;i&gt;&lt;span style="font-family: Utopia-Italic; font-size: 11.0pt; mso-bidi-font-family: Utopia-Italic; mso-bidi-font-size: 9.5pt;"&gt;setter injection&lt;/span&gt;&lt;/i&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;. You can&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;explore different varieties of push DI with examples in a later section when I touch upon&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;the DI features of Spring IOC container.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: Utopia-Bold; font-size: 11.0pt; mso-bidi-font-family: Utopia-Bold; mso-bidi-font-size: 9.5pt;"&gt;Benefits of DI&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;The following are the benefits of DI:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• Dependency injection promotes loose coupling. With a factory helper, for instance,&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;you can remove hard-coded dependencies through P2I. It is possible to configure&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;them outside the application and provide hot-swappable and hot-pluggable&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;implementations.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• It facilitates test-driven development (TDD). Objects can be easily tested because&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;they do not require any particular container to run. They can be tested as long as&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;the dependencies are injected by some mechanism.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• As you will see later with push DI supported by Spring IOC, there is no need for&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;applications to look up objects like EJB remote interfaces.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• DI promotes good object-oriented design and reuse—object composition rather&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;than reuse by inheritance.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;b&gt;&lt;span style="font-family: Utopia-Bold; font-size: 11.0pt; mso-bidi-font-family: Utopia-Bold; mso-bidi-font-size: 9.5pt;"&gt;Drawbacks of DI&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;These are the drawbacks of DI:&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• The dependencies are generally hard-coded in XML configuration files that are&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;proprietary and nonstandard.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• Wiring instances together can become a hazard if there are too many instances&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;and many dependencies that need to be addressed.&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="mso-layout-grid-align: none; text-autospace: none;"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;• Dependency on XML-based metadata and excessive use of reflection and bytecode&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;span style="font-family: Utopia-Regular; font-size: 11.0pt; mso-bidi-font-family: Utopia-Regular; mso-bidi-font-size: 9.5pt;"&gt;manipulation may impact application performance.&lt;/span&gt;&lt;span style="font-size: 15.0pt; mso-bidi-font-size: 12.0pt;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7634848634080023834-8946518361361986928?l=java-boss.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-boss.blogspot.com/feeds/8946518361361986928/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-boss.blogspot.com/2011/07/difference-between-dependency-injection.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/8946518361361986928'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/8946518361361986928'/><link rel='alternate' type='text/html' href='http://java-boss.blogspot.com/2011/07/difference-between-dependency-injection.html' title='Difference between Dependency Injection and IOC , and the difference between Push Dependency Injection and Push Dependency Injection'/><author><name>AmitKumarGupta</name><uri>http://www.blogger.com/profile/10897202134878054444</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://3.bp.blogspot.com/_oAk6ii8gAg4/SlNKTpPP0oI/AAAAAAAAAvM/QV7R3XH2z38/S220/Z17zwqbb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7634848634080023834.post-2324472419916259462</id><published>2011-06-29T15:11:00.000+05:30</published><updated>2011-06-29T15:11:43.002+05:30</updated><category scheme='http://www.blogger.com/atom/ns#' term='improve boost performance design pattern'/><title type='text'>Design Patterns to boost the performance</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span class="Apple-style-span" style="color: #333333; font-family: Arial, Helvetica, Georgia, sans-serif; font-size: 12px; line-height: 18px;"&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;use the flyweight pattern to reduce object creation [The flyweight pattern uses a factory instead of 'new' to reuse objects rather than always create new ones].&lt;/li&gt;&lt;li&gt;The Singleton pattern and the Flyweight (object factory) pattern are useful to limit numbers of objects of various types and to assist with object reuse and reduce garbage collection.&lt;/li&gt;&lt;li&gt;Use the Data Access Object pattern to decouple business logic from data access logic, allowing for optimizations to be made in how data is managed.&lt;/li&gt;&lt;li&gt;Use the Fast-Lane Reader pattern to accelerate read-only data access by not using enterprise beans.&lt;/li&gt;&lt;li&gt;Use the Front Controller pattern to centralize incoming client requests, allowing optimizations to be made in aggregating the resulting view.&lt;/li&gt;&lt;li&gt;Use the Front Controller pattern to channel all client requests through a single decision point, which allows the application to be balanced at runtime.&lt;/li&gt;&lt;li&gt;Use the Page-by-Page Iterator pattern to efficiently access a large, remote list by retrieving its elements one sublist of value objects at a time.&lt;/li&gt;&lt;li&gt;Use the Session Facade pattern to provide a unified, workflow-oriented interface to a set of enterprise beans, thus minimizing client calls to server EJBs.&lt;/li&gt;&lt;li&gt;Use a Session Facade to provide a simple interface to a complex subsystem of enterprise beans, and to reduce network communication requirements&lt;/li&gt;&lt;li&gt;Use the Value Object pattern to efficiently transfer remote, fine-grained data by sending a coarse-grained view of the data.&lt;/li&gt;&lt;li&gt;Use the factory pattern to enable reuse or cloning of objects&lt;/li&gt;&lt;li&gt;The Abstract Factory design pattern uses a single class to create more than one kind of object.&lt;/li&gt;&lt;li&gt;An alternative to the Flyweight pattern is the Prototype pattern, which allows polymorphic copies of existing objects. The Object.clone() method signature provides support for the Prototype pattern.&lt;/li&gt;&lt;li&gt;Prototypes are useful when object initialization is expensive, and you anticipate few variations on the initialization parameters. Then you could keep already-initialized objects in a table, and clone an existing object instead of expensively creating a new one from scratch.&lt;/li&gt;&lt;li&gt;Immutable objects can be returned directly when using Prototyping, avoiding the copying overhead.&lt;/li&gt;&lt;li&gt;Combine multiple remote calls for state information into one call using a value object to wrap the data (the Value Object pattern, superceded by local interfaces in EJB 2.0).&lt;/li&gt;&lt;li&gt;Where long lists of data are returned by queries, use the Page-by-Page Iterator pattern: a server-side object that holds data on the server and supplies batches of results to the client.&lt;/li&gt;&lt;li&gt;When the client would request many small data items which would require many remote calls to satisfy, combine the multiple calls into one call which results in a single Value Object which holds all the data required to be transferred. Use the Value Object to send a single coarse-grained object from the server to the client(s).&lt;/li&gt;&lt;li&gt;For read-only access to a set of data that does not change rapidly, use the Fast Lane Reader pattern which bypasses the EJBs and uses a (possibly non-transactional) data access object which encapsulates access to the data. Use the Fast Lane Reader to read data from the server and display all of them in one shot.&lt;/li&gt;&lt;li&gt;Wrap multiple entity beans in a session bean to change multiple EJB remote calls into one session bean remote call and several local calls (pattern called SessionFacade).&lt;/li&gt;&lt;li&gt;Cache EJBHome references to avoid JNDI lookup overhead (pattern called ServiceLocator).&lt;/li&gt;&lt;li&gt;The ServiceLocator/EJBHomeFactory Pattern reduces the expensive JNDI lookup process by caching EJBHome objects.&lt;/li&gt;&lt;li&gt;The SessionFacade Pattern reduces network calls by combining accesses to multiple Entity beans into one access to the facade object.&lt;/li&gt;&lt;li&gt;The MessageFacade/ServiceActivator Pattern moves method calls into a separate object which can execute asynchronously.&lt;/li&gt;&lt;li&gt;The ValueObject Pattern combines remote data into one serializable object, thus reducing the number of network transfers required to access multiple items of remote data.&lt;/li&gt;&lt;li&gt;The ValueObjectFactory/ValueObjectAssembler Pattern combines remote data from multiple remote objects into one serializable object, thus reducing the number of network transfers required to access multiple items of remote data.&lt;/li&gt;&lt;li&gt;The ValueListHandler Pattern: avoids using multiple Entity beans to access the database, using Data Access Objects which explicitly query the database; and returns the data to the client in batches (which can be terminated) rather than in one big chunk, according to the Page-by-Page Iterator pattern.&lt;/li&gt;&lt;li&gt;The CompositeEntity Pattern reduces the number of actual entity beans by wrapping multiple java objects (which could otherwise be Entity beans) into one Entity bean.&lt;/li&gt;&lt;li&gt;The Recycler pattern fixes only the broken parts of a failed object, to minimize the replacement cost.&lt;/li&gt;&lt;li&gt;Use a factory class instead of directly calling the “new” operator, to allow easier reuse of objects.&lt;/li&gt;&lt;li&gt;Instead of making lots of remote requests for data attributes of an object, combine the attributes into another object and send the object to the client. Then the attributes can be queried efficiently locally (this is called the Value Object pattern). Consider caching the value objects where appropriate.&lt;/li&gt;&lt;li&gt;The Proxy design pattern&lt;/li&gt;&lt;ul&gt;&lt;li&gt;Using a proxy, you can delay image loading until the image is required.&lt;/li&gt;&lt;li&gt;The Proxy pattern often instantiates its real object, the Decorator pattern (which can also use proxy objects) rarely does.&lt;/li&gt;&lt;li&gt;The java.lang.reflect package provides three classes to support the Proxy and Decorator patterns: Proxy, Method, and InvocationHandler.&lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7634848634080023834-2324472419916259462?l=java-boss.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-boss.blogspot.com/feeds/2324472419916259462/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-boss.blogspot.com/2011/06/design-patterns-to-boost-performance.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/2324472419916259462'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/2324472419916259462'/><link rel='alternate' type='text/html' href='http://java-boss.blogspot.com/2011/06/design-patterns-to-boost-performance.html' title='Design Patterns to boost the performance'/><author><name>AmitKumarGupta</name><uri>http://www.blogger.com/profile/10897202134878054444</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://3.bp.blogspot.com/_oAk6ii8gAg4/SlNKTpPP0oI/AAAAAAAAAvM/QV7R3XH2z38/S220/Z17zwqbb.jpg'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7634848634080023834.post-1288035228957373831</id><published>2011-06-25T11:11:00.003+05:30</published><updated>2011-06-25T11:12:52.515+05:30</updated><title type='text'>Core Java Difficult Interview Questions</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;br /&gt;&lt;h1&gt;Can transient variables be declared as 'final' or 'static'?&lt;/h1&gt;Java's serialization provides an elegant, and easy to use mechanism for making an object's state persistent. While controlling object serialization, we might have a particular object data member that we do not want the serialization mechanism to save.&lt;br /&gt;The modifier &lt;em&gt;transient&lt;/em&gt; can be applied to field members of a class to turn off serialization on these field members. Every field marked as &lt;em&gt;transient&lt;/em&gt; will not be &lt;em&gt;serialized&lt;/em&gt;. You use the &lt;em&gt;transient&lt;/em&gt; keyword to indicate to the Java virtual machine that the transient variable is not part of the persistent state of an object.&lt;br /&gt;The &lt;em&gt;transient&lt;/em&gt; modifier applies to variables only.&lt;br /&gt;Surprisingly, the java compiler does not complaint if you declare a &lt;em&gt;static member field&lt;/em&gt; as &lt;em&gt;transient&lt;/em&gt; or&amp;nbsp;a &lt;em&gt;final member field&lt;/em&gt; as &lt;em&gt;transient&lt;/em&gt; in your classes.&amp;nbsp;These should be compile-time errors. Because a "transient" part of an object's state is assumed to be changing within each instance, it can not be static or final. Similarly, a "volatile" variable cannot be final (constant). This restriction matters only in the future, though, when transient and volatile are actually used by Java.&lt;br /&gt;Question:&lt;br /&gt;&lt;br /&gt;Which of the following is true? &lt;br /&gt;A. transient methods must be static &lt;br /&gt;B. native methods violate Java's platform independence.&lt;br /&gt;C. static methods cannot be protected&lt;br /&gt;D. transient variables may not be final or static&lt;br /&gt;&lt;br /&gt;Answer:&lt;br /&gt;A is incorrect as the modifier transient applies to variables only.&lt;br /&gt;B is&amp;nbsp;correct &amp;nbsp;because native methods execute code which lies entirely outside the Java Virtual Machine, this code is compiled for a specific targeted machine and hence makes the application platform independent thereby violating Java's platform independence.&lt;br /&gt;C is incorrect as there is nothing wrong in static methods being protected.&lt;br /&gt;D is correct, as transient variables may not be final or static.&lt;br /&gt;&lt;h1&gt;Can static methods be overridden?&lt;/h1&gt;&lt;br /&gt;The &lt;em&gt;static&lt;/em&gt; methods can not be overridden!&lt;br /&gt;If a subclass defines a&amp;nbsp;static method with the same signature as a&amp;nbsp;static method in the superclass, the method in the subclass &lt;em&gt;hides&lt;/em&gt; the one in the superclass. The distinction between hiding and overriding has important implications.&lt;br /&gt;&lt;h1&gt;Are parameters passed by reference or passed by value in method invocation?&lt;/h1&gt;&lt;div class="MsoNormal"&gt;All primitives are pass-by-value, period. When a primitive value is passed into a method, a copy of the primitive is made. The copy is what is actually manipulated in the method. So, the value of the copy can be changed within the method, but the original value remains unchanged.&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;Objects, however, work a bit differently. When you pass a Java object or array as a parameter, an object reference or array reference is passed into a method. The method can manipulate the attributes of the object that is referenced by the reference (formal parameter).&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3&gt;Why it's only proper to call the start() method to start the thread instead of calling the run() method directly?&lt;/h3&gt;&lt;br /&gt;In both the above examples, the run() method is the most important method in the thread classes, it is also the only method that we need to implement in both cases. Why it's only proper to call the start() method to start the thread instead of calling the run() method directly? Because the run() method is not a regular class method. It should only be called by the JVM. Writing thread classes is not about a single sequential thread, it's about the use of multiple threads running at the same time and performing different tasks in a single program. The JVM needs to work closely with the underneath operating system for the actual implementation of concurrent operations. This is how the performance can be improved, and all other benefits mentioned above can be achieved.&lt;br /&gt;You should not invoke the run() method directly. If you call the run() method directly, it will simply execute in the caller's thread instead of as its own thread. Instead, you need to call the start() method, which schedules the thread with the JVM. The JVM will call the corresponding run() method when the resources and CPU is ready. The JVM is not guaranteed to call the run() method right way when the start() method is called, or in the order that the start() methods are called. Especially for the computers have a single processor, it is impossible to run all running threads at the same time. The JVM must implement a scheduling scheme that shares the processor among all running threads. This is why when you call the start() methods from more than one thread, the sequence of execution of the corresponding run() methods is random, controlled only by the JVM.&lt;br /&gt;&lt;h1&gt;What are the valid signatures of the main() function of a class?&lt;/h1&gt;The Java literature frequently refers to the &lt;em&gt;Signature&lt;/em&gt; of a method. A method &lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;signature&lt;/span&gt;&lt;/strong&gt; is a collection of information about the method, and that includes the &lt;em&gt;name&lt;/em&gt;, &lt;em&gt;type&lt;/em&gt; (e.g., static or non-static), &lt;em&gt;visibility&lt;/em&gt; (e.g., public, private, etc.), &lt;em&gt;arguments&lt;/em&gt; (e.g., formal parameters), and &lt;em&gt;return type&lt;/em&gt;.&lt;br /&gt;The &lt;em&gt;main&lt;/em&gt; method is the entry point of the JVM when the class in launched. The JVM launchs the Java program by invoking the &lt;em&gt;main&lt;/em&gt; method of the class identified in the command to start the program. The method &lt;em&gt;main&lt;/em&gt; must be declared &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;public&lt;/span&gt;&lt;/code&gt;, &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;static&lt;/span&gt;&lt;/code&gt;, and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;void&lt;/span&gt;&lt;/code&gt;. It must accept a single argument that is an array of strings.&lt;br /&gt;The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;main&lt;/span&gt;&lt;/code&gt; method can be declared as either:&lt;br /&gt;&lt;pre&gt;public static void main(String[] args)&lt;/pre&gt;or&lt;br /&gt;&lt;pre&gt;public static void main(String args[])&lt;/pre&gt;A compile-time error will occurs, if more than one &lt;em&gt;main&lt;/em&gt; methods exist in your code with the signatures described in the above. For example, you will get compile-time error when you try to compile the following code.&lt;br /&gt;&lt;pre&gt;public class Program{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String args[]){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;You can have more than one methods with name "main" but have different other signatures. The JVM only looks for the &lt;em&gt;main&lt;/em&gt; method described in the above at starting time. For example, the following code compiles and output "calling from public static void main(String[] args)".&lt;br /&gt;&lt;pre&gt;public class Program{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;main("calling from public static void main(String[] args)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String arg){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(arg);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h2&gt;Is it allowed to declare the main method private?&lt;/h2&gt;Former JVM versions (pre-1.4) allowed the main method to have any accessibility (&lt;em&gt;private&lt;/em&gt;, &lt;em&gt;protected&lt;/em&gt;, etc). This incompatibility with the &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.1.4"&gt;Section 12.1.4 of the Java Language Specification&lt;/a&gt; has been fixed as of version 1.4. In order to invoke a main method from the command-line, it is now mandatory to declare the main method as the above. If the &lt;em&gt;main&lt;/em&gt; method has any access level other than &lt;em&gt;public&lt;/em&gt;, it will no longer run from the command-line.&lt;br /&gt;&lt;h2&gt;String Literal Pool&lt;/h2&gt;String allocation, like all object allocation, proves costly in both time and memory. The JVM performs some trickery while instantiating &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;string literals&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; to increase performance and decrease memory overhead. To cut down the number of String objects created in the JVM, the String class keeps a pool of strings. Each time your code creates a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.&lt;br /&gt;&lt;h1&gt;Can the remainder/modulus operator be used with floating point operands?&lt;/h1&gt;The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;%&lt;/span&gt;&lt;/code&gt; operator, called the modulus or remainder operator, returns the remainder of two numbers. In C and C++, the remainder operator accepts only integral operands, but in the Java programming language, it also accepts floating-point operands. Remeber that the evaluation of a floating-point remainder operator % never throws a run-time exception, even if the right-hand operand is zero. Overflow, underflow, or loss of precision cannot occur. Examples:&lt;br /&gt;&lt;pre&gt;5.0%3.0 produces 2.0&lt;br /&gt;&lt;br /&gt;5.0%(-3.0) produces 2.0&lt;br /&gt;&lt;br /&gt;(-5.0)%3.0 produces -2.0&lt;br /&gt;&lt;br /&gt;(-5.0)%(-3.0) produces -2.0&lt;br /&gt;&lt;br /&gt;5.0/0.0 produces &lt;st1:place&gt;NaN&lt;/st1:place&gt; &lt;/pre&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;Can a Java application have memory leak?&lt;/h1&gt;Yes, there could be memory leak in Java applications. Wait a minute, doesn't Java virtual machine have a garbage collector that will collect and free all unreferenced memory automatically?&lt;br /&gt;Let's find out in general what memory leaks are, and how they occur in applications. If an application fails to return the not-in-use memory back to the heap, the "lost" memory is called memory leak.&lt;br /&gt;Memory leaks occur when the application doesn't free the memory allocated, usually are the objects no longer in use, but the object references are lost. If an object is no longer accessible, there is no way to free its memory. Each time such a leak is re-created, additional memory is used and not freed. Eventually, the process that runs the application will run out of memory and crash.&lt;br /&gt;It's true that for other programming languages, such as C/C++, there is not such a thing called garbage collector. The programmer is responsible for freeing the memory when the object is no longer in use.&lt;br /&gt;In Java, all unreferenced objects are indeed automatically freed by the garbage collector. The garbage collector looks for objects that are no longer needed and to remove them when they can no longer be accessed or referenced. The garbage collector starts at the root nodes, classes that persist throughout the life of a Java application, and sweeps though all of the nodes that are referenced. As it traverses the nodes, it keeps track of which objects are actively being referenced. Any classes that are no longer being referenced are then eligible to be garbage collected. The memory resources used by these objects can be returned to the Java virtual machine (JVM) when the objects are deleted.&lt;br /&gt;But in some situations, when the object is no longer in use, but some references to that object has not been removed. This kind of objects will not be collected by the garbage collector. That means there is a memory leak. Sometimes memory leaks in Java is also referred to as "dangling references". &lt;br /&gt;&lt;h3&gt;What are the symptoms of a memory leak?&lt;/h3&gt;&lt;br /&gt;When the application has a memory leak, basically, you will notice:&lt;br /&gt;&lt;ol start="1" type="1"&gt;&lt;li class="MsoNormal" style="mso-list: l7 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Memory usage consistently      increases during the application life span. Sooner or later the      application will crash because out of memory.&lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l7 level1 lfo1; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Performance consistently      decreases. This is because more and more un collectable objects are in the      heap, which will trigger the garbage collector to work more frequently and      work longer, on the other hand, the application will run slower.&lt;/li&gt;&lt;/ol&gt;&lt;br /&gt;&lt;h3&gt;Typical Leaks&lt;/h3&gt;&lt;br /&gt;Now that we know it is indeed possible to create memory leaks in Java, let's have a look at some typical leaks and what causes them.&lt;br /&gt;&lt;strong&gt;Global collections&lt;/strong&gt;&lt;br /&gt;It is quite common in larger applications to have some kind of global data repository, a JNDI-tree for example, or a session table. In these cases care has to be taken to manage the size of the repository. There has to be some mechanism in place to remove data that is no longer needed from the repository.&lt;br /&gt;&lt;strong&gt;Caches&lt;/strong&gt;&lt;br /&gt;A cache is a data structure used for fast lookup of results for already-executed operations. Therefore, if an operation is slow to execute, you can cache the result of the operation for common input data and use that cached data the next time the operation is invoked. Usually, the application keeps adding new data that was not in the cache, but not controlling the size of the cache. Depends on what data is kept in the cache, the cache will potentially increase to too big for the application to handle. When designing the cache, the program has to make sure the cache has an upper bound on the amount of memory it will use.&lt;br /&gt;&lt;h1&gt;What is variable hiding and shadowing?&lt;/h1&gt;In Java, there are three kinds of variables: local variables, instance variables, and class variables. Variables have their scopes. Different kinds of variables have different scopes. A variable is &lt;strong&gt;shadowed&lt;/strong&gt; if there is another variable with the same name that is closer in scope. In other words, referring to the variable by name will use the one closest in scope , the one in the outer scope is shadowed.&lt;br /&gt;&lt;h1&gt;What is runtime polymorphism in Java?&lt;/h1&gt;Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. In other words, polymorphism allows you define one interface and have multiple implementation. This is one of the basic principles of object oriented programming.&lt;br /&gt;The method overriding is an example of runtime polymorphism. You can have a method in subclass overrides the method in its super classes with the same name and signature. Java virtual machine determines the proper method to call at the runtime, not at the compile time.&lt;br /&gt;Let's take a look at the following example:&lt;br /&gt;&lt;pre&gt;class Animal {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;void whoAmI() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("I am a generic Animal.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Dog extends Animal {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;void whoAmI() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("I am a Dog.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Cow extends Animal {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;void whoAmI() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("I am a Cow.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Snake extends Animal {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;void whoAmI() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("I am a Snake.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class RuntimePolymorphismDemo {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Animal ref1 = new Animal();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Animal ref2 = new Dog();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Animal ref3 = new Cow();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Animal ref4 = new Snake();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ref1.whoAmI();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ref2.whoAmI();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ref3.whoAmI();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ref4.whoAmI();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output is&lt;br /&gt;&lt;pre&gt;I am a generic Animal.&lt;br /&gt;&lt;br /&gt;I am a Dog.&lt;br /&gt;&lt;br /&gt;I am a Cow.&lt;br /&gt;&lt;br /&gt;I am a Snake.&lt;/pre&gt;In the example, there are four variables of type Animal (e.g., &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ref1&lt;/span&gt;&lt;/em&gt;, &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ref2&lt;/span&gt;&lt;/em&gt;, &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ref3&lt;/span&gt;&lt;/em&gt;, and &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ref4&lt;/span&gt;&lt;/em&gt;). Only &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ref1&lt;/span&gt;&lt;/em&gt; refers to an instance of &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Animal&lt;/span&gt;&lt;/em&gt; class, all others refer to an instance of the subclasses of &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Animal&lt;/span&gt;&lt;/em&gt;. From the output results, you can confirm that version of a method is invoked based on the actually object's type.&lt;br /&gt;In Java, a variable declared type of class &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;A&lt;/span&gt;&lt;/em&gt; can hold a reference to an object of class &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;A&lt;/span&gt;&lt;/em&gt; or an object belonging to any subclasses of class &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;A&lt;/span&gt;&lt;/em&gt;. The program is able to resolve the correct method related to the subclass object at runtime. This is called the runtime polymorphism in Java. This provides the ability to override functionality already available in the class hierarchy tree. At runtime, which version of the method will be invoked is based on the type of actual object stored in that reference variable and not on the type of the reference variable.&lt;br /&gt;&lt;h1&gt;What does Class.forname method do?&lt;/h1&gt;A call to &lt;em&gt;Class.forName("X")&lt;/em&gt; causes the class named &lt;em&gt;X&lt;/em&gt; to be dynamically loaded (at runtime). A call to forName("X") causes the class named X to be initialized (i.e., JVM executes all its static block after class loading). &lt;em&gt;Class.forName("X")&lt;/em&gt; returns the &lt;em&gt;Class&lt;/em&gt; object associated with the "X" class. The returned &lt;em&gt;Class&lt;/em&gt; object is not an instance of the "x" class itself.&lt;br /&gt;&lt;em&gt;Class.forName("X")&lt;/em&gt; loads the class if it not already loaded. The JVM keeps track of all the classes that have been previously loaded. This method uses the &lt;em&gt;classloader&lt;/em&gt; of the class that invokes it. The "X" is the fully qualified name of the desired class.&lt;br /&gt;For example,&lt;br /&gt;&lt;pre&gt;package com.xyzws;&lt;br /&gt;&lt;br /&gt;class AClass { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;static { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("static block in AClass");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class Program { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Class c&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;= Class.forName("com.xyzws.AClass"); &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (ClassNotFoundException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output is&lt;br /&gt;&lt;pre&gt;static block in AClass&lt;/pre&gt;Here is one example that uses returned &lt;em&gt;Class&lt;/em&gt; to create an instance of &lt;em&gt;AClass&lt;/em&gt;:&lt;br /&gt;&lt;pre&gt;package com.xyzws;&lt;br /&gt;&lt;br /&gt;class AClass {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public AClass() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("AClass's Constructor");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("static block in AClass");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class Program { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("The first time calls forName:");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Class c&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;= Class.forName("com.xyzws.AClass"); &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;AClass a = (AClass)c.newInstance();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("The second time calls forName:");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Class c1 = Class.forName("com.xyzws.AClass");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (ClassNotFoundException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (InstantiationException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (IllegalAccessException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output is&lt;br /&gt;&lt;pre&gt;The first time calls forName:&lt;br /&gt;&lt;br /&gt;static block in AClass&lt;br /&gt;&lt;br /&gt;AClass's Constructor&lt;br /&gt;&lt;br /&gt;The second time calls forName:&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;//Calss has been loaded so there is not "static block in AClass" printing out&lt;/pre&gt;&lt;h3&gt;JDBC Driver Is a Good Example&lt;/h3&gt;You may have experience working with JDBC Drivers. For example, the classloader attempts to load and link the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Driver&lt;/span&gt;&lt;/code&gt; class in the "org.gjt.mm.mysql" package. If successful, the static initializer is called.&lt;br /&gt;&lt;pre&gt;Class.forName("org.gjt.mm.mysql.Driver");&lt;br /&gt;&lt;br /&gt;Connection con = DriverManager.getConnection(url,?myLogin", "myPassword");&lt;/pre&gt;Let's see why you need Class.forName() to load a driver into memory. All JDBC Drivers have a static block that registers itself with &lt;em&gt;DriverManager&lt;/em&gt; and &lt;em&gt;DriverManager&lt;/em&gt; has static an initializer only.&lt;br /&gt;The MySQL &lt;em&gt;JDBC Driver&lt;/em&gt; has a static initializer looks like this:&lt;br /&gt;&lt;pre&gt;static {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;java.sql.DriverManager.registerDriver(new Driver());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (SQLException E) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;throw new RuntimeException("Can't register driver!");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;JVM executes the static block and the Driver registers itself with the &lt;em&gt;DriverManager&lt;/em&gt;.&lt;br /&gt;You need a database connection to manipulate the database. In order to create the connection to the database, the &lt;em&gt;DriverManager&lt;/em&gt; class has to know which database driver you want to use. It does that by iterating over the array (internally a Vector) of drivers that have registered with it and calls the &lt;em&gt;acceptsURL(url)&lt;/em&gt; method on each driver in the array, effectively asking the driver to tell it whether or not it can handle the JDBC URL.&lt;br /&gt;&lt;h1&gt;Why does TreeSet.add throw ClassCastException?&lt;/h1&gt;The following code throws ClassCastException, why?&lt;br /&gt;&lt;pre&gt;class MyObject {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;int i;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;MyObject(int i) { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.i = i;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Set s = new TreeSet();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.add(new MyObject(1));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.add(new MyObject(2)); //Runtime exception: ClassCastException&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/TreeSet.html#TreeSet%28%29" target="blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;TreeSet() Constructor&lt;/span&gt;&lt;/a&gt; said:&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;Constructs a new, empty set, sorted according to the elements' natural order. &lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;All elements inserted into the set must implement the &lt;/span&gt;&lt;/strong&gt;&lt;em&gt;&lt;b&gt;Comparable&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt; interface&lt;/span&gt;&lt;/strong&gt;. Furthermore, all such elements must be mutually comparable: e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the set.&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;If an element is added to the set that violates this constraint (for example, the user attempts to add a string element to a set whose elements are integers), the add(Object) call will throw a ClassCastException; or if the to be added object cannot be compared with the elements currently in the set, the add(Object) call will throw a ClassCastException.&lt;/div&gt;In the above code, the &lt;em&gt;MyObject&lt;/em&gt; class does not implement the &lt;em&gt;Comparable&lt;/em&gt; interface. Therefore, when the second object is added to the TreeSet, the second object can't be compared with the first element already in the set, the code throws a ClassCastException.&lt;br /&gt;&lt;h1&gt;What is the advantage of using an Iterator compared to the get(index) method?&lt;/h1&gt;You can navigate or access a List by using the &lt;em&gt;get (index)&lt;/em&gt; method or an Iterator. Sometimes the &lt;em&gt;get (index)&lt;/em&gt; method is your only option, and sometimes it's &lt;em&gt;slightly&lt;/em&gt; faster than an Iterator. Other times, however, it can be much, much slower than an Iterator. For example, a LinkedList is a classic example. This class has a &lt;em&gt;get (index)&lt;/em&gt; method but it is very slow. Well, it's not that bad if the list is short, or if you're looking for an item that is close to the beginning or end. But if you need to access the List frequently, you will see a big difference.&lt;br /&gt;Let's take a look at the following example:&lt;br /&gt;&lt;pre&gt;public class TestClass {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main (String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;len = 100000;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;LinkedList linkedLst = new LinkedList(); &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ArrayList&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;arrayLst = new ArrayList();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (int m =0; m!= len; m++) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int x = (int)Math.random();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;linkedLst.add(x);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;arrayLst.add(x);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;long t = System.currentTimeMillis();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (int i = 0; i!=len; i++) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;linkedLst.get(i);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis() - t;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("LinkedList -- get(index) takes "+t +"(ms)");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;(Iterator itr = linkedLst.iterator();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;itr.hasNext();) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;itr.next();&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis() - t;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("LinkedList -- Iterator takes "+t +"(ms)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (int i = 0; i!=len; i++) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;arrayLst.get(i);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis() - t;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("ArrayList -- get(index) takes "+t +"(ms)");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;(Iterator itr = arrayLst.iterator();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;itr.hasNext();) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;itr.next();&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t = System.currentTimeMillis() - t;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("ArrayList -- Iterator takes "+t +"(ms)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output is&lt;br /&gt;&lt;pre&gt;LinkedList -- get (index) takes 25777(ms)&lt;br /&gt;&lt;br /&gt;LinkedList -- Iterator takes 0(ms)&lt;br /&gt;&lt;br /&gt;ArrayList -- get (index) takes 10(ms)&lt;br /&gt;&lt;br /&gt;ArrayList -- Iterator takes 10(ms)&lt;/pre&gt;And in many cases, you don't know for sure whether you want a LinkedList or an ArrayList or some other List implementation. If you use &lt;em&gt;get(index)&lt;/em&gt;, you will get the fastest possible response from an ArrayList, but you will get a very poor response from a LinkedList. If you use an Iterator, you will get something fairly &lt;em&gt;close&lt;/em&gt; to the fastest possible response from an ArrayList, and you will get &lt;em&gt;the&lt;/em&gt; fastest possible response from a LinkedList, too. So in general, an Iterator is a more reliable choice. It's not always the &lt;em&gt;fastest&lt;/em&gt; possible choice, but it's always close. And it protects you from the &lt;em&gt;extremely&lt;/em&gt; slow behavior you would get if you mistakenly used a &lt;em&gt;get(index)&lt;/em&gt; on a LinkedList.&lt;br /&gt;Also, the enhanced for loop will &lt;em&gt;automatically&lt;/em&gt; use a hidden Iterator any time you try to loop over any Collection (or more generally, any Iterable) in JDK 5. Using an Iterator is directly supported by the language so that it's the most &lt;em&gt;convenient&lt;/em&gt; thing for you to do - as well as being fastest, or close to fastest.&lt;br /&gt;&lt;pre&gt;...&lt;br /&gt;&lt;br /&gt;for (Integer i:linkedLst) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(i);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;for (Integer i:arrayLst) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(i);&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;If more than one thread can access a Collection (or Map or array or other group-of-things), you need to synchronize. (Or use something like java.util.concurrent.locks classes from JDK 5+). No matter what you use a get(index) or an Iterator, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances. The main difference is that:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l14 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Some Iterator      implementations (including those of all the general purpose collection      implementations provided by the JRE) may choose to throw &lt;em&gt;ConcurrentModificationException&lt;/em&gt;      exception if this behavior is detected. Iterators that do this are known      as &lt;em&gt;fail-fast&lt;/em&gt; iterators, as they fail quickly and cleanly, rather      that risking arbitrary, non-deterministic behavior at an undetermined time      in the future. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l14 level1 lfo2; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The &lt;em&gt;get(index)&lt;/em&gt;      method will return bad data that makes very difficult to identify the real      problem. &lt;/li&gt;&lt;/ul&gt;A &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;ConcurrentModificationException&lt;/span&gt;&lt;/tt&gt; is a nice signal making us easy to find the problem and to handle the problem. This is another good reason to use an Iterator.&lt;br /&gt;Note that fail-fast behavior cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast operations throw &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;ConcurrentModificationException&lt;/span&gt;&lt;/tt&gt; on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: &lt;tt&gt;&lt;i&gt;&lt;span style="font-size: 10.0pt;"&gt;ConcurrentModificationException&lt;/span&gt;&lt;/i&gt;&lt;/tt&gt;&lt;em&gt; should be used only to detect bugs.&lt;/em&gt;&lt;br /&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;Why always override hashcode ( ) if overriding equals ( )?&lt;/h1&gt;In Java, every object has access to the &lt;em&gt;equals ()&lt;/em&gt; method because it is inherited from the &lt;em&gt;Object&lt;/em&gt; class. However, this default implementation just simply compares the memory addresses of the objects. You can override the default implementation of the &lt;em&gt;equals ()&lt;/em&gt; method defined in &lt;em&gt;java.lang.Object&lt;/em&gt;. If you override the &lt;em&gt;equals ()&lt;/em&gt;, you MUST also override &lt;em&gt;hashCode ()&lt;/em&gt;. Otherwise a violation of the general contract for Object.hashCode will occur, which can have unexpected repercussions when your class is in conjunction with all hash-based collections.&lt;br /&gt;Here is the contract, copied from the &lt;em&gt;java.lang.Object&lt;/em&gt; specialization:&lt;br /&gt;&lt;h3 style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;public int hashCode ()&lt;/h3&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;Returns a hash code value for the object. This method is supported for the benefit of hashtables such as those provided by &lt;em&gt;java.util.Hashtable&lt;/em&gt;.&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;The general contract of hashCode is:&lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l1 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;Whenever it is invoked on the same object more than once during an execution of a Java application, the &lt;em&gt;hashCode&lt;/em&gt; method must consistently return the same integer, provided no information used in &lt;em&gt;equals&lt;/em&gt; comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l1 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;If two objects are equal according to the &lt;/span&gt;&lt;/strong&gt;&lt;em&gt;&lt;b&gt;equals (Object)&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt; method, then calling the &lt;/span&gt;&lt;/strong&gt;&lt;em&gt;&lt;b&gt;hashCode&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt; method on each of the two objects must produce the same integer result.&lt;/span&gt;&lt;/strong&gt; &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l1 level1 lfo3; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;It is &lt;em&gt;not&lt;/em&gt; required that if two objects are unequal according to the &lt;em&gt;equals (java.lang.Object)&lt;/em&gt; method, then calling the &lt;em&gt;hashCode&lt;/em&gt; method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects might improve the performance of hashtables. &lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;As much as is reasonably practical, the &lt;em&gt;hashCode&lt;/em&gt; method defined by class &lt;em&gt;Object&lt;/em&gt; does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java&lt;sup&gt;TM&lt;/sup&gt; programming language.)&lt;/div&gt;The default implementation of &lt;em&gt;equals ()&lt;/em&gt; method checks to see if the two objects have the same identity. Similarly, the default implementation of the hashCode() method returns an integer based on the object's identity and is not based on the values of instance (and class) variables of the object. No matter how many times the values of its instance variables (data fields) change, the hash code calculated by the default hashCode implementation does not change during the life of the object.&lt;br /&gt;Consider the following code, we have overridden &lt;em&gt;equals()&lt;/em&gt; method to check if two objects are equal based on the values of their instance variables. Two objects may be stored at different memory addresses but may still be equal base on their instance variable.&lt;br /&gt;&lt;pre&gt;public class CustomerID {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;private long crmID;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private int nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public CustomerID(long crmID, int nameSpace) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;super();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.crmID = crmID;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.nameSpace = nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public boolean equals(Object obj) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//null instanceof Object will always return false&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (!(obj instanceof CustomerID))&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return false;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (obj == this)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return true;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;this.crmID == ((CustomerID) obj).crmID &amp;amp;&amp;amp;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.nameSpace == ((CustomerID) obj).nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Map m = new HashMap();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m.put(new CustomerID(2345891234L,0),"Jeff Smith");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(m.get(new CustomerID(2345891234L,0)));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Compile and run the above code, the output result is&lt;br /&gt;&lt;pre&gt;null&lt;/pre&gt;What is wrong? The two instances of CustomerID are logically equal according to the class's &lt;em&gt;equals&lt;/em&gt; method. Because the &lt;em&gt;hashCode()&lt;/em&gt; method is not overridden, these two instances' identities are not in common to the default &lt;em&gt;hashCode&lt;/em&gt; implementation. Therefore, the &lt;em&gt;Object.hashCode&lt;/em&gt; returns two seemingly random numbers instead of two equal numbers. Such behavior violates "Equal objects must have equal hash codes" rule defined in the hashCode contract.&lt;br /&gt;Let's provide a simple &lt;em&gt;hashCode()&lt;/em&gt; method to fix this problem:&lt;br /&gt;&lt;pre&gt;public class CustomerID {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private long crmID;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private int nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public CustomerID(long crmID, int nameSpace) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;super();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.crmID = crmID;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.nameSpace = nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public boolean equals(Object obj) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//null instanceof Object will always return false&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (!(obj instanceof CustomerID))&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return false;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (obj == this)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return true;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;this.crmID == ((CustomerID) obj).crmID &amp;amp;&amp;amp;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.nameSpace == ((CustomerID) obj).nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public int hashCode() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int result = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;result = (int)(crmID/12) + nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return result;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Map m = new HashMap();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m.put(new CustomerID(2345891234L,0),"Jeff Smith");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(m.get(new CustomerID(2345891234L,0)));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Compile and run the above code, the output result is&lt;br /&gt;&lt;pre&gt;Jeff Smith&lt;/pre&gt;The hashcode distribution for instances of a class should be random. This is exactly what is meant by the third provision of the &lt;em&gt;hashCode&lt;/em&gt; contract. Write a correct &lt;em&gt;hashCode&lt;/em&gt; method is easy, but to write an effective &lt;em&gt;hashCode&lt;/em&gt; method is extremely difficult.&lt;br /&gt;For example, From &lt;a href="http://www.javaworld.com/javaworld/jw-01-1999/jw-01-object.html" target="blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;How to Avoid Traps and Correctly Override Methods From java.lang.Object&lt;/span&gt;&lt;/a&gt;: If you are unsure how to implement hashCode (), just always return 0 in your implementations. So all of your custom objects will return the same hash code. Yes, it turns hashtable of your objects into one (possibly) long linked-list, but you have implemented hashCode() correctly!&lt;br /&gt;&lt;pre&gt;public int hashCode(){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;return 0;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;It's legal because it ensures that equal objects have the same hash code, but it also indicates that every object has the same hash code. So every object will be hashed into the same bucket, and hash tables degenerate to linked lists. The performance is getting worse when it needs to process a large number of objects. How to implement a good hash function is a big topic and we will not cover here.&lt;br /&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;What is 'instanceof'?&lt;/h1&gt;The &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; operator is used to check whether the run-time type of an object is compatible with a given &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;type&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; (&lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2"&gt;15.20.2 Type Comparison Operator instanceof&lt;/a&gt;):&lt;br /&gt;&lt;pre&gt;expression instanceof type&lt;/pre&gt;The type of an &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;expression&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; operand of the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; operator must be a &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;reference type&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; or the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;null&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; type; otherwise, a compile-time error occurs. The &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;type&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; mentioned after the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; operator must denote a reference type; otherwise, a compile-time error occurs.&lt;br /&gt;An &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; expression evaluates to true if both of the following conditions are met:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l2 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;expression&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;      is not &lt;em&gt;null&lt;/em&gt;. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l2 level1 lfo4; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;expression&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;      can be cast to &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;type&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt;.      That is, a cast expression of the form &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;(type)(expression)&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; will complete      without raising a &lt;em&gt;ClassCastException&lt;/em&gt;. &lt;/li&gt;&lt;/ul&gt;It is a compile-time error if the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;type&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; mentioned after the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; operator does not denote a &lt;em&gt;reifiable type&lt;/em&gt;.&lt;br /&gt;If a cast of the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;expression&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; to the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;type&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; would be rejected as a compile-time error, then the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; relational expression likewise produces a compile-time error. In such a situation, the result of the &lt;strong&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;instanceof&lt;/span&gt;&lt;/i&gt;&lt;/strong&gt; expression could never be true.&lt;br /&gt;Here is an example code:&lt;br /&gt;&lt;pre&gt;public class Test{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;String s = "XyzWS.com";&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;byte[] b = s.getBytes();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(b instanceof byte[]);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(b instanceof Object);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//System.out.println(b instanceof Object[]);&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//byte[] can not cast to Object[]&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//System.out.println(b instanceof byte);&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//byte is primitive type&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;What are method overloading and method overriding?&lt;/h1&gt;Each method in a Java class is uniquely identified by its method signature. A method's signature is its name and the number and the type of its arguments. The method return type, method modifiers, and declared thrown exceptions have no effect on the method signature.&lt;br /&gt;A class cannot contain two methods with the same "method signature."&lt;br /&gt;&lt;h2&gt;What is method overloading?&lt;/h2&gt;With the Java language, you can overload methods. Overloading is the practice of supplying more than one definition for a given method name in the same class. The compiler will automatically select the most appropriate one based on the arguments with which it is called.&lt;br /&gt;Several restrictions govern an acceptable set of overloaded methods:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l3 level1 lfo5; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Any two methods in a set of      overloaded functions must have different argument lists (The name of the      parameter is irrelevant. The type and order of the parameters is what's      important). &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l3 level1 lfo5; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A difference in return type      only is not sufficient to constitute an overload and is illegal. &lt;/li&gt;&lt;/ul&gt;Method overloading is generally used where a class can perform the same operation on more than one type of data.&lt;br /&gt;&lt;h2&gt;What is method overriding?&lt;/h2&gt;A sub class inherits methods from a super class. Sometimes, the sub class may want to provide a &lt;em&gt;new version&lt;/em&gt; of methods defined in the super class. This is referred to as method overriding. Method overriding allows a sub class to provide its own implementation of a method already provided by one of its super classes.&lt;br /&gt;A sub class method is provided with the same signature and return type as a method that is declared in its super class, the subclass is said to &lt;em&gt;override&lt;/em&gt; that method. You can also override a method with the same signature that returns a subclass of the object returned by the original method. This facility (introduced in 5.0) is called &lt;em&gt;covariant return type&lt;/em&gt;.&lt;br /&gt;The subclass method does not inherit the functionality of its super class method unless it explicitly calls the super class method using the "super" keyword.&lt;br /&gt;Several restrictions govern any particular method to override another correctly:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l13 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The return type and method      signature must be identical to those of a method in the super class. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l13 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The accessibility must not      be more restrictive than original method. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l13 level1 lfo6; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The overriding method must      not throw checked exceptions of classes that are not possible for the      original method. &lt;/li&gt;&lt;/ul&gt;&lt;h2&gt;Example&lt;/h2&gt;Consider an example from &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.10.2" target="blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;8.4.10.2 Example: Overloading, Overriding, and Hiding&lt;/span&gt;&lt;/a&gt;. In the example:&lt;br /&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;class Point {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;int x = 0, y = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;void move(int dx, int dy) { x += dx; y += dy; }&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;int color;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class RealPoint extends Point {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;float x = 0.0f, y = 0.0f;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;void move(int dx, int dy) { move((float)dx, (float)dy); }&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;void move(float dx, float dy) { x += dx; y += dy; }&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;the class &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RealPoint&lt;/span&gt;&lt;/code&gt; hides the declarations of the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;int&lt;/span&gt;&lt;/code&gt; instance variables &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;x&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;y&lt;/span&gt;&lt;/code&gt; of class &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Point&lt;/span&gt;&lt;/code&gt; with its own &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;float&lt;/span&gt;&lt;/code&gt; instance variables &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;x&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;y&lt;/span&gt;&lt;/code&gt;, and overrides the method &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;move&lt;/span&gt;&lt;/code&gt; of class &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Point&lt;/span&gt;&lt;/code&gt; with its own &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;move&lt;/span&gt;&lt;/code&gt; method. It also overloads the name &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;move&lt;/span&gt;&lt;/code&gt; with another method with a different signature.&lt;br /&gt;In this example, the members of the class &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RealPoint&lt;/span&gt;&lt;/code&gt; include the instance variable &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;color&lt;/span&gt;&lt;/code&gt; inherited from the class &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Point&lt;/span&gt;&lt;/code&gt;, the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;float&lt;/span&gt;&lt;/code&gt; instance variables &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;x&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;y&lt;/span&gt;&lt;/code&gt; declared in &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RealPoint&lt;/span&gt;&lt;/code&gt;, and the two &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;move&lt;/span&gt;&lt;/code&gt; methods declared in &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RealPoint&lt;/span&gt;&lt;/code&gt;.&lt;br /&gt;Which of these overloaded &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;move&lt;/span&gt;&lt;/code&gt; methods of class &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RealPoint&lt;/span&gt;&lt;/code&gt; will be chosen for any particular method invocation will be determined at compile time by the overloading resolution procedure.&lt;br /&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;Why am I getting unreported exception when the super class default constructor has a 'throws' clause?&lt;/h1&gt;&lt;pre&gt;class Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public Super() throws Exception {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Super Class");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Sub extends Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) throws Exception {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Sub s = new Sub();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Compile it and you have compile-time error:&lt;br /&gt;&lt;pre&gt;Sub.java:6: unreported exception java.lang.Exception in default constructor&lt;br /&gt;&lt;br /&gt;public class Sub extends Super {&lt;br /&gt;&lt;br /&gt;1 error&lt;/pre&gt;Here is a &lt;a href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4278961" target="blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Sun's Bug Report&lt;/span&gt;&lt;/a&gt; which can answer this question:&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;i&gt;When a superclass constructor has a non-empty throws clause, subclasses must define an explicit constructor with an appropriate throws clause, as a default constructor has no throws clause. (This is stated in JLS 2e 8.8.7, ruling out the xxxxx alternative of copying the superclass constructor's throws clause.&lt;o:p&gt;&lt;/o:p&gt;&lt;/i&gt;&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;i&gt;Currently, the compiler generates a default constructor with an empty throws clause, and then generates an error message. Unfortunately, the offending call, the implicit call to the superclass constructor, does not appear in the program text, so the message is confusing.&lt;/i&gt;&lt;i&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;To fix it, you have to explicit to define a default constructor with an appropriate throws clause:&lt;br /&gt;&lt;pre&gt;class Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public Super() throws Exception {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Super Class");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public class Sub extends Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public Sub() throws Exception {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) throws Exception {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Sub s = new Sub();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h1&gt;What is the difference between the 'bit-wise AND' and the 'conditional AND' operators?&lt;/h1&gt;The &amp;amp; and |&amp;nbsp;operators&amp;nbsp;are defined for two integer operands or two boolean operands. The &amp;amp;&amp;amp; and || operators&amp;nbsp;are not defined for anything but two boolean operands. For all other operand types and combinations, a compile-time error shall occur.&lt;br /&gt;&lt;h2&gt;The Integer Type Operands&lt;/h2&gt;The &amp;amp; operator&amp;nbsp;is the "bit-wise AND" operator and | is the "bit-wise OR" operator.&amp;nbsp; This permits you to manipulate individual bits in a number.&lt;br /&gt;&lt;h2&gt;The Boolean Type Operands&lt;/h2&gt;The &amp;amp;&amp;amp; is the "conditional AND" operator and&amp;nbsp; || is the "conditional OR" operator. The &lt;em&gt;&amp;amp;&amp;amp;&lt;/em&gt; and &lt;em&gt;||&lt;/em&gt; operators are evaluated in &lt;em&gt;short circuit&lt;/em&gt; fashion. This means that when you have an expression like:&lt;br /&gt;&lt;pre&gt;e1 &amp;amp;&amp;amp; e2&lt;/pre&gt;if expression &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;e1&lt;/span&gt;&lt;/tt&gt; evaluates to be false, then the value of the expression &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;e2&lt;/span&gt;&lt;/tt&gt; is not calculated. Similarly, if &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;e1&lt;/span&gt;&lt;/tt&gt; evaluates to be true, then the value of &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;e1 || e2&lt;/span&gt;&lt;/tt&gt; is automatically true, and therefore &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;e2&lt;/span&gt;&lt;/tt&gt; is not evaluated.&lt;br /&gt;For example, in the expression&lt;br /&gt;&lt;pre&gt;x != 0 &amp;amp;&amp;amp; 1 / x &amp;gt; x + y // no division by 0&lt;/pre&gt;the second part is never evaluated if &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;x&lt;/span&gt;&lt;/tt&gt; equals zero. Thus, &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;1/x&lt;/span&gt;&lt;/tt&gt; is not computed if &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;x&lt;/span&gt;&lt;/tt&gt; is zero, and no divide-by-zero error can occur.&lt;br /&gt;The&amp;nbsp;&amp;amp; the (unconditional) "logical AND" operator&amp;nbsp;and&amp;nbsp; | is the (unconditional)"logical OR" operator. When applied to boolean values, the &amp;amp; and | operators yield a boolean value. These operators are similar to the &amp;amp;&amp;amp; and || operators, except that the &amp;amp; and | operators are not evaluated in "short-circuit" fashion. That is, both operands are first evaluated before computing the result.&lt;br /&gt;For example&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String [] args){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;boolean[] b = new boolean[3];&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int count = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b[0] = false;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b[1] = false;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b[2] = false;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//The first operand evaluates to false, &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//the evaluation of the second operand is skipped. &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//Therefor, the count does not add 1.&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (false &amp;amp;&amp;amp; b[++count]) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("True -- 1");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("False -- 1");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(count);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//Both operands are evaluated. Therefor, the count adds 1 &lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (false &amp;amp; b[++count]) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("True -- 2");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;else {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("False -- 2");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(count);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output result is&lt;br /&gt;&lt;pre&gt;False -- 1&lt;br /&gt;&lt;br /&gt;0&lt;br /&gt;&lt;br /&gt;False -- 2&lt;br /&gt;&lt;br /&gt;1&lt;/pre&gt;&lt;h1&gt;Does Java support multidimensional arrays?&lt;/h1&gt;The Java programming language does not really support multi-dimensional arrays. It does, however, support &lt;em&gt;arrays of arrays&lt;/em&gt;.&lt;br /&gt;In Java, a two-dimensional array &lt;em&gt;x&lt;/em&gt; is really an array of one-dimensional arrays:&lt;br /&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;String[][] x = new String[5][5];&lt;/pre&gt;The expression &lt;em&gt;x[i]&lt;/em&gt; selects the &lt;em&gt;i&lt;sup&gt;th&lt;/sup&gt;&lt;/em&gt; one-dimensional array; the expression &lt;em&gt;x[i][j]&lt;/em&gt; selects the &lt;em&gt;j&lt;sup&gt;th&lt;/sup&gt;&lt;/em&gt; element from that array.&lt;br /&gt;You may figure out how three-dimensional array y is. It is an array of two-dimensional arrays.&lt;br /&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;String[][][] y = new String[5][5][5];&lt;/pre&gt;The expression &lt;em&gt;y[i]&lt;/em&gt; selects the &lt;em&gt;i&lt;sup&gt;th&lt;/sup&gt;&lt;/em&gt; two-dimensional array; the expression &lt;em&gt;y[i][j]&lt;/em&gt; selects the &lt;em&gt;j&lt;sup&gt;th&lt;/sup&gt;&lt;/em&gt; one-dimensional array; the expression &lt;em&gt;y[i][j][k]&lt;/em&gt; selects the &lt;em&gt;k&lt;sup&gt;th&lt;/sup&gt;&lt;/em&gt; element from the &lt;em&gt;y[i][j]&lt;/em&gt; selected one-dimensional array.&lt;br /&gt;In the Java built-in multi-dimensional arrays, array indices in each dimension range from &lt;em&gt;zero&lt;/em&gt; to &lt;em&gt;length - 1&lt;/em&gt;, where &lt;em&gt;length&lt;/em&gt; is the array length in the given dimension. In the above example, the &lt;em&gt;length&lt;/em&gt; for each dimensional is &lt;em&gt;5&lt;/em&gt;. The number of dimensions and the size of each dimension is fixed once the array has been allocated.&lt;br /&gt;The important difference between multi dimensional arrays, as in C/C++ and Java arrays, is that each array does not have to be of the same length. If you think of a two-dimensional array as a matrix, the matrix does not have to be a rectangle.&lt;br /&gt;Consider the following method of initializing an array of arrays:&lt;br /&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;Stirng[][] z = { {"D00"}, {"D10","D11"}, {"D20","D21","D22"} };&lt;/pre&gt;This does not produce the same set of data as the following method:&lt;br /&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;String[][] z = new String[3][3];&lt;/pre&gt;Both arrays have 3 rows but each row in the first example has a different number of columns, each row in the second example has an equal number of columns.&lt;br /&gt;&lt;h1&gt;Why the fully qualified name of a static final variable is not allowed in static initialization block?&lt;/h1&gt;Let's start with the following example:&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static final int var;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;Program.var = 8;&lt;/span&gt;&lt;/strong&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;// Compilation error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(Program.var);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;And,&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static final int var;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;var = 8;&lt;/span&gt;&lt;/strong&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//OK&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(Program.var);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Why the fully qualified name of the static final variable is not allowed in static initialization block?&lt;br /&gt;The rules governing definite assignment in &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/defAssign.html" target="_blank"&gt;Chapter 16. Definite Assignment&lt;/a&gt;:&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;Similarly, every blank &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final&lt;/span&gt;&lt;/code&gt; variable must be assigned at most once; it must be &lt;em&gt;definitely unassigned&lt;/em&gt; when an assignment to it occurs. Such an assignment is defined to occur if and only if either the simple name of the variable, or its simple name qualified by &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;this&lt;/span&gt;&lt;/code&gt;, occurs on the left hand side of an assignment operator. A Java compiler must carry out a specific conservative flow analysis to make sure that, for every assignment to a blank &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final&lt;/span&gt;&lt;/code&gt; variable, the variable is definitely unassigned before the assignment; otherwise a compile-time error must occur&lt;/div&gt;According to the above rules governing definite assignment, we can have the following code:&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static final int var1;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;final int var2;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;var1 = 8;&lt;/span&gt;&lt;/strong&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//OK&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;this.var2=10;&lt;/span&gt;&lt;/strong&gt; //OK&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h1&gt;What is the difference (or relation) between the Comparator and the Comparable interfaces?&lt;/h1&gt;Classes that implement these two interfaces play different roles in the sorting process. A class that implements the &lt;em&gt;Comparable&lt;/em&gt; interface, i.e., a Comparable type, is the type that is sorted, while a class that implements the &lt;em&gt;Comparator&lt;/em&gt; interface, i.e., a Comparator, is used to compare other types.&lt;br /&gt;A &lt;em&gt;Comparable&lt;/em&gt; object can compare itself to another Object using its &lt;em&gt;compareTo(Object other)&lt;/em&gt; method. The object itself defines how the comparison is done.&lt;br /&gt;Interface Comparable&lt;t&gt; has a method:&lt;/t&gt;&lt;br /&gt;public int compareTo(T o)&lt;br /&gt;A &lt;em&gt;Comparator&lt;/em&gt; object is used to compare two objects of the same type using the compare(Object other1, Object other2) method. When using a Comparator, the objects being compared don't need to define the rule of comparison.&lt;br /&gt;Interface Comparator&lt;t&gt; has a method:&lt;/t&gt;&lt;br /&gt;public int compare(T o1, T o2)&lt;br /&gt;The primary use of a Comparator is to pass it to something that does sorting, either one of the explicit sort methods, or to a data structure than implicitly sorts (e.g., TreeSet or TreeMap).&lt;br /&gt;&lt;pre&gt;...&lt;br /&gt;&lt;br /&gt;Comparator fileComp = new FileComparator();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//... Create a File object for the current directory.&lt;br /&gt;&lt;br /&gt;File dir = new File(".");&lt;br /&gt;&lt;br /&gt;File[] children = dir.listFiles();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Sort using a comparator.&lt;br /&gt;&lt;br /&gt;Arrays.sort(children, fileComp); &lt;br /&gt;&lt;br /&gt;...&lt;/pre&gt;You use &lt;em&gt;Comparable&lt;/em&gt; if you want to implement the comparison method into the value object class itself. If you want to keep the comparison method separate from the value object class, you should use &lt;em&gt;Comparator&lt;/em&gt; and create a separate class that implements that interface.&lt;br /&gt;Here are some rules of thumb:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l25 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;If the comparison rules are      simple and all the objects in the collection are of a single type, as well      as you can change the code of object class. In such case, it's often      easier to make them &lt;em&gt;Comparable&lt;/em&gt; and let the container do the      comparing. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l25 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;If the comparison rules are      complex and/or objects in the collection are a wide variety of object      types, it makes more sense to build an external &lt;em&gt;Comparator&lt;/em&gt;      interface. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l25 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Sometime, implementing &lt;em&gt;Comparator&lt;/em&gt;      is the only option because you can't change the code of the object. But      you still want to sort these objects in a &lt;em&gt;Collection&lt;/em&gt;. You can      create a &lt;em&gt;Comparator&lt;/em&gt; that will take two objects and compare based      on your own criteria, then pass that into the Sorted Collection, or using      the &lt;em&gt;Collections.sort()&lt;/em&gt; method to sort based on your way of      sorting. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l25 level1 lfo7; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Sometimes it's desirable to      have the comparison method separate from the value object class itself.      For example, if there are different possible way of sorting, you could      write different classes that implement &lt;em&gt;Comparator&lt;/em&gt; interface and      choose the one you need at a specific point in the program. &lt;/li&gt;&lt;/ul&gt;A Comparator is a "third-party" tool used by a container to order its contents. The container will pass two objects to the supplied Comparator each time it needs to compare them. The Comparator is responsible for returning an indication of the ordering of the two objects but has no relationship with the objects other than that. The comparator object does not need to be an instance of any class related to the objects being compared, it just needs to know how to compare them.&lt;br /&gt;A Comparable object, on the other hand, knows how to compare itself with another supplied object. If no Comparator is specified then the container must compare its contents by asking each one how it relates to another.&lt;br /&gt;&lt;h1&gt;Can private method be overridden?&lt;/h1&gt;The &lt;em&gt;private&lt;/em&gt; methods are not inherited by subclasses and you cannot be overridden by subclasses. According to Java Language Specification (&lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.8.3"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;8.4.8.3 Requirements in Overriding and Hiding&lt;/span&gt;&lt;/a&gt;), "Note that a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;private&lt;/span&gt;&lt;/code&gt; method cannot be hidden or overridden in the technical sense of those terms. This means that a subclass can declare a method with the same signature as a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;private&lt;/span&gt;&lt;/code&gt; method in one of its superclasses, and there is no requirement that the return type or &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;throws&lt;/span&gt;&lt;/code&gt; clause of such a method bear any relationship to those of the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;private&lt;/span&gt;&lt;/code&gt; method in the superclass."&lt;br /&gt;What does it mean? It means you can have a private method has the exact same name and signature as a private method in the superclass, but you are not overriding the private method in superclass and you are just declaring a new private method in the subclass. The new defined method in the subclass is completely unrelated to the superclass method. A private method of a class can be only accessed by the implementation in its class. A subclass cannot call a private method in its superclasses.&lt;br /&gt;&lt;h1&gt;What is the difference between implementing Runnable and extending Thread?&lt;/h1&gt;One difference between implementing &lt;em&gt;Runnable&lt;/em&gt; and extending &lt;em&gt;Thread&lt;/em&gt; is that by extending &lt;em&gt;Thread&lt;/em&gt;, each of your threads has a unique object associated with it, whereas implementing &lt;em&gt;Runnable&lt;/em&gt;, many threads can share the same object instance. For example,&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main (String[] args) {&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Runner r = new Runner();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread t1 = new Thread(r, "Thread A");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread t2 = new Thread(r, "Thread B");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread s1 = new Strider("Thread C");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread s2 = new Strider("Thread D");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t1.start();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;t2.start();v&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s1.start();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s2.start();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Runner implements Runnable {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private int counter;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public void run() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (int i = 0; i != 2; i++) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(Thread.currentThread().getName() + ": " &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;+ counter++);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread.sleep(1000);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch(InterruptedException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;e.printStackTrace();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Strider extends Thread {&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private int counter;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Strider(String name)&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;super(name);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public void run()&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (int i = 0; i != 2; i++) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(Thread.currentThread().getName() + ": " &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;+ counter++);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Thread.sleep(1000);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch(InterruptedException e)&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;e.printStackTrace();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output result is&lt;br /&gt;&lt;pre&gt;Thread A: 0&lt;br /&gt;&lt;br /&gt;Thread B: 1&lt;br /&gt;&lt;br /&gt;Thread C: 0&lt;br /&gt;&lt;br /&gt;Thread D: 0&lt;br /&gt;&lt;br /&gt;Thread A: 2&lt;br /&gt;&lt;br /&gt;Thread B: 3&lt;br /&gt;&lt;br /&gt;Thread C: 1&lt;br /&gt;&lt;br /&gt;Thread D: 1&lt;/pre&gt;A class that implements &lt;em&gt;Runnable&lt;/em&gt; is not a thread and just a class. For a &lt;em&gt;Runnable&lt;/em&gt; to become a Thread, You need to create an instance of &lt;em&gt;Thread&lt;/em&gt; and passing itself in as the target.&lt;br /&gt;In most cases, the &lt;em&gt;Runnable&lt;/em&gt; interface should be used if you are only planning to override the &lt;em&gt;run()&lt;/em&gt; method and no other &lt;em&gt;Thread&lt;/em&gt; methods. This is important because classes should not be subclassed unless the programmer intends on modifying or enhancing the fundamental behavior of the class.&lt;br /&gt;When there is a need to extend a superclass, implementing the &lt;em&gt;Runnable&lt;/em&gt; interface is more appropriate than using the &lt;em&gt;Thread&lt;/em&gt; class. Because we can extend another class while implementing &lt;em&gt;Runnable&lt;/em&gt; interface to make a thread. But if we just extend the &lt;em&gt;Thread&lt;/em&gt; class we can't inherit from any other class.&lt;br /&gt;&lt;h1&gt;How does a static method access instance variables?&lt;/h1&gt;Static methods can not directly access any instance variables or methods. But they can access them by using their object reference. Static methods may even access &lt;em&gt;private&lt;/em&gt; instance variables via a object reference.&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private int count;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public Program(int ballcount){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;count=ballcount;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String argv[]){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Program s = new Program(99);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//System.out.println(count); //compile time error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//add(10);&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//compile time error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(s.count);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.add(10);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(s.count);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private void add(int num) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;count += num;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output result is&lt;br /&gt;&lt;pre&gt;99&lt;br /&gt;&lt;br /&gt;109&lt;/pre&gt;All &lt;em&gt;private&lt;/em&gt; instance variables are &lt;em&gt;private&lt;/em&gt; to its class, they can be accessed by static methods or non-static method of the class.&lt;br /&gt;&lt;h1&gt;What is the difference between compile time error and run time error?&lt;/h1&gt;At compile time, when the code does not comply with the Java syntactic and semantics rules as described in Java Language Specification (JLS), compile-time errors will occurs. The goal of the compiler is to ensure the code is compliant with these rules. Any rule-violations detected at this stage are reported as compilation errors.&lt;br /&gt;The best way to get to know those rules is to go through all the sections in the JLS containing the key words "compile-time error". In general, these rules include syntax checking: declarations, expressions, lexical parsing, file-naming conventions etc; exception handling: for checked exceptions; accessibility, type-compatibility, name resolution: checking to see all named entities - variables, classes, method calls etc. are reachable through at least one of the declared path; etc.&lt;br /&gt;The following are some common compile time errors:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;a class tries to extend more      than one class &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Overloading or overriding is      not implemented correctly &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Attempt to refer to a      variable that is not in the scope of the current block &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An inner class has the same      name as one of one of its enclosing classes &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A class contains one or more      abstract methods and the class itself is not declared "abstract"      &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;a class tries to reference a      private member of another class &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to create an instance      of an abstract class &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to change the value      of an already initialized constant (final member) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l17 level1 lfo8; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;declare two (class or      instance) members with the same name&lt;/li&gt;&lt;/ul&gt;&lt;a href="http://www.xyzws.com/scjp/studyresources/CompiletimeError.html" target="blank"&gt;Here is a list of conditions that may cause compile-time errors&lt;/a&gt;.&lt;br /&gt;When the code compiles without any error, there is still chance that the code will fail at run time. The errors only occurs at run time are call run time errors. Run time errors are those that passed compiler's checking, but fails when the code gets executed. There are a lot of causes may result in runtime errors, such as incompatible type-casting, referencing an invalid index in an array, using an null-object, resource problems like unavailable file-handles, out of memory situations, thread dead-locks, infinite loops(not detected!), etc.&lt;br /&gt;The following are some common runtime errors:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l8 level1 lfo9; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to invoke a method on      an uninitialized variable (NullPointerException) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l8 level1 lfo9; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;ran out memory (memory      leaks...) (OutOfMemoryError) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l8 level1 lfo9; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to open a file that      doesn't exist (FileNotFoundException) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l8 level1 lfo9; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to pass arguments to a      method which are not within the accepted bounds (IllegalArgumentException)      &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l8 level1 lfo9; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to invoke the start()      method on a dead thread (-) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l8 level1 lfo9; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;trying to invoke wait() or      notify() on an object without owning the object's monitor (IllegalMonitorStateException)&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Why doesn't Iterator work for my collection?&lt;/h1&gt;Let's take a look at the following code:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String args[]) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ArrayList&lt;string&gt; alist = new ArrayList&lt;string&gt;();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("A"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("B"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("C"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (Iterator&lt;string&gt; it = alist.iterator(); it.hasNext(); ) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(alist.get(i++));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;A runtime exception java.lang.IndexOutOfBoundsException is&amp;nbsp;thrown when it goes beyond the end.&lt;br /&gt;What is wrong? The code combines the iterator and index. After &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;hasNext()&lt;/span&gt;&lt;/code&gt; returns true, the only way to advance the iterator is to call &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;next()&lt;/span&gt;&lt;/code&gt;. But the element is retrieved with &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;get (index)&lt;/span&gt;&lt;/code&gt;, so the iterator is never advanced. In the above example, the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;hasNext ()&lt;/span&gt;&lt;/code&gt; will always be true, and eventually the index &lt;em&gt;i&lt;/em&gt; for &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;get(index)&lt;/span&gt;&lt;/code&gt; will beyond the end of &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt;.&lt;br /&gt;Do not mix up the iterator and the index. Let's change the above code to fix the problem:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main (String args []) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ArrayList&lt;string&gt; alist = new ArrayList&lt;string&gt;();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("A"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("B"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("C"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (Iterator&lt;string&gt; it = alist.iterator(); it.hasNext(); ) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(it.next());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;or&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String args[]) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ArrayList&lt;string&gt; alist = new ArrayList&lt;string&gt;();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("A"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("B"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;alist.add(new String("C"));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;for (int i=0; i != alist.size(); i++) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(alist.get(i));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/string&gt;&lt;/string&gt;&lt;/pre&gt;&lt;h1&gt;What are mutable objects and immutable objects?&lt;/h1&gt;An Immutable object is a kind of object whose state cannot be modified after it is created. This is as opposed to a mutable object, which can be modified after it is created.&lt;br /&gt;In Java, objects are referred by references. If an object is known to be immutable, the object reference can be shared. For example, &lt;em&gt;Boolean&lt;/em&gt;, &lt;em&gt;Byte&lt;/em&gt;, &lt;em&gt;Character&lt;/em&gt;, &lt;em&gt;Double&lt;/em&gt;, &lt;em&gt;Float&lt;/em&gt;, &lt;em&gt;Integer&lt;/em&gt;, &lt;em&gt;Long&lt;/em&gt;, &lt;em&gt;Short&lt;/em&gt;, and &lt;em&gt;String&lt;/em&gt; are immutable classes in Java, but the class &lt;em&gt;StringBuffer&lt;/em&gt; is a mutable object in addition to the immutable &lt;em&gt;String&lt;/em&gt;.&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;String str = "HELLO";&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(str);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;str.toLower();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(str);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output result is&lt;br /&gt;&lt;pre&gt;HELLO&lt;br /&gt;&lt;br /&gt;HELLO&lt;/pre&gt;From the above example, the toLower() method does not impact on the original content in &lt;em&gt;str&lt;/em&gt;. What happens is that a new String object "hello" is constructed. The toLower() method returns the new constructed String object's reference. Let's modify the above code:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;String str = "HELLO";&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(str);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;String str1 = str.toLower();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(str1);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output result is&lt;br /&gt;&lt;pre&gt;HELLO&lt;br /&gt;&lt;br /&gt;hello&lt;/pre&gt;The &lt;em&gt;str1&lt;/em&gt; references a new String object that contains "hello". The String object's method never affects the data the String object contains, excluding the constructor.&lt;br /&gt;In Effective Java, Joshua Bloch makes this recommendation: "Classes should be immutable unless there's a very good reason to make them mutable....If a class cannot be made immutable, you should still limit its mutability as much as possible."&lt;br /&gt;What are the guidelines to implement immutable object? Please visit &lt;a href="http://wiki.java.net/bin/view/Javapedia/ImmutableObject"&gt;Immutable Object&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;Immutable Object &lt;/h1&gt;Objects have identity (location in memory), state (data), and behavior (methods). Once constructed, an Immutable Object cannot change state. &lt;br /&gt;Immutable objects greatly simplify your program, since they &lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;are simple to construct,      test, and use &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;are automatically      thread-safe and have no synchronization issues &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;do not need a copy      constructor &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;do not need an      implementation of &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;clone&lt;/span&gt;&lt;/code&gt;      &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;do not need to be copied      defensively when used as a field &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;make good Map keys and Set      elements (these objects must not change state while in the collection) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l5 level1 lfo27; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;a href="http://wiki.java.net/bin/view/Javapedia/ImmutableObject"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Immutable Objects&lt;/span&gt;&lt;/a&gt;      are often also &lt;a href="http://wiki.java.net/bin/view/Javapedia/ValueObject"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ValueObjects&lt;/span&gt;&lt;/a&gt;,      e.g. when used as Map keys &lt;/li&gt;&lt;/ul&gt;Make a class immutable by following these guidelines : &lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l26 level1 lfo28; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;always put all data in the      constructor, instead of using a no-argument constructor combined with subsequent      calls to setXXX methods &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l26 level1 lfo28; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;do not provide any methods      which can change the state of the object in any way - not just setXXX      methods, but any method which can change state &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l26 level1 lfo28; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;ensure no methods can be      overridden - make the class final, or use static factories and keep      constructors private &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l26 level1 lfo28; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;make fields final &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l26 level1 lfo28; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;if a mutable object field's      state is "owned" by the native class, such that no other class      is to be allowed to change the state of the field, then when the field      "crosses the interface" (as in a get method, when the field is      returned to the user, or in the constructor itself), then a defensive copy      must be made, in order to maintain encapsulation. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l26 level1 lfo28; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;if a mutable object field's      state is not "owned" by the native class, then defensive copies      of the object field are not necessary &lt;/li&gt;&lt;/ul&gt;Example&lt;br /&gt;&lt;pre&gt;import java.util.Date;&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;/**&lt;/pre&gt;&lt;pre&gt;* Planet is an immutable class, since there is no way to change&lt;/pre&gt;&lt;pre&gt;* its state after construction.&lt;/pre&gt;&lt;pre&gt;*/&lt;/pre&gt;&lt;pre&gt;public final class Planet {&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;/**&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* Primitive data is always immutable.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;*/&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private final double mass;&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;/**&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* An immutable object field. (String objects never change state.)&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;*/&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private final String name;&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;/**&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* A mutable object field. In this case, the state of this mutable field&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* is to be changed only by this class. (In other cases, it makes perfect&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* sense to allow the state of a field to be changed outside the native&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* class; this is the case when a field acts as a "pointer" to an object&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* created elsewhere.)&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;*/&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private final Date dateOfDiscovery;&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;/** Sole constructor */&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public Planet (double mass, String name, Date dateOfDiscovery) {&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.mass = mass;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.name = name;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//make a private copy of dateOfDiscovery&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//this is the only way to keep the dateOfDiscovery&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//field private, and shields this class from any changes&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//to the original dateOfDiscovery object&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.dateOfDiscovery = new Date(dateOfDiscovery.getTime());&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//gets but no sets, and no methods which change state&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public double getMass() {&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return mass;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public String getName() {&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return name;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;/**&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* Returns a defensive copy of the field.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* The caller of this method can do anything they want with the&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* returned Date object, without affecting the internals of this&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;* class in any way.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;*/&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public Date getDateOfDiscovery() {&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return new Date(dateOfDiscovery.getTime());&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;br /&gt;&lt;h1&gt;Can an overriding method have a different return type than the overridden method?&lt;/h1&gt;Before Java 5.0, when you override a method, both parameters and return type must match exactly. In Java 5.0, it introduces a new facility called &lt;em&gt;covariant&lt;/em&gt; return type. You can override a method with the same signature but returns a subclass of the object returned. In another words, a method in a subclass can return an object whose type is a subclass of the type returned by the method with the same signature in the superclass.&lt;br /&gt;For example, the following code compiles and the narrower type B is a legal return type for the getObject method in the subclass, &lt;em&gt;Sub&lt;/em&gt;.&lt;br /&gt;&lt;pre&gt;class A {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class B extends A {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public A getObject() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Super::getObject");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return new A();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Sub extends Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public B getObject() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Sub::getObject");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return new B();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Super s = new Sub();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.getObject();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output of the above code is:&lt;br /&gt;Sub::getObject&lt;br /&gt;But, the following code will not compile because &lt;em&gt;String&lt;/em&gt; is not a legal return type for the getObject method in the subclass, &lt;em&gt;Sub&lt;/em&gt;. &lt;em&gt;String&lt;/em&gt; does not extends from either A or B.&lt;br /&gt;&lt;pre&gt;class A {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class B extends A {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public A getObject() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Super::getObject");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return new A();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class Sub extends Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public B getObject() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Sub::getObject");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return new B();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public String getObject() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return "getObject()";&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Super s = new Sub();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.getObject();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h1&gt;What is the difference between an abstract class and an interface?&lt;/h1&gt;An abstract class can be partly finished so that the child class can fill in appropriate missing info. An interface simply demands that an implementing class to implement a certain behavior, leaving it up to the implementing class as to how this is to be done. Neither interface or abstract classes can be instantiated.&lt;br /&gt;What is the defferent between them?&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l6 level1 lfo10; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An abstract class can      contain both non-abstract methods (methods with a body) and abstract      methods (methods without a body). An interface only contains abstract      methods. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l6 level1 lfo10; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A class can implement      multiple interfaces, but can only subclass one abstract class. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l6 level1 lfo10; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An abstract class can have      instance variables and instance initializers. An interface cannot. Any      variables defined in an interface are implicitly &lt;em&gt;public&lt;/em&gt;, &lt;em&gt;static&lt;/em&gt;,      and &lt;em&gt;final&lt;/em&gt; (the variables of an interface are final they must be      initialized). &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l6 level1 lfo10; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An abstract class can define      constructor. An interface cannot. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l6 level1 lfo10; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An abstract class can have &lt;em&gt;public&lt;/em&gt;      or &lt;em&gt;protected&lt;/em&gt; methods. An interface can have only &lt;em&gt;public&lt;/em&gt;      methods. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l6 level1 lfo10; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An abstract class inherits      from &lt;em&gt;Object&lt;/em&gt; and includes methods such as &lt;em&gt;clone()&lt;/em&gt; and &lt;em&gt;equals()&lt;/em&gt;.      &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Why narrowing primitive conversions from primitive long requires explicit casting?&lt;/h1&gt;Converting a large primitive type to a smaller primitive type is called &lt;em&gt;narrowing primitive conversion&lt;/em&gt;. There's a special case known as an &lt;em&gt;assignment conversion&lt;/em&gt; that handles some conversion without explicit casting.&lt;br /&gt;Let's take a look at the following code:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;final int iVar = 10;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;byte&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;bVar = iVar;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;fianl int iVar1 = 345;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;byte&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;bVar1 = iVar1; //Compile time error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;In this example, a variable of type &lt;em&gt;byte&lt;/em&gt; is being assigned a value of a constant expression of type &lt;em&gt;int&lt;/em&gt;. This implies a narrowing conversion. We know that the value of constant expression must be in the range of variable's type for the assignment conversion to happen. In our example, as long as the value of &lt;em&gt;iVar&lt;/em&gt; is for -128 to 127, the narrowing conversion is used in the assignment conversion.&lt;br /&gt;The following code does not compile, even with the value of constant expression is in the range of type &lt;em&gt;byte&lt;/em&gt;:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;final long lVar = 10;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;byte bVar = lVar; //compile time error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Why the assignment conversion doesn't work here? We have a constant expression and the value is representable for the type &lt;em&gt;byte&lt;/em&gt;. Here is answer from JLS:&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;5.2 Assignment Conversion in Java Language Specification 3rd Edition:&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;In addition, if the expression is a constant expression of type &lt;strong&gt;byte&lt;/strong&gt;, &lt;strong&gt;short&lt;/strong&gt;, &lt;strong&gt;char&lt;/strong&gt; or &lt;strong&gt;int&lt;/strong&gt;:&lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l9 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;A narrowing primitive conversion MAY BE used if the type of the variable is &lt;strong&gt;byte&lt;/strong&gt;, &lt;strong&gt;short&lt;/strong&gt;, or &lt;strong&gt;char&lt;/strong&gt;, and the value of the constant expression is representable in the type of the variable. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l9 level1 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;A narrowing primitive conversion followed by a boxing conversion may be used if the type of the variable is : &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.5in; margin-right: .5in; mso-list: l9 level2 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-list: Ignore;"&gt;o&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;Byte and the value of the constant expression is representable in the type byte. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.5in; margin-right: .5in; mso-list: l9 level2 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-list: Ignore;"&gt;o&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;Short and the value of the constant expression is representable in the type short. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.5in; margin-right: .5in; mso-list: l9 level2 lfo11; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;; font-size: 10.0pt; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-list: Ignore;"&gt;o&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;Character and the value of the constant expression is representable in the type char. &lt;/div&gt;In the above example, the expression is a constant expression of type &lt;em&gt;long&lt;/em&gt; and it is not covered by the special case. Therefore, a explicit &lt;em&gt;cast&lt;/em&gt; must be used.&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;final long lVar = 10;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;byte bVar = (byte)lVar;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h1&gt;When is an explicit object reference casting is required?&lt;/h1&gt;General speaking, if the left hand side of an assignment is a more specific type (subtype) and the right hand side is a more general type (supertype), then explicit casting is required. On the other hand, when you assign a subclass reference to a variable of superclass, the casting is performed automatically, or not required.&lt;br /&gt;Another case is calling methods from an object reference, you can not access methods that are only declared and implemented in its subclass. You have to explicit cast the object reference into the actual object type.&lt;br /&gt;For example,&lt;br /&gt;&lt;pre&gt;class Super {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Sub extends Super {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;public void writeLog() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("log");&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Sub b = new Sub();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// sub type reference can be assigned to super type without casting&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Super a = b; &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// super type reference has to be casted before assigned to sub type&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;b = (Sub)a;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;((Sub)a).writeLog(); // method only defined in sub type&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Java compiler is not responsible for checking if the casting is correct or not, just like some of the bindings only occur at run time ( &lt;a href="http://www.xyzws.com/javafaq/run-time-binding-or-compile-time-binding/30"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;XyzWs Java FAQ: Run time binding or compile time binding?&lt;/span&gt;&lt;/a&gt;). Java virtual machine does the checking at run time to find out whether the actual reference object is a legitimate object of the new type. If not, there will be a runtime exception: &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;ClassCastException&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;&lt;h1&gt;When is an object eligible for garbage collection?&lt;/h1&gt;An object is eligible for garbage collection when there are no more &lt;em&gt;root set of references&lt;/em&gt; to that object. References that are held in a variable are usually dropped when the variable goes out of scope.&lt;br /&gt;What are root sets of references? The object references in the local variables, operand stack of any stack frame and any object references in any class variables, and object references that were passed to native methods that either haven't been "released" by the native method.&lt;br /&gt;&lt;br /&gt;In &lt;a href="http://java.sun.com/developer/technicalArticles/ALT/RefObj/index.html" target="_blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Reference Objects and Garbage Collection&lt;/span&gt;&lt;/a&gt;:&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;An executing Java program consists of a set of threads, each of which is actively executing a set of methods (one having called the next). Each of these methods can have arguments or local variables that are references to objects. These references are said to belong to a &lt;em&gt;root set&lt;/em&gt; of references that are immediately accessible to the program. Other references in the root set include static reference variables defined in loaded classes, and references registered through the Java Native Interface (JNI) API.&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;All objects referenced by this root set of references are said to be &lt;em&gt;reachable&lt;/em&gt; by the program in its current state and must not be collected. Also, those objects might contain references to still other objects, which are also reachable, and so on.&lt;/div&gt;In &lt;a href="http://www.artima.com/insidejvm/ed2/gc2.html" target="_blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Garbage Collection&lt;/span&gt;&lt;/a&gt;:&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;The root set in a Java virtual machine is implementation dependent, but would always include any object references in the local variables and operand stack of any stack frame and any object references in any class variables. Another source of roots are any object references, such as strings, in the constant pool of loaded classes. The constant pool of a loaded class may refer to strings stored on the heap, such as the class name, superclass name, superinterface names, field names, field signatures, method names, and method signatures. Another source of roots may be any object references that were passed to native methods that either haven't been "released" by the native method. (Depending upon the native method interface, a native method may be able to release references by simply returning, by explicitly invoking a call back that releases passed references, or some combination of both.) Another potential source of roots is any part of the Java virtual machine's runtime data areas that are allocated from the garbage-collected heap. For example, the class data in the method area itself could be placed on the garbage-collected heap in some implementations, allowing the same garbage collection algorithm that frees objects to detect and unload unreferenced classes.&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;What is '&lt;st1:place&gt;Islands&lt;/st1:place&gt; of Isolation' in garbage collection?&lt;/h1&gt;You may already know that when an object is not referenced by other objects, it's eligiable for garbage collection. But do you know the following two statemenats are also true?&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l15 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;"If an object &lt;em&gt;obj1&lt;/em&gt;      is garbage collected, but another object &lt;em&gt;obj2&lt;/em&gt; contains a      reference to it, then &lt;em&gt;obj2&lt;/em&gt; is also eligible for garbage      collection"&lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l15 level1 lfo14; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;"If object &lt;em&gt;obj2&lt;/em&gt;      can access object &lt;em&gt;obj1&lt;/em&gt; that is eligible for garbage collection,      then &lt;em&gt;obj2&lt;/em&gt; is also eligible for garbage collection" &lt;/li&gt;&lt;/ul&gt;This is called "&lt;st1:place&gt;&lt;st1:placetype&gt;Island&lt;/st1:placetype&gt; of &lt;st1:placename&gt;Isolation&lt;/st1:placename&gt;&lt;/st1:place&gt;". An "island of isolation" describes one or more objects have NO references to them from active parts of an application.&lt;br /&gt;In &lt;a href="http://www.xyzws.com/faq.do?cat=faq&amp;amp;article=41"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;When is an object eligible for garbage collection?&lt;/span&gt;&lt;/a&gt;, we talked about: any object, that are not accessible from root set of references, is eligible for garbage collection. If object &lt;em&gt;obj1&lt;/em&gt; is eligible for garbage collection meaning it is not reachable by any objects from root set of references. Then the garbage collection algorithm tries to find any objects that have ONLY refernce to object &lt;em&gt;obj1&lt;/em&gt; (in this case object &lt;em&gt;obj2&lt;/em&gt;) which also become eligible for garbage collection.&lt;br /&gt;If &lt;em&gt;obj2&lt;/em&gt; was accessible from root then the object &lt;em&gt;obj1&lt;/em&gt; would never be eligible for garbage collection in the first place. Therefore, it must be that the object &lt;em&gt;obj2&lt;/em&gt; cannot be referenced from the active part of the program, and so object &lt;em&gt;obj2&lt;/em&gt; is eligible for garbage collection.&lt;br /&gt;Here is an example,&lt;br /&gt;&lt;pre&gt;class Person {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public Person firend;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Person obj1 = new Person();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Person obj2 = new Person();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;obj2.firend = obj1;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;obj1 = null;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//Line A&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;obj2 = null;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//Line B&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;After &lt;strong&gt;Line A&lt;/strong&gt; executes, The object &lt;em&gt;obj2&lt;/em&gt; still has a reference to the object obj1 and the object &lt;em&gt;obj2&lt;/em&gt; is still referenced by the variable &lt;em&gt;obj2&lt;/em&gt;. Therefore, the object &lt;em&gt;obj1&lt;/em&gt; can not be eligable for garbage collection. After &lt;strong&gt;Line B&lt;/strong&gt; exectues, there are no more references to the object &lt;em&gt;obj2&lt;/em&gt;. There still is a reference to object &lt;em&gt;obj1&lt;/em&gt; inside the object &lt;em&gt;obj2&lt;/em&gt;. Now, the object &lt;em&gt;obj1&lt;/em&gt; and &lt;em&gt;obj2&lt;/em&gt; has no reference from root set of references. Therefore, both of objects are eligible for garbage collection.&lt;br /&gt;&lt;h1&gt;What are the differences between interrupted() and isInterrupted() method of the Thread class?&lt;/h1&gt;There are some subtle differences between the &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Thread.html" target="_blank"&gt;Thread (Thread API Document)&lt;/a&gt; methods interrupted() and isInterrupted():&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l12 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The &lt;em&gt;interrupted()&lt;/em&gt;      is a &lt;em&gt;&lt;b&gt;static&lt;/b&gt;&lt;/em&gt; method in Thread class that determines if the      current thread has been interrupted. "&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;The &lt;/span&gt;&lt;/strong&gt;&lt;em&gt;&lt;b&gt;interrupted status&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt; of the thread is cleared by      this method&lt;/span&gt;&lt;/strong&gt;". Therefore, if a thread was interrupted,      calling &lt;em&gt;interrupted()&lt;/em&gt; once would return &lt;em&gt;true&lt;/em&gt;, while a      second call to it would return &lt;em&gt;false&lt;/em&gt; until the current thread is      interrupted again. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l12 level1 lfo15; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The &lt;em&gt;isInterrupted()&lt;/em&gt;      is an &lt;em&gt;&lt;b&gt;instance&lt;/b&gt;&lt;/em&gt; method that tests if this thread instance      has been interrupted. "&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;The &lt;/span&gt;&lt;/strong&gt;&lt;em&gt;&lt;b&gt;interrupted status&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt; of the thread is unaffected      by this method&lt;/span&gt;&lt;/strong&gt;". &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Why can any non-final class be casted to an interface?&lt;/h1&gt;A cast to an &lt;em&gt;interface&lt;/em&gt; is always allowed at compile-time unless the class is &lt;em&gt;final class&lt;/em&gt; and does not implement this interface.&lt;br /&gt;&lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/conversions.html#5.5" target="_blank"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;5.5 Casting Conversion in JLS&lt;/span&gt;&lt;/a&gt;, you are trying to cast a reference type S to a reference type T:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l23 level1 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;If S is a class type: &lt;/li&gt;&lt;ul type="circle"&gt;&lt;li class="MsoNormal" style="mso-list: l23 level2 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;If T is a class       type, then either |S| &amp;lt;: |T|, or |T| &amp;lt;: |S|; otherwise a       compile-time error occurs. Furthermore, if there exists a supertype X of       T, and a supertype Y of S, such that both X and Y are provably distinct       parameterized types, and that the erasures of X and Y are the same, a       compile-time error occurs. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l23 level2 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;If T is an interface       type: &lt;/li&gt;&lt;ul type="square"&gt;&lt;li class="MsoNormal" style="mso-list: l23 level3 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.5in;"&gt;If S is not a final        class, then, if there exists a supertype X of T, and a supertype Y of S,        such that both X and Y are provably distinct parameterized types, and        that the erasures of X and Y are the same, a compile-time error occurs.        Otherwise, the cast is always legal at compile time (because even if S        does not implement T, a subclass of S might). &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l23 level3 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.5in;"&gt;If S is a final        class, then S must implement T, or a compile-time error occurs. &lt;/li&gt;&lt;/ul&gt;&lt;li class="MsoNormal" style="mso-list: l23 level2 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;If T is a type       variable, then this algorithm is applied recursively, using the upper       bound of T in place of T. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l23 level2 lfo16; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;If T is an array       type, then S must be the class Object, or a compile-time error occurs. &lt;/li&gt;&lt;/ul&gt;&lt;/ul&gt;The idea is that even if the class does not implement the interface, but one of its subclass maght. If the actually object class does not implement the interface then you will get a &lt;em&gt;ClassCastException&lt;/em&gt; error at runtime.&lt;br /&gt;For example,&lt;br /&gt;&lt;pre&gt;interface MyInterface{}&lt;br /&gt;&lt;br /&gt;class MyObject{}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String arg[]){&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Program po = new Program();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyInterface it = (MyInterface)po; //compile fine&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyObject obj = (MyObject)po; //compile time error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Why not &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final&lt;/span&gt;&lt;/code&gt; class? The compiler knows at compile time exactly what interfaces are implemented by the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final class&lt;/span&gt;&lt;/code&gt;. If the compiler can determine at compile time that the final class can never be instanceof the interface, that's a compile time error.&lt;br /&gt;&lt;h1&gt;Can an object access a private member of another object of the same class?&lt;/h1&gt;Yes, an object can access private instant members of other objects in the same class. Let's re-exam an example from "&lt;a href="http://www.xyzws.com/javafaq/why-always-override-hashcode-if-overriding-equals/20"&gt;Why always override hashcode() if overriding equals()?&lt;/a&gt;" in XyzWs Java FAQ.&lt;br /&gt;&lt;pre&gt;public class CustomerID {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private long crmID;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private int nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public CustomerID(long crmID, int nameSpace) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;super();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.crmID = crmID;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.nameSpace = nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public boolean equals(Object obj) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//null instanceof Object will always return false&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (!(obj instanceof CustomerID))&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return false;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (obj == this)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return true;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;this.crmID == ((CustomerID) obj).crmID &amp;amp;&amp;amp;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;this.nameSpace == ((CustomerID) obj).nameSpace;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Map m = new HashMap();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;m.put(new CustomerID(2345891234L,0),"Jeff Smith");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(m.get(new CustomerID(2345891234L,0)));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;In the above example code, you can see that &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;crmID&lt;/span&gt;&lt;/em&gt; and &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;nameSpace&lt;/span&gt;&lt;/em&gt; are private fields.&lt;br /&gt;&lt;h1&gt;ArrayList vs. LinkedList -- Which one is better?&lt;/h1&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt; implements the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RandomAccess&lt;/span&gt;&lt;/code&gt; interface, and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; does not. Note that &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Collections.binarySearch&lt;/span&gt;&lt;/code&gt; does take      advantage of the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;RandomAccess&lt;/span&gt;&lt;/code&gt;      property, to optimize searches. A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt;      does not support efficient random access &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt; is much faster than a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; for random access, that      is, when accessing arbitrary list elements using the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;get&lt;/span&gt;&lt;/code&gt; method. The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;get&lt;/span&gt;&lt;/code&gt; method is implemented for &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedLists&lt;/span&gt;&lt;/code&gt;, but it requires a      sequential scan from the front or back of the list. This scan is very      slow.&amp;nbsp;(see &lt;a href="http://www.xyzws.com/faq.do?cat=faq&amp;amp;article=19" target="_blank"&gt;What is the advantage of using an Iterator compared to the      get(index) method?&lt;/a&gt;) &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;An &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt; is much faster than &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; doing a binary search on      the large list of sorted element. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; are more efficient speed      wise than &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt;      when inserting and removing at random places in the list multiple times. If      you're just adding to the end of the list, an &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt; is what you want. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; is faster than an &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ArrayList&lt;/span&gt;&lt;/code&gt; when elements are only      added to the beginning of the list. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; has a simple growth      pattern of just adding and removing nodes when it needs to, but the      ArrayList has a growth algorithm of (n*3)/2+1, meaning that each time the      buffer is too small it will create a new one of size (n*3)/2+1 where n is      the number of elements of the current buffer and there will be a      significant amount of space wasted at the end. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l21 level1 lfo17; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The reversing a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;LinkedList&lt;/span&gt;&lt;/code&gt; using &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Collections.reverse&lt;/span&gt;&lt;/code&gt;. The internal      algorithm does this, and gets reasonable performance, by using forward and      backward iterators. &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;What are the differences between synchronized method and synchronized block (statement)?&lt;/h1&gt;The synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared resource while another thread is in the process of using or updating that resource.&lt;br /&gt;There two-synchronization syntax in Java Language. The practical differences are in controlling scope and the monitor. With a synchronized method, the lock is obtained for the duration of the entire method. With synchronized blocks you can specify exactly when the lock is needed.&lt;br /&gt;Basically, synchronized blocks are more general, and synchronized methods can be rewritten to use synchronized blocks:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public &lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/strong&gt; void f() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;.........&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;is equivalent to&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public void f() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized(this){&lt;/span&gt;&lt;/strong&gt;&lt;b&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;/span&gt;&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/span&gt;&lt;/strong&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;For example, You have a method with some parts that need synchronized and others don't. The synchronized block lets you synchronize only the partial line codes that really need it.&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private static Object locker1 = new Object();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;private static Object locker1 = new Object();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public void doSomething1() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;synchronized(locker1) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;......... //do something protected;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public void doSomething2() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;synchronized(locker2) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;......... //do something protected;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The synchronized block can only be executed after a thread has acquired the lock for the object or class referenced, for example the "locker1" or "locker2" in above code, in the synchronized statement.&lt;br /&gt;The above code shows that synchronized block can be holding different object monitors. Maybe it's necessary to protect &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;doSomething1()&lt;/span&gt;&lt;/code&gt; method and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;doSomething2()&lt;/span&gt;&lt;/code&gt; method from multiple threads, but it's fine if one thread is in the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;doSomething1()&lt;/span&gt;&lt;/code&gt; method and another is in the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;doSomething2()&lt;/span&gt;&lt;/code&gt; method. But the synchronized method can not do it.&lt;br /&gt;A synchronized method synchronizes on the object instance or the class. A thread only executes a synchronized method after it has acquired the lock for the method's object or class.&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l27 level1 lfo18; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;static&lt;/span&gt;&lt;/em&gt; synchronized methods synchronize on the      class object. If one thread is executing a static synchronized method, all      other threads trying to execute any static synchronized methods, in the      same class, will block. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l27 level1 lfo18; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;non-static&lt;/span&gt;&lt;/em&gt; synchronized methods synchronize on      "this" (the instance object). If one thread is executing a      synchronized method, all other threads trying to execute any synchronized      methods, in the same class, will block.&lt;/li&gt;&lt;/ul&gt;These are very public monitors, meaning some other thread could synchronize on them for the wrong reason, leading to slowdowns or deadlocks.&lt;br /&gt;&lt;h1&gt;What can you catch when try block does not throw exception?&lt;/h1&gt;Let's take a look at the following code:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;method();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch (FileNotFoundException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static void method() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The above code will generate a compile-time error: "Unreachable catch block ....".&lt;br /&gt;But the following code does not generate a compile time error:&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;method();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch (Exception e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static void method() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The Java compiler does not require you to catch an unchecked exception. If you do catch an unchecked exception even the code does not throwing it, the compiler will not generate a compile-time error. The unchecked exceptions have the benefit of not forcing the client code to explicitly deal with them.&lt;br /&gt;The &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;RuntimeException&lt;/span&gt;&lt;/em&gt; and its subclasses are unchecked exceptions. &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Error&lt;/span&gt;&lt;/em&gt; and its subclasses are also unchecked exceptions. The &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;RuntimeException&lt;/span&gt;&lt;/em&gt; is the subclass of &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Exception&lt;/span&gt;&lt;/em&gt;, Therefore, catching the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Exception&lt;/span&gt;&lt;/em&gt; exception without throwing any exceptions in try block will not generate comiple-time error.&lt;br /&gt;Summary for try-catch without throw exception:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l4 level1 lfo19; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Catching &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Throwable&lt;/span&gt;&lt;/em&gt;, &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Error (including its      subclasses)&lt;/span&gt;&lt;/em&gt;, &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Exception&lt;/span&gt;&lt;/em&gt;,      and &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;RuntimeException      (including its subclasses)&lt;/span&gt;&lt;/em&gt; always compile.&lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l4 level1 lfo19; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Catching directly subclasses      of &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Throwable&lt;/span&gt;&lt;/em&gt;      except for &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Exception&lt;/span&gt;&lt;/em&gt;      and &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Error&lt;/span&gt;&lt;/em&gt;      will generate a compile time error. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l4 level1 lfo19; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Catching subclasses of &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;Exception&lt;/span&gt;&lt;/em&gt; except      for &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;RuntimeException&lt;/span&gt;&lt;/em&gt;      will generate a compile time error&lt;/li&gt;&lt;/ul&gt;&lt;br /&gt;&lt;h1&gt;Is a class subclass of itself?&lt;/h1&gt;No. A class is not a subclass of itself.&lt;br /&gt;&lt;h1&gt;What are the differences between System.gc() and Runtime.gc()?&lt;/h1&gt;There is no difference between these two methods.&lt;br /&gt;Both methods suggest that the Java Virtual Machine expend effort toward recycling unused objects in order to make the memory they currently occupy available for quick reuse. When control returns from the method call, the Java Virtual Machine has made a best effort to reclaim space from all discarded objects.&lt;br /&gt;The System.gc() is a static method so it's a little bit more convenient to use. The call System.gc() is effectively equivalent to the call:&lt;br /&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Runtime.getRuntime().gc()&lt;/pre&gt;&lt;h1&gt;Why it is not required to declare the exceptions declared in the superclass' method when overriding it?&lt;/h1&gt;An implementation of an overriding method in a subclass may be totally different with its overridden method in the superclass. It may not have any exceptions needed to throw at all. The only restriction in Java compiler (&lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.6" target="_blank"&gt;8.4.6 Method Throws&lt;/a&gt;)is that the overriding method in subclass cannot be declared to throw border or new checked exceptions than the overridden method in the superclass (otherwise, you may break other codes because the runtime polymorphism).&lt;br /&gt;&lt;br /&gt;&lt;h1&gt;When should I use volatile modifier?&lt;/h1&gt;A variable that might be concurrently modified by multiple threads (without locks or a similar form of mutual exclusion) should be declared volatile.&lt;br /&gt;The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; modifier can be used to inform the compiler that it should not attempt to perform optimizations on the field, which could cause unpredictable results when the field is accessed by multiple threads. For example, A multiprocessor system(&amp;gt;2 CPUs) with 2 threads sharing a single variable in memory.&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l24 level1 lfo20; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Thread 1 reads the variable      and caches it in one of the registers for efficient access. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l24 level1 lfo20; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Thread 2 reads the memory      and changes its value. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l24 level1 lfo20; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Now if Thread 1 wants to      access the value, it may get it from its cache and thus end up with a      wrong value. &lt;/li&gt;&lt;/ul&gt;To avoid these scenarios a variable can be declared as &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt;, thus informing the JVM that the value can change asynchronously and has to be fetched always from memory or in other words it is like saying to JVM "Please do not optimize this variable for any reason".&lt;br /&gt;&lt;h3&gt;synchronized vs. volatile&lt;/h3&gt;It is important to understand that atomic operations do not automatically mean thread-safe operations. In addition, whenever multiple threads share variables, it is important that they are accessed in a synchronized method or block, or are declared with the volatile keyword. This ensures that the variables are properly reconciled with main memory, thereby guaranteeing correct values at all times.&lt;br /&gt;Whether you use &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; or &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;synchronized&lt;/span&gt;&lt;/code&gt; depends on several factors. If concurrency is important and you are not updating many variables, consider using volatile. If you are updating many variables, however, using &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; might be slower than using synchronization. Remember that when variables are declared &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt;, they are reconciled with main memory on every access. By contrast, when &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;synchronized&lt;/span&gt;&lt;/code&gt; is used, the variables are reconciled with main memory only when the lock is obtained and when the lock is released.&lt;br /&gt;Consider using &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;synchronized&lt;/span&gt;&lt;/code&gt; if you are updating many variables and do not want the cost of reconciling each of them with main memory on every access, or you want to eliminate concurrency for another reason.&lt;br /&gt;The following summarizes the differences between the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;synchronized&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; keywords.&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l19 level1 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/strong&gt;      &lt;/li&gt;&lt;ul type="circle"&gt;&lt;li class="MsoNormal" style="mso-list: l19 level2 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;Pro: Private working       memory is reconciled with main memory when the lock is obtained and when       the lock is released. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l19 level2 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;Con: Eliminates       concurrency. &lt;/li&gt;&lt;/ul&gt;&lt;li class="MsoNormal" style="mso-list: l19 level1 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;&lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;volatile&lt;/span&gt;&lt;/strong&gt; &lt;/li&gt;&lt;ul type="circle"&gt;&lt;li class="MsoNormal" style="mso-list: l19 level2 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;Pro: Allows       concurrency. The visibility of the changes made by a thread to the &lt;strong&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;content&lt;/span&gt;&lt;/strong&gt; of       the variable respect others threads &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l19 level2 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list 1.0in;"&gt;Con: Private working       memory is reconciled with main memory on each variable access. &lt;/li&gt;&lt;/ul&gt;&lt;li class="MsoNormal" style="mso-list: l19 level1 lfo21; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The volatile has nothing to      do with preventing simultaneous execution of a portion of code but      synchronized does. &lt;/li&gt;&lt;/ul&gt;&lt;h3&gt;Why can not a variable be &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final&lt;/span&gt;&lt;/code&gt;?&lt;/h3&gt;A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; variable means it could be changed by several threads so a thread should always reconcile the value with what is saved in memory rather than assume that the variable it finds is valid. If a variable is &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final&lt;/span&gt;&lt;/code&gt; it can't be changed so there is no reason to need to reconcile values. Thus &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;volatile&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;final&lt;/span&gt;&lt;/code&gt; are contradictory in a way. It doesn't make sense to be able to combine them thus java does not allow it.&lt;br /&gt;&lt;h1&gt;How differently does Java handle checked and unchecked exceptions?&lt;/h1&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l16 level1 lfo22; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;If a method may throw checked      exceptions, the calling code must handle the exception by either catching      it or by declaring in the signature of the method (as throws). Unchecked      exceptions do not have to be handled by the calling code. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l16 level1 lfo22; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;If a method might throw a      checked exception, it must be declared in the signature of the method.      Unchecked exceptions do not have to be listed in the method signature. &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;How many ways can I access static members in a class?&lt;/h1&gt;A static member (static method or static variable) can be accessed by using the dot operator on the class name (e.g., Math.max(..);). This is a &lt;strong&gt;standard and right&lt;/strong&gt; way to access a static member in a class.&lt;br /&gt;But Java language also allows you to use an object reference to access a static member. This is &lt;strong&gt;bad&lt;/strong&gt; or at least poor form because it creates the impression that some instance variables in the object are used, but this isn't the case. For example,&lt;br /&gt;&lt;pre&gt;//Access static member from within the same class&lt;br /&gt;&lt;br /&gt;class MyObject1 {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;static int staticVariable = 10;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;void m() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyObject1 o = null;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i1 = staticVariable;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i2 = this.staticVariable;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i3 = MyObject1.staticVariable;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i4 = o.staticVariable;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;//Access static member from outside the class&lt;br /&gt;&lt;br /&gt;class MyObject2 {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;void m() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyObject1 o = new MyObject1();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i5 = MyObject1.staticVariable;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i6 = o.staticVariable;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;When we access a static member on an object reference, what the value of the object reference is (point to an instance or null). Only the type of the object reference matters. That is why &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;int i4 = o.staticVariable;&lt;/span&gt;&lt;/code&gt; will not throw a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;NullPointerException&lt;/span&gt;&lt;/code&gt;.&lt;br /&gt;It's best practice to always invoke static member using the class name rather then an object reference of the class.&lt;br /&gt;&lt;h1&gt;When to use abstract classes?&lt;/h1&gt;&lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Abstract&lt;/span&gt;&lt;/code&gt; classes in Java are just like regular Java classes but you cannot instantiate them directly. They may not completely implement all methods defined in classes. This incomplete implementation may be different in different context. Derived class implements the abstract class in its context.&lt;br /&gt;When you have a common implementation, &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;abstract&lt;/span&gt;&lt;/code&gt; classes shine. Using &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;abstract&lt;/span&gt;&lt;/code&gt; classes you can enforce an implementation hierarchy and avoid duplicate code. Using &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;abstract&lt;/span&gt;&lt;/code&gt; classes, however, should not affect your decision to use &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;interface&lt;/span&gt;&lt;/code&gt;s to define your behavior.&lt;br /&gt;An &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;abstract&lt;/span&gt;&lt;/code&gt; class allows to add new methods without breaking compatibility. The derived class can just provide working implementation of a subset of the methods defined in the abstract class. While if you add new methods to an interface, you'd need to update all the classes that implements the interface.&lt;br /&gt;&lt;h1&gt;&lt;a href="" name="amit"&gt;&lt;/a&gt;Why Interface is useful?&lt;/h1&gt;In Java, an interface defines new a type including a set of unimplement operation on it. An interface defines a protocol of behavior that can be implemented by any classes.&lt;br /&gt;A non-abstract class that &lt;em&gt;implements&lt;/em&gt; the interface must implement all the methods defined in the interface. The interface presents what its methods are and leave how to do them to the implementation class. A same method in the interface can have totally different implementation in different classes. The interface provides a clearner separation of behavior and implementation.&lt;br /&gt;Because the class must implement all the methods defined in the interface, any changes on the method of the interface will affect to all the classes that implements the interface.&lt;br /&gt;&lt;h1&gt;Is PriorityQueue an ordered and sorted collection?&lt;/h1&gt;&lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; is certainly ordered, but is it sorted? Yes it is. However it's not of the type for the sorted collections, such as &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;SortedSet&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;SortedMap&lt;/span&gt;&lt;/code&gt;.&lt;br /&gt;&lt;br /&gt;Ordered and sorted are not the same thing. All sorted collections are also ordered, but not all ordered collections are sorted. For example, A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;List&lt;/span&gt;&lt;/code&gt; is always ordered - its elements have definite positions - but it isn't sorted unless you sort it somehow (e.g. with Collections. sort ()).&lt;br /&gt;&lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; orders elements according to an order specified at construction time, which is specified either according to their natural order (see Comparable), or according to a Comparator, depending on which constructor is used. A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; does not permit null elements. A &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; relying on natural ordering also does not permit insertion of non-comparable objects (doing so may result in ClassCastException). When you do not create the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; with a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Comparator&lt;/span&gt;&lt;/code&gt;, the elements that you add to the queue must be mutually Comparable (e.g., This means that your element class need to to implement Comparable interface).&lt;br /&gt;The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; class and its &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Iterator&lt;/span&gt;&lt;/code&gt; implement all of the optional methods of the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Collection&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Iterator&lt;/span&gt;&lt;/code&gt; interfaces. The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Iterator&lt;/span&gt;&lt;/code&gt; provided in method iterator() is not guaranteed to traverse the elements of the PriorityQueue in any particular order. If you need ordered traversal, consider using Arrays.sort(pq.toArray()).&lt;br /&gt;Only the order as seen by the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Queue&lt;/span&gt;&lt;/code&gt; methods (element(), peek(), poll(), remove()) is guaranteed to reflect the sorted order. The head of this queue is the least element with respect to the specified ordering. If multiple elements are tied for least value, the head is one of those elements -- ties are broken arbitrarily.&lt;br /&gt;Therefore, &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;PriorityQueue&lt;/span&gt;&lt;/code&gt; is a bit outside Java's "standard" sorted collections as defined by SortedSet and SortedMap. For this reason, some people would say that PriorityQueue is ordered but not sorted.&lt;br /&gt;&lt;h1&gt;How does the result differ between the String and number operands of the '+' operator?&lt;/h1&gt;Let's take a look at the following example,&lt;br /&gt;&lt;pre&gt;class Program {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(1 + 2 + " fiddlers");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("fiddlers " + 1 + 2);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output is:&lt;br /&gt;&lt;pre&gt;3 fiddlers&lt;br /&gt;&lt;br /&gt;fiddlers 12&lt;/pre&gt;The + operator is syntactically left-associative, the expression &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;a + b + c&lt;/span&gt;&lt;/code&gt; is always regarded as &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;(a + b) + c&lt;/span&gt;&lt;/code&gt;. The type analysis to represent string concatenation or addition is based on the evaluation order.&lt;br /&gt;In the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;1 + 2 + " fiddlers"&lt;/span&gt;&lt;/code&gt;, the first two are int, so the result is 3. Then since the next operand is a String object, 3 is converted to String and "fiddlers" is concatenated to 3, so the result is "3 fidders".&lt;br /&gt;In the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;"fiddlers " + 1 + 2&lt;/span&gt;&lt;/code&gt;, since the first operand is a String, the rest of the operands are converted to String then concatenated to it, so the result is "fiddlers 12".&lt;br /&gt;&lt;h1&gt;When initialization occurs in an interface?&lt;/h1&gt;According to Java language specification, initialization of an interface consists of executing the initializers for fields declared in the interface. Before a class is initialized, its direct superclass must be initialized, but interfaces implemented by the class need not be initialized. Similarly, the superinterfaces of an interface need not be initialized before the interface is initialized. Initialization of an interface does not, of itself, cause initialization of any of its superinterfaces.&lt;br /&gt;Here is an example listed in JLS. More detail information and example can find in &lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/execution.html#12.4" target="_blank"&gt;12.4 Initialization of Classes and Interfaces&lt;/a&gt;.&lt;br /&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;interface I {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int i = 1, ii = Test.out("ii", 2);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;interface J extends I {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int j = Test.out("j", 3), jj = Test.out("jj", 4);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;interface K extends J {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int k = Test.out("k", 5);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Test {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(J.i);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(K.j);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;static int out(String s, int i) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(s + "=" + i);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return i;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;produces the output:&lt;/div&gt;&lt;pre style="margin-bottom: .0001pt; margin-bottom: 0in; margin-left: .5in; margin-right: .5in; margin-top: 0in;"&gt;1&lt;br /&gt;&lt;br /&gt;j=3&lt;br /&gt;&lt;br /&gt;jj=4&lt;br /&gt;&lt;br /&gt;3&lt;/pre&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;The reference to J.i is to a field that is a compile-time constant; therefore, it does not cause I to be initialized. The reference to K.j is a reference to a field actually declared in interface J that is not a compile-time constant; this causes initialization of the fields of interface J, but not those of its superinterface I, nor those of interface K. Despite the fact that the name K is used to refer to field j of interface J, interface K is not initialized.&lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l11 level1 lfo23; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;System.out.println(J.i); // The i variable is inherited from I but since it is a constant - you don't have to go through the whole process of initializing I, just reference it and get on with things. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l11 level1 lfo23; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;System.out.println(K.j); // now this is more complicated. Since the variable holds something other than a constant, the first thing that we have to do is initialize J so that we can figure out what to DO to come up with the value of j. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l11 level1 lfo23; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;Initializing interface J causes variable j to be initialized first: executing Test.out("j",3) which prints j=3, and setting the variable j to 3 (note the return type on the method). Now jj is initialized executing Test.out("jj",4) which prints jj=4 and sets the variable jj to 4. &lt;/div&gt;&lt;div class="MsoNormal" style="margin-left: 1.0in; margin-right: .5in; mso-list: l11 level1 lfo23; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in; text-indent: -.25in;"&gt;&lt;!--[if !supportLists]--&gt;&lt;span style="font-family: Symbol; font-size: 10.0pt; mso-bidi-font-family: Symbol; mso-bidi-font-size: 12.0pt; mso-fareast-font-family: Symbol;"&gt;&lt;span style="mso-list: Ignore;"&gt;·&lt;span style="font: 7.0pt &amp;quot;Times New Roman&amp;quot;;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;!--[endif]--&gt;Now that initialization is done we can evaluate and execute the println for K.j which prints the current value of j which is 3. &lt;/div&gt;&lt;h1&gt;Why down casting throws ClassCastException?&lt;/h1&gt;You cannot assign a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;superclass&lt;/span&gt;&lt;/code&gt; reference variable to a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;subclass&lt;/span&gt;&lt;/code&gt; reference without a cast of the subclass type. Examples can find in &lt;a href="http://www.xyzws.com/javafaq/when-is-an-explicit-object-reference-casting-is-required/35"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;When is an explicit object reference casting is required?&lt;/span&gt;&lt;/a&gt;. The compiler is happy when you explict cast the superclass reference to subclass reference, but the compiler does not care what the actual object holded by the reference. Does it actually have a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;superclass&lt;/span&gt;&lt;/code&gt; object, or just a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;superclass reference&lt;/span&gt;&lt;/code&gt; holding a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;subclass&lt;/span&gt;&lt;/code&gt; object? No answer from compile time but it has to answer this quesiton.&lt;br /&gt;You can not just take a parent object and suddenly turn it into a child though. The parent object is not an instance of the subclass. If the actual object holded by the reference is a superclass object, casting it to a subclass reference result in a compile time error.&lt;br /&gt;&lt;pre&gt;class SuperClass {&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;class SubClass extends SuperClass {&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;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// case 1: actual SuperClass object&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SuperClass p1 = new SuperClass(); &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// case 2: SubClass object is referred by a SuperClass reference &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SuperClass p2 = new SubClass();&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SubClass s1 = (SubClass)p1; //run time error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;SubClass s2 = (SubClass)p2; //OK&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;h1&gt;Why wait(), notify(), notifyAll() must be called inside a synchronized method/block?&lt;/h1&gt;In Java, any object can act as a &lt;strong&gt;monitor&lt;/strong&gt; - that's an entity with a &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;single lock&lt;/span&gt;&lt;/em&gt;, an &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;entry queue&lt;/span&gt;&lt;/em&gt;, and a &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;waiting queue&lt;/span&gt;&lt;/em&gt;. An object's&amp;nbsp;method without qualifed by the keyword &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; &amp;nbsp;can be&amp;nbsp;invoked by&amp;nbsp;any number of &amp;nbsp;threads at any time, the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt; is ignored. The &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; method of an object, &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;one and only one&lt;/span&gt;&lt;/em&gt; thread, who owns the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt; of that object, can be permitted to run that method at any time;i.e. a &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; method is &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;mutually exclusive&lt;/span&gt;&lt;/em&gt; . If, at the time of invocation, another thread owns the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt;, then the calling thread will be put in the Blocked state and is added to the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;entry queue&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;The &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt;, &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt;, and &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll()&lt;/span&gt;&lt;/em&gt; methods should be called for an object only when the current thread has already locked the object's &lt;strong&gt;lock&lt;/strong&gt;.&amp;nbsp;This point sometimes goes unnoticed because programmers are used to calling these methods from within &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; methods or blocks. Otherwise, you will get "java.lang.IllegalMonitorStateException: current thread not owner" at runtime.&lt;br /&gt;When a thread running in a &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; method of an object is calling the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt; method of the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;same&lt;/span&gt;&lt;/em&gt; object, that thread releases the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt; of the object and&amp;nbsp;is added to that object's &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;waiting queue&lt;/span&gt;&lt;/em&gt;. As long as it's there, it sits idle. Note also that &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt; forces the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;thread&lt;/span&gt;&lt;/em&gt; to release its &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt;. This means that it &lt;strong&gt;must own&lt;/strong&gt; the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt; of an object before calling the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt; method of that (same) object. Hence the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;thread&lt;/span&gt;&lt;/em&gt; must be in one of the object's &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; methods or &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized block&lt;/span&gt;&lt;/em&gt; before calling &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;When a thread invokes an object's &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt; or &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll()&lt;/span&gt;&lt;/em&gt; method, one (an arbitrary thread) or all of the threads in its &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;waiting queue&lt;/span&gt;&lt;/em&gt; are removed from the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;waiting queue&lt;/span&gt;&lt;/em&gt; to the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;entry queue&lt;/span&gt;&lt;/em&gt;. They then actively contend for the object's &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt;, and the one that gets the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt; goes on to execute. If no threads are waiting in the waiting queue, then &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt; and &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll()&lt;/span&gt;&lt;/em&gt; have no effect. Before calling the&amp;nbsp;&lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt; or &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll()&lt;/span&gt;&lt;/em&gt; method of an object, a thread &lt;strong&gt;must own&lt;/strong&gt; the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;lock&lt;/span&gt;&lt;/em&gt; of the object. Hence it must be in one of the object's&amp;nbsp; &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; methods or &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized block&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;A thread in the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;waiting queue&lt;/span&gt;&lt;/em&gt; of an object can run again only when some other thread calls the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt; (or the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll&lt;/span&gt;&lt;/em&gt;) method of the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;same&lt;/span&gt;&lt;/em&gt; object.&lt;br /&gt;The reason to call &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt; is that the thread does not want to execute a block of code until a particular state to be achieved. It wants to wait until a particular state to be achieved. The reason to call &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt; or &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll()&lt;/span&gt;&lt;/em&gt; method is that the thread will signal others that "&lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;a particular state has been achieved&lt;/span&gt;&lt;/em&gt;". The &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;state&lt;/span&gt;&lt;/em&gt; is a communication channel between threads and it must be shared &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;mutable state&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;For example, one thread read data from a buffer and one thread write data into buffer. The reading data thread needs to wait until the writing data thread completly write a block data into the buffer. The wirting data thread needs to wait until the reading data thread completly read the data from the buffer.&amp;nbsp;&lt;strong&gt;If&amp;nbsp;&lt;/strong&gt;&lt;em&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;, &lt;/strong&gt;&lt;em&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/b&gt;&lt;/em&gt;&lt;strong&gt;, and &lt;/strong&gt;&lt;em&gt;&lt;b&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notifyAll()&lt;/span&gt;&lt;/b&gt;&lt;/em&gt;&lt;strong&gt; methods can&amp;nbsp;be called by a ordinary method&lt;/strong&gt; ,&amp;nbsp;the reading thread calls &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;wait()&lt;/span&gt;&lt;/em&gt; and the thread is being added to &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;waiting queue&lt;/span&gt;&lt;/em&gt; . At just the same moment, the writing thread calls &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;notify()&lt;/span&gt;&lt;/em&gt; to signal the condition changes. The reading thread misses the change and waits forever. Hence, they must be called inside a&amp;nbsp;&lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; method or block which is &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;mutually exclusive&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;&lt;h1&gt;What is the difference between parseXxx() and valueOf()?&lt;/h1&gt;The parseXxx() methods and the valueOf() methods are defined in most of the numeric primitive wrapper classes, such as Integer, Long, Double, FLoat, etc.&lt;br /&gt;Both methods take a String argument and convert it into the corresponding primitive type with the value that the String represents. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value. If the String argument is not properly formed, both throws a NumberFormatException.&lt;br /&gt;The biggest difference between these two methods is that:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l0 level1 lfo24; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;parseXxx() returns the      primitive type; &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l0 level1 lfo24; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;valueOf() returns a wrapper      object reference of the type. &lt;/li&gt;&lt;/ul&gt;Let's take a look at the following examples:&lt;br /&gt;int i = Integer.parseInt("100"); // the result is 100&lt;br /&gt;Integer i = Integer.valueOf("100"); // create a Integer object with value 100&lt;br /&gt;double d = Double.parseDouble("12.34");&lt;br /&gt;Double d = Double.valueOf("12.34");&lt;br /&gt;Both methods has a overloaded counter part, which takes a radix as the second argument. The methods that doesn't takes the radix as the second argument, uses 10 as the radix. For example:&lt;br /&gt;long l = Long.parseLong("10101010", 2); // binary String to long, the value is 170&lt;br /&gt;Long l = Long.valueOf("10101010", 2); // created a Long object with value 170&lt;br /&gt;&lt;h1&gt;What Should I Know about AutoBoxing and UnBoxing in Java 5.0?&lt;/h1&gt;Within Java 5.0, wrapper classes have become easier to use. Java 5.0 introduced automatic conversion between a primitive type and the corresponding wrapper class.&lt;br /&gt;From primitive type to it corresponse wrapper class is called &lt;em&gt;autoboxing&lt;/em&gt;, the reverse process is called &lt;em&gt;unboxing&lt;/em&gt;. &lt;em&gt;Autoboxing&lt;/em&gt; and &lt;em&gt;unboxing&lt;/em&gt; also apply to methods calls. For example, you can pass an argument of type &lt;em&gt;int&lt;/em&gt; to a method that has a formal parameter of type &lt;em&gt;Integer&lt;/em&gt;.&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l22 level1 lfo25; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A &lt;em&gt;NullpointerException&lt;/em&gt;      exception occurs when unboxing a &lt;em&gt;null&lt;/em&gt; wrapper class's reference      to its primitive type. For example, the code will compile but it will      throw a &lt;em&gt;NullpointerException&lt;/em&gt; at runtime. &lt;/li&gt;&lt;/ul&gt;&lt;pre style="margin-left: .5in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;Long L = null;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;long l = L;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;/pre&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l22 level1 lfo25; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Boxing conversion converts      values of primitive type to corresponding values of reference type. But      the primitive types can not be widened/Narrowed to the Wrapper classes and      vice versa. For example, &lt;/li&gt;&lt;/ul&gt;&lt;pre style="margin-bottom: 12.0pt; margin-left: .5in; margin-right: 0in; margin-top: 0in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;byte&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;b&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;= 43;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Integer I1 = 23;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//Constant integer value&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Integer I2 = (int)b;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;//Cast to int type&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Long&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;L1 = 23;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//compile error because 23 is integer value&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Long&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;L2 = (Long)23; //can not cast integer value to Long wrapper class&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Long&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;L3 = 23L;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Long&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;L4 = (long)23;&lt;/pre&gt;&lt;div style="margin-left: .5in;"&gt;This restriction is also applied to method invocation:&lt;/div&gt;&lt;pre style="margin-left: .5in;"&gt;public class MyClass&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(Long i) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Here");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyClass s = new MyClasslass();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//s.method(12);&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;// error&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.method(12L);&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;// ok&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l22 level1 lfo25; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;When invoking a method from      multiple overloading methods, For the matching method process, the Java      compiler will perferance the order of primitive types (Widening Primitive      Conversion), wrapper class (Boxing Conversion), and var-args. For example,      &lt;/li&gt;&lt;/ul&gt;&lt;pre style="margin-left: .5in;"&gt;public class MyClass&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;public void method(Long x, Long y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(Long x, Long y)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(long x, long y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(long x, long y)"); &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(long... x) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(long... x)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;long x, y;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x = y = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyClass s = new MyClass();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.method(x, y);&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="margin-left: .5in;"&gt;The result is "&lt;em&gt;method(long x, long y)&lt;/em&gt;". The Java compiler will check for the matching primitive types, then it will search for the Wrapper types.&lt;/div&gt;&lt;pre style="margin-left: .5in;"&gt;public class MyClass&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(Long x, Long y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(Long x, Long y)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(int x, int y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(int x, int y)"); &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(long... x) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(long... x)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;long x, y;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x = y = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyClass s = new MyClass();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;s.method(x, y);&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="margin-left: .5in;"&gt;The result is "&lt;em&gt;method(Long x, Long y)&lt;/em&gt;". The Java compiler gives preferance to the matching Wrapper class method signature other than the primitive varargs method.&lt;/div&gt;&lt;pre style="margin-left: .5in;"&gt;public class MyClass&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(Double x, Double y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(Double x, (Double y)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(int x, int y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(int x, int y)"); &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(long... x) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(long... x)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;long x, y;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x = y = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyClass s = new MyClass();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.method(x, y);&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="margin-left: .5in;"&gt;The result is "&lt;em&gt;method(long ...x)&lt;/em&gt;". The compiler will not narrow down "long" primitive value to "int"; Also, it can not winden long to Double class. Only the var-args method can be used.&lt;/div&gt;&lt;pre style="margin-left: .5in;"&gt;public class MyClass&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void method(Long x, Long y) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("method(Long x, Long y)");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;int x, y;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x = y = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyClass s = new MyClass();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;s.method(x, y);&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="margin-left: .5in;"&gt;The arguments can not winden to "long" and then box to "Long". You will get compile error.&lt;/div&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l22 level1 lfo25; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;In order to save memory,      two instances of the following wrapper objects will always be == when      their primitive values are the same. Please read &lt;a href="http://www.xyzws.com/javafaq/why-does-the-autoboxing-conversion-sometimes-return-the-same-reference/34"&gt;Why      does the autoboxing conversion sometimes return the same reference?&lt;/a&gt; &lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;Can we synchronize static method/variable in a Java class?&lt;/h1&gt;Yes, you can do it.&lt;br /&gt;A synchronized method or block works on a given monitor. Synchronized non-static methods all synchronize on the Java &lt;em&gt;instance&lt;/em&gt; of a class. Each instance has a lock monitor. For the case of static methods, what object does static synchronized methods use for locking? The static synchronized statements obtain a lock on the &lt;em&gt;Class&lt;/em&gt; object. The following example show you few ways to access the class object:&lt;br /&gt;&lt;pre&gt;class MyClass {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//you can access class object through an instance of that class&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;MyClass objectinstance = new MyClass();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;java.lang.Class myclass1 = objectinstance.getClass();&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//non-instance ways&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;java.lang.Class myclass2 = Myclass.class;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;java.lang.Class myclass3 = Class.forName("MyClass");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The JVM creates a Class object when the class is loaded (When it is used for the first time). Once a class is loaded into a JVM, the same class will not be loaded again. The JVM creates one instance of &lt;em&gt;Class&lt;/em&gt; for each class that is loaded, The &lt;em&gt;Class instances&lt;/em&gt; are Objects and can be synchronized via static synchronized methods.&lt;br /&gt;For example&lt;br /&gt;&lt;pre&gt;class MyClass&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public synchronized static someMethod() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;It is the equivalent to the following static synchronized block:&lt;br /&gt;&lt;pre&gt;synchronized (MyClass.class) {&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;In some situatios, you need to protect access static variables, the locking an object instance does not automatically protect access to static variables, because there may have more than one instances and all of them use the same static variables. You have to obtain a lock on the Class vs an instance of the class. A static synchronized block can be use to protect access to static varibales, a lock on a static method has no effect on any instances of that class (see the Java Language Specification). For example,&lt;br /&gt;&lt;pre style="margin-bottom: 12.0pt;"&gt;class BumpTest {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;int count;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;static int classCount;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;void synchronized bump() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;count++;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;strong&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized (BumpTest.class)&lt;/span&gt;&lt;/strong&gt; { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//or synchronized (Class.forname("BumpTest"))&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 4;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;classCount++;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 3;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 2;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (ClassNotFoundException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 4;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Static variables, like static methods, are not inherited, but are accessible from within the body of the class definition and through an explicit reference to the defining class's name. Adding a new static synchronized method in a subclass cannot protect other threads to access static variables defined in its superclass nor should you use &lt;em&gt;synchronized(this.getClass())&lt;/em&gt; which locks the &lt;em&gt;actual Class&lt;/em&gt; might be the subclass. An explicit block synchronization with "none-instance way to get Class" is the preferred way.&lt;br /&gt;&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;strong&gt;&lt;a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.3.6"&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;8.4.3.6 synchronized Methods&lt;/span&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;A synchronized method acquires a monitor (??17.1) before it executes. For a class (static) method, the monitor associated with the Class object for the method's class is used. For an instance method, the monitor associated with this (the object for which the method was invoked) is used.&lt;/div&gt;&lt;h1&gt;How to use the synchronized keyword?&lt;/h1&gt;There are two syntactic forms based on the synchronized keyword, methods and blocks. A synchronized method acquires a lock before it executes. For example, the following synchronized method&lt;br /&gt;&lt;pre&gt;class MyClass {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public synchronized void function() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;is equivalent to the following synchronized block&lt;br /&gt;&lt;pre&gt;class MyClass {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void function() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;synchronized(this) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;Few notes about &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt; keyword:&lt;br /&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l18 level1 lfo26; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The constructors and      initializers cannot be synchronized and a compiler error will occur if you      put &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;synchronized&lt;/span&gt;&lt;/em&gt;      front of a constructor. But constructors and initializers of a class can contain      synchronized blocks. For example, &lt;/li&gt;&lt;/ul&gt;&lt;pre style="margin-left: .5in;"&gt;class MyClass {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;static int x = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;static int y = 0;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;synchronized (MyClass.class) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;x++;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public MyClass() {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;synchronized(MyClass.class) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;y++;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;ul type="disc"&gt;&lt;li class="MsoNormal" style="mso-list: l18 level1 lfo26; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;Although the synchronized      keyword can appear in a method header, the synchronized keyword is not      part of the method signature. Therefore, it is not automatically inherited      when subclasses override superclass methods. The synchronized methods can      be overrided&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;to be non-synchronous.      The synchronized instance methods in subclasses use the same locks as      their superclasses. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l18 level1 lfo26; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The interface's methods      cannot be declared synchronized. The Synchronization is part of the      implementation but not part of the interface. &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l18 level1 lfo26; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;The synchronization of an      Inner Class is independent on it's outer class. That means locks on      inner/outer objects are independent. Getting a lock on outer object      doesn??t mean getting the lock on an inner object as well, that lock      should be obtained separately &lt;/li&gt;&lt;li class="MsoNormal" style="mso-list: l18 level1 lfo26; mso-margin-bottom-alt: auto; mso-margin-top-alt: auto; tab-stops: list .5in;"&gt;A non-static inner class      method can lock it's containing class by using a synchronized block. The      inner class can reference the outer class with &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;OuterClass.this&lt;/span&gt;&lt;/em&gt;: &lt;/li&gt;&lt;/ul&gt;&lt;pre style="margin-left: .5in;"&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;synchronized(OuterClass.this) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;....&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/pre&gt;&lt;h1&gt;How to run external programs from java under window?&lt;/h1&gt;The &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Runtime&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Process&lt;/span&gt;&lt;/code&gt; classes are available for executing and communicating with external programs. With an instance of the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Runtime&lt;/span&gt;&lt;/code&gt; class, it can execute an external program and return an instance of a subclass of &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Process&lt;/span&gt;&lt;/code&gt;. The class Process provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.&lt;br /&gt;&lt;pre&gt;import java.io.*;&lt;br /&gt;&lt;br /&gt;import static java.lang.System.out;&lt;br /&gt;&lt;br /&gt;public class RumExternal&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;String fileList="cmd /k dir";&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Runtime rt = Runtime.getRuntime ();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Process process = rt.exec (fileList);&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;BufferedReader br = new BufferedReader (&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;new InputStreamReader(&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;process.getInputStream()&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;String line;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while ((line = br.readLine ()) != null)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;out.println (line);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;br.close();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch (IOException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;e.printStackTrace();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;Implement callback routines in Java&lt;/h1&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 12.0pt;"&gt;Developers conversant in the event-driven programming model of MS-Windows and the X Window System are accustomed to passing function pointers that are invoked (that is, "called back") when something happens. Java's object-oriented model does not currently support method pointers, and thus seems to preclude using this comfortable mechanism. But all is not lost! &lt;/div&gt;Java's support of &lt;em&gt;interfaces&lt;/em&gt; provides a mechanism by which we can get the equivalent of callbacks. The trick is to define a simple interface that declares the method we wish to be invoked. &lt;br /&gt;For example, suppose we want to be notified when an event happens. We can define an interface:&lt;br /&gt;&lt;pre&gt;public interface InterestingEvent&lt;/pre&gt;&lt;pre&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// This is just a regular method so it can return something or&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// take arguments if you like.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void interestingEvent ();&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;div class="MsoNormal" style="margin-bottom: 12.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;This gives us a grip on any objects of classes that &lt;em&gt;implement&lt;/em&gt; the interface. So, we need not concern ourselves with any other extraneous type information. This is &lt;em&gt;much&lt;/em&gt; nicer than hacking trampoline C functions that use the &lt;em&gt;data&lt;/em&gt; field of widgets to hold an object pointer when using C++ code with Motif. &lt;br /&gt;The class that will signal the event needs to expect objects that implement the &lt;strong&gt;InterestingEvent&lt;/strong&gt; interface and then invoke the &lt;strong&gt;interestingEvent()&lt;/strong&gt; method as appropriate. &lt;br /&gt;&lt;pre&gt;public class EventNotifier&lt;/pre&gt;&lt;pre&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private InterestingEvent ie;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private boolean somethingHappened; &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;public EventNotifier (InterestingEvent event)&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Save the event object for later use.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;ie = event; &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;// Nothing to report yet.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;somethingHappened = false;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//...&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;public void doWork ()&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Check the predicate, which is set elsewhere.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if (somethingHappened)&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Signal the even by invoking the interface's method.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;ie.interestingEvent ();&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;//...&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;// ...&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;div class="MsoNormal" style="margin-bottom: 12.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;In that example, I used the &lt;strong&gt;somethingHappened&lt;/strong&gt; predicate to track whether or not the event should be triggered. In many instances, the very fact that the method was called is enough to warrant signaling the &lt;strong&gt;interestingEvent()&lt;/strong&gt;. &lt;br /&gt;The code that wishes to receive the event notification must implement the &lt;strong&gt;InterestingEvent&lt;/strong&gt; interface and just pass a reference to itself to the event notifier. &lt;br /&gt;&lt;pre&gt;public class CallMe implements InterestingEvent&lt;/pre&gt;&lt;pre&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;private EventNotifier en; &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;public CallMe ()&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Create the event notifier and pass ourself to it.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;en = new EventNotifier (this);&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;// Define the actual handler for the event.&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public void interestingEvent ()&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;{&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Wow!&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;Something really interesting must have occurred!&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;// Do something...&lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} &lt;/pre&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;//...&lt;/pre&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;span style="mso-bidi-font-family: &amp;quot;Courier New&amp;quot;;"&gt;}&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;&lt;h1&gt;How to get default character set for the Java virtual machine?&lt;/h1&gt;Every instance of the Java virtual machine has a default charset, which may or may not be one of the standard charsets. The default charset is determined during virtual-machine startup and typically depends upon the locale and charset being used by the underlying operating system.&lt;br /&gt;In Java 5.0, the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.nio.charset&lt;/span&gt;&lt;/code&gt; class provides a static method, called the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;defaultCharset()&lt;/span&gt;&lt;/code&gt;, to retrieve the &lt;em&gt;&lt;span style="mso-fareast-font-family: &amp;quot;Courier New&amp;quot;;"&gt;default charset&lt;/span&gt;&lt;/em&gt;.&lt;br /&gt;&lt;pre&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;...&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Charset dfset = Charset.defaultCharset();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;out.println(dfset.name());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;...&lt;/pre&gt;In the previous version of Java (JDK 1.4), you can get it from the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;getEncoding()&lt;/span&gt;&lt;/code&gt; methods of the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;InputStreamReader&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;OutputStreamWriter&lt;/span&gt;&lt;/code&gt; classes. The method returns the name of the character encoding being used. If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.&lt;br /&gt;&lt;h1&gt;How Can I Catch All Possible Exceptions in Java?&lt;/h1&gt;All exceptions come from the "mother class" called &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Throwable&lt;/span&gt;&lt;/code&gt; and one of two subclasses called &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Error&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Exception&lt;/span&gt;&lt;/code&gt;. A block of code that is executed when an exception occurs is called an &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Exception handler&lt;/span&gt;&lt;/code&gt;. By catching &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Throwable&lt;/span&gt;&lt;/code&gt;, it is possible to handle all unexpected conditions.&lt;br /&gt;&lt;pre&gt;...&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch(Throwable e) {&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;...&lt;/pre&gt;There are some special exceptions that used by the JVM, those are the sub-classes of &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Error&lt;/span&gt;&lt;/code&gt;. We are not suppose the catch them in our real code and we usually catch &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;java.lang.Exception&lt;/span&gt;&lt;/code&gt; for all application and runtime exceptions.&lt;br /&gt;&lt;pre&gt;...&lt;br /&gt;&lt;br /&gt;try {&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;catch(Exception e) {&lt;br /&gt;&lt;br /&gt;...&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;...&lt;/pre&gt;&lt;h1&gt;How to Use StringTokenizer in Java?&lt;/h1&gt;The string tokenizer class allows an application to break a string into tokens. A token is returned by taking a substring of the string that was used to create the &lt;tt&gt;&lt;span style="font-size: 10.0pt;"&gt;StringTokenizer&lt;/span&gt;&lt;/tt&gt; object. There are three ways to construct a StringTokenizer.&lt;br /&gt;&lt;pre&gt;public class Program { &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Case 1.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;StringTokenizer st = new StringTokenizer("this is a test");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while (st.hasMoreTokens()) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(st.nextToken());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Case 2.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;st = new StringTokenizer("this,is a=test", " ,=");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while (st.hasMoreTokens()) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(st.nextToken());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Case 3.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;st = new StringTokenizer("this,is a=test", " ,=", true);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while (st.hasMoreTokens()) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(st.nextToken());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Case 4.");&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;st = new StringTokenizer("this,is a=test", " ,=", false);&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;while (st.hasMoreTokens()) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println(st.nextToken());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;The output is&lt;br /&gt;&lt;pre&gt;Case 1.&lt;br /&gt;&lt;br /&gt;this&lt;br /&gt;&lt;br /&gt;is&lt;br /&gt;&lt;br /&gt;a&lt;br /&gt;&lt;br /&gt;test&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Case 2.&lt;br /&gt;&lt;br /&gt;this&lt;br /&gt;&lt;br /&gt;is&lt;br /&gt;&lt;br /&gt;a&lt;br /&gt;&lt;br /&gt;test&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Case 3.&lt;br /&gt;&lt;br /&gt;this&lt;br /&gt;&lt;br /&gt;,&lt;br /&gt;&lt;br /&gt;is&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;a&lt;br /&gt;&lt;br /&gt;=&lt;br /&gt;&lt;br /&gt;test&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Case 4.&lt;br /&gt;&lt;br /&gt;this&lt;br /&gt;&lt;br /&gt;is&lt;br /&gt;&lt;br /&gt;a&lt;br /&gt;&lt;br /&gt;test&lt;/pre&gt;StringTokenizer is pretty straight forward. You can separate a String by any delimiters that you choose such as a blank space, or a comma. Once you create a StringTokenizer object with a String, like above example. You can call &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;nextToken()&lt;/span&gt;&lt;/code&gt; to get the next block of String (token).&lt;br /&gt;&lt;div class="MsoNormal"&gt;&lt;span style="color: white; font-size: 7.5pt;"&gt;Advertisement&lt;/span&gt; &lt;/div&gt;&lt;div class="MsoNormal" style="margin-bottom: 12.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;How to get memory information in Java program?&lt;/h1&gt;In Java, memory is allocated in various places such as the stack, heap, etc. While your application is running, you can sniff out the memory using: &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Runtime.getRuntime().freeMemory()&lt;/span&gt;&lt;/code&gt; and &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;Runtime.getRuntime().totalMemory()&lt;/span&gt;&lt;/code&gt;.&lt;br /&gt;&lt;pre&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-tab-count: 1;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;public static void main(String... args)&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;{&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Max Memory&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;: " +&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;nbsp;Runtime.getRuntime().maxMemory());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Total Memory : " +&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&amp;nbsp;Runtime.getRuntime().totalMemory());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Free Memory&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;: " + &lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Runtime.getRuntime().freeMemory());&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;&lt;div class="MsoNormal"&gt;&lt;br /&gt;&lt;/div&gt;&lt;h1&gt;How to enable JDBC tracing?&lt;/h1&gt;A good way to find out what JDBC calls are doing is to enable JDBC tracing. The JDBC trace contains a detailed listing of the activity occurring in the system that is related to JDBC operations.&lt;br /&gt;If you use the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;DriverManager&lt;/span&gt;&lt;/code&gt; facility to establish your database connection, you use the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;DriverManager.setLogWriter&lt;/span&gt;&lt;/code&gt; method to enable tracing of JDBC operations.&lt;br /&gt;&lt;pre&gt;import java.io.PrintWriter;&lt;br /&gt;&lt;br /&gt;import java.sql.Connection;&lt;br /&gt;&lt;br /&gt;import java.sql.DriverManager;&lt;br /&gt;&lt;br /&gt;import java.sql.SQLException;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public class Program {&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;public static void main(String... args) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Connection con = null;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Class.forName("org.gjt.mm.mysql.Driver").newInstance();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;DriverManager.setLogWriter(new PrintWriter(System.out));&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch(Exception e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Exception: " + e.getMessage());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;return ;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb",&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;"usr", "sql");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if(!con.isClosed())&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Successfully connected to " +&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;"MySQL server using TCP/IP...");&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} catch(Exception e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;System.out.println("Exception: " + e.getMessage());&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;} finally {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;try {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;if(con != null)&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;con.close();&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;catch(SQLException e) {&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="mso-spacerun: yes;"&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;/pre&gt;If you use a &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;DataSource&lt;/span&gt;&lt;/code&gt; object to connect to a data source, you use the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;DataSource.setLogWriter&lt;/span&gt;&lt;/code&gt; method to enable tracing. (For pooled connections, you use the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;ConnectionPoolDataSource.setLogWriter&lt;/span&gt;&lt;/code&gt; method, and for connections that can participate in distributed transactions, you use the &lt;code&gt;&lt;span style="font-size: 10.0pt;"&gt;XADataSource.setLogWriter&lt;/span&gt;&lt;/code&gt; method.)&lt;br /&gt;&lt;div style="margin-bottom: 5.0pt; margin-left: .5in; margin-right: .5in; margin-top: 5.0pt;"&gt;&lt;br /&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7634848634080023834-1288035228957373831?l=java-boss.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://java-boss.blogspot.com/feeds/1288035228957373831/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://java-boss.blogspot.com/2011/06/core-java-difficult-interview-questions.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/1288035228957373831'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7634848634080023834/posts/default/1288035228957373831'/><link rel='alternate' type='text/html' href='http://java-boss.blogspot.com/2011/06/core-java-difficult-interview-questions.html' title='Core Java Difficult Interview Questions'/><author><name>AmitKumarGupta</name><uri>http://www.blogger.com/profile/10897202134878054444</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='24' height='32' src='http://3.bp.blogspot.com/_oAk6ii8gAg4/SlNKTpPP0oI/AAAAAAAAAvM/QV7R3XH2z38/S220/Z17zwqbb.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7634848634080023834.post-7626249162321722206</id><published>2011-06-25T10:43:00.003+05:30</published><updated>2011-06-25T10:45:32.309+05:30</updated><title type='text'>Java Interview Questions and Answers Collection</title><content type='html'>Q:	What is the difference between an Interface and an Abstract class? &lt;br /&gt;A:	An abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.&lt;br /&gt;. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the purpose of garbage collection in Java, and when is it used?&lt;br /&gt;A:	The purpose of garbage collection is to identify and discard objects that are no longer needed by a program so that their resources can be reclaimed and reused. A Java object is subject to garbage collection when it becomes unreachable to the program in which it is used. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Describe synchronization in respect to multithreading.&lt;br /&gt;A:	With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchonization, it is possible for one thread to modify a shared variable while another thread is in the process of using or updating same shared variable. This usually leads to significant errors.  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Explain different way of using thread? &lt;br /&gt;A:	The thread could be implemented by using runnable interface or by inheriting from the Thread class. The former is more advantageous, 'cause when you are going for multiple inheritance..the only interface can help.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are pass by reference and passby value? &lt;br /&gt;A:	Pass By Reference means the passing the address itself rather than passing the value. Passby Value means passing a copy of the value to be passed. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is HashMap and Map?&lt;br /&gt;A:	Map is Interface and Hashmap is class that implements that.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Difference between HashMap and HashTable?&lt;br /&gt;A:	The HashMap class is roughly equivalent to Hashtable, except that it is unsynchronized and permits nulls. (HashMap allows null values as key and value whereas Hashtable doesnt allow). HashMap does not guarantee that the order of the map will remain constant over time. HashMap is unsynchronized and Hashtable is synchronized. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Difference between Vector and ArrayList?&lt;br /&gt;A:	Vector is synchronized whereas arraylist is not.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Difference between Swing and Awt?&lt;br /&gt;A:	AWT are heavy-weight componenets. Swings are light-weight components. Hence swing works faster than AWT.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between a constructor and a method? &lt;br /&gt;A:	A constructor is a member function of a class that is used to create objects of that class. It has the same name as the class itself, has no return type, and is invoked using the new operator.&lt;br /&gt;A method is an ordinary member function of a class. It has its own name, a return type (which may be void), and is invoked using the dot operator.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is an Iterator?&lt;br /&gt;A:	Some of the collection classes provide traversal of their contents via a java.util.Iterator interface. This interface allows you to walk through a collection of objects, operating on each object in turn. Remember when using Iterators that they contain a snapshot of the collection at the time the Iterator was obtained; generally it is not advisable to modify the collection itself while traversing an Iterator.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	State the significance of public, private, protected, default modifiers both singly and in combination and state the effect of package relationships on declared items qualified by these modifiers.&lt;br /&gt;A:	public : Public class is visible in other packages, field is visible everywhere (class must be public too)&lt;br /&gt;private : Private variables or methods may be used only by an instance of the same class that declares the variable or method, A private feature may only be accessed by the class that owns the feature.&lt;br /&gt;protected : Is available to all classes in the same package and also available to all subclasses of the class that owns the protected feature.This access is provided even to subclasses that reside in a different package from the class that owns the protected feature.&lt;br /&gt;default :What you get by default ie, without any access modifier (ie, public private or protected).It means that it is visible to all within a particular package.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is an abstract class?&lt;br /&gt;A:	Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie, you may not call its constructor), abstract class may contain static data. Any class with an abstract method is automatically abstract itself, and must be declared as such.&lt;br /&gt;A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is static in java?&lt;br /&gt;A:	Static means one per class, not one for each object no matter how many instance of a class might exist. This means that you can use them without creating an instance of a class.Static methods are implicitly final, because overriding is done based on the type of the object, and static methods are attached to a class, not an object. A static method in a superclass can be shadowed by another static method in a subclass, as long as the original method was not declared final. However, you can't override a static method with a nonstatic method. In other words, you can't change a static method into an instance method in a subclass.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is final?&lt;br /&gt;A:	A final class can't be extended ie., final class may not be subclassed. A final method can't be overridden when its class is inherited. You can't change value of a final variable (is a constant).&lt;br /&gt;Q:	What if the main method is declared as private?&lt;br /&gt;A:	The program compiles properly but at runtime it will give "Main method not public." message.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What if the static modifier is removed from the signature of the main method?&lt;br /&gt;A:	Program compiles. But at runtime throws an error "NoSuchMethodError". &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What if I write static public void instead of public static void?&lt;br /&gt;A:	Program compiles and runs properly. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What if I do not provide the String array as the argument to the method?&lt;br /&gt;A:	Program compiles but throws a runtime error "NoSuchMethodError". &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the first argument of the String array in main method?&lt;br /&gt;A:	The String array is empty. It does not have any element. This is unlike C/C++ where the first element by default is the program name.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	If I do not provide any arguments on the command line, then the String array of Main method will be empty or null?&lt;br /&gt;A:	It is empty. But not null.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can one prove that the array is not null but empty using one line of code?&lt;br /&gt;A:	Print args.length. It will print 0. That means it is empty. But if it would have been null then it would have thrown a NullPointerException on attempting to print args.length.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What environment variables do I need to set on my machine in order to be able to run Java programs?&lt;br /&gt;A:	CLASSPATH and PATH are the two variables.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can an application have multiple classes having main method?&lt;br /&gt;A:	Yes it is possible. While starting the application we mention the class name to be run. The JVM will look for the Main method only in the class whose name you have mentioned. Hence there is not conflict amongst the multiple classes having main method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can I have multiple main methods in the same class?&lt;br /&gt;A:	No the program fails to compile. The compiler says that the main method is already defined in the class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Do I need to import java.lang package any time? Why ?&lt;br /&gt;A:	No. It is by default loaded internally by the JVM.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can I import same package/class twice? Will the JVM load the package twice at runtime?&lt;br /&gt;A:	One can import the same package or same class multiple times. Neither compiler nor JVM complains abt it. And the JVM will internally load the class only once no matter how many times you import the same class.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are Checked and UnChecked Exception?&lt;br /&gt;A:	A checked exception is some subclass of Exception (or Exception itself), excluding class RuntimeException and its subclasses.&lt;br /&gt;Making an exception checked forces client programmers to deal with the possibility that the exception will be thrown. eg, IOException thrown by java.io.FileInputStream's read() method•&lt;br /&gt;Unchecked exceptions are RuntimeException and any of its subclasses. Class Error and its subclasses also are unchecked. With an unchecked exception, however, the compiler doesn't force client programmers either to catch the&lt;br /&gt;exception or declare it in a throws clause. In fact, client programmers may not even know that the exception could be thrown. eg, StringIndexOutOfBoundsException thrown by String's charAt() method• Checked exceptions must be caught at compile time. Runtime exceptions do not need to be. Errors often cannot be.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Overriding?&lt;br /&gt;A:	When a class defines a method using the same name, return type, and arguments as a method in its superclass, the method in the class overrides the method in the superclass.&lt;br /&gt;When the method is invoked for an object of the class, it is the new definition of the method that is called, and not the method definition from superclass. Methods may be overridden to be more public, not more private. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are different types of inner classes?&lt;br /&gt;A:	Nested -level classes, Member classes, Local classes, Anonymous classes&lt;br /&gt;Nested -level classes- If you declare a class within a class and specify the static modifier, the compiler treats the class just like any other -level class.&lt;br /&gt;Any class outside the declaring class accesses the nested class with the declaring class name acting similarly to a package. eg, outer.inner. -level inner classes implicitly have access only to static variables.There can also be inner interfaces. All of these are of the nested -level variety.&lt;br /&gt;&lt;br /&gt;Member classes - Member inner classes are just like other member methods and member variables and access to the member class is restricted, just like methods and variables. This means a public member class acts similarly to a nested -level class. The primary difference between member classes and nested -level classes is that member classes have access to the specific instance of the enclosing class.&lt;br /&gt;&lt;br /&gt;Local classes - Local classes are like local variables, specific to a block of code. Their visibility is only within the block of their declaration. In order for the class to be useful beyond the declaration block, it would need to implement a&lt;br /&gt;more publicly available interface.Because local classes are not members, the modifiers public, protected, private, and static are not usable.&lt;br /&gt;&lt;br /&gt;Anonymous classes - Anonymous inner classes extend local inner classes one level further. As anonymous classes have no name, you cannot provide a constructor.&lt;br /&gt;&lt;br /&gt;Q:	Are the imports checked for validity at compile time? e.g. will the code containing an import such as java.lang.ABCD compile?&lt;br /&gt;A:	Yes the imports are checked for the semantic validity at compile time. The code containing above line of import will not compile. It will throw an error saying,can not resolve symbol&lt;br /&gt;symbol : class ABCD&lt;br /&gt;location: package io&lt;br /&gt;import java.io.ABCD; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Does importing a package imports the subpackages as well? e.g. Does importing com.MyTest.* also import com.MyTest.UnitTests.*?&lt;br /&gt;A:	No you will have to import the subpackages explicitly. Importing com.MyTest.* will import classes in the package MyTest only. It will not import any class in any of it's subpackage.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between declaring a variable and defining a variable?&lt;br /&gt;A:	In declaration we just mention the type of the variable and it's name. We do not initialize it. But defining means declaration + initialization.&lt;br /&gt;e.g String s; is just a declaration while String s = new String ("abcd"); Or String s = "abcd"; are both definitions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the default value of an object reference declared as an instance variable?&lt;br /&gt;A:	null unless we define it explicitly.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can a  level class be private or protected?&lt;br /&gt;A:	No. A  level class can not be private or protected. It can have either "public" or no modifier. If it does not have a modifier it is supposed to have a default access.If a  level class is declared as private the compiler will complain that the "modifier private is not allowed here". This means that a  level class can not be private. Same is the case with protected.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What type of parameter passing does Java support?&lt;br /&gt;A:	In Java the arguments are always passed by value .&lt;br /&gt;[ Update from Eki and Jyothish Venu] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Primitive data types are passed by reference or pass by value?&lt;br /&gt;A:	Primitive data types are passed by value.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Objects are passed by value or by reference?&lt;br /&gt;A:	Java only supports pass by value. With objects, the object reference itself is passed by value and so both the original reference and parameter copy both refer to the same object .&lt;br /&gt;[ Update from Eki and Jyothish Venu] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is serialization?&lt;br /&gt;A:	Serialization is a mechanism by which you can save the state of an object by converting it to a byte stream.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How do I serialize an object to a file?&lt;br /&gt;A:	The class whose instances are to be serialized should implement an interface Serializable. Then you pass the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Which methods of Serializable interface should I implement?&lt;br /&gt;A:	The serializable interface is an empty interface, it does not contain any methods. So we do not implement any methods.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can I customize the seralization process? i.e. how can one have a control over the serialization process?&lt;br /&gt;A:	Yes it is possible to have control over serialization process. The class should implement Externalizable interface. This interface contains two methods namely readExternal and writeExternal. You should implement these methods and write the logic for customizing the serialization process.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the common usage of serialization?&lt;br /&gt;A:	Whenever an object is to be sent over the network, objects need to be serialized. Moreover if the state of an object is to be saved, objects need to be serilazed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Externalizable interface?&lt;br /&gt;A:	Externalizable is an interface which contains two methods readExternal and writeExternal. These methods give you a control over the serialization mechanism. Thus if your class implements this interface, you can customize the serialization process by implementing these methods.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	When you serialize an object, what happens to the object references included in the object?&lt;br /&gt;A:	The serialization mechanism generates an object graph for serialization. Thus it determines whether the included object references are serializable or not. This is a recursive process. Thus when an object is serialized, all the included objects are also serialized alongwith the original obect.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What one should take care of while serializing the object?&lt;br /&gt;A:	One should make sure that all the included objects are also serializable. If any of the objects is not serializable then it throws a NotSerializableException.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What happens to the static fields of a class during serialization? &lt;br /&gt;A:	There are three exceptions in which serialization doesnot necessarily read and write to the stream. These are&lt;br /&gt;1. Serialization ignores static fields, because they are not part of ay particular state state.&lt;br /&gt;2. Base class fields are only hendled if the base class itself is serializable.&lt;br /&gt;3. Transient fields. &lt;br /&gt;[ Received from Sandesh Sadhale Modified after P.John David comments.] 	 &lt;br /&gt;&lt;br /&gt;Q:	Does Java provide any construct to find out the size of an object?&lt;br /&gt;A:	No there is not sizeof operator in Java. So there is not direct way to determine the size of an object directly in Java.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Give a simplest way to find out the time a method takes for execution without using any profiling tool?&lt;br /&gt;A:	Read the system time just before the method is invoked and immediately after method returns. Take the time difference, which will give you the time taken by a method for execution. &lt;br /&gt;To put it in code...&lt;br /&gt;long start = System.currentTimeMillis ();&lt;br /&gt;method ();&lt;br /&gt;long end = System.currentTimeMillis ();&lt;br /&gt;System.out.println ("Time taken for execution is " + (end - start));&lt;br /&gt;Remember that if the time taken for execution is too small, it might show that it is taking zero milliseconds for execution. Try it on a method which is big enough, in the sense the one which is doing considerable amout of processing.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are wrapper classes?&lt;br /&gt;A:	Java provides specialized classes corresponding to each of the primitive data types. These are called wrapper classes. They are e.g. Integer, Character, Double etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Why do we need wrapper classes?&lt;br /&gt;A:	It is sometimes easier to deal with primitives as objects. Moreover most of the collection classes store objects and not primitive data types. And also the wrapper classes provide many utility methods also. Because of these resons we need wrapper classes. And since we create instances of these classes we can store them in any of the collection classes and pass them around as a collection. Also we can pass them around as method parameters where a method expects an object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are checked exceptions?&lt;br /&gt;A:	Checked exception are those which the Java compiler forces you to catch. e.g. IOException are checked Exceptions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are runtime exceptions?&lt;br /&gt;A:	Runtime exceptions are those exceptions that are thrown at runtime because of either wrong input data or because of wrong business logic etc. These are not checked by the compiler at compile time.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between error and an exception?&lt;br /&gt;A:	An error is an irrecoverable condition occurring at runtime. Such as OutOfMemory error. These JVM errors and you can not repair them at runtime. While exceptions are conditions that occur because of bad input etc. e.g. FileNotFoundException will be thrown if the specified file does not exist. Or a NullPointerException will take place if you try using a null reference. In most of the cases it is possible to recover from an exception (probably by giving user a feedback for entering proper values etc.).&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How to create custom exceptions?&lt;br /&gt;A:	Your class should extend class Exception, or some more specific type thereof.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	If I want an object of my class to be thrown as an exception object, what should I do?&lt;br /&gt;A:	The class should extend from Exception class. Or you can extend your class from some more precise exception type also.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	If my class already extends from some other class what should I do if I want an instance of my class to be thrown as an exception object?&lt;br /&gt;A:	One can not do anytihng in this scenarion. Because Java does not allow multiple inheritance and does not provide any exception interface as well.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How does an exception permeate through the code?&lt;br /&gt;A:	An unhandled exception moves up the method stack in search of a matching When an exception is thrown from a code which is wrapped in a try block followed by one or more catch blocks, a search is made for matching catch block. If a matching type is found then that block will be invoked. If a matching type is not found then the exception moves up the method stack and reaches the caller method. Same procedure is repeated if the caller method is included in a try catch block. This process continues until a catch block handling the appropriate type of exception is found. If it does not find such a block then finally the program terminates.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the different ways to handle exceptions?&lt;br /&gt;A:	There are two ways to handle exceptions, &lt;br /&gt;1. By wrapping the desired code in a try block followed by a catch block to catch the exceptions. and &lt;br /&gt;2. List the desired exceptions in the throws clause of the method and let the caller of the method hadle those exceptions.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the basic difference between the 2 approaches to exception handling.&lt;br /&gt;1&gt; try catch block and &lt;br /&gt;2&gt; specifying the candidate exceptions in the throws clause?&lt;br /&gt;When should you use which approach?&lt;br /&gt;A:	In the first approach as a programmer of the method, you urself are dealing with the exception. This is fine if you are in a best position to decide should be done in case of an exception. Whereas if it is not the responsibility of the method to deal with it's own exceptions, then do not use this approach. In this case use the second approach. In the second approach we are forcing the caller of the method to catch the exceptions, that the method is likely to throw. This is often the approach library creators use. They list the exception in the throws clause and we must catch them. You will find the same approach throughout the java libraries we use.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is it necessary that each try block must be followed by a catch block?&lt;br /&gt;A:	It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	If I write return at the end of the try block, will the finally block still execute?&lt;br /&gt;A:	Yes even if you write return as the last statement in the try block and no exception occurs, the finally block will execute. The finally block will execute and then the control return.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	If I write System.exit (0); at the end of the try block, will the finally block still execute?&lt;br /&gt;A:	No in this case the finally block will not execute because when you say System.exit (0); the control immediately goes out of the program, and thus finally never executes.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How are Observer and Observable used?&lt;br /&gt;A:	Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects.&lt;br /&gt;[Received from Venkateswara Manam] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is synchronization and why is it important?&lt;br /&gt;A:	With respect to multithreading, synchronization is the capability to control&lt;br /&gt;the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors.&lt;br /&gt;[ Received from Venkateswara Manam] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How does Java handle integer overflows and underflows?&lt;br /&gt;A:	It uses those low order bytes of the result that can fit into the size of the type allowed by the operation.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Does garbage collection guarantee that a program will not run out of memory?&lt;br /&gt;A:	Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use up memory resources faster than they are garbage collected. It is also possible for programs to create objects that are not subject to garbage collection&lt;br /&gt;.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between preemptive scheduling and time slicing?&lt;br /&gt;A:	Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	When a thread is created and started, what is its initial state?&lt;br /&gt;A:	A thread is in the ready state after it has been created and started.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the purpose of finalization?&lt;br /&gt;A:	The purpose of finalization is to give an unreachable object the opportunity to perform any cleanup processing before the object is garbage collected.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Locale class?&lt;br /&gt;A:	The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between a while statement and a do statement?&lt;br /&gt;A:	A while statement checks at the beginning of a loop to see whether the next loop iteration should occur. A do statement checks at the end of a loop to see whether the next iteration of a loop should occur. The do statement will always execute the body of a loop at least once.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between static and non-static variables?&lt;br /&gt;A:	A static variable is associated with the class as a whole rather than with specific instances of a class. Non-static variables take on unique values with each object instance.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How are this() and super() used with constructors?&lt;br /&gt;A:	This() is used to invoke a constructor of the same class. super() is used to invoke a superclass constructor.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are synchronized methods and synchronized statements?&lt;br /&gt;A:	Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.&lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is daemon thread and which method is used to create the daemon thread?&lt;br /&gt;A:	Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.&lt;br /&gt;[ Received from Shipra Kamra]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can applets communicate with each other?&lt;br /&gt;A:	At this point in time applets may communicate with other applets running in the same virtual machine. If the applets are of the same class, they can communicate via shared static variables. If the applets are of different classes, then each will need a reference to the same class with static variables. In any case the basic idea is to pass the information back and forth through a static variable. &lt;br /&gt;&lt;br /&gt;An applet can also get references to all other applets on the same page using the getApplets() method of java.applet.AppletContext. Once you get the reference to an applet, you can communicate with it by using its public members. &lt;br /&gt;&lt;br /&gt;It is conceivable to have applets in different virtual machines that talk to a server somewhere on the Internet and store any data that needs to be serialized there. Then, when another applet needs this data, it could connect to this same server. Implementing this is non-trivial. &lt;br /&gt;[ Received from Krishna Kumar ]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the steps in the JDBC connection?&lt;br /&gt;A:	  While making a JDBC connection we go through the following steps : &lt;br /&gt;&lt;br /&gt;Step 1 : Register the database driver by using : &lt;br /&gt;Class.forName(\" driver classs for that specific database\" );&lt;br /&gt;Step 2 : Now create a database connection using :&lt;br /&gt;Connection con = DriverManager.getConnection(url,username,password);&lt;br /&gt;Step 3: Now Create a query using :&lt;br /&gt;Statement stmt = Connection.Statement(\"select * from TABLE NAME\");&lt;br /&gt;Step 4 : Exceute the query :&lt;br /&gt;stmt.exceuteUpdate();&lt;br /&gt;[ Received from Shri Prakash Kunwar]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How does a try statement determine which catch clause should be used to handle an exception?&lt;br /&gt;A:	When an exception is thrown within the body of a try statement, the catch clauses of the try statement are examined in the order in which they appear. The first catch clause that is capable of handling the exceptionis executed. The remaining catch clauses are ignored.&lt;br /&gt;[ Received from P Rajesh&lt;br /&gt;&lt;br /&gt;Q:	Can an unreachable object become reachable again?&lt;br /&gt;A:	An unreachable object may become reachable again. This can happen when the object's finalize() method is invoked and the object performs an operation which causes it to become accessible to reachable objects.&lt;br /&gt;[Received from P Rajesh] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What method must be implemented by all threads?&lt;br /&gt;A:	All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.&lt;br /&gt;[ Received from P Rajesh] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are synchronized methods and synchronized statements?&lt;br /&gt;A:	Synchronized methods are methods that are used to control access to an object. A thread only executes a synchronized method after it has acquired the lock for the method's object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be executed after a thread has acquired the lock for the object or class referenced in the synchronized statement.&lt;br /&gt;[ Received from P Rajesh]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Externalizable? &lt;br /&gt;A:	Externalizable is an Interface that extends Serializable Interface. And sends data into Streams in Compressed Format. It has two methods, writeExternal(ObjectOuput out) and readExternal(ObjectInput in) &lt;br /&gt;[ Received from Venkateswara Manam]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What modifiers are allowed for methods in an Interface?&lt;br /&gt;A:	Only public and abstract modifiers are allowed for methods in interfaces. &lt;br /&gt;[ Received from P Rajesh]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are some alternatives to inheritance?&lt;br /&gt;A:	Delegation is an alternative to inheritance. Delegation means that you include an instance of another class as an instance variable, and forward messages to the instance. It is often safer than inheritance because it forces you to think about each message you forward, because the instance is of a known class, rather than a new class, and because it doesn't force you to accept all the methods of the super class: you can provide only the methods that really make sense. On the other hand, it makes you write more code, and it is harder to re-use (because it is not a subclass).&lt;br /&gt;[ Received from P Rajesh]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What does it mean that a method or field is "static"? &lt;br /&gt;A:	Static variables and methods are instantiated only once per class. In other words they are class variables, not instance variables. If you change the value of a static variable in a particular object, the value of that variable changes for all instances of that class. &lt;br /&gt;Static methods can be referenced with the name of the class rather than the name of a particular object of the class (though that works too). That's how library methods like System.out.println() work out is a static field in the java.lang.System class. &lt;br /&gt;[ Received from P Rajesh]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between preemptive scheduling and time slicing?&lt;br /&gt;A:	Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence. Under time slicing, a task executes for a predefined slice of time and then reenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors. &lt;br /&gt;[ Received from P Rajesh]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the catch or declare rule for method declarations? &lt;br /&gt;A:	If a checked exception may be thrown within the body of a method, the method must either catch the exception or declare it in its throws clause. &lt;br /&gt;[ Received from P Rajesh]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is Empty .java file a valid source file?&lt;br /&gt;A:	Yes, an empty .java file is a perfectly valid source file.&lt;br /&gt;[Received from Sandesh Sadhale] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can a .java file contain more than one java classes?&lt;br /&gt;A:	Yes, a .java file contain more than one java classes, provided at the most one of them is a public class.&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is String a primitive data type in Java?&lt;br /&gt;A:	No String is not a primitive data type in Java, even though it is one of the most extensively used object. Strings in Java are instances of String class defined in java.lang package.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is main a keyword in Java? &lt;br /&gt;A:	No, main is not a keyword in Java. &lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is next a keyword in Java?&lt;br /&gt;A:	No, next is not a keyword. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is delete a keyword in Java?&lt;br /&gt;A:	No, delete is not a keyword in Java. Java does not make use of explicit destructors the way C++ does.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is exit a keyword in Java? &lt;br /&gt;A:	No. To exit a program explicitly you use exit method in System object.&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What happens if you dont initialize an instance variable of any of the primitive types in Java?&lt;br /&gt;A:	Java by default initializes it to the default value for that primitive type. Thus an int will be initialized to 0, a boolean will be initialized to false. &lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What will be the initial value of an object reference which is defined as an instance variable? &lt;br /&gt;A:	The object references are all initialized to null in Java. However in order to do anything useful with these references, you must set them to a valid object, else you will get NullPointerExceptions everywhere you try to use such default initialized references.&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the different scopes for Java variables? &lt;br /&gt;A:	The scope of a Java variable is determined by the context in which the variable is declared. Thus a java variable can have one of the three scopes at any given point in time.&lt;br /&gt;1. Instance : - These are typical object level variables, they are initialized to default values at the time of creation of object, and remain accessible as long as the object accessible.&lt;br /&gt;2. Local : - These are the variables that are defined within a method. They remain accessbile only during the course of method excecution. When the method finishes execution, these variables fall out of scope.&lt;br /&gt;3. Static: - These are the class level variables. They are initialized when the class is loaded in JVM for the first time and remain there as long as the class remains loaded. They are not tied to any particular object instance.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the default value of the local variables? &lt;br /&gt;A:	The local variables are not initialized to any default value, neither primitives nor object references. If you try to use these variables without initializing them explicitly, the java compiler will not compile the code. It will complain abt the local varaible not being initilized..&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How many objects are created in the following piece of code?&lt;br /&gt;MyClass c1, c2, c3;&lt;br /&gt;c1 = new MyClass ();&lt;br /&gt;c3 = new MyClass ();&lt;br /&gt;A:	Only 2 objects are created, c1 and c3. The reference c2 is only declared and not initialized.&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can a public class MyClass be defined in a source file named YourClass.java? &lt;br /&gt;A:	No the source file name, if it contains a public class, must be the same as the public class name itself with a .java extension.&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can main method be declared final? &lt;br /&gt;A:	Yes, the main method can be declared final, in addition to being public static.&lt;br /&gt;[ Received fromSandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What will be the output of the following statement?&lt;br /&gt;System.out.println ("1" + 3); &lt;br /&gt;A:	It will print 13.&lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What will be the default values of all the elements of an array defined as an instance variable? &lt;br /&gt;A:	If the array is an array of primitive types, then all the elements of the array will be initialized to the default value corresponding to that primitive type. e.g. All the elements of an array of int will be initialized to 0, while that of boolean type will be initialized to false. Whereas if the array is an array of references (of any type), all the elements will be initialized to null. &lt;br /&gt;[ Received from Sandesh Sadhale]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Collections API? &lt;br /&gt;A:	The Collections API is a set of classes and interfaces that support operations on collections of objects.&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the List interface?&lt;br /&gt;A:	The List interface provides support for ordered collections of objects. &lt;br /&gt;[ Received from SPrasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Vector class?&lt;br /&gt;A:	The Vector class provides the capability to implement a growable array of objects.  &lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is an Iterator interface? &lt;br /&gt;A:	The Iterator interface is used to step through the elements of a Collection .&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Which java.util classes and interfaces support event handling? &lt;br /&gt;A:	The EventObject class and the EventListener interface support event processing.&lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the GregorianCalendar class? &lt;br /&gt;A:	The GregorianCalendar provides support for traditional Western calendars &lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Locale class? &lt;br /&gt;A:	The Locale class is used to tailor program output to the conventions of a particular geographic, political, or cultural region .&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the SimpleTimeZone class?&lt;br /&gt;A:	The SimpleTimeZone class provides support for a Gregorian calendar .&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Map interface? &lt;br /&gt;A:	The Map interface replaces the JDK 1.1 Dictionary class and is used associate keys with values.&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the highest-level event class of the event-delegation model?&lt;br /&gt;A:	The java.util.EventObject class is the highest-level class in the event-delegation class hierarchy.&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Collection interface? &lt;br /&gt;A:	The Collection interface provides support for the implementation of a mathematical bag - an unordered collection of objects that may contain duplicates.&lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the Set interface? &lt;br /&gt;A:	The Set interface provides methods for accessing the elements of a finite mathematical set. Sets do not allow duplicate elements. &lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the typical use of Hashtable?&lt;br /&gt;A:	Whenever a program wants to store a key value pair, one can use Hashtable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	I am trying to store an object using a key in a Hashtable. And some other object already exists in that location, then what will happen? The existing object will be overwritten? Or the new object will be stored elsewhere?&lt;br /&gt;A:	The existing object will be overwritten and thus it will be lost.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between the size and capacity of a Vector?&lt;br /&gt;A:	The size is the number of elements actually stored in the vector, while capacity is the maximum number of elements it can store at a given instance of time.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can a vector contain heterogenous objects?&lt;br /&gt;A:	Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can a ArrayList contain heterogenous objects?&lt;br /&gt;A:	Yes a ArrayList can contain heterogenous objects. Because a ArrayList stores everything in terms of Object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is an enumeration?&lt;br /&gt;A:	An enumeration is an interface containing methods for accessing the underlying data structure from which the enumeration is obtained. It is a construct which collection classes return when you request a collection of all the objects stored in the collection. It allows sequential access to all the elements stored in the collection.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Considering the basic properties of Vector and ArrayList, where will you use Vector and where will you use ArrayList?&lt;br /&gt;A:	The basic difference between a Vector and an ArrayList is that, vector is synchronized while ArrayList is not. Thus whenever there is a possibility of multiple threads accessing the same instance, one should use Vector. While if not multiple threads are going to access the same instance then use ArrayList. Non synchronized data structure will give better performance than the synchronized one.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can a vector contain heterogenous objects?&lt;br /&gt;A:	Yes a Vector can contain heterogenous objects. Because a Vector stores everything in terms of Object.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is a output comment? &lt;br /&gt;A:	A comment that is sent to the client in the viewable page source.The JSP engine handles an output comment as uninterpreted HTML text, returning the comment in the HTML output sent to the client. You can see the comment by viewing the page source from your Web browser. &lt;br /&gt;JSP Syntax&lt;br /&gt;&lt;!-- comment [ &lt;%= expression %&gt; ] --&gt;&lt;br /&gt;&lt;br /&gt;Example 1&lt;br /&gt;&lt;!-- This is a commnet sent to client on&lt;%= (new java.util.Date()).toLocaleString() %&gt;&lt;br /&gt;--&gt;&lt;br /&gt;&lt;br /&gt;Displays in the page source:&lt;br /&gt;&lt;!-- This is a commnet sent to client on January 24, 2004 --&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is a Hidden Comment?&lt;br /&gt;A:	A comments that documents the JSP page but is not sent to the client. The JSP engine ignores a hidden comment, and does not process any code within hidden comment tags. A hidden comment is not sent to the client, either in the displayed JSP page or the HTML page source. The hidden comment is useful when you want to hide or "comment out" part of your JSP page. &lt;br /&gt;You can use any characters in the body of the comment except the closing --%&gt; combination. If you need to use --%&gt; in your comment, you can escape it by typing --%\&gt;. &lt;br /&gt;JSP Syntax&lt;br /&gt;&lt;%-- comment --%&gt;&lt;br /&gt;Examples&lt;br /&gt;&lt;%@ page language="java" %&gt;&lt;br /&gt;&lt;html&gt;&lt;br /&gt;&lt;head&gt;&lt;title&gt;A Hidden Comment &lt;/title&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;&lt;%-- This comment will not be visible to the colent in the page source --%&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is a Expression?&lt;br /&gt;A:	An expression tag contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within text in a JSP file. Like&lt;br /&gt;&lt;%= someexpression %&gt; &lt;br /&gt;&lt;%= (new java.util.Date()).toLocaleString() %&gt;&lt;br /&gt;You cannot use a semicolon to end an expression &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is a Declaration? &lt;br /&gt;A:	A declaration declares one or more variables or methods for use later in the JSP source file. &lt;br /&gt;A declaration must contain at least one complete declarative statement. You can declare any number of variables or methods within one declaration tag, as long as they are separated by semicolons. The declaration must be valid in the scripting language used in the JSP file. &lt;br /&gt;&lt;br /&gt;&lt;%! somedeclarations %&gt;&lt;br /&gt;&lt;%! int i = 0; %&gt;&lt;br /&gt;&lt;%! int a, b, c; %&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is a Scriptlet? &lt;br /&gt;A:	A scriptlet can contain any number of language statements, variable or method declarations, or expressions that are valid in the page scripting language.Within scriptlet tags, you can &lt;br /&gt;1.Declare variables or methods to use later in the file (see also Declaration).&lt;br /&gt;&lt;br /&gt;2.Write expressions valid in the page scripting language (see also Expression). &lt;br /&gt;&lt;br /&gt;3.Use any of the JSP implicit objects or any object declared with a &lt;jsp:useBean&gt; tag. &lt;br /&gt;You must write plain text, HTML-encoded text, or other JSP tags outside the scriptlet. &lt;br /&gt;Scriptlets are executed at request time, when the JSP engine processes the client request. If the scriptlet produces output, the output is stored in the out object, from which you can display it.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are implicit objects? List them?&lt;br /&gt;A:	Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects re listed below&lt;br /&gt;•	request &lt;br /&gt;•	response &lt;br /&gt;•	pageContext &lt;br /&gt;•	session &lt;br /&gt;•	application &lt;br /&gt;•	out &lt;br /&gt;•	config &lt;br /&gt;•	page &lt;br /&gt;•	exception &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Difference between forward and sendRedirect?&lt;br /&gt;A:	When you invoke a forward request, the request is sent to another resource on the server, without the client being informed that a different resource is going to process the request. This process occurs completly with in the web container. When a sendRedirtect method is invoked, it causes the web container to return to the browser indicating that a new URL should be requested. Because the browser issues a completly new request any object that are stored as request attributes before the redirect occurs will be lost. This extra round trip a redirect is slower than forward. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the different scope valiues for the &lt;jsp:useBean&gt;?&lt;br /&gt;A:	The different scope values for &lt;jsp:useBean&gt; are &lt;br /&gt;1. page&lt;br /&gt;2. request&lt;br /&gt;3.session&lt;br /&gt;4.application&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Explain the life-cycle mehtods in JSP?&lt;br /&gt;A:	THe generated servlet class for a JSP page implements the HttpJspPage interface of the javax.servlet.jsp package. Hte HttpJspPage interface extends the JspPage interface which inturn extends the Servlet interface of the javax.servlet package. the generated servlet class thus implements all the methods of the these three interfaces. The JspPage interface declares only two mehtods - jspInit() and jspDestroy() that must be implemented by all JSP pages regardless of the client-server protocol. However the JSP specification has provided the HttpJspPage interfaec specifically for the JSp pages serving HTTP requests. This interface declares one method _jspService(). &lt;br /&gt;The jspInit()- The container calls the jspInit() to initialize te servlet instance.It is called before any other method, and is called only once for a servlet instance.&lt;br /&gt;The _jspservice()- The container calls the _jspservice() for each request, passing it the request and the response objects.&lt;br /&gt;The jspDestroy()- The container calls this when it decides take the instance out of service. It is the last method called n the servlet instance.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How do I prevent the output of my JSP or Servlet pages from being cached by the browser?&lt;br /&gt;A:	You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions. &lt;br /&gt;&lt;%response.setHeader("Cache-Control","no-store"); //HTTP 1.1response.setHeader("Pragma\","no-cache"); //HTTP 1.0response.setDateHeader ("Expires", 0); //prevents caching at the proxy server%&gt;&lt;br /&gt;[ Received from Sumit Dhamija ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How does JSP handle run-time exceptions? &lt;br /&gt;A:	You can use the errorPage attribute of the page directive to have uncaught run-time exceptions automatically forwarded to an error processing page. For example:&lt;br /&gt;&lt;%@ page errorPage=\"error.jsp\" %&gt; redirects the browser to the JSP page error.jsp if an uncaught exception is encountered during request processing. Within error.jsp, if you indicate that it is an error-processing page, via the directive: &lt;%@ page isErrorPage=\"true\" %&gt; Throwable object describing the exception may be accessed within the error page via the exception implicit object. Note: You must always use a relative URL as the value for the errorPage attribute. &lt;br /&gt;[ Received from Sumit Dhamija ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?&lt;br /&gt;A:	You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive &lt;%@ page isThreadSafe="false" %&gt; within your JSP page. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine. More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition. SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use &lt;%! %&gt;. You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe .&lt;br /&gt;[ Received from Sumit Dhamija ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How do I use a scriptlet to initialize a newly instantiated bean? &lt;br /&gt;A:	A jsp:useBean action may optionally have a body. If the body is specified, its contents will be automatically invoked when the specified bean is instantiated. Typically, the body will contain scriptlets or jsp:setProperty tags to initialize the newly instantiated bean, although you are not restricted to using those alone.&lt;br /&gt;The following example shows the “today” property of the Foo bean initialized to the current date when it is instantiated. Note that here, we make use of a JSP expression within the jsp:setProperty action.&lt;br /&gt;&lt;br /&gt;&lt;jsp:useBean id="foo" class="com.Bar.Foo" &gt; &lt;br /&gt;&lt;jsp:setProperty name="foo" property="today" value="&lt;%=java.text.DateFormat.getDateInstance().format(new java.util.Date()) %&gt;" / &gt;&lt;br /&gt;&lt;%-- scriptlets calling bean setter methods go here --%&gt;&lt;br /&gt;&lt;/jsp:useBean &gt; &lt;br /&gt;[ Received from Sumit Dhamija ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can I prevent the word "null" from appearing in my HTML input text fields when I populate them with a resultset that has null values? &lt;br /&gt;A:	You could make a simple wrapper function, like &lt;br /&gt;&lt;%!String blanknull(String s) {return (s == null) ? \"\" : s;}%&gt;&lt;br /&gt;then use it inside your JSP form, like&lt;br /&gt;&lt;input type="text" name="lastName" value="&lt;%=blanknull(lastName)% &gt;" &gt;&lt;br /&gt;[ Received from Sumit Dhamija ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What's a better approach for enabling thread-safe servlets and JSPs? SingleThreadModel Interface or Synchronization?&lt;br /&gt;A:	Although the SingleThreadModel technique is easy to use, and works well for low volume sites, it does not scale well. If you anticipate your users to increase in the future, you may be better off implementing explicit synchronization for your shared data. The key however, is to effectively minimize the amount of code that is synchronzied so that you take maximum advantage of multithreading. &lt;br /&gt;Also, note that SingleThreadModel is pretty resource intensive from the server\'s perspective. The most serious issue however is when the number of concurrent requests exhaust the servlet instance pool. In that case, all the unserviced requests are queued until something becomes free - which results in poor performance. Since the usage is non-deterministic, it may not help much even if you did add more memory and increased the size of the instance pool. &lt;br /&gt;[ Received from Sumit Dhamija ] 	  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can I enable session tracking for JSP pages if the browser has disabled cookies?&lt;br /&gt;A:	We know that session tracking uses cookies by default to associate a session identifier with a unique user. If the browser does not support cookies, or if cookies are disabled, you can still enable session tracking using URL rewriting. URL rewriting essentially includes the session ID within the link itself as a name/value pair. However, for this to be effective, you need to append the session ID for each and every link that is part of your servlet response. Adding the session ID to a link is greatly simplified by means of of a couple of methods: response.encodeURL() associates a session ID with a given URL, and if you are using redirection, response.encodeRedirectURL() can be used by giving the redirected URL as input. Both encodeURL() and encodeRedirectedURL() first determine whether cookies are supported by the browser; if so, the input URL is returned unchanged since the session ID will be persisted as a cookie.&lt;br /&gt;&lt;br /&gt;Consider the following example, in which two JSP files, say hello1.jsp and hello2.jsp, interact with each other. Basically, we create a new session within hello1.jsp and place an object within this session. The user can then traverse to hello2.jsp by clicking on the link present within the page. Within hello2.jsp, we simply extract the object that was earlier placed in the session and display its contents. Notice that we invoke the encodeURL() within hello1.jsp on the link used to invoke hello2.jsp; if cookies are disabled, the session ID is automatically appended to the URL, allowing hello2.jsp to still retrieve the session object. Try this example first with cookies enabled. Then disable cookie support, restart the brower, and try again. Each time you should see the maintenance of the session across pages. Do note that to get this example to work with cookies disabled at the browser, your JSP engine has to support URL rewriting. &lt;br /&gt;hello1.jsp&lt;br /&gt;&lt;%@ page session=\"true\" %&gt;&lt;br /&gt;&lt;%Integer num = new Integer(100);session.putValue("num",num);String url =response.encodeURL("hello2.jsp");%&gt;&lt;br /&gt;&lt;a href=\'&lt;%=url%&gt;\'&gt;hello2.jsp&lt;/a&gt;&lt;br /&gt;hello2.jsp&lt;br /&gt;&lt;%@ page session="true" %&gt;&lt;br /&gt;&lt;%Integer i= (Integer )session.getValue("num");out.println("Num value in session is " + i.intValue());%&gt; &lt;br /&gt;[ Received from Vishal Khasgiwala ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference b/w variable declared inside a declaration part and variable declared in scriplet part?&lt;br /&gt;A:	Variable declared inside declaration part is treated as a global variable.that means after convertion jsp file into servlet that variable will be in outside of service method or it will be declared as instance variable.And the scope is available to complete jsp and to complete in the converted servlet class.where as if u declare a variable inside a scriplet that variable will be declared inside a service method and the scope is with in the service method. &lt;br /&gt;[ Received from Neelam Gangadhar] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is there a way to execute a JSP from the comandline or from my own application?&lt;br /&gt;A:	There is a little tool called JSPExecutor that allows you to do just that. The developers (Hendrik Schreiber &lt;hs@webapp.de&gt; &amp; Peter Rossbach &lt;pr@webapp.de&gt;) aim was not to write a full blown servlet engine, but to provide means to use JSP for generating source code or reports. Therefore most HTTP-specific features (headers, sessions, etc) are not implemented, i.e. no reponseline or header is generated. Nevertheless you can use it to precompile JSP for your website. &lt;br /&gt;[ Received from Nitin Jawarkar ] 	 &lt;br /&gt;&lt;br /&gt;Q:	Explain the life cycle methods of a Servlet.&lt;br /&gt;A:	The javax.servlet.Servlet interface defines the three methods known as life-cycle method.&lt;br /&gt;public void init(ServletConfig config) throws ServletException&lt;br /&gt;public void service( ServletRequest req, ServletResponse res) throws ServletException, IOException&lt;br /&gt;public void destroy()&lt;br /&gt;First the servlet is constructed, then initialized wih the init() method.&lt;br /&gt;Any request from client are handled initially by the service() method before delegating to the doXxx() methods in the case of HttpServlet.&lt;br /&gt;&lt;br /&gt;The servlet is removed from service, destroyed with the destroy() methid, then garbaged collected and finalized.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between the getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface and javax.servlet.ServletContext interface?&lt;br /&gt;A:	The getRequestDispatcher(String path) method of javax.servlet.ServletRequest interface accepts parameter the path to the resource to be included or forwarded to, which can be relative to the request of the calling servlet. If the path begins with a "/" it is interpreted as relative to the current context root.&lt;br /&gt;&lt;br /&gt;The getRequestDispatcher(String path) method of javax.servlet.ServletContext interface cannot accepts relative paths. All path must sart with a "/" and are interpreted as relative to curent context root.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Explain the directory structure of a web application.&lt;br /&gt;A:	The directory structure of a web application consists of two parts. &lt;br /&gt;A private directory called WEB-INF&lt;br /&gt;A public resource directory which contains public resource folder.&lt;br /&gt;&lt;br /&gt;WEB-INF folder consists of &lt;br /&gt;1. web.xml&lt;br /&gt;2. classes directory&lt;br /&gt;3. lib directory&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the common mechanisms used for session tracking?&lt;br /&gt;A:	Cookies&lt;br /&gt;SSL sessions&lt;br /&gt;URL- rewriting&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Explain ServletContext.&lt;br /&gt;A:	ServletContext interface is a window for a servlet to view it's environment. A servlet can use this interface to get information such as initialization parameters for the web applicationor servlet container's version. Every web application has one and only one ServletContext and is accessible to all active resource of that application.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is preinitialization of a servlet?&lt;br /&gt;A:	A container doesnot initialize the servlets ass soon as it starts up, it initializes a servlet when it receives a request for that servlet first time. This is called lazy loading. The servlet specification defines the &lt;load-on-startup&gt; element, which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up. The process of loading a servlet before any request comes in is called preloading or preinitializing a servlet.&lt;br /&gt;[ Received from Amit Bhoir ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between Difference between doGet() and doPost()?&lt;br /&gt;A:	A doGet() method is limited with 2k of data to be sent, and doPost() method doesn't have this limitation. A request string for doGet() looks like the following: &lt;br /&gt;http://www.allapplabs.com/svt1?p1=v1&amp;p2=v2&amp;...&amp;pN=vN&lt;br /&gt;doPost() method call doesn't need a long text tail after a servlet name in a request. All parameters are stored in a request itself, not in a request string, and it's impossible to guess the data transmitted to a servlet only looking at a request string.&lt;br /&gt;[ Received from Amit Bhoir ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between HttpServlet and GenericServlet?&lt;br /&gt;A:	A GenericServlet has a service() method aimed to handle requests. HttpServlet extends GenericServlet and adds support for doGet(), doPost(), doHead() methods (HTTP 1.0) plus doPut(), doOptions(), doDelete(), doTrace() methods (HTTP 1.1). &lt;br /&gt;Both these classes are abstract.&lt;br /&gt;[ Received from Amit Bhoir ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between ServletContext and ServletConfig?&lt;br /&gt;A:	ServletContext: Defines a set of methods that a servlet uses to communicate with its servlet container, for example, to get the MIME type of a file, dispatch requests, or write to a log file.The ServletContext object is contained within the ServletConfig object, which the Web server provides the servlet when the servlet is initialized &lt;br /&gt;&lt;br /&gt;ServletConfig: The object created after a servlet is instantiated and its default constructor is read. It is created to pass initialization information to the servlet. &lt;br /&gt;[ Received from Sivagopal Balivada ] 	&lt;br /&gt;&lt;br /&gt;Q:	What are the different kinds of enterprise beans?&lt;br /&gt;A:	Different kind of enterrise beans are Stateless session bean, Stateful session bean, Entity bean, Message-driven bean...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Session Bean?&lt;br /&gt;A:	A session bean is a non-persistent object that implements some business logic running on the server. One way to think of a session object...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Entity Bean?&lt;br /&gt;A:	The entity bean is used to represent data in the database. It provides an object-oriented interface to ...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the methods of Entity Bean?&lt;br /&gt;A:	An entity bean consists of 4 groups of methods, create methods...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between Container-Managed Persistent (CMP) bean and Bean-Managed Persistent(BMP) ?&lt;br /&gt;A:	Container-managed persistence (CMP) and bean-managed persistence (BMP). With CMP, the container manages the persistence of the entity bean............&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the callback methods in Entity beans?&lt;br /&gt;A:	Callback methods allows the container to notify the bean of events in&lt;br /&gt;its life cycle. The callback methods are defined in the javax.ejb.EntityBean interface............&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is software architecture of EJB?&lt;br /&gt;A:	Session and Entity EJBs consist of 4 and 5 parts respectively, a remote interface...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can Entity Beans have no create() methods?&lt;br /&gt;A:	Yes. In some cases the data is inserted NOT using Java application,...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is bean managed transaction?&lt;br /&gt;A:	If a developer doesn't want a Container to manage transactions, it's possible to implement all database operations manually...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are transaction attributes? &lt;br /&gt;A:	The transaction attribute specifies how the Container must manage transactions for a method when a client invokes the method via the enterprise bean’s home or...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are transaction isolation levels in EJB?&lt;br /&gt;A:	Transaction_read_uncommitted , Transaction_read_committed , Transaction_repeatable_read...........&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How EJB Invocation happens?&lt;br /&gt;A:	Step 1: Retrieve Home Object reference from Naming Service via JNDI.&lt;br /&gt;step 2: Return Home Object reference to the client.&lt;br /&gt;step 3: Create me a new EJB Object through Home Object interface.&lt;br /&gt;step 4: Create EJB Object from the Ejb Object&lt;br /&gt;step 5: Return EJB Object reference to the client.&lt;br /&gt;step 6: Invoke business method using EJB Object reference.&lt;br /&gt;step 7: Delegate request to Bean (Enterprise Bean). &lt;br /&gt;[ Received from Ramana Bhavanasi] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Is it possible to share an HttpSession between a JSP and EJB? What happens when I change a value in the HttpSession from inside an EJB?&lt;br /&gt;A:	  You can pass the HttpSession as parameter to an EJB method, only if all objects in session are serializable.This has to be consider as ?passed-by-value", that means that it?s read-only in the EJB. If anything is altered from inside the EJB, it won?t be reflected back to the HttpSession of the Servlet Container.The ?pass-by-reference? can be used between EJBs Remote Interfaces, as they are remote references. While it IS possible to pass an HttpSession as a parameter to an EJB object, it is considered to be ?bad practice ? in terms of object oriented design. This is because you are creating an unnecessary coupling between back-end objects (ejbs) and front-end objects (HttpSession). Create a higher-level of abstraction for your ejb?s api. Rather than passing the whole, fat, HttpSession (which carries with it a bunch of http semantics), create a class that acts as a value object (or structure) that holds all the data you need to pass back and forth between front-end/back-end. Consider the case where your ejb needs to support a non-http-based client. This higher level of abstraction will be flexible enough to support it. &lt;br /&gt;[ Received from Nishit Kamdar] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	The EJB container implements the EJBHome and EJBObject classes. For every request from a unique client, does the container create a separate instance of the generated EJBHome and EJBObject classes?&lt;br /&gt;A:	The EJB container maintains an instance pool. The container uses these instances for the EJB Home reference irrespective of the client request. while refering the EJB Object classes the container creates a separate instance for each client request. The instance pool maintainence is up to the implementation of the container. If the container provides one, it is available otherwise it is not mandatory for the provider to implement it. Having said that, yes most of the container providers implement the pooling functionality to increase the performance of the application server. The way it is implemented is again up to the implementer. &lt;br /&gt;[ Received from Vishal Khasgiwala ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can the primary key in the entity bean be a Java primitive type such as int? &lt;br /&gt;A:	The primary key can't be a primitive type--use the primitive wrapper classes, instead. For example, you can use java.lang.Integer as the primary key class, but not int (it has to be a class, not a primitive) &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can you control when passivation occurs? &lt;br /&gt;A:	The developer, according to the specification, cannot directly control when passivation occurs. Although for Stateful Session Beans, the container cannot passivate an instance that is inside a transaction. So using transactions can be a a strategy to control passivation.&lt;br /&gt;The ejbPassivate() method is called during passivation, so the developer has control over what to do during this exercise and can implement the require optimized logic.&lt;br /&gt;Some EJB containers, such as BEA WebLogic, provide the ability to tune the container to minimize passivation calls. &lt;br /&gt;Taken from the WebLogic 6.0 DTD -"The passivation-strategy can be either "default" or "transaction". With the default setting the container will attempt to keep a working set of beans in the cache. With the "transaction" setting, the container will passivate the bean after every transaction (or method call for a non-transactional invocation).&lt;br /&gt;[ Received from Prasanna Inamanamelluri ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the advantage of using Entity bean for database operations, over directly using JDBC API to do database operations? When would I use one over the other? &lt;br /&gt;A:	Entity Beans actually represents the data in a database. It is not that Entity Beans replaces JDBC API. There are two types of Entity Beans Container Managed and Bean Mananged. In Container Managed Entity Bean - Whenever the instance of the bean is created the container automatically retrieves the data from the DB/Persistance storage and assigns to the object variables in bean for user to manipulate or use them. For this the developer needs to map the fields in the database to the variables in deployment descriptor files (which varies for each vendor).&lt;br /&gt;In the Bean Managed Entity Bean - The developer has to specifically make connection, retrive values, assign them to the objects in the ejbLoad() which will be called by the container when it instatiates a bean object. Similarly in the ejbStore() the container saves the object values back the the persistance storage. ejbLoad and ejbStore are callback methods and can be only invoked by the container. Apart from this, when you use Entity beans you dont need to worry about database transaction handling, database connection pooling etc. which are taken care by the ejb container. But in case of JDBC you have to explicitly do the above features. what suresh told is exactly perfect. ofcourse, this comes under the database transations, but i want to add this. the great thing about the entity beans of container managed, whenever the connection is failed during the transaction processing, the database consistancy is mantained automatically. the container writes the data stored at persistant storage of the entity beans to the database again to provide the database consistancy. where as in jdbc api, we, developers has to do manually.&lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	  &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is EJB QL? &lt;br /&gt;A:	EJB QL is a Query Language provided for navigation across a network of enterprise beans and dependent objects defined by means of container managed persistence. EJB QL is introduced in the EJB 2.0 specification. The EJB QL query language defines finder methods for entity beans with container managed persistenceand is portable across containers and persistence managers. EJB QL is used for queries of two types of finder methods: Finder methods that are defined in the home interface of an entity bean and which return entity objects. Select methods, which are not exposed to the client, but which are used by the Bean Provider to select persistent values that are maintained by the Persistence Manager or to select entity objects that are related to the entity bean on which the query is defined. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Brief description about local interfaces? &lt;br /&gt;A:	EEJB was originally designed around remote invocation using the Java Remote Method Invocation (RMI) mechanism, and later extended to support to standard CORBA transport for these calls using RMI/IIOP. This design allowed for maximum flexibility in developing applications without consideration for the deployment scenario, and was a strong feature in support of a goal of component reuse in J2EE. &lt;br /&gt;Many developers are using EJBs locally -- that is, some or all of their EJB calls are between beans in a single container. &lt;br /&gt;With this feedback in mind, the EJB 2.0 expert group has created a local interface mechanism. The local interface may be defined for a bean during development, to allow streamlined calls to the bean if a caller is in the same container. This does not involve the overhead involved with RMI like marshalling etc. This facility will thus improve the performance of applications in which co-location is planned. &lt;br /&gt;Local interfaces also provide the foundation for container-managed relationships among entity beans with container-managed persistence. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the special design care that must be taken when you work with local interfaces? &lt;br /&gt;A:	EIt is important to understand that the calling semantics of local interfaces are different from those of remote interfaces. For example, remote interfaces pass parameters using call-by-value semantics, while local interfaces use call-by-reference. &lt;br /&gt;This means that in order to use local interfaces safely, application developers need to carefully consider potential deployment scenarios up front, then decide which interfaces can be local and which remote, and finally, develop the application code with these choices in mind. &lt;br /&gt;While EJB 2.0 local interfaces are extremely useful in some situations, the long-term costs of these choices, especially when changing requirements and component reuse are taken into account, need to be factored into the design decision.&lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What happens if remove( ) is never invoked on a session bean? &lt;br /&gt;A:	In case of a stateless session bean it may not matter if we call or not as in both cases nothing is done. The number of beans in cache is managed by the container. &lt;br /&gt;In case of stateful session bean, the bean may be kept in cache till either the session times out, in which case the bean is removed or when there is a requirement for memory in which case the data is cached and the bean is sent to free pool. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between Message Driven Beans and Stateless Session beans? &lt;br /&gt;A:	In several ways, the dynamic creation and allocation of message-driven bean instances mimics the behavior of stateless session EJB instances, which exist only for the duration of a particular method call. However, message-driven beans are different from stateless session EJBs (and other types of EJBs) in several significant ways:&lt;br /&gt;&lt;br /&gt;Message-driven beans process multiple JMS messages asynchronously, rather than processing a serialized sequence of method calls. &lt;br /&gt;Message-driven beans have no home or remote interface, and therefore cannot be directly accessed by internal or external clients. Clients interact with message-driven beans only indirectly, by sending a message to a JMS Queue or ic. &lt;br /&gt;Note: Only the container directly interacts with a message-driven bean by creating bean instances and passing JMS messages to those instances as necessary. &lt;br /&gt;The Container maintains the entire lifecycle of a message-driven bean; instances cannot be created or removed as a result of client requests or other API calls. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can I call one EJB from inside of another EJB? &lt;br /&gt;A:	EJBs can be clients of other EJBs. It just works. Use JNDI to locate the Home Interface of the other bean, then acquire an instance reference, and so forth. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is an EJB Context? &lt;br /&gt;A:	EJBContext is an interface that is implemented by the container, and it is also a part of the bean-container contract. Entity beans use a subclass of EJBContext called EntityContext. Session beans use a subclass called SessionContext. These EJBContext objects provide the bean class with information about its container, the client using the bean and the bean itself. They also provide other functions. See the API docs and the spec for more details. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	The EJB container implements the EJBHome and EJBObject classes. For every request from a unique client, does the container create a separate instance of the generated EJBHome and EJBObject classes?&lt;br /&gt;A:	The EJB container maintains an instance pool. The container uses these instances for the EJB Home reference irrespective of the client request. While refering the EJB Object classes the container creates a separate instance for each client request. The instance pool maintainence is up to the implementation of the container. If the container provides one, it is available otherwise it is not mandatory for the provider to implement it. Having said that, yes most of the container providers implement the pooling functionality to increase the performance of the application server. The way it is implemented is again up to the implementer.&lt;br /&gt;[ Received from Vishal Khasgiwala ] 	 &lt;br /&gt;&lt;br /&gt;Q:	What is JMS?&lt;br /&gt;A:	JMS is an acronym used for Java Messaging Service. It is Java's answer to creating software using asynchronous messaging. It is one of the official specifications of the J2EE technologies and is a key technology.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How JMS is different from RPC?&lt;br /&gt;A:	In RPC the method invoker waits for the method to finish execution and return the control back to the invoker. Thus it is completely synchronous in nature. While in JMS the message sender just sends the message to the destination and continues it's own processing. The sender does not wait for the receiver to respond. This is asynchronous behavior.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the advantages of JMS?&lt;br /&gt;A:	JMS is asynchronous in nature. Thus not all the pieces need to be up all the time for the application to function as a whole. Even if the receiver is down the MOM will store the messages on it's behalf and will send them once it comes back up. Thus at least a part of application can still function as there is no blocking.   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Are you aware of any major JMS products available in the market? &lt;br /&gt;A:	IBM's MQ Series is one of the most popular product used as Message Oriented Middleware. Some of the other products are SonicMQ, iBus etc.All the J2EE compliant application servers come built with thier own implementation of JMS. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the different types of messages available in the JMS API? &lt;br /&gt;A:	Message, TextMessage, BytesMessage, StreamMessage, ObjectMessage, MapMessage are the different messages available in the JMS API. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the different messaging paradigms JMS supports?&lt;br /&gt;A:	Publish and Subscribe i.e. pub/suc and Point to Point i.e. p2p.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between ic and queue?&lt;br /&gt;A:	A ic is typically used for one to many messaging i.e. it supports publish subscribe model of messaging. While queue is used for one-to-one messaging i.e. it supports Point to Point Messaging.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the role of JMS in enterprise solution development?&lt;br /&gt;A:	JMS is typically used in the following scenarios&lt;br /&gt;1. Enterprise Application Integration: - Where a legacy application is integrated with a new application via messaging.&lt;br /&gt;2. B2B or Business to Business: - Businesses can interact with each other via messaging because JMS allows organizations to cooperate without tightly coupling their business systems.&lt;br /&gt;3. Geographically dispersed units: - JMS can ensure safe exchange of data amongst the geographically dispersed units of an organization.&lt;br /&gt;4. One to many applications: - The applications that need to push data in packet to huge number of clients in a one-to-many fashion are good candidates for the use JMS. Typical such applications are Auction Sites, Stock Quote Services etc.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the use of Message object?&lt;br /&gt;A:	Message is a light weight message having only header and properties and no payload. Thus if theIf the receivers are to be notified abt an event, and no data needs to be exchanged then using Message can be very efficient.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the basic difference between Publish Subscribe model and P2P model?&lt;br /&gt;A:	Publish Subscribe model is typically used in one-to-many situation. It is unreliable but very fast. P2P model is used in one-to-one situation. It is highly reliable.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the use of BytesMessage?&lt;br /&gt;A:	BytesMessage contains an array of primitive bytes in it's payload. Thus it can be used for transfer of data between two applications in their native format which may not be compatible with other Message types. It is also useful where JMS is used purely as a transport between two systems and the message payload is opaque to the JMS client. Whenever you store any primitive type, it is converted into it's byte representation and then stored in the payload. There is no boundary line between the different data types stored. Thus you can even read a long as short. This would result in erroneous data and hence it is advisable that the payload be read in the same order and using the same type in which it was created by the sender.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the use of StreamMessage?&lt;br /&gt;A:	StreamMessage carries a stream of Java primitive types as it's payload. It contains some conveient methods for reading the data stored in the payload. However StreamMessage prevents reading a long value as short, something that is allwed in case of BytesMessage. This is so because the StreamMessage also writes the type information alonwgith the value of the primitive type and enforces a set of strict conversion rules which actually prevents reading of one primitive type as another. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the use of TextMessage?&lt;br /&gt;A:	TextMessage contains instance of java.lang.String as it's payload. Thus it is very useful for exchanging textual data. It can also be used for exchanging complex character data such as an XML document.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the use of ObjectMessage?&lt;br /&gt;A:	ObjectMessage contains a Serializable java object as it's payload. Thus it allows exchange of Java objects between applications. This in itself mandates that both the applications be Java applications. The consumer of the message must typecast the object received to it's appropriate type. Thus the consumer should before hand know the actual type of the object sent by the sender. Wrong type casting would result in ClassCastException. Moreover the class definition of the object set in the payload should be available on both the machine, the sender as well as the consumer. If the class definition is not available in the consumer machine, an attempt to type cast would result in ClassNotFoundException. Some of the MOMs might support dynamic loading of the desired class over the network, but the JMS specification does not mandate this behavior and would be a value added service if provided by your vendor. And relying on any such vendor specific functionality would hamper the portability of your application. Most of the time the class need to be put in the classpath of both, the sender and the consumer, manually by the developer.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the use of MapMessage?&lt;br /&gt;A:	A MapMessage carries name-value pair as it's payload. Thus it's payload is similar to the java.util.Properties object of Java. The values can be Java primitives or their wrappers.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference between BytesMessage and StreamMessage??&lt;br /&gt;A:	BytesMessage stores the primitive data types by converting them to their byte representation. Thus the message is one contiguous stream of bytes. While the StreamMessage maintains a boundary between the different data types stored because it also stores the type information along with the value of the primitive being stored. BytesMessage allows data to be read using any type. Thus even if your payload contains a long value, you can invoke a method to read a short and it will return you something. It will not give you a semantically correct data but the call will succeed in reading the first two bytes of data. This is strictly prohibited in the StreamMessage. It maintains the type information of the data being stored and enforces strict conversion rules on the data being read.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is point-to-point messaging?&lt;br /&gt;A:	With point-to-point message passing the sending application/client establishes a named message queue in the JMS broker/server and sends messages to this queue. The receiving client registers with the broker to receive messages posted to this queue. There is a one-to-one relationship between the sending and receiving clients. &lt;br /&gt;[ Received from Prasanna Inamanamelluri ] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Can two different JMS services talk to each other? For instance, if A and B are two different JMS providers, can Provider A send messages directly to Provider B? If not, then can a subscriber to Provider A act as a publisher to Provider B? &lt;br /&gt;A:	The answers are no to the first question and yes to the second. The JMS specification does not require that one JMS provider be able to send messages directly to another provider. However, the specification does require that a JMS client must be able to accept a message created by a different JMS provider, so a message received by a subscriber to Provider A can then be published to Provider B. One caveat is that the publisher to Provider B is not required to handle a JMSReplyTo header that refers to a destination that is specific to Provider A. &lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the advantage of persistent message delivery compared to nonpersistent delivery? &lt;br /&gt;A:	If the JMS server experiences a failure, for example, a power outage, any message that it is holding in primary storage potentially could be lost. With persistent storage, the JMS server logs every message to secondary storage. (The logging occurs on the front end, that is, as part of handling the send operation from the message producing client.) The logged message is removed from secondary storage only after it has been successfully delivered to all consuming clients .   &lt;br /&gt;[ Received from Prasanna Inamanamelluri] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Give an example of using the publish/subscribe model. &lt;br /&gt;A:	JMS can be used to broadcast shutdown messages to clients connected to the Weblogic server on a module wise basis. If an application has six modules, each module behaves like a subscriber to a named ic on the server. &lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Why doesn't the JMS API provide end-to-end synchronous message delivery and notification of delivery? &lt;br /&gt;A:	Some messaging systems provide synchronous delivery to destinations as a mechanism for implementing reliable applications. Some systems provide clients with various forms of delivery notification so that the clients can detect dropped or ignored messages. This is not the model defined by the JMS API. &lt;br /&gt;&lt;br /&gt;JMS API messaging provides guaranteed delivery via the once-and-only-once delivery semantics of PERSISTENT messages. In addition, message consumers can ensure reliable processing of messages by using either CLIENT_ACKNOWLEDGE mode or transacted sessions. This achieves reliable delivery with minimum synchronization and is the enterprise messaging model most vendors and developers prefer. &lt;br /&gt;&lt;br /&gt;The JMS API does not define a schema of systems messages (such as delivery notifications). If an application requires acknowledgment of message receipt, it can define an application-level acknowledgment message. &lt;br /&gt;&lt;br /&gt;Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the various message types supported by JMS? &lt;br /&gt;A:	Stream Messages ? Group of Java Primitives&lt;br /&gt;Map Messages ? Name Value Pairs. Name being a string&amp; Value being a java primitive &lt;br /&gt;Text Messages ? String messages (since being widely used a separate messaging Type has been supported) &lt;br /&gt;Object Messages ? Group of serialize able java object &lt;br /&gt;Bytes Message ? Stream of uninterrupted bytes &lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How is a java object message delivered to a non-java Client? &lt;br /&gt;A:	It is according to the specification that the message sent should be received in the same format. A non-java client cannot receive a message in the form of java object. The provider in between handles the conversion of the data type and the message is transferred to the other end. &lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is MDB and What is the special feature of that? &lt;br /&gt;A:	MDB is Message driven bean, which very much resembles the Stateless session bean. The incoming and out going messages can be handled by the Message driven bean. The ability to communicate asynchronously is the special feature about the Message driven bean.&lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the types of messaging? &lt;br /&gt;A:	There are two kinds of Messaging.&lt;br /&gt;Synchronous Messaging: Synchronous messaging involves a client that waits for the server to respond to a message. &lt;br /&gt;Asynchronous Messaging: Asynchronous messaging involves a client that does not wait for a message from the server. An event is used to trigger a message from a server. &lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the core JMS-related objects required for each JMS-enabled application? &lt;br /&gt;A:	: Each JMS-enabled client must establish the following:&lt;br /&gt;• A connection object provided by the JMS server (the message broker) &lt;br /&gt;• Within a connection, one or more sessions, which provide a context for message sending and receiving &lt;br /&gt;• Within a session, either a queue or ic object representing the destination (the message staging area) within the message broker &lt;br /&gt;• Within a session, the appropriate sender or publisher or receiver or subscriber object (depending on whether the client is a message producer or consumer and uses a point-to-point or publish/subscribe strategy, respectively) &lt;br /&gt;Within a session, a message object (to send or to receive) &lt;br /&gt;.&lt;br /&gt;[ Received from Prasanna Inamanamelluri]&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Struts?&lt;br /&gt;A:	The core of the Struts framework is a flexible control layer based on standard technologies like Java Servlets, JavaBeans, ResourceBundles, and XML, as well as various Jakarta Commons packages. Struts encourages application architectures based on the Model 2 approach, a variation of the classic Model-View-Controller (MVC) design paradigm. &lt;br /&gt;Struts provides its own Controller component and integrates with other technologies to provide the Model and the View. For the Model, Struts can interact with standard data access technologies, like JDBC and EJB, as well as most any third-party packages, like Hibernate, iBATIS, or Object Relational Bridge. For the View, Struts works well with JavaServer Pages, including JSTL and JSF, as well as Velocity Templates, XSLT, and other presentation systems. &lt;br /&gt;The Struts framework provides the invisible underpinnings every professional web application needs to survive. Struts helps you create an extensible development environment for your application, based on published standards and proven design patterns. &lt;br /&gt;[ Received from Ramakrishna Potluri ] 	 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Jakarta Struts Framework?&lt;br /&gt;A:	Jakarta Struts is open source implementation of MVC (Model-View-Controller) pattern for the development of web based applications. Jakarta Struts is robust architecture and can be used for the development of application of any size. Struts framework makes it much easier to design scalable, reliable Web applications with Java.&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is ActionServlet?&lt;br /&gt;A:	The class org.apache.struts.action.ActionServlet is the called the ActionServlet. In the the Jakarta Struts Framework this class plays the role of controller. All the requests to the server goes through the controller. Controller is responsible for handling all the requests.&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How you will make available any Message Resources Definitions file to the Struts Framework Environment?&lt;br /&gt;A:	T Message Resources Definitions file are simple .properties files and these files contains the messages that can be used in the struts project. Message Resources Definitions files can be added to the struts-config.xml file through &lt;message-resources /&gt; tag. &lt;br /&gt;Example:&lt;br /&gt;&lt;message-resources parameter=\"MessageResources\" /&gt;.&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Action Class?&lt;br /&gt;A:	The Action Class is part of the Model and is a wrapper around the business logic. The purpose of Action Class is to translate the HttpServletRequest to the business logic. To use the Action, we need to Subclass and overwrite the execute() method. In the Action Class all the database/business processing are done. It is advisable to perform all the database related stuffs in the Action Class. The ActionServlet (commad) passes the parameterized class to Action Form using the execute() method. The return type of the execute method is ActionForward which is used by the Struts Framework to forward the request to the file as per the value of the returned ActionForward object. &lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is ActionForm?&lt;br /&gt;A:	An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm. ActionForm maintains the session state for web application and the ActionForm object is automatically populated on the server side with data entered from a form on the client side. &lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is Struts Validator Framework?&lt;br /&gt;A:	Struts Framework provides the functionality to validate the form data. It can be use to validate the data on the users browser as well as on the server side. Struts Framework emits the java scripts and it can be used validate the form data on the client browser. Server side validation of form can be accomplished by sub classing your From Bean with DynaValidatorForm class. &lt;br /&gt;The Validator framework was developed by David Winterfeldt as third-party add-on to Struts. Now the Validator framework is a part of Jakarta Commons project and it can be used with or without Struts. The Validator framework comes integrated with the Struts Framework and can be used without doing any extra settings.&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Give the Details of XML files used in Validator Framework?&lt;br /&gt;A:	The Validator Framework uses two XML configuration files validator-rules.xml and validation.xml. The validator-rules.xml defines the standard validation routines, these are reusable and used in validation.xml. to define the form specific validations. The validation.xml defines the validations applied to a form bean.&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How you will display validation fail errors on jsp page?&lt;br /&gt;A:	Following tag displays all the errors: &lt;br /&gt;&lt;html:errors/&gt;&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How you will enable front-end validation based on the xml in validation.xml?&lt;br /&gt;A:	The &lt;html:javascript&gt; tag to allow front-end validation based on the xml in validation.xml. For example the code: &lt;html:javascript formName=\"logonForm\" dynamicJavascript=\"true\" staticJavascript=\"true\" /&gt; generates the client side java script for the form \"logonForm\" as defined in the validation.xml file. The &lt;html:javascript&gt; when added in the jsp file generates the client site validation script.&lt;br /&gt;[ Received from Dhiraj Sharma] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How to get data from the velocity page in a action class?&lt;br /&gt;A:	We can get the values in the action classes by using data.getParameter(\"variable name defined in the velocity page\");&lt;br /&gt;[ Received from Shri Prakash Kunwar] &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is SQL? &lt;br /&gt;A:	SQL stands for 'Structured Query Language'.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is SELECT statement?&lt;br /&gt;A:	The SELECT statement lets you select a set of values from a table in a database. The values selected from the database table would depend on the various conditions that are specified in the SQL query.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can you compare a part of the name rather than the entire name?&lt;br /&gt;A:	SELECT * FROM people WHERE empname LIKE '%ab%'&lt;br /&gt;Would return a recordset with records consisting empname the sequence 'ab' in empname .&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the INSERT statement? &lt;br /&gt;A:	The INSERT statement lets you insert information into a database.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How do you delete a record from a database? &lt;br /&gt;A:	Use the DELETE statement to remove records or any particular column values from a database.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How could I get distinct entries from a table?&lt;br /&gt;A:	The SELECT statement in conjunction with DISTINCT lets you select a set of distinct values from a table in a database. The values selected from the database table would of course depend on the various conditions that are specified in the SQL query. Example&lt;br /&gt;SELECT DISTINCT empname FROM emptable &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How to get the results of a Query sorted in any order?&lt;br /&gt;A:	You can sort the results and return the sorted results to your program by using ORDER BY keyword thus saving you the pain of carrying out the sorting yourself. The ORDER BY keyword is used for sorting.&lt;br /&gt;&lt;br /&gt;SELECT empname, age, city FROM emptable ORDER BY empname&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	How can I find the total number of records in a table?&lt;br /&gt;A:	You could use the COUNT keyword , example&lt;br /&gt;&lt;br /&gt;SELECT COUNT(*) FROM emp WHERE age&gt;40 &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is GROUP BY?&lt;br /&gt;A:	The GROUP BY keywords have been added to SQL because aggregate functions (like SUM) return the aggregate of all column values every time they are called. Without the GROUP BY functionality, finding the sum for each individual group of column values was not possible. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is the difference among "dropping a table", "truncating a table" and "deleting all records" from a table.&lt;br /&gt;A:	Dropping :  (Table structure  + Data are deleted), Invalidates the dependent objects ,Drops the indexes &lt;br /&gt;Truncating:  (Data alone deleted), Performs an automatic commit, Faster than delete&lt;br /&gt;Delete : (Data alone deleted), Doesn’t perform automatic commit&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are the Large object types suported by Oracle? &lt;br /&gt;A:	Blob and Clob.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	Difference between a "where" clause and a "having" clause.&lt;br /&gt;A:	Having clause is used only with group functions whereas Where is not used with.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What's the difference between a primary key and a unique key? &lt;br /&gt;A:	Both primary key and unique enforce uniqueness of the column on which they are defined. But by default primary key creates a clustered index on the column, where are unique creates a nonclustered index by default. Another major difference is that, primary key doesn't allow NULLs, but unique key allows one NULL only.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are cursors? Explain different types of cursors. What are the disadvantages of cursors? How can you avoid cursors? &lt;br /&gt;A:	Cursors allow row-by-row prcessing of the resultsets.&lt;br /&gt;Types of cursors: Static, Dynamic, Forward-only, Keyset-driven. See books online for more information.&lt;br /&gt;Disadvantages of cursors: Each time you fetch a row from the cursor, it results in a network roundtrip, where as a normal SELECT query makes only one rowundtrip, however large the resultset is. Cursors are also costly because they require more resources and temporary storage (results in more IO operations). Furthere, there are restrictions on the SELECT statements that can be used with some types of cursors.&lt;br /&gt;Most of the times, set based operations can be used instead of cursors. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What are triggers? How to invoke a trigger on demand? &lt;br /&gt;A:	Triggers are special kind of stored procedures that get executed automatically when an INSERT, UPDATE or DELETE operation takes place on a table. &lt;br /&gt;Triggers can't be invoked on demand. They get triggered only when an associated action (INSERT, UPDATE, DELETE) happens on the table on which they are defined.&lt;br /&gt;Triggers are generally used to implement business rules, auditing. Triggers can also be used to extend the referential integrity checks, but wherever possible, use constraints for this purpose, instead of triggers, as constraints are much faster.&lt;br /&gt;&lt;br /&gt;Q:	What is a join and explain different types of joins. &lt;br /&gt;A:	Joins are used in queries to explain how different tables are related. Joins also let you select data from a table depending upon data from another table. &lt;br /&gt;Types of joins: INNER JOINs, OUTER JOINs, CROSS JOINs. OUTER JOINs are further classified as LEFT OUTER JOINS, RIGHT OUTER JOINS and FULL OUTER JOINS.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Q:	What is a self join?&lt;br /&gt;A:	Self join is just like any other join, except that two instances of the same table will be joined in the query. &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Question: How could Java classes direct program messages to the system console, but error messages, say to a file? &lt;br /&gt;Answer: The class System has a variable out that represents the standard output, and the variable err that represents the standard error device. By default, they both point at the system console. This how the standard output could be re-directed: &lt;br /&gt;Stream st = new Stream(new FileOutputStream("output.txt")); System.setErr(st); System.setOut(st);   &lt;br /&gt;Question: What's the difference between an interface and an abstract class? &lt;br /&gt;Answer: An abstract class may contain code in method bodies, which is not allowed in an interface. With abstract classes, you have to inherit your class from it and Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class. &lt;br /&gt;Question: Why would you use a synchronized block vs. synchronized method? &lt;br /&gt;Answer: Synchronized blocks place locks for shorter periods than synchronized methods. &lt;br /&gt;Question: Explain the usage of the keyword transient? &lt;br /&gt;Answer: This keyword indicates that the value of this member variable does not have to be serialized with the object. When the class will be de-serialized, this variable will be initialized with a default value of its data type (i.e. zero for integers).&lt;br /&gt;&lt;br /&gt;Question: How can you force garbage collection? &lt;br /&gt;Answer: You can't force GC, but could request it by calling System.gc(). JVM does not guarantee that GC will be started immediately. &lt;br /&gt;&lt;br /&gt;Question: How do you know if an explicit object casting is needed? &lt;br /&gt;Answer: If you assign a superclass object to a variable of a subclass's data type, you need to do explicit casting. For example: &lt;br /&gt;Object a; Customer b; b = (Customer) a; &lt;br /&gt;When you assign a subclass to a variable having a supeclass type, the casting is performed automatically.  &lt;br /&gt;Question: What's the difference between the methods sleep() and wait() &lt;br /&gt;Answer: The code sleep(1000); puts thread aside for exactly one second. The code wait(1000), causes a wait of up to one second. A thread could s waiting earlier if it receives the notify() or notifyAll() call. The method wait() is defined in the class Object and the method sleep() is defined in the class Thread. &lt;br /&gt;Question: Can you write a Java class that could be used both as an applet as well as an application? &lt;br /&gt;Answer: Yes. Add a main() method to the applet. &lt;br /&gt;Question: What's the difference between constructors and other methods? &lt;br /&gt;Answer: Constructors must have the same name as the class and can not return a value. They are only called once while regular methods could be called many times. &lt;br /&gt;Question: Can you call one constructor from another if a class has multiple constructors &lt;br /&gt;Answer: Yes. Use this() syntax. &lt;br /&gt;Question: Explain the usage of Java packages. &lt;br /&gt;Answer: This is a way to organize files when a project consists of multiple modules. It also helps resolve naming conflicts when different packages have classes with the same names. Packages access level also allows you to protect data from being used by the non-authorized classes. &lt;br /&gt;Question: If a class is located in a package, what do you need to change in the OS environment to be able to use it? &lt;br /&gt;Answer: You need to add a directory or a jar file that contains the package directories to the CLASSPATH environment variable. Let's say a class Employee belongs to a package com.xyz.hr; and is located in the file c:\dev\com\xyz\hr\Employee.java. In this case, you'd need to add c:\dev to the variable CLASSPATH. If this class contains the method main(), you could test it from a command prompt window as follows: &lt;br /&gt;c:\&gt;java com.xyz.hr.Employee &lt;br /&gt;Question: What's the difference between J2SDK 1.5 and J2SDK 5.0? &lt;br /&gt;Answer:  There's no difference, Sun Microsystems just re-branded this version. &lt;br /&gt;&lt;br /&gt;Question: What would you use to compare two String variables - the operator == or the method equals()? &lt;br /&gt;Answer:  I'd use the method equals() to compare the values of the Strings and the == to check if two variables point at the same instance of a String object.&lt;br /&gt;&lt;br /&gt;Question: Does it matter in what order catch statements for FileNotFoundException and IOExceptipon are written? &lt;br /&gt;Answer: Yes, it does. The FileNoFoundException is inherited from the IOException. Exception's subclasses have to be caught first. &lt;br /&gt;Question: Can an inner class declared inside of a method access local variables of this method? &lt;br /&gt;Answer: It's possible if these variables are final. &lt;br /&gt;Question: What can go wrong if you replace &amp;&amp; with &amp; in the following code: String a=null; if (a!=null &amp;&amp; a.length()&gt;10) {...} &lt;br /&gt;Answer: A single ampersand here would lead to a NullPointerException. &lt;br /&gt;Question: What's the main difference between a Vector and an ArrayList &lt;br /&gt;Answer: Java Vector class is internally synchronized and ArrayList is not.&lt;br /&gt;&lt;br /&gt;Question: When should the method invokeLater()be used?&lt;br /&gt;Answer: This method is used to ensure that Swing components are updated through the event-dispatching thread. &lt;br /&gt;Question: How can a subclass call a method or a constructor defined in a superclass? &lt;br /&gt;Answer: Use the following syntax: super.myMethod(); To call a constructor of the superclass, just write super(); in the first line of the subclass's constructor. &lt;br /&gt;For senior-level developers: &lt;br /&gt;Question: What's the difference between a queue and a stack? &lt;br /&gt;Answer: Stacks works by last-in-first-out rule (LIFO), while queues use the FIFO rule &lt;br /&gt;Question: You can create an abstract class that contains only abstract methods. On the other hand, you can create an interface that declares the same methods. So can you use abstract classes instead of interfaces? &lt;br /&gt;Answer: Sometimes. But your class may be a descendent of another class and in this case the interface is your only option. &lt;br /&gt;Question: What comes to mind when you hear about a young generation in Java? &lt;br /&gt;Answer: Garbage collection. &lt;br /&gt;Question: What comes to mind when someone mentions a shallow copy in Java? &lt;br /&gt;Answer: Object cloning. &lt;br /&gt;Question: If you're overriding the method equals() of an object, which other method you might also consider? &lt;br /&gt;Answer: hashCode() &lt;br /&gt;Question: You are planning to do an indexed search in a list of objects. Which of the two Java collections should you use: ArrayList or LinkedList? &lt;br /&gt;Answer: ArrayList &lt;br /&gt;Question: How would you make a copy of an entire Java object with its state? &lt;br /&gt;Answer: Have this class implement Cloneable interface and call its method clone(). &lt;br /&gt;Question: How can you minimize the need of garbage collection and make the memory use more effective? &lt;br /&gt;Answer: Use object pooling and weak object references. &lt;br /&gt;Question: There are two classes: A and B. The class B need to inform a class A when some important event has happened. What Java technique would you use to implement it? &lt;br /&gt;Answer: If these classes are threads I'd consider notify() or notifyAll(). For regular classes you can use the Observer interface. &lt;br /&gt;Question: What access level do you need to specify in the class declaration to ensure that only classes from the same directory can access it? &lt;br /&gt;Answer: You do not need to specify any access level, and Java will use a default package access level .&lt;br /&gt;&lt;br /&gt;Question: When you declare a method as abstract method ? &lt;br /&gt;Answer: When i want child class to implement the behavior of the method. &lt;br /&gt;Question: Can I call a abstract method from a non abstract method ?  &lt;br /&gt;Answer: Yes, We can call a abstract method from a Non abstract method in a Java abstract class          &lt;br /&gt;Question: What is the difference between an Abstract class and Interface in Java ? or can you explain when you use Abstract classes ?  &lt;br /&gt;Answer: Abstract classes let you define some behaviors; they force your subclasses to provide others. These abstract classes will provide the basic funcationality of your applicatoin, child class which inherited this class will provide the funtionality of the abstract methods in abstract class. When base class calls this method, Java calls the method defined by the child class. &lt;br /&gt;•	An Interface can only declare constants and instance methods, but cannot implement default behavior. &lt;br /&gt;•	Interfaces provide a form of multiple inheritance. A class can extend only one other class. &lt;br /&gt;•	Interfaces are limited to public methods and constants with no implementation. Abstract classes can have a partial implementation, protected parts, static methods, etc. &lt;br /&gt;•	A Class may implement several interfaces. But in case of abstract class, a class may extend only one abstract class. &lt;br /&gt;•	Interfaces are slow as it requires extra indirection to find corresponding method in the actual class. Abstract classes are fast. &lt;br /&gt;Question: What is user-defined exception in java ?  &lt;br /&gt;Answer: User-defined expections are the exceptions defined by the application developer which are errors related to specific application. Application Developer can define the user defined exception by inherite the Exception class as shown below. Using this class we can throw new exceptions. &lt;br /&gt;Java Example : public class noFundException extends Exception { } Throw an exception using a throw statement: public class Fund { ... public Object getFunds() throws noFundException { if (Empty()) throw new noFundException(); ... } } User-defined exceptions should usually be checked&lt;br /&gt;&lt;br /&gt;Question: What is the difference between checked and Unchecked Exceptions in Java ? &lt;br /&gt;Answer: All predefined exceptions in Java are either a checked exception or an unchecked exception. Checked exceptions must be caught using try .. catch() block or we should throw the exception using throws clause. If you dont, compilation of program will fail. &lt;br /&gt;Java Exception Hierarchy +--------+ | Object | +--------+ | | +-----------+ | Throwable | +-----------+ / \ / \ +-------+ +-----------+ | Error | | Exception | +-------+ +-----------+ / | \ / | \ \________/ \______/ \ +------------------+ unchecked checked | RuntimeException | +------------------+ / | | \ \_________________/ unchecked &lt;br /&gt;Question: Explain garbage collection ?  &lt;br /&gt;Answer: Garbage collection is an important part of Java's security strategy. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects from the memory. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed. &lt;br /&gt;Question: How you can force the garbage collection ? &lt;br /&gt;Answer: Garbage collection automatic process and can't be forced. We can call garbage collector in Java by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. &lt;br /&gt;Question: What are the field/method access levels (specifiers) and class access levels ? &lt;br /&gt;Answer: Each field and method has an access level: &lt;br /&gt;•	private: accessible only in this class &lt;br /&gt;•	(package): accessible only in this package &lt;br /&gt;•	protected: accessible only in this package and in all subclasses of this class &lt;br /&gt;•	public: accessible everywhere this class is available &lt;br /&gt;Similarly, each class has one of two possible access levels: &lt;br /&gt;•	(package): class objects can only be declared and manipulated by code in this package &lt;br /&gt;•	public: class objects can be declared and manipulated by code in any package &lt;br /&gt;Question: What are the static fields &amp; static Methods ?  &lt;br /&gt;Answer: If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static method &lt;br /&gt;&lt;br /&gt;Example Java Code &lt;br /&gt;&lt;br /&gt;static int counter = 0; &lt;br /&gt;&lt;br /&gt;A public static field or method can be accessed from outside the class using either the usual notation: &lt;br /&gt;&lt;br /&gt;Java-class-object.field-or-method-name &lt;br /&gt;&lt;br /&gt;or using the class name instead of the name of the class object: &lt;br /&gt;&lt;br /&gt;Java- class-name.field-or-method-name &lt;br /&gt;Question: What are the Final fields &amp; Final Methods ? &lt;br /&gt;Answer: Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned to again. &lt;br /&gt;&lt;br /&gt;Java Code &lt;br /&gt;&lt;br /&gt;private static final int MAXATTEMPTS = 10; &lt;br /&gt;Question: Describe the wrapper classes in Java ? &lt;br /&gt;Answer: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type. &lt;br /&gt;&lt;br /&gt;Following table lists the primitive types and the corresponding wrapper classes: &lt;br /&gt;Primitive	Wrapper &lt;br /&gt;boolean 	java.lang.Boolean &lt;br /&gt;byte 	java.lang.Byte &lt;br /&gt;char 	java.lang.Character &lt;br /&gt;double 	java.lang.Double &lt;br /&gt;float 	java.lang.Float &lt;br /&gt;int 	java.lang.Integer &lt;br /&gt;long 	java.lang.Long &lt;br /&gt;short 	java.lang.Short &lt;br /&gt;void 	java.lang.Void &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Question: What are different types of inner classes ? &lt;br /&gt;Answer: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types &lt;br /&gt;•	Static member classes &lt;br /&gt;•	Member classes &lt;br /&gt;•	Local classes &lt;br /&gt;•	Anonymous classes &lt;br /&gt;Static member classes - a static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or -level, class. &lt;br /&gt;&lt;br /&gt;Member Classes - a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference. &lt;br /&gt;&lt;br /&gt;Local Classes - Local Classes declared within a block of code and these classes are visible only within the block. &lt;br /&gt;&lt;br /&gt;Anonymous Classes - These type of classes does not have any name and its like a local class &lt;br /&gt;Java Anonymous Class Example public class SomeGUI extends JFrame { ... button member declarations ... protected void buildGUI() { button1 = new JButton(); button2 = new JButton(); ... button1.addActionListener( new java.awt.event.ActionListener() &lt;------ Anonymous Class { public void actionPerformed(java.awt.event.ActionEvent e) { // do something } } ); Question: What are the uses of Serialization? Answer: In some types of applications you have to write the code to serialize objects, but in many cases serialization is performed behind the scenes by various server-side containers. These are some of the typical uses of serialization: •	To persist data for future use. •	To send data to a remote computer using such client/server Java technologies as RMI or socket programming. •	To "flatten" an object into array of bytes in memory. •	To exchange data between applets and servlets. •	To store user session in Web applications. •	To activate/passivate enterprise java beans. •	To send objects between the servers in a cluster. Question: what is a collection ? Answer: Collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs Eg. HashMap and Hashtable. Question: For concatenation of strings, which method is good, StringBuffer or String ? Answer: StringBuffer is faster than String for concatenation. Question: What is Runnable interface ? Are there any other ways to make a java program as multithred java program?  Answer: There are two ways to create new kinds of threads: - Define a new class that extends the Thread class - Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor. - An advantage of the second approach is that the new class can be a subclass of any class, not just of the Thread class. Here is a very simple example just to illustrate how to use the second approach to creating threads: class myThread implements Runnable { public void run() { System.out.println("I'm running!"); } } public class tstRunnable { public static void main(String[] args) { myThread my1 = new myThread(); myThread my2 = new myThread(); new Thread(my1).start(); new Thread(my2).start(); }Question: How can i tell what state a thread is in ?  Answer: Prior to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two. Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States. A thread can only be in one of the following states at a given point in time. NEW 	A Fresh thread that has not yet started to execute. RUNNABLE 	A thread that is executing in the Java virtual machine. BLOCKED 	A thread that is blocked waiting for a monitor lock. WAITING 	A thread that is wating to be notified by another thread. TIMED_WAITING 	A thread that is wating to be notified by another thread for a specific amount of time TERMINATED 	A thread whos run method has ended. The folowing code prints out all thread states. public class ThreadStates{ public static void main(String[] args){ Thread t = new Thread(); Thread.State e = t.getState(); Thread.State[] ts = e.values(); for(int i = 0; i &lt; ts.length; i++){ System.out.println(ts[i]); } } } Question: What methods java providing for Thread communications ?  Answer: Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll. These methods are defined for all Objects (not just Threads). The idea is that a method called by a thread may need to wait for some condition to be satisfied by another thread; in that case, it can call the wait method, which causes its thread to wait until another thread calls notify or notifyAll. Question: What is the difference between notify and notify All methods ? Answer: A call to notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait). A call to notifyAll causes all threads waiting on the same object to be notified. If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify (so it is often better to use notifyAll than notify). Question: What is synchronized keyword? In what situations you will Use it? Answer: Synchronization is the act of serializing access to critical sections of code. We will use this keyword when we expect multiple threads to access/modify the same data. To understand synchronization we need to look into thread execution manner. Threads may execute in a manner where their paths of execution are completely independent of each other. Neither thread depends upon the other for assistance. For example, one thread might execute a print job, while a second thread repaints a window. And then there are threads that require synchronization, the act of serializing access to critical sections of code, at various moments during their executions. For example, say that two threads need to send data packets over a single network connection. Each thread must be able to send its entire data packet before the other thread starts sending its data packet; otherwise, the data is scrambled. This scenario requires each thread to synchronize its access to the code that does the actual data-packet sending. If you feel a method is very critical for business that needs to be executed by only one thread at a time (to prevent data loss or corruption), then we need to use synchronized keyword. EXAMPLESome real-world tasks are better modeled by a program that uses threads than by a normal, sequential program. For example, consider a bank whose accounts can be accessed and updated by any of a number of automatic teller machines (ATMs). Each ATM could be a separate thread, responding to deposit and withdrawal requests from different users simultaneously. Of course, it would be important to make sure that two users did not access the same account simultaneously. This is done in Java using synchronization, which can be applied to individual methods, or to sequences of statements. One or more methods of a class can be declared to be synchronized. When a thread calls an object's synchronized method, the whole object is locked. This means that if another thread tries to call any synchronized method of the same object, the call will block until the lock is released (which happens when the original call finishes). In general, if the value of a field of an object can be changed, then all methods that read or write that field should be synchronized to prevent two threads from trying to write the field at the same time, and to prevent one thread from reading the field while another thread is in the process of writing it. Here is an example of a BankAccount class that uses synchronized methods to ensure that deposits and withdrawals cannot be performed simultaneously, and to ensure that the account balance cannot be read while either a deposit or a withdrawal is in progress. (To keep the example simple, no check is done to ensure that a withdrawal does not lead to a negative balance.) public class BankAccount { private double balance; // constructor: set balance to given amount public BankAccount( double initialDeposit ) { balance = initialDeposit; } public synchronized double Balance( ) { return balance; } public synchronized void Deposit( double deposit ) { balance += deposit; } public synchronized void Withdraw( double withdrawal ) { balance -= withdrawal; } } Note: that the BankAccount's constructor is not declared to be synchronized. That is because it can only be executed when the object is being created, and no other method can be called until that creation is finished. There are cases where we need to synchronize a group of statements, we can do that using synchrozed statement. Java Code Example synchronized ( B ) { if ( D &gt; B.Balance() ) { ReportInsuffucientFunds(); } else { B.Withdraw( D ); } }&lt;br /&gt;&lt;br /&gt;Question: What is serialization ?  &lt;br /&gt;Answer: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket. &lt;br /&gt;Question: What does the Serializable interface do ?  &lt;br /&gt;Answer: Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface. &lt;br /&gt;Question: How do I serialize an object to a file ? &lt;br /&gt;Answer: To serialize an object into a stream perform the following actions: &lt;br /&gt;&lt;br /&gt;- Open one of the output streams, for exaample FileOutputStream &lt;br /&gt;- Chain it with the ObjectOutputStream - Call the method writeObject() providingg the instance of a Serializable object as an argument. &lt;br /&gt;- Close the streams &lt;br /&gt;&lt;br /&gt;Java Code --------- try{ fOut= new FileOutputStream("c:\\emp.ser"); out = new ObjectOutputStream(fOut); out.writeObject(employee); //serializing System.out.println("An employee is serialized into c:\\emp.ser"); } catch(IOException e){ e.printStackTrace(); } &lt;br /&gt;Question: How do I deserilaize an Object?  &lt;br /&gt;Answer: To deserialize an object, perform the following steps: &lt;br /&gt;&lt;br /&gt;- Open an input stream &lt;br /&gt;- Chain it with the ObjectInputStream - Call the method readObject() and cast tthe returned object to the class that is being deserialized. &lt;br /&gt;- Close the streams &lt;br /&gt;Java Code try{ fIn= new FileInputStream("c:\\emp.ser"); in = new ObjectInputStream(fIn); //de-serializing employee Employee emp = (Employee) in.readObject(); System.out.println("Deserialized " + emp.fName + " " + emp.lName + " from emp.ser "); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); } &lt;br /&gt;Question: What is Externalizable Interface ?  &lt;br /&gt;Answer : Externalizable interface is a subclass of Serializable. Java provides Externalizable interface that gives you more control over what is being serialized and it can produce smaller object footprint. ( You can serialize whatever field values you want to serialize) &lt;br /&gt;&lt;br /&gt;This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence. &lt;br /&gt;Question: Explain garbage collection ?  &lt;br /&gt;Answer: Garbage collection is an important part of Java's security strategy. Garbage collection is also called automatic memory management as JVM automatically removes the unused variables/objects from the memory. The name "garbage collection" implies that objects that are no longer needed by the program are "garbage" and can be thrown away. A more accurate and up-to-date metaphor might be "memory recycling." When an object is no longer referenced by the program, the heap space it occupies must be recycled so that the space is available for subsequent new objects. The garbage collector must somehow determine which objects are no longer referenced by the program and make available the heap space occupied by such unreferenced objects. In the process of freeing unreferenced objects, the garbage collector must run any finalizers of objects being freed &lt;br /&gt;Question : How you can force the garbage collection ?  &lt;br /&gt;Answer : Garbage collection automatic process and can't be forced. We can call garbage collector in Java by calling System.gc() and Runtime.gc(), JVM tries to recycle the unused objects, but there is no guarantee when all the objects will garbage collected. &lt;br /&gt;Question : What are the field/method access levels (specifiers) and class access levels ?  &lt;br /&gt;Answer: Each field and method has an access level: &lt;br /&gt;•	private: accessible only in this class &lt;br /&gt;•	(package): accessible only in this package &lt;br /&gt;•	protected: accessible only in this package and in all subclasses of this class &lt;br /&gt;•	public: accessible everywhere this class is available &lt;br /&gt;Similarly, each class has one of two possible access levels: &lt;br /&gt;•	(package): class objects can only be declared and manipulated by code in this package &lt;br /&gt;public: class objects can be declared and manipulated by code in any package&lt;br /&gt;&lt;br /&gt;Question: What are the static fields &amp; static Methods ?  &lt;br /&gt;Answer: If a field or method defined as a static, there is only one copy for entire class, rather than one copy for each instance of class. static method cannot accecss non-static field or call non-static method &lt;br /&gt;&lt;br /&gt;Example Java Code &lt;br /&gt;&lt;br /&gt;static int counter = 0; &lt;br /&gt;&lt;br /&gt;A public static field or method can be accessed from outside the class using either the usual notation: &lt;br /&gt;&lt;br /&gt;Java-class-object.field-or-method-name &lt;br /&gt;&lt;br /&gt;or using the class name instead of the name of the class object: &lt;br /&gt;&lt;br /&gt;Java- class-name.field-or-method-name &lt;br /&gt;Question: What are the Final fields &amp; Final Methods ?  &lt;br /&gt;Answer: Fields and methods can also be declared final. A final method cannot be overridden in a subclass. A final field is like a constant: once it has been given a value, it cannot be assigned to again. &lt;br /&gt;&lt;br /&gt;Java Code &lt;br /&gt;&lt;br /&gt;private static final int MAXATTEMPTS = 10; &lt;br /&gt;Question: Describe the wrapper classes in Java ?  &lt;br /&gt;Answer: Wrapper class is wrapper around a primitive data type. An instance of a wrapper class contains, or wraps, a primitive value of the corresponding type. &lt;br /&gt;&lt;br /&gt;Following table lists the primitive types and the corresponding wrapper classes: &lt;br /&gt;Primitive 	Wrapper &lt;br /&gt;boolean 	java.lang.Boolean &lt;br /&gt;byte 	java.lang.Byte &lt;br /&gt;char 	java.lang.Character &lt;br /&gt;double 	java.lang.Double &lt;br /&gt;float 	java.lang.Float &lt;br /&gt;int 	java.lang.Integer &lt;br /&gt;long 	java.lang.Long &lt;br /&gt;short 	java.lang.Short &lt;br /&gt;void 	java.lang.Void &lt;br /&gt;Question: What are different types of inner classes ?  &lt;br /&gt;Answer: Inner classes nest within other classes. A normal class is a direct member of a package. Inner classes, which became available with Java 1.1, are four types &lt;br /&gt;•	Static member classes &lt;br /&gt;•	Member classes &lt;br /&gt;•	Local classes &lt;br /&gt;•	Anonymous classes &lt;br /&gt;Static member classes - a static member class is a static member of a class. Like any other static method, a static member class has access to all static methods of the parent, or -level, class. &lt;br /&gt;&lt;br /&gt;Member Classes - a member class is also defined as a member of a class. Unlike the static variety, the member class is instance specific and has access to any and all methods and members, even the parent's this reference. &lt;br /&gt;&lt;br /&gt;Local Classes - Local Classes declared within a block of code and these classes are visible only within the block. &lt;br /&gt;&lt;br /&gt;Anonymous Classes - These type of classes does not have any name and its like a local class &lt;br /&gt;Java Anonymous Class Example public class SomeGUI extends JFrame { ... button member declarations ... protected void buildGUI() { button1 = new JButton(); button2 = new JButton(); ... button1.addActionListener( new java.awt.event.ActionListener() &lt;------ Anonymous Class { public void actionPerformed(java.awt.event.ActionEvent e) { // do something } } );Question: What are the uses of Serialization?  Answer: In some types of applications you have to write the code to serialize objects, but in many cases serialization is performed behind the scenes by various server-side containers. These are some of the typical uses of serialization: •	To persist data for future use. •	To send data to a remote computer using such client/server Java technologies as RMI or socket programming. •	To "flatten" an object into array of bytes in memory. •	To exchange data between applets and servlets. •	To store user session in Web applications . •	To activate/passivate enterprise java beans. •	To send objects between the servers in a cluster. Question: what is a collection ?  Answer: Collection is a group of objects. java.util package provides important types of collections. There are two fundamental types of collections they are Collection and Map. Collection types hold a group of objects, Eg. Lists and Sets where as Map types hold group of objects as key, value pairs Eg. HashMap and Hashtable. Question: For concatenation of strings, which method is good, StringBuffer or String ? Answer: StringBuffer is faster than String for concatenation. Question: What is Runnable interface ? Are there any other ways to make a java program as multithred java program? Answer: There are two ways to create new kinds of threads: - Define a new class that extends the Thread class - Define a new class that implements the Runnable interface, and pass an object of that class to a Thread's constructor. - An advantage of the second approach is that the new class can be a subclass of any class, not just of the Thread class. Here is a very simple example just to illustrate how to use the second approach to creating threads: class myThread implements Runnable { public void run() { System.out.println("I'm running!"); } } public class tstRunnable { public static void main(String[] args) { myThread my1 = new myThread(); myThread my2 = new myThread(); new Thread(my1).start(); new Thread(my2).start(); } Question: How can i tell what state a thread is in ? Answer: Prior to Java 5, isAlive() was commonly used to test a threads state. If isAlive() returned false the thread was either new or terminated but there was simply no way to differentiate between the two. Starting with the release of Tiger (Java 5) you can now get what state a thread is in by using the getState() method which returns an Enum of Thread.States. A thread can only be in one of the following states at a given point in time. NEW 	A Fresh thread that has not yet started to execute. RUNNABLE 	A thread that is executing in the Java virtual machine. BLOCKED 	A thread that is blocked waiting for a monitor lock. WAITING 	A thread that is wating to be notified by another thread. TIMED_WAITING 	A thread that is wating to be notified by another thread for a specific amount of time TERMINATED 	A thread whos run method has ended.   The folowing code prints out all thread states. public class ThreadStates{ public static void main(String[] args){ Thread t = new Thread(); Thread.State e = t.getState(); Thread.State[] ts = e.values(); for(int i = 0; i &lt; ts.length; i++){ System.out.println(ts[i]); } } }Question: What methods java providing for Thread communications ? Answer: Java provides three methods that threads can use to communicate with each other: wait, notify, and notifyAll. These methods are defined for all Objects (not just Threads). The idea is that a method called by a thread may need to wait for some condition to be satisfied by another thread; in that case, it can call the wait method, which causes its thread to wait until another thread calls notify or notifyAll.       Question: What is the difference between notify and notify All methods ?  Answer: A call to notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait). A call to notifyAll causes all threads waiting on the same object to be notified. If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify (so it is often better to use notifyAll than notify). Question: What is synchronized keyword? In what situations you will Use it?  Answer: Synchronization is the act of serializing access to critical sections of code. We will use this keyword when we expect multiple threads to access/modify the same data. To understand synchronization we need to look into thread execution manner. Threads may execute in a manner where their paths of execution are completely independent of each other. Neither thread depends upon the other for assistance. For example, one thread might execute a print job, while a second thread repaints a window. And then there are threads that require synchronization, the act of serializing access to critical sections of code, at various moments during their executions. For example, say that two threads need to send data packets over a single network connection. Each thread must be able to send its entire data packet before the other thread starts sending its data packet; otherwise, the data is scrambled. This scenario requires each thread to synchronize its access to the code that does the actual data-packet sending. If you feel a method is very critical for business that needs to be executed by only one thread at a time (to prevent data loss or corruption), then we need to use synchronized keyword. EXAMPLESome real-world tasks are better modeled by a program that uses threads than by a normal, sequential program. For example, consider a bank whose accounts can be accessed and updated by any of a number of automatic teller machines (ATMs). Each ATM could be a separate thread, responding to deposit and withdrawal requests from different users simultaneously. Of course, it would be important to make sure that two users did not access the same account simultaneously. This is done in Java using synchronization, which can be applied to individual methods, or to sequences of statements. One or more methods of a class can be declared to be synchronized. When a thread calls an object's synchronized method, the whole object is locked. This means that if another thread tries to call any synchronized method of the same object, the call will block until the lock is released (which happens when the original call finishes). In general, if the value of a field of an object can be changed, then all methods that read or write that field should be synchronized to prevent two threads from trying to write the field at the same time, and to prevent one thread from reading the field while another thread is in the process of writing it. Here is an example of a BankAccount class that uses synchronized methods to ensure that deposits and withdrawals cannot be performed simultaneously, and to ensure that the account balance cannot be read while either a deposit or a withdrawal is in progress. (To keep the example simple, no check is done to ensure that a withdrawal does not lead to a negative balance.) public class BankAccount { private double balance; // constructor: set balance to given amount public BankAccount( double initialDeposit ) { balance = initialDeposit; } public synchronized double Balance( ) { return balance; } public synchronized void Deposit( double deposit ) { balance += deposit; } public synchronized void Withdraw( double withdrawal ) { balance -= withdrawal; } } Note: that the BankAccount's constructor is not declared to be synchronized. That is because it can only be executed when the object is being created, and no other method can be called until that creation is finished. There are cases where we need to synchronize a group of statements, we can do that using synchrozed statement. Java Code Example synchronized ( B ) { if ( D &gt; B.Balance() ) { ReportInsuffucientFunds(); } else { B.Withdraw( D ); } }&lt;br /&gt;&lt;br /&gt;Question: What is serialization ?  &lt;br /&gt;Answer: Serialization is the process of writing complete state of java object into output stream, that stream can be file or byte array or stream associated with TCP/IP socket. &lt;br /&gt;Question: What does the Serializable interface do ?  &lt;br /&gt;Answer: Serializable is a tagging interface; it prescribes no methods. It serves to assign the Serializable data type to the tagged class and to identify the class as one which the developer has designed for persistence. ObjectOutputStream serializes only those objects which implement this interface.  &lt;br /&gt;Question: How do I serialize an object to a file ?  &lt;br /&gt;Answer: To serialize an object into a stream perform the following actions: &lt;br /&gt;&lt;br /&gt;- Open one of the output streams, for exxample FileOutputStream &lt;br /&gt;- Chain it with the ObjectOutputStream &lt; - Call the method writeObject() providinng the instance of a Serializable object as an argument. - Close the streams Java Code --------- try{ fOut= new FileOutputStream("c:\\emp.ser"); out = new ObjectOutputStream(fOut); out.writeObject(employee); //serializing System.out.println("An employee is serialized into c:\\emp.ser"); } catch(IOException e){ e.printStackTrace(); } Question: How do I deserilaize an Object?  Answer: To deserialize an object, perform the following steps: - Open an input stream - Chain it with the ObjectInputStream - Call the method readObject() and cast the returned object to the class that is being deserialized. - Close the streams Java Code try{ fIn= new FileInputStream("c:\\emp.ser"); in = new ObjectInputStream(fIn); //de-serializing employee Employee emp = (Employee) in.readObject(); System.out.println("Deserialized " + emp.fName + " " + emp.lName + " from emp.ser "); }catch(IOException e){ e.printStackTrace(); }catch(ClassNotFoundException e){ e.printStackTrace(); } Question: What is Externalizable Interface ? Answer: Externalizable interface is a subclass of Serializable. Java provides Externalizable interface that gives you more control over what is being serialized and it can produce smaller object footprint. ( You can serialize whatever field values you want to serialize) This interface defines 2 methods: readExternal() and writeExternal() and you have to implement these methods in the class that will be serialized. In these methods you'll have to write code that reads/writes only the values of the attributes you are interested in. Programs that perform serialization and deserialization have to write and read these attributes in the same sequence.Question: What is a transient variable?Answer: A transient variable is a variable that may not be serialized. Question: Which containers use a border Layout as their default layout?Answer: The window, Frame and Dialog classes use a border layout as their default layout. Question: Why do threads block on I/O?Answer: Threads block on i/o (that is enters the waiting state) so that other threads may execute while the i/o Operation is performed. Question: How are Observer and Observable used?Answer: Objects that subclass the Observable class maintain a list of observers. When an Observable object is updated it invokes the update() method of each of its observers to notify the observers that it has changed state. The Observer interface is implemented by objects that observe Observable objects. Question: What is synchronization and why is it important?Answer: With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object's value. This often leads to significant errors. Question: Can a lock be acquired on a class?Answer:  Yes, a lock can be acquired on a class. This lock is acquired on the class's Class object. Question: What's new with the s(), suspend() and resume() methods in JDK 1.2?Answer:  The s(), suspend() and resume() methods have been deprecated in JDK 1.2. Question: Is null a keyword?Answer:  The null value is not a keyword. Question: What is the preferred size of a component?Answer: The preferred size of a component is the minimum component size that will allow the component to display normally. Question: What method is used to specify a container's layout? Answer: The setLayout() method is used to specify a container's layout. Question: Which containers use a FlowLayout as their default layout? Answer: The Panel and Applet classes use the FlowLayout as their default layout. Question: What state does a thread enter when it terminates its processing? Answer: When a thread terminates its processing, it enters the dead state. Question: What is the Collections API? Answer: The Collections API is a set of classes and interfaces that support operations on collections of objects. Question: Which characters may be used as the second character of an identifier, but not as the first character of an identifier?Answer: The digits 0 through 9 may not be used as the first character of an identifier but they may be used after the first character of an identifier. Question: What is the List interface? Answer: The List interface provides support for ordered collections of objects. Question: How does Java handle integer overflows and underflows? Answer: It uses those low order bytes of the result that can fit into the size of the type allowed by the operation. Question: What is the Vector class? Answer: The Vector class provides the capability to implement a growable array of objects Question: What modifiers may be used with an inner class that is a member of an outer class? Answer: A (non-local) inner class may be declared as public, protected, private, static, final, or abstract. Question: What is an Iterator interface?Answer: The Iterator interface is used to step through the elements of a Collection. Question: What is the difference between the &gt;&gt; and &gt;&gt;&gt; operators? &lt;br /&gt;Answer: The &gt;&gt; operator carries the sign bit when shifting right. The &gt;&gt;&gt; zero-fills bits that have been shifted out. &lt;br /&gt;Question: Which method of the Component class is used to set the position and size of a component?&lt;br /&gt;Answer: setBounds() &lt;br /&gt;Question: How many bits are used to represent Unicode, ASCII, UTF-16, and UTF-8 characters? &lt;br /&gt;Answer: Unicode requires 16 bits and ASCII require 7 bits. Although the ASCII character set uses only 7 bits, it is usually represented as 8 bits. UTF-8 represents characters using 8, 16, and 18 bit patterns. UTF-16 uses 16-bit and larger bit patterns. &lt;br /&gt;Question: What is the difference between yielding and sleeping? &lt;br /&gt;Answer: When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state. &lt;br /&gt;Question: Which java.util classes and interfaces support event handling? &lt;br /&gt;Answer: The EventObject class and the EventListener interface support event processing. &lt;br /&gt;Question: Is sizeof a keyword? &lt;br /&gt;Answer: The sizeof operator is not a keyword. &lt;br /&gt;Question: What are wrapped classes? &lt;br /&gt;Answer: Wrapped classes are classes that allow primitive types to be accessed as objects. &lt;br /&gt;Question: Does garbage collection guarantee that a program will not run out of memory? &lt;br /&gt;Answer: Garbage collection does not guarantee that a program will not run out of memory. It is possible for programs to use u
