<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/atom10full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" gd:etag="W/&quot;DUYHRnw5fip7ImA9WhNTF0Q.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393</id><updated>2012-10-20T22:25:37.226-07:00</updated><category term="Row Checkbox Problem in JSF" /><category term="Resetting the password" /><category term="Advertisement Videos" /><category term="Brahmanandam comedy scends" /><category term="Suneel Comedy Scenes" /><category term="MIMICRY" /><title>Hot Java</title><subtitle type="html" /><link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/posts/default" /><link rel="alternate" type="text/html" href="http://javabrowsers.blogspot.com/" /><link rel="next" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default?start-index=26&amp;max-results=25&amp;redirect=false&amp;v=2" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><generator version="7.00" uri="http://www.blogger.com">Blogger</generator><openSearch:totalResults>56</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/atom+xml" href="http://feeds.feedburner.com/blogspot/nwOEG" /><feedburner:info uri="blogspot/nwoeg" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><entry gd:etag="W/&quot;DUcMSXg6fip7ImA9WhNTF0Q.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-5668612389033823722</id><published>2012-10-20T22:24:00.001-07:00</published><updated>2012-10-20T22:24:48.616-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-10-20T22:24:48.616-07:00</app:edited><title>kajal</title><content type="html">&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-AJSnOKRTOnc/UIOHE3P9p_I/AAAAAAAAASM/qqSBbrE1yFk/s1600/k.11.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-AJSnOKRTOnc/UIOHE3P9p_I/AAAAAAAAASM/qqSBbrE1yFk/s1600/k.11.JPG" height="135" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/jZocV394sT4" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/5668612389033823722/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/10/kajal.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/5668612389033823722?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/5668612389033823722?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/jZocV394sT4/kajal.html" title="kajal" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-AJSnOKRTOnc/UIOHE3P9p_I/AAAAAAAAASM/qqSBbrE1yFk/s72-c/k.11.JPG" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/10/kajal.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUIFSXY8cCp7ImA9WhJSGU0.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4167619547505300946</id><published>2012-07-09T23:58:00.002-07:00</published><updated>2012-07-09T23:58:38.878-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-09T23:58:38.878-07:00</app:edited><title>clone() in java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;clone() in java&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;Here we address purpose of cloning objects, types of cloning, etc.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;Definition:- &amp;nbsp;Cloning means creating a new object by copying existing object state.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;clone is not for initialization and instantiation. It should not be synonymously used as creating a new object, because the constructor of the cloned object never invoked in the process. Its about copying the object and creating new.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;There are 2 types of cloning.They are:-&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ul style="text-align: left;"&gt;
&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Shallow Cloning&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Deep Cloning&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;b style="text-decoration: underline;"&gt;Shallow Cloning:&lt;/b&gt;&amp;nbsp; means object state is partially cloned.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;That is, Suppose i am having Person object. In Person object I am having fields like name, age, sex, location, and Address object.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;when an object containing a reference to another object is cloned, the reference object is not cloned and will be shared between the cloned object and new object. This is partial cloning also called as shallow cloning.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;In Person object , Address object is a reference. so when we clone Person object, Address object will not be cloned. It is just shared between cloned Person object and new Person object.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;Ex:- Object class provides a clone() and provides support for shallow copy. It returns Object as type and we need to explicitly type cast to original object.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;In Object class, clone() is protected. As clone() is protected we cannot use it in all our classes.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;The class which needs to be cloned must implement clone().&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;Also class &amp;nbsp;must implement Cloneable interface. Cloneable interface is a marker interface , means which does not contain any methods. Cloneable interface just tells the compiler that object can be cloned. If Cloneable interface is not implemented then CloneNotSupportedException is thrown.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;public class Person implements Cloneable&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp;private String name="abdul";&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp;private int age = 35;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp; private char sex = "M";&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; //generate setters and getter&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; public Object clone() throws CloneNotSupportedException()&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&amp;nbsp; return super.clone();&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="background-color: white;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="background-color: white;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="background-color: white;"&gt;&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Problems with Object class cloning:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="background-color: white;"&gt;&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;u style="font-weight: bold;"&gt;Deep Cloning:- &lt;/u&gt;&amp;nbsp;means object state is fully cloned.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;means Address object also cloned in deep cloning.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;To get a deep cloning, we need to implement clone() ourselves. If we don't want to implement deep cloning ourselves then we can go for serialization.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;serialization implements deep copy implicitly.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;cloning using serialization:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); color: #555555; font-size: 9pt; line-height: 15px; margin: 0em 0em 3em; padding: 0em; text-align: -webkit-auto;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;class&lt;/span&gt; CloneExample &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;implements&lt;/span&gt; Cloneable, Serializable {

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;int&lt;/span&gt; num;
 Thing thing;

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; CloneExample clone() {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;try&lt;/span&gt; {
   &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; (CloneExample) &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;super&lt;/span&gt;.clone();
  } &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;catch&lt;/span&gt; (CloneNotSupportedException e) {
   &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; null;
  }
 }

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; CloneExample deepClone() {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;try&lt;/span&gt; {
   ByteArrayOutputStream baos = &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;new&lt;/span&gt; ByteArrayOutputStream();
   ObjectOutputStream oos = &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;new&lt;/span&gt; ObjectOutputStream(baos);
   oos.writeObject(&lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;this&lt;/span&gt;);

   ByteArrayInputStream bais = &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;new&lt;/span&gt; ByteArrayInputStream(baos.toByteArray());
   ObjectInputStream ois = &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;new&lt;/span&gt; ObjectInputStream(bais);
   &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; (CloneExample) ois.readObject();
  } &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;catch&lt;/span&gt; (IOException e) {
   &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; null;
  } &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;catch&lt;/span&gt; (ClassNotFoundException e) {
   &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; null;
  }
 }

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;int&lt;/span&gt; getNum() {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; num;
 }

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;void&lt;/span&gt; setNum(&lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;int&lt;/span&gt; num) {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;this&lt;/span&gt;.num = num;
 }

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; Thing getThing() {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; thing;
 }

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;void&lt;/span&gt; setThing(Thing thing) {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;this&lt;/span&gt;.thing = thing;
 }

 &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;public&lt;/span&gt; String toString() {
  &lt;span class="javaKeyword" style="color: #8f1151; font-weight: bold; margin: 0em; padding: 0em;"&gt;return&lt;/span&gt; &lt;span class="javaString" style="color: #4c2fbf; font-weight: bold; margin: 0em; padding: 0em;"&gt;"num:"&lt;/span&gt; + num + &lt;span class="javaString" style="color: #4c2fbf; font-weight: bold; margin: 0em; padding: 0em;"&gt;", thing:"&lt;/span&gt; + thing;
 }
}&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;ByteArrayOutputStream implements an output stream in which data is written to a byte array. buffer automatically grows as data is written to it. data can be retrieved using toByteArray() and toString(). The methods in this class can be called after the stream has closed.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;ByteArrayInputStream:&amp;nbsp;&lt;span style="text-align: -webkit-auto;"&gt;A&amp;nbsp;&lt;/span&gt;&lt;code style="text-align: -webkit-auto;"&gt;ByteArrayInputStream&lt;/code&gt;&lt;span style="text-align: -webkit-auto;"&gt;&amp;nbsp;contains an internal buffer that contains bytes that may be read from the stream. An internal counter keeps track of the next byte to be supplied by the&amp;nbsp;&lt;/span&gt;&lt;code style="text-align: -webkit-auto;"&gt;read&lt;/code&gt;&lt;span style="text-align: -webkit-auto;"&gt;&amp;nbsp;method.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="text-align: -webkit-auto;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="text-align: -webkit-auto;"&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="text-align: -webkit-auto;"&gt;&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Purpose of cloning an object:&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="text-align: -webkit-auto;"&gt;&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: left;"&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;span style="line-height: 17px;"&gt;Cloning of an object to avoid creation. If the cost of&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;span style="line-height: 17px;"&gt;creating a new object is large and creation is resource intensive, we clone the object. We use the interface Cloneable and call its method clone() to clone the object.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;Disadvantage of cloning :-&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;span style="line-height: 21px;"&gt;Most of the&amp;nbsp;&lt;/span&gt;&lt;a href="http://javapapers.com/core-java/abstract-and-interface-core-java-2/difference-between-a-java-interface-and-a-java-abstract-class/" style="background-color: #f1f991; color: #4a6b82; cursor: pointer; line-height: 21px; text-decoration: none;" title="Difference Between Interface and Abstract Class"&gt;interface / abstract&lt;/a&gt;&lt;span style="line-height: 21px;"&gt;&amp;nbsp;class writers in java forget to put a public clone method. For example you can take List. So when you want to clone their implementations you have to ignore the abstract type and use actual implementations like ArrayList by name. This completely removes the advantage and goodness of abstractness.&lt;/span&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;u&gt;&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style="font-family: Verdana, sans-serif;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/eyGXn21ZawU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4167619547505300946/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/07/clone-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4167619547505300946?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4167619547505300946?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/eyGXn21ZawU/clone-in-java.html" title="clone() in java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/07/clone-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8ERnk-eip7ImA9WhJSGEw.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4765385830414199086</id><published>2012-07-09T00:10:00.002-07:00</published><updated>2012-07-09T00:10:07.752-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-09T00:10:07.752-07:00</app:edited><title>Dirty Checking in Hibernate</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
Dirty Checking &amp;nbsp;is one of the features of hibernate. In dirty checking, hibernate automatically detects whether &amp;nbsp; &amp;nbsp;an object is modified (or) not and need to be updated. As long as the object is in persistent state i.e., bound to a particular Session(org.hibernate.Session). Hibernate monitors any changes to the objects and executes sql.&lt;br /&gt;
&lt;br /&gt;
Note:- For dirty checking to work, the object must exist in cache.&lt;br /&gt;
&lt;br /&gt;
Example:-&lt;br /&gt;
I used oracle database and so oracle driver.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;b&gt;hibernate.cfg.xml&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"&lt;br /&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;"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;lt;hibernate-configuration&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;session-factory&amp;gt;&lt;br /&gt;
&amp;lt;!-- connection properties--&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;property name="hibernate.connection.driver_class"&amp;gt;oracle.jdbc.driver.OracleDriver&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;property name="hibernate.connection.url"&amp;gt;jdbc:oracle:thin:@localhost:1521:serviceName&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;property name="hibernate.connection.username"&amp;gt;userName&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style="background-color: white;"&gt;&amp;lt;property name="hibernate.connection.password"&amp;gt;password&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;!-- limit the connection pool connections--&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;property name="hibernate.connection.pool_size"&amp;gt;10&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;lt;!-- Hibernate properties --&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;!-- Dialect class is used to convert hql ddl, dml etc queries to its database dependent queries. This is &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;developed by database vendors --&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;property name="hibernate.dialect"&amp;gt;&lt;/span&gt;&lt;span style="background-color: white;"&gt;org.hibernate.dialect.OracleDialect&lt;/span&gt;&lt;span style="background-color: white;"&gt;&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;!-- this &amp;nbsp;property displays the sql query on the console --&amp;gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;property name="hibernate.show_sql"&amp;gt;true&amp;lt;/property&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;lt;!-- mapping hibernate mapping files --&amp;gt;&lt;br /&gt;
&amp;lt;mapping resource="Gender.hbm.xml"/&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/session-factory&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;lt;/hibernate-configuration&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Gender.hbm.xml&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"&lt;br /&gt;
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&amp;gt;&lt;br /&gt;
&amp;lt;hibernate-mapping&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;class name="packageName.Gender" table="M_COM_GENDER"&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;id name="id" type="java.lang.Long" column="GNDR_N_ID"&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;generator class="increment"/&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/id&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;span style="background-color: white;"&gt;&amp;lt;property name="name" type="java.lang.String" column="GNDR_VC_TYP"&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;property name="status" type="java.lang.String" column="GNDR_C_STS"&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;
&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Gender.java&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
//This is the pojo class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
package packageName;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;public class Gender {&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private Long id;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private String name;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private String status;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public Long getId() {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return id;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private void setId(Long id) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.id = id;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public String getName() {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return name;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void setName(String name) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.name = name;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public String getStatus() {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return status;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void setStatus(String status) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.status = status;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;ClientApp.java&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
package packageName;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
import org.hibernate.cfg.Configuration;&lt;br /&gt;
import org.hibernate.SessionFactory;&lt;br /&gt;
import org.hibernate.Session;&lt;br /&gt;
import org.hibernate.Transaction;&lt;br /&gt;
public class ClientApp {&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Configuration con = new Configuration();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;con.configure("com/spry/hibernate/dirtychecking/NewFile.cfg.xml");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;SessionFactory sf = con.buildSessionFactory();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Session session = sf.openSession();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Transaction trans = session.beginTransaction();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;try&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&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;//loading a gender object of genderId = 1&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;Gender gender = (Gender)session.get(Gender.class, new Long(1));&lt;br /&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;//print the gender naem&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.println("gender.getName()=======&amp;gt;"+gender.getName());&lt;br /&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; //modify the gender name&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;gender.setName("female");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;session.getTransaction().commit();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;session.flush();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch(Exception e)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}finally&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;session.close();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
In the above client application, gender object is loaded first. and then object state is changed by altering the name &amp;nbsp;of the object. Here, we are not calling update() , even though object state is written to the database. This is called automatic dirty checking. Hibernate monitors whether any changes in the session objects and automatically synchronizes them to database.&lt;br /&gt;
session.getTransaction.commit() is mandatory. Because, after any modifications, if commit() is not called then data will not be modified in the database eventhough update query is executed.&lt;br /&gt;
&lt;br /&gt;
This also works for collections. &lt;br /&gt;
&lt;div style="text-decoration: underline;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/7vqf_QUzzxM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4765385830414199086/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/07/dirty-checking-in-hibernate.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4765385830414199086?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4765385830414199086?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/7vqf_QUzzxM/dirty-checking-in-hibernate.html" title="Dirty Checking in Hibernate" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/07/dirty-checking-in-hibernate.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0IHQXo6fip7ImA9WhJSFkk.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-7711866220899515405</id><published>2012-07-06T23:12:00.000-07:00</published><updated>2012-07-06T23:12:10.416-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-06T23:12:10.416-07:00</app:edited><title>difference between GET and POST or GET VS POST</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;&lt;u&gt;GET VS POST:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style="background-color: white; text-indent: -0.25in;"&gt;&lt;b&gt;1)&lt;/b&gt; “get” is the default method in forms., method=”get”.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: 7pt; text-indent: -0.25in;"&gt;&lt;b&gt;2)&lt;/b&gt; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: white; text-indent: -0.25in;"&gt;Hyperlinks use get request only.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-size: 7pt; text-indent: -0.25in;"&gt;&amp;nbsp;&lt;b&gt;3)&lt;/b&gt; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: white; text-indent: -0.25in;"&gt;“get” is idempotent.(Acc. To HTTP spec)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="MsoListParagraphCxSpMiddle" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"&gt;·&lt;span style="font-family: 'Times New Roman'; font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;It should not change anything on the server.&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpMiddle" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"&gt;·&lt;span style="font-family: 'Times New Roman'; font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;It can be executed more than once without any
bad side effects.&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpLast" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"&gt;·&lt;span style="font-family: 'Times New Roman'; font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;It’s safe because it does not change anything on
the server (unlike&amp;nbsp; post). &lt;/div&gt;
&lt;div class="MsoNormal" style="background-color: white; margin-left: 0.5in;"&gt;
There are 3 main limitations:&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpFirst" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"&gt;·&lt;span style="font-family: 'Times New Roman'; font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;b&gt;Size: &lt;/b&gt;The
total number of character that can be sent using get request is limited. Only 255
bytes.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpMiddle" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"&gt;·&lt;span style="font-family: 'Times New Roman'; font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;b&gt;Security:
&lt;/b&gt;data sent with get request is insecure as all data sent will be appended to
url and visible as query string. We should not use&amp;nbsp; get for passwords or sensitive information.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpLast" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;span style="font-family: Symbol; mso-bidi-font-family: Symbol; mso-fareast-font-family: Symbol;"&gt;·&lt;span style="font-family: 'Times New Roman'; font-size: 7pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
&lt;/span&gt;&lt;/span&gt;&lt;b&gt;Bookmarking:
&lt;/b&gt;get submissions can be book marked which should not be done.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoNormal" style="background-color: white;"&gt;
&lt;b&gt;Post:&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpFirst" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;b&gt;1)&lt;span style="font-size: 7pt; font-weight: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;With
post we can either request a resource (or) can send form data to server.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpMiddle" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;b&gt;2)&lt;span style="font-size: 7pt; font-weight: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;Form
data is sent to server in body of the request.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpMiddle" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;b&gt;3)&lt;span style="font-size: 7pt; font-weight: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;No
limit for the amount of data sent.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpMiddle" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;b&gt;4)&lt;span style="font-size: 7pt; font-weight: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;As
get the default in form’s, post must be explicitly specified.&lt;b&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class="MsoListParagraphCxSpLast" style="background-color: white; margin-left: 1in; text-indent: -0.25in;"&gt;
&lt;b&gt;5)&lt;span style="font-size: 7pt; font-weight: normal;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/b&gt;Post
is not idempotent. Post is intended to change things on the server. So double
submissions will be a problem. Double submissions cause undesirable results on the
server.&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/d6WytkoZx9M" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/7711866220899515405/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/07/difference-between-get-and-post-or-get.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7711866220899515405?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7711866220899515405?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/d6WytkoZx9M/difference-between-get-and-post-or-get.html" title="difference between GET and POST or GET VS POST" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/07/difference-between-get-and-post-or-get.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEMBRng9eyp7ImA9WhJSFUo.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-6924591840251712448</id><published>2012-07-06T05:07:00.001-07:00</published><updated>2012-07-06T05:07:37.663-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-06T05:07:37.663-07:00</app:edited><title>difference between Iterator and Enumeration</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;b&gt;&lt;u&gt;Difference between Iterator and Enumeration&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;&lt;ol&gt;
&lt;li&gt;Iterators are fail-fast . i.e., while iterating the collection, suppose any other thread tries to modify the list by add/remove operations then the iterator fails quickly and throws "Concurrent Modification Exception".&amp;nbsp;&lt;/li&gt;
&lt;li&gt;Enumeration is a legacy class.&lt;/li&gt;
&lt;li&gt;Method names have improve in Iterator than Enumeration.&lt;/li&gt;
&lt;li&gt;Iterator has remove() while Enumeration does not have remove().&lt;/li&gt;
&lt;li&gt;Iterator methods: hasNext(), next(), remove().&lt;/li&gt;
&lt;/ol&gt;
&lt;/ol&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Enumeration methods: hasMoreElements(),nextElement().&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/yctafcVg4E0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/6924591840251712448/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/07/difference-between-iterator-and.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/6924591840251712448?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/6924591840251712448?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/yctafcVg4E0/difference-between-iterator-and.html" title="difference between Iterator and Enumeration" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/07/difference-between-iterator-and.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUABRX89eCp7ImA9WhVbFEk.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-1581390824303499501</id><published>2012-05-31T00:02:00.002-07:00</published><updated>2012-05-31T00:02:34.160-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-31T00:02:34.160-07:00</app:edited><title>final variables in Java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
There are 3 types of variables in java. They are:&lt;br /&gt;
1) Instance Variables.&lt;br /&gt;
2) Static Variables.&lt;br /&gt;
3) Local Variables.&lt;br /&gt;
&lt;br /&gt;
Each object will have its own copy of instance variables. static variables are shared by the objects.&lt;br /&gt;
&lt;br /&gt;
we will associate final keyword to these 3 types of variables and check the results.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&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; variables that specify the state of an object are called instance variables. sometimes we can have the state constant. using final keyword we can make variables of an object never change.Final means the state of an object once initialized, it never changes.&lt;br /&gt;
Example:-&lt;br /&gt;
&lt;br /&gt;
class FinalEx&lt;br /&gt;
{&lt;br /&gt;
final int a = 10,b = 20;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
In the above example, 'a' and 'b' instance variables are final. Means their state once assigned cannot be changed.&lt;br /&gt;
Initialization can be done while declaring or before completion of constructor for instance final fields.&lt;br /&gt;
Initialization can be done while declaring or in the static intializer block for static final fields.&lt;br /&gt;
&lt;br /&gt;
final primitives are considered as compile-time constants and actual values are assigned at compile-time only.&lt;br /&gt;
For Example:-&lt;br /&gt;
&lt;br /&gt;
final int a = 10, b = 20;&lt;br /&gt;
do&lt;br /&gt;
{&lt;br /&gt;
System.out.println("Hello");&lt;br /&gt;
}while(a&amp;lt;b);&lt;br /&gt;
System.out.println("Hi");&lt;br /&gt;
&lt;br /&gt;
Output:- Compiler error: Unreachable statement.&lt;br /&gt;
"Hi" will be unreachable statement. As values are assigned at compile-time only compiler checks a&amp;lt;b and safely throws the error.&lt;br /&gt;
This error is thrown for "while and for" loops only. For if condition this error is not thrown.&lt;br /&gt;
&lt;br /&gt;
unlike final primitives, final wrapper objects are not considered as compile-time constants.&lt;br /&gt;
&lt;br /&gt;
Instance and class final fields whether used (or) not used , they must be compulsory initialized.&lt;br /&gt;
local final fields if not used then no need to initialize.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/wX_iHPtrEV0" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/1581390824303499501/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/final-variables-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/1581390824303499501?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/1581390824303499501?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/wX_iHPtrEV0/final-variables-in-java.html" title="final variables in Java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/final-variables-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DU8BR344fSp7ImA9WhVbE00.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4677188185129024634</id><published>2012-05-29T09:10:00.001-07:00</published><updated>2012-05-29T09:10:56.035-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-29T09:10:56.035-07:00</app:edited><title>Ways to create object to a class in java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;u&gt;Class:&lt;/u&gt; A class is a template for which objects can be created. It can also be treated as visual, i.e. no physical existence.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Object:&lt;/u&gt;&amp;nbsp;An object exists physically. It consists of properties and behaviour.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; Dog is a class. Tommy is an object.&lt;br /&gt;
that means, Dog doesn't exist physically. tommy exists physically.&lt;br /&gt;
Dog is just like a template for which different objects llike tommy, cesar , can be created..&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Ways of creating objects in java&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;1) Using new operator:&amp;nbsp;&lt;/u&gt;&lt;br /&gt;
Example:&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
A a &amp;nbsp;= new A();&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;2) Class.forName("packageName.className").newInstance() &lt;/u&gt;returns super class type Object..we have to type cast to our specific object..&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
class A&lt;br /&gt;
{&lt;br /&gt;
}&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;A a = (A)Class.forName("A").newInstance();&lt;br /&gt;
}&lt;br /&gt;
catch(ClassNotFoundException e)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;e.printStackTrace();&lt;br /&gt;
}&lt;br /&gt;
Class.forName("") throws ClassNotFoundException.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;3) Deserialization:&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
serialization and deserialization helps to persist or store an object state to database, file system and to retrieve the object state from database, file system etc.&lt;br /&gt;
&lt;br /&gt;
serialization means storing&amp;nbsp;the object state to database, file system etc&lt;br /&gt;
deserialization means retrieving the object state from database, file system etc&lt;br /&gt;
&lt;br /&gt;
In deserialization process, we open a stream and read the objects from database or file system..this means indirectly we are creating object.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Serialization example:&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
import java.io.IOException;&lt;br /&gt;
import java.io.ObjectOutputStream;&lt;br /&gt;
import java.io.Serializable;&lt;br /&gt;
import java.io.FileOutputStream;&lt;br /&gt;
import java.io.FileNotFoundException;&lt;br /&gt;
class Test implements Serializable&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;int a = 10;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;int b = 20;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public int getA() {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return a;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void setA(int a) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.a = a;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public int getB() {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return b;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void setB(int b) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.b = b;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Serialization {&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;FileOutputStream out = null;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;ObjectOutputStream oos = null;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Test t = new Test();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;try&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;out = new FileOutputStream("abc");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;oos = new ObjectOutputStream(out);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;oos.writeObject(t);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch(FileNotFoundException e)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch(IOException e)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
Test class needs to implement Serializable interface. Serializable interface is a tagged or marker interface .&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;4) By using this.getClass().getClassLoader().loadClass(className).newInstance also returns the object.&lt;br /&gt;
5) By using cloning also we can create objects...&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/8dNRV-Mff08" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4677188185129024634/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/ways-to-create-object-to-class-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4677188185129024634?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4677188185129024634?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/8dNRV-Mff08/ways-to-create-object-to-class-in-java.html" title="Ways to create object to a class in java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/ways-to-create-object-to-class-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CU4HSHo8eSp7ImA9WhVUE08.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-5054910018497415798</id><published>2012-05-17T23:45:00.002-07:00</published><updated>2012-05-17T23:52:19.471-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-17T23:52:19.471-07:00</app:edited><title>Configuring Second Level Cache in Hibernate</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;u&gt;Hibernate's Second Level Cache&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&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;caching is one of the features of Hibernate. caching is introduced mainly for performance reasons.&lt;br /&gt;
For Example:&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;In normal JDBC, if we update a transaction number of times &amp;nbsp;it will hit the database is&lt;br /&gt;
&lt;span style="color: #cc0000;"&gt;number of hits to database = &amp;nbsp;number of updates.&lt;/span&gt;&lt;span style="color: #e06666;"&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; In hibernate, the object is maintained in cache. If any update is done in the object, then changes are synchronized. In hibernate if we update a transaction number number of times , it will hit the database is&lt;br /&gt;
&lt;span style="color: #e06666;"&gt;number of hits to database = 1 time.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In this way there are a number of performance benefits with hibernate caching. There are 3 ways of caching.&lt;br /&gt;
&lt;br /&gt;
1) Session level caching&lt;br /&gt;
2) Second level caching&lt;br /&gt;
3) Query Caching.&lt;br /&gt;
&lt;br /&gt;
Now we are going to deal with Second level caching in hiberate. It comprises of configuring &amp;amp;&amp;nbsp;execution of&amp;nbsp;&amp;nbsp;second level cache &amp;nbsp;of &amp;nbsp;a small standalone application .&lt;br /&gt;
&lt;br /&gt;
Tools Used: Eclipse IDE (Helios).&lt;br /&gt;
&lt;br /&gt;
create a new java project. Add Hibernate libraries.I used hibernate 3.0 library.&lt;br /&gt;
Download hibernate library from&amp;nbsp;&lt;a href="http://www.hibernate.org/downloads"&gt;http://www.hibernate.org/downloads&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
After downloading it unzip the zip folder. you will have hibernate &amp;amp; all supporting jar files.&lt;br /&gt;
&lt;br /&gt;
In Eclipse , Right - Click on the project name. you will get a pop up showing different options. select properties option.&lt;br /&gt;
click on the java build path option. add jar files by clicking "Add External Jars" button.&lt;br /&gt;
locate the directory and add jar files,&lt;br /&gt;
&lt;br /&gt;
Now we are ready with environment set. In the src folder create a package of yours. My package name is com.example.hibernate.cache.&lt;br /&gt;
&lt;br /&gt;
Now create following files.&lt;br /&gt;
&lt;br /&gt;
1) configuration file &amp;nbsp;--&amp;gt; which contains Database (or) DataSource details and hibernate properties details.&lt;br /&gt;
2) mapping file &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;--&amp;gt; which contains hbm file. A hbm file maps pojo class to table name.&lt;br /&gt;
3) Pojo class &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; --&amp;gt; Pojo class is normal java class with properties , setters &amp;amp; getters.&lt;br /&gt;
4) client applicaton &amp;nbsp; --&amp;gt; client who uses and runs the second level caching.&lt;br /&gt;
&lt;br /&gt;
Also , create a table M_COM_GNDR in database.&lt;br /&gt;
Fields like&amp;nbsp;GNDR_N_ID number, &amp;nbsp;GNDR_VC_TYP varchar,&amp;nbsp;GNDR_C_STS varchar.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Insert data into it.&lt;br /&gt;
Like&amp;nbsp;GNDR_N_ID &amp;nbsp; &amp;nbsp; ------------&amp;gt; &amp;nbsp;1&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;GNDR_VC_TYP------------&amp;gt; Male&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;GNDR_C_STS --------------&amp;gt; A&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;GNDR_N_ID &amp;nbsp; &amp;nbsp; ------------&amp;gt; &amp;nbsp;2&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; GNDR_VC_TYP------------&amp;gt; Female&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; GNDR_C_STS --------------&amp;gt; A&lt;br /&gt;
&lt;br /&gt;
"A" means Active.&lt;br /&gt;
1) Configuration file: It is an xml file. I used oracle database and so oracle driver and its port number. If any other database is used then its driver is used. I used ojdbc14.jar driver. If it is not there download it from www.java2s.com.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"&lt;br /&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;"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"&amp;gt;&lt;br /&gt;
&amp;lt;hibernate-configuration&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;lt;session-factory&amp;gt;&lt;br /&gt;
&amp;lt;!-- database connection properties &amp;nbsp;--&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.connection.driver_class"&amp;gt;oracle.jdbc.driver.OracleDriver&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.connection.url"&amp;gt;jdbc:oracle:thin:@localhost:1521:servicename&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.connection.username"&amp;gt;elearn&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.connection.password"&amp;gt;estudy&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- hibernate properties--&amp;gt;&lt;br /&gt;
&lt;span style="color: purple;"&gt;&amp;lt;!-- Oracle Dilect is important. It converts hibernate queries to sql queries. every database has it own dialect. --&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.dialect"&amp;gt;org.hibernate.dialect.OracleDialect&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: purple;"&gt;&amp;lt;!-- Enabling this property shows the converted query on the console--&amp;gt;&lt;/span&gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.show_sql"&amp;gt;true&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- To use second level cache we have to configure this property--&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;property name="hibernate.cache.provider_class"&amp;gt;org.hibernate.cache.EhCacheProvider&amp;lt;/property&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- Mapping property--&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;lt;mapping class="Gender" package="com.example.hibernate.cache" resource="com/example/hibernate/cache/Gender.hbm.xml"/&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;lt;/session-factory&amp;gt;&lt;br /&gt;
&amp;lt;/hibernate-configuration&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
2) Mapping File:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;?xml version="1.0"?&amp;gt;&lt;br /&gt;
&amp;lt;!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"&lt;br /&gt;
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&amp;gt;&lt;br /&gt;
&amp;lt;!-- Generated Mar 31, 2012 11:48:38 AM by Hibernate Tools 3.4.0.CR1 --&amp;gt;&lt;br /&gt;
&amp;lt;hibernate-mapping&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!-- here the pojo class is mapped to table name and every field is mapped to column.--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;class name="com.example.hibernate.cache.Gender" table="M_COM_GNDR"&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;cache usage="read-write"/&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;id name="id" type="java.lang.Long" column="GNDR_N_ID"&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;lt;generator class="assigned" /&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;lt;/id&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;property name="name" type="java.lang.String" column="GNDR_VC_TYP"&amp;gt;&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;lt;property name="status" type="java.lang.String" column="GNDR_C_STS"&amp;gt;&amp;lt;/property&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;lt;/class&amp;gt;&lt;br /&gt;
&amp;lt;/hibernate-mapping&amp;gt;&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
3) Pojo class:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
package com.example.hibernate.cache;&lt;/div&gt;
&lt;div&gt;
public class Gender {&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private Long id;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private String name;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private String status;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public Long getId() {&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return id;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private void setId(Long id) {&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.id = id;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public String getName() {&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return name;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void setName(String name) {&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.name = name;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public String getStatus() {&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return status;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void setStatus(String status) {&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;this.status = status;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
4) Client App&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
package com.spry.hibernate.cache;&lt;br /&gt;
&lt;br /&gt;
import org.hibernate.Session;&lt;br /&gt;
import org.hibernate.SessionFactory;&lt;br /&gt;
import org.hibernate.Transaction;&lt;br /&gt;
import org.hibernate.cfg.Configuration;&lt;br /&gt;
&lt;br /&gt;
import com.example.hibernate.cache.Gender;&lt;br /&gt;
&lt;br /&gt;
public class ClientApp {&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Configuration con = new Configuration();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;con.configure("com/example/hibernate/cache/NewFile.cfg.xml");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;SessionFactory sf = con.buildSessionFactory();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Session session = sf.openSession();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Transaction trans = session.beginTransaction();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;try&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&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;// loading the gender object from database for the first time.&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;Gender gender = (Gender)session.get(Gender.class, new Long(1));&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.println("gender.getName()=======&amp;gt;"+gender.getName());&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;//Closed the sesssion.&lt;br /&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;session.close();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;//Again opening the session.&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;session = sf.openSession();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt; trans = session.beginTransaction();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //Again when we load the object it hits the second level cache not the database.&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt; gender = (Gender)session.get(Gender.class, new Long(1));&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt; System.out.println("gender.getName()=======&amp;gt;"+gender.getName());&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt; session.close();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch(Exception e)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
we can see the output as&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Hibernate: select gender0_.GNDR_N_ID as GNDR1_0_0_, gender0_.GNDR_VC_TYP as GNDR2_0_0_, gender0_.GNDR_C_STS as GNDR3_0_0_ from M_COM_GNDR gender0_ where gender0_.GNDR_N_ID=?&lt;br /&gt;
gender.getName()=======&amp;gt;male&lt;br /&gt;
gender.getName()=======&amp;gt;male&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
in the console.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Here session means one database connection. we opened database connection in the program twice. loaded the object twice. But in the console the query hit the database only one time. second time the object is loaded from the second level cache...&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
when second level cache is enabled, hibernate loads objects from database when specified and keeps a copy of the object in second level cache. so that next time no need to hit the database. Directly we can get the object from second level cache.&lt;/div&gt;
&lt;br /&gt;
If you want to test whether second level caching is working or not, just comment out&lt;br /&gt;
"&amp;lt;cache usage="read-write"/&amp;gt;" in hbm file. Then execute the client app.&lt;br /&gt;
&lt;br /&gt;
output will be&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Hibernate: select gender0_.GNDR_N_ID as GNDR1_0_0_, gender0_.GNDR_VC_TYP as GNDR2_0_0_, gender0_.GNDR_C_STS as GNDR3_0_0_ from M_COM_GNDR gender0_ where gender0_.GNDR_N_ID=?&lt;br /&gt;
gender.getName()=======&amp;gt;male&lt;br /&gt;
Hibernate: select gender0_.GNDR_N_ID as GNDR1_0_0_, gender0_.GNDR_VC_TYP as GNDR2_0_0_, gender0_.GNDR_C_STS as GNDR3_0_0_ from M_COM_GNDR gender0_ where gender0_.GNDR_N_ID=?&lt;br /&gt;
gender.getName()=======&amp;gt;male&lt;br /&gt;
&lt;br /&gt;
2 times query is executed.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/Llzz7F3Bc7o" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/5054910018497415798/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/configuring-second-level-cache-in.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/5054910018497415798?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/5054910018497415798?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/Llzz7F3Bc7o/configuring-second-level-cache-in.html" title="Configuring Second Level Cache in Hibernate" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/configuring-second-level-cache-in.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEQFQns_eSp7ImA9WhVUEUg.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-2309280610147681483</id><published>2012-05-16T00:00:00.002-07:00</published><updated>2012-05-16T00:11:53.541-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-16T00:11:53.541-07:00</app:edited><title>Daemon Thread in Java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;span style="color: orange;"&gt;&lt;u&gt;Daemon Thread:&lt;/u&gt;&lt;/span&gt;&lt;br /&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;Daemon thread is a special kind of thread. Normally when a thread is created in java, default it is Non-Daemon.&lt;br /&gt;
Daemon thread is created by setting the setDaemon(boolean value) of Thread to "true".&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;Example&lt;/u&gt;&lt;br /&gt;
//DaemonThread implementing Runnable interface. Has to compulsory over-ride run().&lt;br /&gt;
class DaemonThread implements Runnable&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;public void run()&lt;br /&gt;
&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //background running task like memory clean, and long running processes&lt;br /&gt;
&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//DaemonClient is the client program which uses DaemonThread&lt;br /&gt;
class DaemonClient&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; public static void main(String a[])&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;DaemonThread dt = new DaemonThread(); //created DaemonThread&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;dt.setDaemon(true); &amp;nbsp;// This statement sets the thread to Daemon Thread.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;dt.start(); //started the daemon thread.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Above example consists of &amp;nbsp;Daemon thread &amp;amp; our main thread.&lt;br /&gt;
&lt;br /&gt;
There is no difference between Daemon Thread &amp;amp; Non-Daemon Thread, except the JVM waits for the non-daemon threads to finish their execution, unlike daemon threads. JVM doesn't show any mercy on Daemon Threads.&lt;br /&gt;
&lt;br /&gt;
Purpose of Daemon threads is they run in the background to support non-daemon threads.&lt;br /&gt;
Garbage Collector is an example of Daemon thread.&amp;nbsp;
&lt;br /&gt;
Garbage collector removes unreferenced objects and saves memory.&lt;br /&gt;
&lt;br /&gt;
Before starting a thread, setDaemon() must be called.
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;IllegalThreadStateException:&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&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;IllegalThreadStateException is a sub-class of IllegalArgumentException. If the setDaemon(boolean arg) is called when the thread is running then IllegalThreadStateException is thrown.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Priority of Daemon thread is similar to any other thread. Default NORM_PRIORITY : 5&lt;br /&gt;
&lt;br /&gt;
If you want to change the priority , can change by using setPriority() which takes 1 to 10 as input.&lt;br /&gt;
Other than that is input is provided, throws IllegalArgumentExcpetion.&lt;br /&gt;
&lt;br /&gt;
we can use isDaemon() to test whether a thread is daemon or not. isDaemon() returns true , if thread is daemon, else false.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/hEGX2tLAEIU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/2309280610147681483/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/daemon-thread-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/2309280610147681483?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/2309280610147681483?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/hEGX2tLAEIU/daemon-thread-in-java.html" title="Daemon Thread in Java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/daemon-thread-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DkADQHsyeip7ImA9WhVUEEU.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-2392757417633368714</id><published>2012-05-15T05:26:00.001-07:00</published><updated>2012-05-15T05:26:11.592-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-15T05:26:11.592-07:00</app:edited><title>Constructor in Java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;u&gt;Constructor:&lt;/u&gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A constructor is a special method that is used to initialize properties of a newly created object and is also called just after memory is allocated for the object.&lt;br /&gt;
Rules:&lt;br /&gt;
1) class name and method name must be same.&lt;br /&gt;
2) should have no return type, even void.&lt;br /&gt;
&lt;br /&gt;
If a constructor is not specified in a class , compiler default provides a no-argument constructor.&lt;br /&gt;
suppose ,&lt;br /&gt;
public class A&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;br /&gt;
}&lt;br /&gt;
When compiler executes the above class, a default constructor is provided by the compiler.&lt;br /&gt;
&lt;br /&gt;
suppose if i provide a constructor with a parameter then calling default constructor is a compiler error.&lt;br /&gt;
Because , compiler won't provide default constructor( i.e. no-arg constructor) if a constructor is explicitly specified.&lt;br /&gt;
&lt;br /&gt;
so&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
public class A&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;private String name;&lt;br /&gt;
&amp;nbsp; &amp;nbsp;A(String name)&lt;br /&gt;
&amp;nbsp; &amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;this.name = name;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
A a = new A(); &amp;nbsp;//gives compiler error as default constructor doesn't exist.&lt;br /&gt;
A a = new A("compiler"); //works fine.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;OverLoading of Constructors:&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
Overloading is an example for polymorphism. Constructor overloading means constructors having same name but varies with parameter list.&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
Constructors can be overloaded. Within a single class we can have any number of constructors that differs in parameter list.&lt;br /&gt;
Example:&lt;br /&gt;
public class A&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
public A()&lt;br /&gt;
{&lt;br /&gt;
// &amp;nbsp;Default constructor&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public A(String name)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; this.name = name; &amp;nbsp;// constructor with single parameter&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
/*&lt;br /&gt;
like this we can have any number of constructors with varying parameter list and types..&lt;br /&gt;
*/&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Constructor Chaining:&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
Constructor chaining means calling a constructor from another constructor.&lt;br /&gt;
&lt;br /&gt;
Constructor chaining can be done in 2 ways.&lt;br /&gt;
&lt;br /&gt;
1) we can call a constructor from another constructor in the same class using this keyword.&lt;br /&gt;
This can be done as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
public class Person&lt;br /&gt;
{&lt;br /&gt;
private String name;&lt;br /&gt;
private int age;&lt;br /&gt;
public&amp;nbsp;
Person&amp;nbsp;()&lt;br /&gt;
{&lt;br /&gt;
// &amp;nbsp;Default constructor&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public&amp;nbsp;
Person&amp;nbsp;(String name)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; this.name = name;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;this(name,25);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public&amp;nbsp;
Person&amp;nbsp;(String name, int age)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;this.age = age;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
The above example calls a constructor from another constructor using this keyword.&lt;br /&gt;
Suppose only name is specified then , Person object age is default taken as 25&lt;br /&gt;
you can also customize person age using the 3rd constructor.&lt;br /&gt;
&lt;br /&gt;
2) we can call the super class constructor using super keyword if inheritence is allowed.&lt;br /&gt;
&lt;br /&gt;
suppose we have 2 classes . one parent and child class.&lt;br /&gt;
class Parent&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;Parent()&lt;br /&gt;
&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;System.out.println("Parent constructor");&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
class Child extends Parent&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp;Child()&lt;br /&gt;
&amp;nbsp; {&lt;br /&gt;
&amp;nbsp; &amp;nbsp; System.out.println("child constructor");&lt;br /&gt;
&amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Client&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; public static void main(String a[])&lt;br /&gt;
&amp;nbsp; &amp;nbsp;{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Child c = new Child();&lt;br /&gt;
&amp;nbsp; &amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
If the above example is executed, we get output as:&lt;br /&gt;
Parent Constructor&lt;br /&gt;
Child Constructor&lt;br /&gt;
&lt;br /&gt;
Here we did not call super class constructor. But implicitly super class constructor will be called by the child class constructor. In the child class constructor , super() is the first statement implicitly provided by the compiler if default constructor is provided.&lt;br /&gt;
&lt;br /&gt;
* Suppose in the super class we provided a constructor with parameter. In the child class we provided a default constructor, then compiler error is generated.&lt;br /&gt;
&lt;br /&gt;
Example:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
class Parent&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Parent(String name)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
}&lt;br /&gt;
class Child extends Parent&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: red;"&gt;Compiler Error: &lt;/span&gt;&amp;nbsp; implicit super constructor Parent() is undefined for default constructor. Must define an explicit&amp;nbsp;constructor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;Subclasses constructors&amp;nbsp;&lt;/span&gt;&lt;em style="background-attachment: initial; background-clip: initial; background-color: white; background-image: initial; background-origin: initial; border-bottom-width: 0px; border-color: initial; border-image: initial; border-left-width: 0px; border-right-width: 0px; border-style: initial; border-top-width: 0px; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px; vertical-align: baseline;"&gt;may&lt;/em&gt;&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&amp;nbsp;specify as the first thing they do which constructor in the superclass to invoke before executing the code in the subclass's constructor.&lt;/span&gt;
&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;* &amp;nbsp;suppose if super class constructor access is given as private then inheritence is not possible. suppose if Child tries to extend Parent then compiler error is shown.&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;* &amp;nbsp;&lt;/span&gt;&lt;span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;constructor can throw same Exception or supertype of parentclass exception&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: x-small; line-height: 16px;"&gt;but cannot handle them..&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;Suppose if super class constructor throws any checked exception then child class constructor cannot handle it. It has to throw the exception to caller.&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 16px;"&gt;we cannot handle the exception because in the child constructor the first statement must be super() (calling the super class constructor) . we cannot write try-catch blocks. so we cannot handle the exceptions.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: Arial, 'Liberation Sans', 'DejaVu Sans', sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
Thank You,&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/JDWA_PGO7eQ" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/2392757417633368714/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/constructor-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/2392757417633368714?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/2392757417633368714?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/JDWA_PGO7eQ/constructor-in-java.html" title="Constructor in Java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/constructor-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQMRnY7eCp7ImA9WhVVGUQ.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-7413904358735737701</id><published>2012-05-14T04:52:00.000-07:00</published><updated>2012-05-14T04:53:07.800-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-14T04:53:07.800-07:00</app:edited><title>wait() and notify() in java Multithreading</title><content type="html">Multithreading: multiple threads executing parallely. Multiple threads sharing a resource gives race condition.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Race condition:&lt;/u&gt;&amp;nbsp;Multiple threads sharing a resource gives inefficinent results.&lt;br /&gt;
&lt;br /&gt;
To know about Race condition please click on this link:&lt;br /&gt;
&lt;a href="http://javabrowsers.blogspot.in/2012/05/what-is-race-condition.html"&gt;http://javabrowsers.blogspot.in/2012/05/what-is-race-condition.html&lt;/a&gt;
&lt;br /&gt;
&lt;br /&gt;
To avoid Race condition we use synchronized methods and blocks. To intercommunicate threads we use wait(), notify() or notifyAll()'s. wait/notify mechanism is a way to communicate between threads.&lt;br /&gt;
The idea is as follows:&lt;br /&gt;
1) one or more threads wait for a signal.&lt;br /&gt;
2) another thread comes in and notifies the waiting threads.&lt;br /&gt;
&lt;br /&gt;
The wait-notify pattern is used when one thread needs to tell other threads that some event has occurred.&lt;br /&gt;
&lt;br /&gt;
These methods are final methods in Object class. wait() throws InterruptedException. These methods wait(), notify() and notifyAll() must be used in synchronized method or block only. If used outside of synchronized method or block a run-time exception occurs, IllegalMonitorStateException.&lt;br /&gt;
&lt;br /&gt;
To avoid Race condition , we write wait(), notify(),notifyAll()'s in synchronized method or block.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;

suppose take producer - cosumer problem:&lt;br /&gt;&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;h4&gt;

&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;1. The Producer thread tests the condition (buffer is full or not) and confirms that it must wait (after finding buffer is full).&lt;/span&gt;&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;2. The Consumer thread sets the condition after consuming an element from buffer.&lt;/span&gt;&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;3. The Consumer thread calls the notify () method; this goes unheard since the Producer thread is &amp;nbsp; &amp;nbsp; &amp;nbsp;not yet waiting.&lt;/span&gt;&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;4. The Producer thread calls the wait () method and goes into waiting state.&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h4&gt;
&lt;h4&gt;

&lt;span style="font-family: Verdana; font-size: 8pt;"&gt;So due to race condition here we potential lost a notification and if we use buffer or just one element Produce thread will&amp;nbsp;&lt;span style="color: #0066cc;"&gt;be waiting&lt;/span&gt;&amp;nbsp;forever and your program will hang.&lt;/span&gt;&lt;/h4&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small; text-align: left;"&gt;For example cosider following limited connection pool example using wait(),notify()&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small; text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="text-align: left;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;package threads;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;import java.util.*;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;import java.sql.*;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;//implements connection pool&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;class ThreadWait&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private static List&amp;lt;Connection&amp;gt; connections = createConnections(); //shared resource&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;//method creates 5 db connections&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static List&amp;lt;Connection&amp;gt; createConnections()&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Connection con = null;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;List&amp;lt;Connection&amp;gt; connections = new ArrayList&amp;lt;Connection&amp;gt;();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;try&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;Class.forName("oracle.jdbc.driver.OracleDriver");&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;for(int i = 0;i&amp;lt;5;i++)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:serviceName","username","password");&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;connections.add(con);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;   &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.println("connections.size()===&amp;gt;"+connections.size());&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch(ClassNotFoundException e)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;catch(Exception e)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return connections;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;// End of connections&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;//gets a connection&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;public static Connection getConnection() throws InterruptedException&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Connection con = null;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;//If connections list is empty then wait() is called on connections object.&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;synchronized(connections)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;if(connections.isEmpty())&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;connections.wait();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;con = connections.remove(0);&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("in get Connection "+Thread.currentThread()+" ------ "+connections.size());&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;return con;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;//end of getConnection&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;public static void returnConnections(Connection con)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;synchronized(connections)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;connections.add(con);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;connections.notify();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("in return Connection"+Thread.currentThread()+" ------ "+connections.size());&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;//job to be done by threads&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;class ThreadDemo implements Runnable&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public void run()&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println("in run method"+Thread.currentThread());&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Connection con &amp;nbsp;= null;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Statement stmt = null;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;try {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;con = ThreadWait.getConnection();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.println("got connection object: "+Thread.currentThread());&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;stmt = con.createStatement();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;stmt.executeQuery("select * FROM DUAL");&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;ThreadWait.returnConnections(con);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.println("connection returned"+Thread.currentThread());&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;} catch (InterruptedException e) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;// TODO Auto-generated catch block&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch (SQLException e) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;// TODO Auto-generated catch block&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;e.printStackTrace();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;//client program&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;public class ThreadAdvance {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;ThreadDemo demo = new ThreadDemo();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread1 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread2 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread3 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread4 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread5 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread6 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread7 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Thread thread8 = new Thread(demo);&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread1.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread2.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread3.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread4.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread5.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread6.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread7.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;thread8.start();&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: x-small;"&gt;}&lt;/span&gt;&lt;br /&gt;
&lt;div style="font-family: 'Trebuchet MS', Verdana, Arial, sans-serif; font-size: small;"&gt;
&lt;br /&gt;&lt;/div&gt;
ThreadWait is the class that implements connection pool. copy this program to eclipse or any ide and run the program.&lt;br /&gt;
&lt;span style="text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="text-align: left;"&gt;Multiple threads request connection objects from list connections.&lt;/span&gt;
&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;If no connection is currently available (i.e.&amp;nbsp;&lt;/span&gt;&lt;tt style="font-size: 13px;"&gt;connections&lt;/tt&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;&amp;nbsp;is empty), then we need to wait until one becomes available. Then we return the first available connection.&lt;/span&gt;&lt;br /&gt;
&lt;div style="text-align: -webkit-auto;"&gt;
&lt;span style="text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: -webkit-auto;"&gt;
&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;Note first of all that we synchronize on the connection list. We then check if the list is empty. If, and while, it is, we "wait" on the list. In order to wait on an object, we&amp;nbsp;&lt;/span&gt;&lt;strong style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;must be synchronized&lt;/strong&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;&amp;nbsp;on that object. But our thread will automatically&amp;nbsp;&lt;/span&gt;&lt;strong style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;release the lock temporarily while waiting&lt;/strong&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;. Calling&amp;nbsp;&lt;/span&gt;&lt;tt style="font-size: 13px;"&gt;wait()&lt;/tt&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;&amp;nbsp;means that our thread will be suspended until it is "notified". Our thread will be "notified", and thus woken up, when another thread calls&amp;nbsp;&lt;/span&gt;&lt;tt style="font-size: 13px;"&gt;notify()&lt;/tt&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;&amp;nbsp;on the object that we're waiting on (in this case, the connection list). When our thread wakes up, it automatically regains the lock. We can now check again that the list is not empty, and if it isn't, safely take out the first connection. This checking and removing will be atomic because we have the lock on the list.&lt;/span&gt;&amp;nbsp;&lt;span style="text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: -webkit-auto;"&gt;
&lt;span style="text-align: left;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style="text-align: -webkit-auto;"&gt;
&lt;/div&gt;
&lt;div style="font-size: 13px;"&gt;
&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;Again with a&amp;nbsp;&lt;/span&gt;&lt;span style="font-family: monospace;"&gt;synchronized&lt;/span&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;&amp;nbsp;lock on the list, we add the given connection to the list. Then, while still synchronized on it, we call&amp;nbsp;&lt;/span&gt;&lt;tt style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;notify()&lt;/tt&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;&amp;nbsp;on the connection. Calling&amp;nbsp;&lt;/span&gt;&lt;tt style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;notify()&lt;/tt&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;&amp;nbsp;means: "if there is at least one thread waiting on this object, please wake up&amp;nbsp;&lt;/span&gt;&lt;em style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;one&lt;/em&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;&amp;nbsp;of those threads". In cases such as this, waking up a single random thread is the functionality we want: we've only added one connection to the list, so there's no point waking up more than one waiting thread. Note that we have no control over&amp;nbsp;&lt;/span&gt;&lt;em style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;which&lt;/em&gt;&lt;span style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif;"&gt;&amp;nbsp;waiting thread is woken up. In particular, we can't say "wake up the one that's been waiting longest". (Nor will most JVMs or OSs use such a policy: ensuring this kind of "fairness" turns out to decrease throughput considerably.)&lt;/span&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;
Note that although&amp;nbsp;&lt;tt&gt;notify()&lt;/tt&gt;&amp;nbsp;wakes up one of the waiting threads, the first thing that that thread needs to do is re-acquire the lock that our thread is currently holding. So&amp;nbsp;&lt;strong&gt;after calling&amp;nbsp;&lt;tt&gt;notify()&lt;/tt&gt;, we should exit the&amp;nbsp;&lt;tt&gt;synchronized&lt;/tt&gt;&amp;nbsp;block as quickly as possible&lt;/strong&gt;.&lt;/div&gt;
&lt;div style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="font-family: Verdana, Helvetica, 'Gill Sans', Verdana, arial, sans-serif; font-size: 13px;"&gt;
&lt;/div&gt;
A couple of small points about this wait-notify pattern are worth clarifying:&lt;br /&gt;
&lt;ul style="padding-left: 0.75em;"&gt;
&lt;li&gt;The waiting thread can be&amp;nbsp;&lt;em&gt;interrupted&lt;/em&gt;&amp;nbsp;(either for some spurious OS/hardware reason or, more likely, because&lt;tt&gt;interrupt()&lt;/tt&gt;&amp;nbsp;is called on the thread from within Java). Thus,&amp;nbsp;&lt;tt&gt;wait()&lt;/tt&gt;&amp;nbsp;can throw&amp;nbsp;&lt;tt&gt;InterruptedException&lt;/tt&gt;. Unless you're in code that's dealing with the "outer logic" of a thread's function, the most appropriate thing is usually just to throw the exception up.&lt;/li&gt;
&lt;li&gt;When a thread is 'awoken' from&amp;nbsp;&lt;tt&gt;wait()&lt;/tt&gt;, it can't tell&amp;nbsp;&lt;em&gt;why&lt;/em&gt;&amp;nbsp;it's being woken. In particular, it&amp;nbsp;&lt;strong&gt;isn't necessarily because it has been notified&lt;/strong&gt;! (The OS could just spuriously wake it up for some reason.) So we have to&amp;nbsp;&lt;strong&gt;check the size of the list again&lt;/strong&gt;&amp;nbsp;before pulling out a connection.&lt;/li&gt;
&lt;li&gt;It's crucial that we&amp;nbsp;&lt;strong&gt;don't wait if the list&amp;nbsp;&lt;em&gt;isn't&lt;/em&gt;&amp;nbsp;empty&lt;/strong&gt;. If we did, we would essentially sit waiting for a notify that wasn't about to come. (This is actually quite a common programming error in this type of code, because when you're writing it, your mind is saying "I need to wait until there's something in the list".)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/4msxCWdC4TU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/7413904358735737701/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/wait-and-notify-in-java-multithreading.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7413904358735737701?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7413904358735737701?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/4msxCWdC4TU/wait-and-notify-in-java-multithreading.html" title="wait() and notify() in java Multithreading" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/wait-and-notify-in-java-multithreading.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CUUBQXk8fCp7ImA9WhVVFk4.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-726214648461102909</id><published>2012-05-10T00:00:00.001-07:00</published><updated>2012-05-10T00:00:50.774-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-10T00:00:50.774-07:00</app:edited><title>Use of join() of Thread in java</title><content type="html">&lt;br /&gt;
&lt;pre&gt;public final void &lt;b&gt;join&lt;/b&gt;()
                throws &lt;a href="file:///D:/1Vipul%20Reddy/ALL%20API/J2SDK%20API/java/lang/InterruptedException.html" title="class in java.lang"&gt;InterruptedException&lt;/a&gt;&lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre&gt;The thread on which this method is called will execute until its termination and remainig threads have to wait until its death.&lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre&gt;Suppose i am having 4 threads. Each thread does me a sequence process. This means my threads have to execute in a sequence, step-by-step. For this , i use join() of Thred class.&lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;u&gt;Example:-&lt;/u&gt;&lt;/pre&gt;
&lt;pre&gt;// Created 4 threads Thread1, Thread2, Thread3 , Thread4&lt;/pre&gt;
&lt;pre&gt;class Thread1 extends Thread
{
 public void run()
 {
  System.out.println("First Thread");
 }
}
class Thread2 extends Thread
{
 public void run()
 {
  System.out.println("Second Thread");
 }
}
class Thread3 extends Thread
{
 public void run()
 {
  System.out.println("Third Thread");
 }
}
class Thread4 extends Thread
{
 public void run()
 {
  System.out.println("Fourth Thread");
 }
}
public class Immutable {

 public static void main(String[] args) {
 Thread1 t1 = new Thread1();
 Thread2 t2 = new Thread2();
 Thread3 t3 = new Thread3();
 Thread4 t4 = new Thread4();
 
 System.out.println("main thread");
 
  t1.start();
  t2.start();
  t3.start();
  t4.start();
  
 
 }

}
&lt;/pre&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;pre&gt;Created 4 threads Thread1, Thread2, Thread3 , Thread4. Each thread prints a statement on the console.When this program is executed, sop's print in a random order.&lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre&gt;But, i want the order in sequence.Like, &lt;/pre&gt;
&lt;pre&gt;first thread&lt;/pre&gt;
&lt;pre&gt;second thread&lt;/pre&gt;
&lt;pre&gt;third thread&lt;/pre&gt;
&lt;pre&gt;fourth thread&lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre&gt;then &lt;/pre&gt;
&lt;pre&gt;
&lt;/pre&gt;
&lt;pre&gt;&lt;u&gt;Program:-&lt;/u&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;u&gt;
&lt;/u&gt;&lt;/pre&gt;
&lt;pre&gt;class Thread1 extends Thread
{
 public void run()
 {
  System.out.println("First Thread");
 }
}
class Thread2 extends Thread
{
 public void run()
 {
  System.out.println("Second Thread");
 }
}
class Thread3 extends Thread
{
 public void run()
 {
  System.out.println("Third Thread");
 }
}
class Thread4 extends Thread
{
 public void run()
 {
  System.out.println("Fourth Thread");
 }
}
public class Immutable {

 public static void main(String[] args) {
 Thread1 t1 = new Thread1();
 Thread2 t2 = new Thread2();
 Thread3 t3 = new Thread3();
 Thread4 t4 = new Thread4();
 
 System.out.println("main thread");
 try
 {
  t1.start();
  t1.join();
  t2.start();
  t2.join();
  t3.start();
  t3.join();
  t4.start();
  t4.join();
 }catch(InterruptedException e)
 {
  e.printStackTrace();
 }
 }

}&lt;u&gt;
&lt;/u&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
Then the output is sequntial as expected. Thank You.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/qWbZrhraxxo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/726214648461102909/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/use-of-join-of-thread-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/726214648461102909?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/726214648461102909?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/qWbZrhraxxo/use-of-join-of-thread-in-java.html" title="Use of join() of Thread in java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/use-of-join-of-thread-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEYBQ349eyp7ImA9WhJSFUo.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-393935815658189641</id><published>2012-05-09T00:38:00.001-07:00</published><updated>2012-07-06T05:02:32.063-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-07-06T05:02:32.063-07:00</app:edited><title>Create an Immutable Object in Java</title><content type="html">&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;
&lt;u&gt;Immutable Objects:&lt;/u&gt;&amp;nbsp; &amp;nbsp;When you have a reference to an instance of an object, the contents of that object cannot be altered.&lt;br /&gt;
&lt;div&gt;
&lt;u&gt;Mutable Objects:&lt;/u&gt;&amp;nbsp;When you have a reference to an istance of an object, the contents of that object can be altered.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
String is an immutable class. That means state of the object after creation cannot be changed.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;u&gt;For Example:&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
String myString = "oldString";&lt;/div&gt;
&lt;div&gt;
myString.toUpperCase();&lt;/div&gt;
&lt;div&gt;
System.out.println(oldString);&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Here the state of the object is not changed. But a new string "OLDSTRING" is created in the string constant pool. we will know about String constant pool in our next discussions.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Template of Immutable Class:-&lt;/div&gt;
&lt;div&gt;
1) &amp;nbsp;fields must be private.&lt;/div&gt;
&lt;div&gt;
2) No setters. Only once the propeties have to be set.&lt;/div&gt;
&lt;div&gt;
3) Ensure that methods cannot be over-ridden by either making the class final (Strong Immutability) or the &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;methods final(Weak Immutability).&lt;/div&gt;
&lt;div&gt;
4) If &amp;nbsp;a filed isn't primitive (or) immutable, make a deep clone on the way in and on the way out.( i mean while setting and retrieving).&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Example:-&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
final public class Person&lt;/div&gt;
&lt;div&gt;
{&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; private String firstName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; private String lastName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; private Date dob;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; Person(String firstName,String lastName,Date dob)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.firstName = firstName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.lastName = lastName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.dob = dob;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; public String getFirstName()&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return this.firstName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;nbsp; public String getLastName()&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return this.lastName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;nbsp; public String getDob()&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return this.dob;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
}&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Above class is strong immutable . but Date class is mutable.&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
so we should make deep copies of mutable data.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Date d1 = new Date();&lt;/div&gt;
&lt;div&gt;
Person p = new Person("first","last",d);&lt;/div&gt;
&lt;div&gt;
Date d2 = d.getDob();&lt;/div&gt;
&lt;div&gt;
d2.setMonth(d2.getMonth()+1); //changes the state of the date&lt;/div&gt;
&lt;div&gt;
System.out.println(p.getDob());&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
In the above code, we created &amp;nbsp;a Person object. As Date object is mutable , the month is modified. Now the dob field refers to altered Date. This breaks the immutability. To overcome that&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Example:&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
final public class Person&lt;/div&gt;
&lt;div&gt;
{&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; private String firstName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; private String lastName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; private Date dob;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; Person(String firstName,String lastName,Date dob)&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.firstName = firstName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.lastName = lastName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;this.dob = new Date(dob.getTime());&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; }&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; public String getFirstName()&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return this.firstName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;nbsp; public String getLastName()&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return this.lastName;&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
&amp;nbsp; public String getDob()&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; {&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;return new Date(this.dob.getTime());&lt;/div&gt;
&lt;div&gt;
&amp;nbsp; &amp;nbsp;}&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
}&amp;nbsp;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
This class is fully immutable..&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
String and wrapper classes like Integer, Float, Double, Long etc., are all Immutable.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Advantage of Immutability:&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;ol style="text-align: left;"&gt;
&lt;li&gt;Immutable objects are inherently thread safe. They require no synchronization. 2 or more threads can work on the same immutable object without any conflict. Therefore, immutable objects can be shared freely.&lt;/li&gt;
&lt;li&gt;Security: Application is ensured that any sensitive read-only information cannot be altered.&lt;/li&gt;
&lt;li&gt;Immutable objects are less prone to error and are more secure.&lt;/li&gt;
&lt;li&gt;Immutable objects are simple.&lt;/li&gt;
&lt;li&gt;An immutable object can be in exactly one state, &amp;nbsp;the state in which it was created.&lt;/li&gt;
&lt;/ol&gt;
&lt;div&gt;
The only disadvantage of immutable classes is that they require a separate object for each distinct value. separate object means new object.&lt;/div&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/mAmmLnsPF6w" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/393935815658189641/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/create-immutable-object-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/393935815658189641?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/393935815658189641?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/mAmmLnsPF6w/create-immutable-object-in-java.html" title="Create an Immutable Object in Java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/create-immutable-object-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0IGQH88eSp7ImA9WhVVGUQ.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-8952755017481757510</id><published>2012-05-08T00:25:00.001-07:00</published><updated>2012-05-14T04:38:41.171-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-14T04:38:41.171-07:00</app:edited><title>What is Race Condition</title><content type="html">&lt;u&gt;Race Condition:&lt;/u&gt;&lt;br /&gt;
Race conditions occur in multi - threaded applications.&amp;nbsp;A race condition exists when multiple threads access a shared resource that could give in unexpected results.&lt;br /&gt;
&lt;br /&gt;
Ex:- &lt;u&gt;Check &amp;amp; Act&lt;/u&gt;&lt;br /&gt;
x = 5;&lt;br /&gt;
if(x == 5) //check&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; y &amp;nbsp;= x *2; &amp;nbsp;//act&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
//If x is changed by another thread in between the if(x==5) and y = x*2, y will not be equal to 10.&lt;br /&gt;
&lt;br /&gt;
In order to prevent Race conditions occurring, typically we put a lock around the shared data to ensure that only one thread access the data at a time.&lt;br /&gt;
&lt;br /&gt;
//obtain the lock for x&lt;br /&gt;
&lt;br /&gt;
if(x == 5) //check&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp; &amp;nbsp; y &amp;nbsp;= x *2; &amp;nbsp;//Nothing can change the x value until the lock is released.&lt;br /&gt;
}&lt;br /&gt;
//Release the lock.&lt;br /&gt;
&lt;br /&gt;
lock mechanism is done by synchronized method or block.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/CjZ5yngqn0A" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/8952755017481757510/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/what-is-race-condition.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/8952755017481757510?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/8952755017481757510?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/CjZ5yngqn0A/what-is-race-condition.html" title="What is Race Condition" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/what-is-race-condition.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DEQHR3k-fSp7ImA9WhVVEE4.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-7049818821925259956</id><published>2012-05-03T02:12:00.002-07:00</published><updated>2012-05-03T02:12:16.755-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-03T02:12:16.755-07:00</app:edited><title>No. of sundays fell on the first of the month during twentieth century</title><content type="html">&lt;u&gt;Program:-&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
import java.util.Calendar;&lt;br /&gt;
public class TwentiethCentury {&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Calendar calendar = Calendar.getInstance();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int count = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for(int i = 1901;i&amp;lt;=2000;i++)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;int j = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;while(j&amp;lt;=11)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;calendar.set(i, j, 1);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;System.out.println(calendar.getTime());&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;int result = calendar.get(Calendar.DAY_OF_WEEK);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;if(result==1)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;count++;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;j++;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.print(count);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;div style="text-decoration: underline;"&gt;
&lt;br /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/0gnLJ69hTII" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/7049818821925259956/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/no-of-sundays-fell-on-first-of-month.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7049818821925259956?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7049818821925259956?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/0gnLJ69hTII/no-of-sundays-fell-on-first-of-month.html" title="No. of sundays fell on the first of the month during twentieth century" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/no-of-sundays-fell-on-first-of-month.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04GRngzeCp7ImA9WhVVEE8.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4335592304129251926</id><published>2012-05-02T23:18:00.002-07:00</published><updated>2012-05-02T23:18:47.680-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-02T23:18:47.680-07:00</app:edited><title>Finding the Nth Prime Number</title><content type="html">Generally speaking 1 is not a prime. The first prime number is 2. After 2 there are no prime numbers that are even. so only odd numbers fill the remaining set of primes.&lt;br /&gt;
&amp;nbsp; so start at 3 for the second prime, check the primeness, then increment by 2. Repeat until You find nth prime.&lt;br /&gt;
we will check prime of odd numbers here. odd numbers are not divisible by 2. so iteration can start at 3.&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Program:-&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
import java.util.Scanner;&lt;br /&gt;
public class NPrimeNumber {&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Scanner console = new Scanner(System.in);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println("enter the nth prime number to find: ");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int nthPrime = console.nextInt();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int prime = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;if(nthPrime&amp;lt;=0)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.println("Wrong Input");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;else&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;if(nthPrime==1)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;System.out.println("Prime value "+2);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;}else&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;if(nthPrime==2)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;System.out.println("Prime value "+3);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;}else&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;int number = 3;//last prime value i tested&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;prime = 2;//identified the first 2 primes.&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;//&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;continue testing until prime == nth prime&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;while(prime&amp;lt;nthPrime)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;boolean isFlag = true;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;number+=2;&lt;br /&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; int sqrt =&amp;nbsp;Math.sqrt(number);&lt;br /&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;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;for(int i = 3;i&amp;lt;=sqrt;i++)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;{&lt;br /&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;//isFlag Becomes false only if the number is odd number and not prime.&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;       &lt;/span&gt;if(number%i==0)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;       &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;        &lt;/span&gt;isFlag = false;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;        &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;       &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;        &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;}&lt;br /&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; //prime value increments if and only if number is prime number&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;if(isFlag==true)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;       &lt;/span&gt;prime += 1;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;      &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;         &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;System.out.println("Prime value "+number);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/Ny0kP5KIAgo" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4335592304129251926/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/finding-nth-prime-number.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4335592304129251926?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4335592304129251926?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/Ny0kP5KIAgo/finding-nth-prime-number.html" title="Finding the Nth Prime Number" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/finding-nth-prime-number.html</feedburner:origLink></entry><entry gd:etag="W/&quot;C0QNQ3Y5fSp7ImA9WhVWGUk.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-5507985668738115550</id><published>2012-05-01T23:49:00.003-07:00</published><updated>2012-05-01T23:49:52.825-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-01T23:49:52.825-07:00</app:edited><title>Printing Prime Numbers within Given Number</title><content type="html">This program prints prime numbers within the specified range:&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Program:&lt;/u&gt;&lt;br /&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
//Printing prime numbers specified in the given range.&lt;br /&gt;
import java.util.Scanner;&lt;br /&gt;
public class PrintPrime&lt;br /&gt;
{&lt;br /&gt;
public static void main(String a[])&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Scanner console &amp;nbsp;= new Scanner(System.in);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("Enter the number");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;int num = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; num = console.nextInt();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("Prime Numbers are: ");&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;for(int i=2;i&amp;lt;=num;i++)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;int count = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for(int j=2;j&amp;lt;=i;j++)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;if(i%j==0)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;count++;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;if(count == 1)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.print(i+" ");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Enhanced Program that reduces for loop iterations:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//Printing prime numbers specified in the given range.&lt;br /&gt;
import java.util.Scanner;&lt;br /&gt;
public class PrintPrime&lt;br /&gt;
{&lt;br /&gt;
public static void main(String a[])&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;Scanner console &amp;nbsp;= new Scanner(System.in);&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("Enter the number");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;int num = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt; num = console.nextInt();&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("Prime Numbers are: ");&lt;br /&gt;
&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;for(int i=2;i&amp;lt;=num;i++)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;int count = 0;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for(int j=2;j&amp;lt;=Math.sqrt(i);j++)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;if(i%j==0)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;count++;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;if(count==0)&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.print(i+" ");&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/zbjlDq155XA" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/5507985668738115550/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/printing-prime-numbers-within-given.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/5507985668738115550?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/5507985668738115550?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/zbjlDq155XA/printing-prime-numbers-within-given.html" title="Printing Prime Numbers within Given Number" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/printing-prime-numbers-within-given.html</feedburner:origLink></entry><entry gd:etag="W/&quot;A0YFQH48eSp7ImA9WhVWGU4.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-2601355361248348824</id><published>2012-05-01T22:53:00.001-07:00</published><updated>2012-05-01T23:11:51.071-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-05-01T23:11:51.071-07:00</app:edited><title>Check Whether a Given Number is Prime OR Not</title><content type="html">//Program takes input using Scanner class and checks whether the input number is prime (or) not.&lt;br /&gt;
&lt;br /&gt;
//Program:-&lt;br /&gt;
&lt;br /&gt;
//check for prime number existence or not&lt;br /&gt;
import java.util.Scanner;&lt;br /&gt;
public class PrimeExisting&lt;br /&gt;
{&lt;br /&gt;
public static void main(String a[])&lt;br /&gt;
{&lt;br /&gt;
Scanner console = new Scanner(System.in); //Scanner associated with Standard Input Device&lt;br /&gt;
int givenNum = 0;&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;givenNum &amp;nbsp;= console.nextInt();&lt;br /&gt;
//Reading input From console. if input is not a number Scanner throws Excpetion (InputMismatchException)&lt;br /&gt;
}catch(Exception e)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("Wrong Input:");&lt;br /&gt;
}&lt;br /&gt;
int count = 0; //counts the number of times the givenNum divided by the iteration number gives remainder '0'&lt;br /&gt;
for(int i=2;i&amp;lt;=givenNum;i++) //No need to check with 1&lt;br /&gt;
{&lt;br /&gt;
if(givenNum%i==0)&lt;br /&gt;
{&lt;br /&gt;
count++;&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
System.out.println(count);&lt;br /&gt;
if(count==1)&lt;br /&gt;
{&lt;br /&gt;
System.out.println(givenNum+" is prime");&lt;br /&gt;
}else&lt;br /&gt;
{&lt;br /&gt;
System.out.println(givenNum+" is not prime");&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;u&gt;Enhanced Prime Number checking Program:&lt;/u&gt;&lt;br /&gt;
&lt;br /&gt;
In this program we square root the given number to reduce the for loop iterations.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
//check for prime number existence or not&lt;br /&gt;
import java.util.Scanner;&lt;br /&gt;
public class PrimeExisting&lt;br /&gt;
{&lt;br /&gt;
public static void main(String a[])&lt;br /&gt;
{&lt;br /&gt;
Scanner console = new Scanner(System.in);&lt;br /&gt;
int givenNum = 0;&lt;br /&gt;
try&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;givenNum &amp;nbsp;= console.nextInt();&lt;br /&gt;
}catch(Exception e)&lt;br /&gt;
{&lt;br /&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;System.out.println("Wrong Input:");&lt;br /&gt;
}&lt;br /&gt;
int count = 0;&lt;br /&gt;
double sqrt = Math.sqrt(givenNum);&lt;br /&gt;
System.out.println("sqrt value: &lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;"+sqrt);&lt;br /&gt;
for(int i=2;i&amp;lt;=sqrt;i++)&lt;br /&gt;
{&lt;br /&gt;
if(givenNum%i==0)&lt;br /&gt;
{&lt;br /&gt;
count++;&lt;br /&gt;
}&lt;br /&gt;
System.out.println("i value: "+i);&lt;br /&gt;
}&lt;br /&gt;
System.out.println(count);&lt;br /&gt;
if(count==0)&lt;br /&gt;
{&lt;br /&gt;
System.out.println(givenNum+" is prime");&lt;br /&gt;
}else&lt;br /&gt;
{&lt;br /&gt;
System.out.println(givenNum+" is not prime");&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
O/P:- javac PrimeExisting.java , press Enter.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; java PrimeExisting &amp;nbsp;877&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://users.tru.eastlink.ca/~brsears/math/primperf.htm" target="_blank"&gt;Reference site to check prime numbers below 1000&lt;/a&gt;&lt;br /&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/9lNYqNDcUfM" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/2601355361248348824/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/05/check-whether-given-number-is-prime-or.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/2601355361248348824?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/2601355361248348824?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/9lNYqNDcUfM/check-whether-given-number-is-prime-or.html" title="Check Whether a Given Number is Prime OR Not" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/05/check-whether-given-number-is-prime-or.html</feedburner:origLink></entry><entry gd:etag="W/&quot;Ak4ARH4_eyp7ImA9WhVWF0o.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-3920466768916437813</id><published>2012-04-30T02:42:00.002-07:00</published><updated>2012-04-30T02:42:25.043-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-30T02:42:25.043-07:00</app:edited><title>Fibonacci Numbers in O(1) Time</title><content type="html">&lt;br /&gt;
&lt;ol style="background-color: white; font-family: Verdana, sans-serif; font-size: 11px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 3em; padding-right: 0px; padding-top: 0px; text-align: -webkit-left;"&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&lt;span style="font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;public&lt;/span&gt;&amp;nbsp;&lt;span style="color: #993333; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;int&lt;/span&gt;&amp;nbsp;fib&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #993333; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;int&lt;/span&gt;&amp;nbsp;n&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;{&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style="color: #993333; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;double&lt;/span&gt;&amp;nbsp;x =&amp;nbsp;&lt;a href="http://www.google.com/search?q=allinurl%3AMath+java.sun.com&amp;amp;bntl=1" style="color: black; cursor: pointer; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: #aaaadd; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Math&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: #006600; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;sqrt&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;5&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;/&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;5&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&amp;nbsp; &amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style="color: #993333; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;double&lt;/span&gt;&amp;nbsp;termOne = x *&amp;nbsp;&lt;a href="http://www.google.com/search?q=allinurl%3AMath+java.sun.com&amp;amp;bntl=1" style="color: black; cursor: pointer; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: #aaaadd; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Math&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: #006600; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;pow&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;+sqrt&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;5&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;/&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt;, n&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style="color: #993333; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;double&lt;/span&gt;&amp;nbsp;termTwo = -x *&amp;nbsp;&lt;a href="http://www.google.com/search?q=allinurl%3AMath+java.sun.com&amp;amp;bntl=1" style="color: black; cursor: pointer; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;&lt;span style="color: #aaaadd; font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Math&lt;/span&gt;&lt;/a&gt;.&lt;span style="color: #006600; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;pow&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;1&lt;/span&gt;-sqrt&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;5&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;/&lt;span style="color: #cc66cc; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;2&lt;/span&gt;, n&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;span style="font-weight: bold; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;return&lt;/span&gt;&amp;nbsp;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;&lt;span style="color: #993333; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;int&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;(&lt;/span&gt;termOne + termTwo&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;)&lt;/span&gt;;&lt;/div&gt;
&lt;/li&gt;
&lt;li style="background-color: #fbfbfb; border-top-color: white; border-top-style: solid; border-top-width: 3px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 1px; padding-left: 0px; padding-right: 0px; padding-top: 1px;"&gt;&lt;div style="display: inline; font-family: 'Courier New', Courier, monospace; font-size: 14px !important; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 10px; padding-right: 0px; padding-top: 0px;"&gt;
&lt;span style="color: #66cc66; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;}&lt;/span&gt;&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div style="text-align: -webkit-left;"&gt;
This solution generates the nth Fibonacci number through solving the linear recurrence. The solution is: F(n) = (sqrt(5)/5)((1+sqrt(5))/2)^n - (sqrt(5)/5)((1-sqrt(5))/2)^n.&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="background-color: white; font-family: Verdana, sans-serif; font-size: 11px; text-align: -webkit-auto;"&gt;It's actually taught in Discrete Math courses. You can solve linear, homogenous recurrences like linear, homogenous differential equations.&lt;/span&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/1SoY42Y1JrY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/3920466768916437813/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/fibonacci-numbers-in-o1-time.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/3920466768916437813?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/3920466768916437813?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/1SoY42Y1JrY/fibonacci-numbers-in-o1-time.html" title="Fibonacci Numbers in O(1) Time" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/fibonacci-numbers-in-o1-time.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D04DRH04fSp7ImA9WhVWF0o.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-754309381594040770</id><published>2012-04-30T01:52:00.003-07:00</published><updated>2012-04-30T01:52:55.335-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-30T01:52:55.335-07:00</app:edited><title>Bubble Sort In Java</title><content type="html">&lt;a href="http://en.wikipedia.org/wiki/Bubble_sort" target="_blank"&gt;Bubble sort&lt;/a&gt; is the simplest sorting algorithm in java&amp;nbsp;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and&amp;nbsp;&lt;/span&gt;&lt;a href="http://en.wikipedia.org/wiki/Swap_(computer_science)" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; color: #0b0080; font-family: sans-serif; font-size: 13px; line-height: 19px; text-decoration: none;" title="Swap (computer science)"&gt;swapping&lt;/a&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&amp;nbsp;them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;Bubble sort has worst-case and average complexity both&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;О&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;(&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: white; font-family: sans-serif; line-height: 1em;"&gt;2&lt;/sup&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;), where&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;n&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&amp;nbsp;is the number of items being sorted. There exist many sorting algorithms with substantially better worst-case or average complexity of&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;O&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;(&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;n&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&amp;nbsp;log&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;n&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;). Even other&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;О&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;(&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: white; font-family: sans-serif; line-height: 1em;"&gt;2&lt;/sup&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;) sorting algorithms, such as&amp;nbsp;&lt;/span&gt;
&lt;a href="http://en.wikipedia.org/wiki/Insertion_sort" style="background-attachment: initial; background-clip: initial; background-color: white; background-image: none; background-origin: initial; color: #0b0080; font-family: sans-serif; font-size: 13px; line-height: 19px; text-decoration: none;" title="Insertion sort"&gt;insertion sort&lt;/a&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;,&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&amp;nbsp;tend to have better performance than bubble sort. Therefore, bubble sort is not a practical sorting algorithm when&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;n&lt;/i&gt;&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&amp;nbsp;is large.&lt;/span&gt;
&lt;br /&gt;
&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;u&gt;Java Program for Bubble Sort&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: sans-serif; font-size: 13px; line-height: 19px;"&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;import java.util.Scanner;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;public class BubbleSort {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] args) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Scanner scaner = new Scanner(System.in);//Reads the input from standard input device through console&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println("Enter the number of elements to sort: ");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int num = scaner.nextInt(); //input no. of elements for sorting&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int sort[] = new int[num];&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for (int i = 0; i &amp;lt; num; i++) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;sort[i] = scaner.nextInt(); //reading each element from cosole.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println("Elements Before Sorting: ");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for (int i = 0; i &amp;lt; num; i++) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.print(sort[i] + " ");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;sort = bubbleSort(sort); //method that does the bubble sort and returns the sorted array&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println("Elements After sorting: ");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;for (int i = 0; i &amp;lt; num; i++) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;System.out.print(sort[i] + " ");&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;static private int[] bubbleSort(int[] sort) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="font-family: sans-serif; font-size: x-small; line-height: 19px;"&gt;for (int i = 0; i &amp;lt; sort.length - 1; i++) {&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;for (int j = i + 1; j &amp;lt; sort.length; j++) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;if (sort[i] &amp;gt; sort[j]) { &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //comparison and sorting.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;int temp = 0;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;temp = sort[i];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;sort[i] = sort[j];&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     &lt;/span&gt;sort[j] = temp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;    &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;return sort;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: sans-serif; font-size: x-small;"&gt;&lt;span style="line-height: 19px;"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style="text-decoration: underline;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Run the above code. you will be asked number of elements for sorting. after specifying the number of elements give each number by clicking enter. Atlast you will get the output.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3 style="background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; border-bottom-color: initial; border-bottom-style: none; border-bottom-width: initial; font-family: sans-serif; font-size: 17px; line-height: 19px; margin-bottom: 0.3em; margin-left: 0px; margin-right: 0px; margin-top: 0px; overflow-x: hidden; overflow-y: hidden; padding-bottom: 0.17em; padding-top: 0.5em; width: auto;"&gt;
&lt;span class="mw-headline" id="Step-by-step_example"&gt;Step-by-step example&lt;/span&gt;&lt;/h3&gt;
&lt;div style="font-family: sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 0.5em; margin-top: 0.4em;"&gt;
Let us take the array of numbers "5 1 4 2 8", and sort the array from lowest number to greatest number using bubble sort algorithm. In each step, elements written in&amp;nbsp;&lt;b&gt;bold&lt;/b&gt;&amp;nbsp;are being compared. Three passes will be required.&lt;/div&gt;
&lt;div style="font-family: sans-serif; font-size: 13px; line-height: 19px; margin-bottom: 0.5em; margin-top: 0.4em;"&gt;
&lt;b&gt;First Pass:&lt;/b&gt;&lt;br /&gt;(&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;1&lt;/b&gt;&amp;nbsp;4 2 8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;(&amp;nbsp;&lt;b&gt;1&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;4 2 8 ), Here, algorithm compares the first two elements, and swaps them.&lt;br /&gt;( 1&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;2 8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;2 8 ), Swap since 5 &amp;gt; 4&lt;br /&gt;( 1 4&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1 4&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;8 ), Swap since 5 &amp;gt; 2&lt;br /&gt;( 1 4 2&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;8&lt;/b&gt;&amp;nbsp;)&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1 4 2&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;8&lt;/b&gt;&amp;nbsp;), Now, since these elements are already in order (8 &amp;gt; 5), algorithm does not swap them.&lt;br /&gt;&lt;b&gt;Second Pass:&lt;/b&gt;&lt;br /&gt;(&amp;nbsp;&lt;b&gt;1&lt;/b&gt;&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;2 5 8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;(&amp;nbsp;&lt;b&gt;1&lt;/b&gt;&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;2 5 8 )&lt;br /&gt;( 1&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;5 8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;5 8 ), Swap since 4 &amp;gt; 2&lt;br /&gt;( 1 2&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1 2&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;8 )&lt;br /&gt;( 1 2 4&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;8&lt;/b&gt;&amp;nbsp;)&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1 2 4&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;8&lt;/b&gt;&amp;nbsp;)&lt;br /&gt;Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one&amp;nbsp;&lt;b&gt;whole&lt;/b&gt;&amp;nbsp;pass without&amp;nbsp;&lt;b&gt;any&lt;/b&gt;&amp;nbsp;swap to know it is sorted.&lt;br /&gt;&lt;b&gt;Third Pass:&lt;/b&gt;&lt;br /&gt;(&amp;nbsp;&lt;b&gt;1&lt;/b&gt;&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;4 5 8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;(&amp;nbsp;&lt;b&gt;1&lt;/b&gt;&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;4 5 8 )&lt;br /&gt;( 1&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;5 8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1&amp;nbsp;&lt;b&gt;2&lt;/b&gt;&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;5 8 )&lt;br /&gt;( 1 2&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;8 )&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1 2&amp;nbsp;&lt;b&gt;4&lt;/b&gt;&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;8 )&lt;br /&gt;( 1 2 4&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;8&lt;/b&gt;&amp;nbsp;)&amp;nbsp;&lt;img alt="\to" class="tex" src="http://upload.wikimedia.org/wikipedia/en/math/d/a/5/da558173e1f2ddfeb273751d481f9a52.png" style="border-bottom-style: none; border-color: initial; border-image: initial; border-left-style: none; border-right-style: none; border-top-style: none; border-width: initial; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; vertical-align: middle;" /&gt;&amp;nbsp;( 1 2 4&amp;nbsp;&lt;b&gt;5&lt;/b&gt;&amp;nbsp;&lt;b&gt;8&lt;/b&gt;&amp;nbsp;)&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/sdJ4Wb3nIjI" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/754309381594040770/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/bubble-sort-in-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/754309381594040770?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/754309381594040770?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/sdJ4Wb3nIjI/bubble-sort-in-java.html" title="Bubble Sort In Java" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/bubble-sort-in-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;DUIHR3w6eSp7ImA9WhVWF0s.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-7103623291773057937</id><published>2012-04-29T23:32:00.000-07:00</published><updated>2012-04-29T23:32:16.211-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-29T23:32:16.211-07:00</app:edited><title>How To Create A Java Executable Jar File</title><content type="html">&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;Creating a jar file is not difficult to do, and it is something that should certainly be learned by beginning Java programmers. If you don't like reading, try&amp;nbsp;&lt;/span&gt;&lt;a class="bbc_url" href="http://www.youtube.com/watch?v=WTfjbOIfi10" rel="external" style="background-color: white; color: rgb(0, 0, 204) !important; cursor: pointer; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="External link"&gt;my YouTube video&lt;/a&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;&amp;nbsp;of this. It might make more sense.&lt;/span&gt;
&lt;br /&gt;
&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;1. Right-click and create a new folder on your desktop. The title doesn't matter. This is where you will store all of the files that will make up your jar file. Using a folder is helpful when you have a project of multiple classes.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;2. Either save or drag and drop your Java files into your folder.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;3. Now you will need to open the command prompt. Do this by holding the Windows key, then pressing the R key. This should bring up your Run application in which you should type "cmd" - press OK and this should bring up your command prompt.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;4. In the command prompt, type "cd Desktop" to change your directory to the desktop, the location of your folder. Type "dir" to show what is in the Desktop directory. You should see the folder that has your Java files in it among the list. Type "cd &amp;lt;foldername&amp;gt;" or, for example, "cd MyJavaFolder" - this should change your directory to your folder. To see what is in it, type "dir" again.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;5. By typing "dir" you should see your Java files inside of the folder. In order to turn these into class files, we need to compile them by using the "javac" command. This command requires the JDK and it requires you to change the classpath. This will not be covered in this tutorial. To compile the files, type "javac MyJavaFile.java" - to compile multiple files just put a space between them and use the same command "javac MyJavaFile1.java MyJavaFile2.java".&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;6. Type "dir" again and you should see your Java files and now your class files as well. You can minimize the command prompt and open the folder on your desktop. Inside the folder, right-click and create a new text document (notepad). In notepad, you need to show what your main class is for the jar file. Here's how you do this:&lt;/span&gt;&lt;br /&gt;
&lt;div class="syntaxhighlighter  " id="highlighter_564783" style="background-attachment: initial !important; background-clip: initial !important; background-color: rgb(255, 255, 255) !important; background-image: none !important; background-origin: initial !important; border-bottom-color: rgb(204, 204, 204) !important; border-bottom-style: dotted !important; border-bottom-width: 1px !important; border-color: initial !important; border-image: initial !important; border-left-color: rgb(204, 204, 204) !important; border-left-style: dotted !important; border-left-width: 1px !important; border-right-color: rgb(204, 204, 204) !important; border-right-style: dotted !important; border-right-width: 1px !important; border-style: initial !important; border-top-color: rgb(204, 204, 204) !important; border-top-style: dotted !important; border-top-width: 1px !important; bottom: auto !important; color: #222222; float: none !important; font-family: Verdana, sans-serif; font-size: 14px; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px; max-height: 500px; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; overflow-x: auto; overflow-y: auto; padding-bottom: 1px !important; padding-left: 1px !important; padding-right: 1px !important; padding-top: 1px !important; position: relative !important; right: auto !important; text-align: left !important; top: auto !important; vertical-align: baseline !important; width: 445px;"&gt;
&lt;div class="lines" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;div class="line alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;table style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-collapse: collapse !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: left !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;&lt;tbody style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;tr style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;&lt;td class="number" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; color: rgb(175, 175, 175) !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: top !important; width: 3em !important;"&gt;&lt;code style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; display: block !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0.3em !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; width: 2.7em !important;"&gt;1&lt;/code&gt;&lt;/td&gt;&lt;td class="content" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-color: rgb(108, 226, 108) !important; border-left-style: solid !important; border-left-width: 3px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0.5em !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: top !important; width: auto !important;"&gt;&lt;code class="plain" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;Main-Class: MyMainClass&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class="line alt2" style="background-attachment: initial !important; background-clip: initial !important; background-color: rgb(243, 243, 243) !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;table style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-collapse: collapse !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: left !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;&lt;tbody style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;tr style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;&lt;td class="number" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; color: rgb(175, 175, 175) !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: top !important; width: 3em !important;"&gt;&lt;code style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; display: block !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0.3em !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; width: 2.7em !important;"&gt;2&lt;/code&gt;&lt;/td&gt;&lt;td class="content" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-color: rgb(108, 226, 108) !important; border-left-style: solid !important; border-left-width: 3px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0.5em !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: top !important; width: auto !important;"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;div class="line alt1" style="background-attachment: initial !important; background-clip: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;table style="-webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-collapse: collapse !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: left !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;&lt;tbody style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;
&lt;tr style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;&lt;td class="number" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; color: rgb(175, 175, 175) !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: top !important; width: 3em !important;"&gt;&lt;code style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; display: block !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0.3em !important; padding-top: 0px !important; position: static !important; right: auto !important; text-align: right !important; top: auto !important; vertical-align: baseline !important; width: 2.7em !important;"&gt;3&lt;/code&gt;&lt;/td&gt;&lt;td class="content" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-color: rgb(108, 226, 108) !important; border-left-style: solid !important; border-left-width: 3px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0.5em !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: top !important; width: auto !important;"&gt;&lt;code class="plain" style="background-attachment: initial !important; background-clip: initial !important; background-color: initial !important; background-image: none !important; background-origin: initial !important; border-bottom-width: 0px !important; border-color: initial !important; border-image: initial !important; border-left-width: 0px !important; border-right-width: 0px !important; border-style: initial !important; border-top-width: 0px !important; bottom: auto !important; float: none !important; font-family: Consolas, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace !important; font-size: 1em !important; height: auto !important; left: auto !important; line-height: 1.4em !important; margin-bottom: 0px !important; margin-left: 0px !important; margin-right: 0px !important; margin-top: 0px !important; min-height: inherit !important; outline-color: initial !important; outline-style: initial !important; outline-width: 0px !important; padding-bottom: 0px !important; padding-left: 0px !important; padding-right: 0px !important; padding-top: 0px !important; position: static !important; right: auto !important; top: auto !important; vertical-align: baseline !important; width: auto !important;"&gt;|&lt;/code&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;This part is very important, so make sure to take note of the capitalization and the spacing. Don't add the .class ending to your specified main class.&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: white; color: red; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;You MUST press enter twice from the line you write on so that your curser ends up exactly where I placed the vertical bar.&lt;/span&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;&amp;nbsp;From that point, save it (to your folder of course) as "MyManifest.txt". The name doesn't really matter, but it's called a manifest document.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;7. Now go back into your command prompt and navigate back to your folder if you didn't just minimize the command prompt. Type "dir" once in your folder directory and you should see your manifest document.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;8.&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: white; color: navy; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;"&gt;Here's how you make your jar file once you have the files all ready to go. You should be in your folder directory in the command prompt. Type "jar cfm JarFileName.jar MyManifest.txt MyJavaFile1.class MyJavaFile2.class" - Type "dir" and you should see your jar file! You choose what your jar file is called in this command as well. The order matters.&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;9. Now, what good does a jar file do if you can't run it? Well, not much. If your program is made to run in the command line, then you will still have to run it by typing a command (unfortunately). Otherwise, if you have a GUI, you can go to your folder and double-click your jar file.&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;span style="background-color: white; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;&lt;span style="color: red;"&gt;To run a jar file from the command prompt, type&lt;/span&gt;&lt;/span&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt; "java -jar MyJarFile.jar".&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;Now you have a way to show off your programs on someone else's computer without having to download the JDK and configure the classpath and so on. All you need is the JRE, which most people have. If you're not into making GUIs yet, your jar file is still just as portable as the ones you can double-click. All you need to run the jar file is a computer with the JRE and, of course, the command to run a jar file.&amp;nbsp;&lt;/span&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;br style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px; margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding-bottom: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" /&gt;&lt;span style="background-color: white; color: #222222; font-family: Verdana, sans-serif; font-size: 14px; line-height: 21px;"&gt;Making executable jar files is a fun way to wrap up your code into a portable package that can run almost anywhere. I hope this tutorial helped you! Let me know if you have further questions, and let me know if I made a mistake. But again, I hope it made sense and wasn't too complicated or difficult to copy.&lt;/span&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/LFtEFHMmqHY" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/7103623291773057937/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/how-to-create-java-executable-jar-file.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7103623291773057937?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/7103623291773057937?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/LFtEFHMmqHY/how-to-create-java-executable-jar-file.html" title="How To Create A Java Executable Jar File" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/how-to-create-java-executable-jar-file.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CE4FR344fCp7ImA9WhVWF0o.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4108088960377473690</id><published>2012-04-29T22:40:00.001-07:00</published><updated>2012-04-30T01:01:56.034-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-30T01:01:56.034-07:00</app:edited><title>String Programming Question :1</title><content type="html">&lt;br /&gt;
Problem:- Given an "out" string length 4, such as "&amp;lt;&amp;lt;&amp;gt;&amp;gt;", and a word, return a new string where the word is in the middle of the out string, e.g. "&amp;lt;&amp;lt;word&amp;gt;&amp;gt;". Note: use str.substring(i, j) to extract the String starting at index i and going up to but not including index j.&lt;br /&gt;
&lt;br /&gt;
makeOutWord("&amp;lt;&amp;lt;&amp;gt;&amp;gt;", "Yay") ? "&amp;lt;&amp;lt;Yay&amp;gt;&amp;gt;"&lt;br /&gt;
makeOutWord("&amp;lt;&amp;lt;&amp;gt;&amp;gt;", "WooHoo") ? "&amp;lt;&amp;lt;WooHoo&amp;gt;&amp;gt;"&lt;br /&gt;
makeOutWord("[[]]", "word") ? "[[word]]"&lt;br /&gt;
&lt;br /&gt;
Solution:-&lt;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;br /&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
public class makeOutWord {&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&amp;nbsp; &amp;nbsp; public static void main(String[] args) {&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;String result = makeOutWord("&amp;lt;&amp;lt;&amp;gt;&amp;gt;","vipul");&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println(result);&lt;/blockquote&gt;
&lt;/blockquote&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;br /&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;blockquote class="tr_bq"&gt;
}&amp;nbsp;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;br /&gt;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private static String makeOutWord(String out, String word)&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;String result = "";&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int j = out.lastIndexOf("&amp;lt;");&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;int k = out.indexOf("&amp;gt;");&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;String first = out.substring(0, 2);&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;String last = out.substring(2);&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;result = first+""+word+""+last;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;return result;&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/blockquote&gt;
&lt;blockquote class="tr_bq"&gt;
}&lt;/blockquote&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/blockquote&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/hsE21iOIGYg" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4108088960377473690/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/string-programming-question-1.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4108088960377473690?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4108088960377473690?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/hsE21iOIGYg/string-programming-question-1.html" title="String Programming Question :1" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/string-programming-question-1.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CEUCQXg_eCp7ImA9WhVWF0s.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4909808876618042218</id><published>2012-04-29T22:04:00.002-07:00</published><updated>2012-04-29T22:04:20.640-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-29T22:04:20.640-07:00</app:edited><title>Introduction to Programming Using Java, Sixth Edition</title><content type="html">&lt;a href="http://math.hws.edu/javanotes/index.html"&gt;http://math.hws.edu/javanotes/index.html&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/0-tmjk5ZhdU" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4909808876618042218/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/introduction-to-programming-using-java.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4909808876618042218?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4909808876618042218?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/0-tmjk5ZhdU/introduction-to-programming-using-java.html" title="Introduction to Programming Using Java, Sixth Edition" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/introduction-to-programming-using-java.html</feedburner:origLink></entry><entry gd:etag="W/&quot;CkUNQHw-eSp7ImA9WhVWF0s.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-8365691492293078893</id><published>2012-04-29T21:31:00.002-07:00</published><updated>2012-04-29T21:31:31.251-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-29T21:31:31.251-07:00</app:edited><title>Link For Java Exception Handling</title><content type="html">Click on the below link.&lt;br /&gt;
&lt;a href="http://www.indiabix.com/java-programming/exceptions/"&gt;http://www.indiabix.com/java-programming/exceptions/&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/VFOuKePsges" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/8365691492293078893/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/link-for-java-exception-handling.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/8365691492293078893?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/8365691492293078893?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/VFOuKePsges/link-for-java-exception-handling.html" title="Link For Java Exception Handling" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/link-for-java-exception-handling.html</feedburner:origLink></entry><entry gd:etag="W/&quot;D0UGR386cCp7ImA9WhVWF0o.&quot;"><id>tag:blogger.com,1999:blog-1971275027497532393.post-4704494909375929436</id><published>2012-04-27T01:29:00.001-07:00</published><updated>2012-04-30T01:40:26.118-07:00</updated><app:edited xmlns:app="http://www.w3.org/2007/app">2012-04-30T01:40:26.118-07:00</app:edited><title>Date Exists Or Not</title><content type="html">Problem:-&lt;br /&gt;
&lt;div&gt;
Write a program which takes a date as input and checks whether the date exists (or) not?&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
Solution:-&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div&gt;
import java.text.ParseException;&lt;/div&gt;
&lt;div&gt;
import java.util.Scanner;&lt;/div&gt;
&lt;div&gt;
import java.util.Calendar;&lt;/div&gt;
&lt;div&gt;
import java.text.ParseException;&lt;/div&gt;
&lt;div&gt;
import java.text.SimpleDateFormat;&lt;/div&gt;
&lt;div&gt;
import java.util.Date;&lt;/div&gt;
&lt;div&gt;
public class InputDate {&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;public static void main(String[] a)&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;//Scanner class takes input from console.&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;Scanner input = new Scanner(System.in);&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;String dateString = input.next();&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;boolean exists = new InputDate().dateExists(dateString);&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;System.out.println(exists);&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;private boolean dateExists(String date)&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;{&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;try&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;//This method Specifies whether or not date/time interpretation is to be lenient.&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;sdf.setLenient(false);&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;//throws ParseExcpetion if date not in correct format.&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;sdf.parse(date);&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;return true;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}catch(ParseException e)&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;{&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;   &lt;/span&gt;return false;&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;}&lt;/div&gt;
&lt;div&gt;
&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
}&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/blogspot/nwOEG/~4/lPVTyzkUXy8" height="1" width="1"/&gt;</content><link rel="replies" type="application/atom+xml" href="http://javabrowsers.blogspot.com/feeds/4704494909375929436/comments/default" title="Post Comments" /><link rel="replies" type="text/html" href="http://javabrowsers.blogspot.com/2012/04/date-exists-or-not.html#comment-form" title="0 Comments" /><link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4704494909375929436?v=2" /><link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/1971275027497532393/posts/default/4704494909375929436?v=2" /><link rel="alternate" type="text/html" href="http://feedproxy.google.com/~r/blogspot/nwOEG/~3/lPVTyzkUXy8/date-exists-or-not.html" title="Date Exists Or Not" /><author><name>Vipul Reddy</name><email>noreply@blogger.com</email><gd:image rel="http://schemas.google.com/g/2005#thumbnail" width="32" height="32" src="http://1.bp.blogspot.com/__LC_pfj4bXI/S6d4EPVjHRI/AAAAAAAAAEo/HKUrG8n71qg/S220/images1.jpeg" /></author><thr:total>0</thr:total><feedburner:origLink>http://javabrowsers.blogspot.com/2012/04/date-exists-or-not.html</feedburner:origLink></entry></feed>
