<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5082823859993823215</id><updated>2026-05-27T23:51:25.818-07:00</updated><category term="java"/><category term="Java Interview"/><category term="Code"/><category term="String"/><category term="Windows"/><category term="j2ee"/><category term="Security"/><category term="Solution"/><category term="Blogger Tips"/><category term="Error"/><category term="Integer"/><category term="Linux"/><category term="Struts"/><category term="Struts Interview"/><category term="interview"/><category term="Ajax"/><category term="Array"/><category term="Commands"/><category term="Date"/><category term="Eclipse"/><category term="HashMap"/><category term="JBoss"/><category term="JSP"/><category term="Override"/><category term="Ports"/><category term="Recursion"/><category term="Sorting"/><category term="equal"/><category term="regex"/><category term="Array List"/><category term="Class Path"/><category term="Collection"/><category term="DB2 Interview"/><category term="Dart"/><category term="Database"/><category term="File"/><category term="Generics"/><category term="Google Map"/><category term="IPL"/><category term="JNDI"/><category term="JRE"/><category term="JSP Interview"/><category term="Latest"/><category term="MD5"/><category term="MVC"/><category term="Password"/><category term="Path"/><category term="Phishing"/><category term="Reflection"/><category term="SSH"/><category term="SSL"/><category term="Security Audit"/><category term="Serverside Validation"/><category term="Servlet"/><category term="Site Map"/><category term="StringBuffer"/><category term="Tech Service"/><category term="Thread"/><category term="TreeMap"/><category term="Tricks"/><category term="VNC"/><category term="Validation"/><category term="WSAD"/><category term="Webmaster Tools"/><category term="YouTube"/><category term="hashCode"/><category term="salt"/><title type='text'>Java Online</title><subtitle type='html'>This is my latest technology blog that covers my experience in  Java , J2ee , Struts Framework, Also covers  Tips and  Tricks , Codes , Tutorials ,  Solutions  for Developers , Interview Questions &amp;amp; Answers</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>61</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-4579749914596473374</id><published>2012-01-22T12:06:00.000-08:00</published><updated>2013-10-24T21:32:11.805-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Generics"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><title type='text'>What are Generics in Java with example. How to improve type safety of your code?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div align=&quot;justify&quot;&gt;
In core java interview , question like  &lt;b&gt;&quot;what are generics? and the  advantages of using generics?&quot;&lt;/b&gt;  may be asked. This tutorial answers the above questions with example code.&lt;br /&gt;
Introduction of generics in the Java programming   improves the &lt;b&gt;type safety &lt;/b&gt; of  your code also helps to reduce the  runtime errors because it makes more of your bugs detectable at compile time.  This  powerful feature of Java  introduced in the release  of  JDK 5 which  allows a type or method to operate on objects of various types .  To implement generics ,  special  syntax is used .  Generics also  enhance the Java language with special syntax to iterate entities of Lists, Map,etc.   Following code  illustrates how  coding mistake   can happen without using  generics and how to avoid  it ?    ArrayList can contain multiple data types like String ,  Integer  and others.  Before JDK 5,  there was nothing in Java to restrict  this behavior, which caused many coding mistakes.  &lt;br /&gt;
&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;background-color: #e6e6e6; border: 0px dashed rgb(150, 150, 150); color: black; line-height: 14px; margin: 1px; overflow: auto; width: auto;&quot;&gt;&lt;code&gt;
import java.util.ArrayList;
public class generics
{
public static void main(String arg[])
{
List al= new ArrayList();
al.add(&quot;one&quot;);
al.add(2);  // no compile error occurs
al.add(&quot;three&quot;);
accessList(al);
}
private static void accessList(List al) {
   for (int k = 0; k &amp;lt; al.size(); k++) {
     String s =(String) al.get(k);  //  runtime error  &lt;b&gt;&quot;java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String &quot;&lt;/b&gt;occurs when the integer value is accessed 
   System.out.println(s);
   }
} &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
To ensure the assignment to a variable of type String  is type safe, the type casting is required.  Now the above code can be written using generics to avoid the runtime errors.&lt;br /&gt;
&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;background-color: #e6e6e6; border: 0px dashed rgb(150, 150, 150); color: black; line-height: 14px; margin: 1px; overflow: auto; width: auto;&quot;&gt;&lt;code&gt;
List&amp;lt;String&amp;gt; genericsAL = new ArrayList&amp;lt;String&amp;gt;();
genericsAL.add(&quot;one&quot;);
//genericsAL.add(2); // this line makes compile error 
genericsAL.add(&quot;three&quot;);
accessGenericsList(genericsAL);
}
private static void accessGenericsList(List&amp;lt;String&amp;gt; al) {
   for (int k = 0; k &amp;lt; al.size(); k++) {
     String s =al.get(k);      //  No type casting is required. 
   System.out.println(s);
   }
} &lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
List&amp;lt;String&amp;gt; genericsAL = new ArrayList&amp;lt;String&amp;gt;(); Generic used in the statement  enforces to accept only Strings.  The following statement gives compile time error. &lt;br /&gt;
List&amp;lt;Object&amp;gt; genericsO = genericsAL;&lt;br /&gt;
&lt;b&gt;List&amp;lt;String&amp;gt; genericsAL = new ArrayList&amp;lt;String&amp;gt;() &lt;/b&gt; -  List is a generic interface that takes a type parameter  (in this case String) .  The type declaration for the variable genericsAL specifies that this is a List of Strings, written List&amp;lt;String&amp;gt; and  no type casting is required for the line String s =al.get(k)&lt;br /&gt;
&lt;b&gt;&lt;u&gt;iterate using  generics &lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
If you want to iterate the above List using generics , you can use the following statement.   For iterating the list using generics , a special syntax is used &lt;br /&gt;
&lt;b&gt;for (String s : al ) System.out.println(s);&lt;/b&gt;&lt;br /&gt;
Let us see the how to define a Simple Generics . The interfaces List,  Collection and Iterator are defined  in package java.util for using generics as follows&lt;br /&gt;
&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;background-color: #e6e6e6; border: 0px dashed rgb(150, 150, 150); color: black; line-height: 14px; margin: 1px; overflow: auto; width: auto;&quot;&gt;&lt;code&gt;
public interface List&amp;lt;E&amp;gt; extends Collection&amp;lt;E&amp;gt;{
    void add(E x);
    Iterator&amp;lt;E&amp;gt; iterator();
}
public interface Iterator&amp;lt;E&amp;gt;{
    E next();
    boolean hasNext();
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
The element within the angle brackets are the declarations of the &lt;b&gt;formal type parameters&lt;/b&gt; of the interfaces List ,  Collection and Iterator .All occurrences of the formal type parameter  are replaced by the &lt;b&gt;actual type argument&lt;/b&gt;. In our example , &lt;b&gt;E&lt;/b&gt; is replaced with &lt;b&gt;String&lt;/b&gt;.  &lt;br /&gt;
The following code snippet accepts  any kind of collection as a parameter and prints the elements. &lt;br /&gt;
&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;background-color: #e6e6e6; border: 0px dashed rgb(150, 150, 150); color: black; line-height: 14px; margin: 1px; overflow: auto; width: auto;&quot;&gt;&lt;code&gt;
   void displayCollection(Collection c) {
    Iterator i = c.iterator();
    for (k = 0; k &amp;lt; c.size(); k++) {
        System.out.println(i.next());
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
How to write the equivalent code for the above code using generics.  We can&#39;t use  Collection&amp;lt;Object&amp;gt;  . Because it accepts only  Object type.  Use wildcard type to accept any kind of collection. i.e  Collection&amp;lt;?&amp;gt; -&lt;b&gt;(called as collection of unknown )&lt;/b&gt;  collection whose element type matches anything . The code snippet  is as follows &lt;br /&gt;
&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;background-color: #e6e6e6; border: 0px dashed rgb(150, 150, 150); color: black; line-height: 14px; margin: 1px; overflow: auto; width: auto;&quot;&gt;&lt;code&gt;
 void displayCollection(Collection&amp;lt;?&amp;gt; c) {
    for (Object e : c) {
        System.out.println(e);
    }
}&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
For  example of HashMap  using generics , you can visit my earlier post , &lt;a href=&quot;http://www.j2eeforum.blogspot.com/2012/01/iterate-all-elements-in-map-code-to.html&quot;&gt;Traverse HashMap &lt;/a&gt;&lt;br /&gt;
Note :  Just like type declarations, method declarations can be generic . This is called generic methods. We can see generic methods with example in the later post. &lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/4579749914596473374/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2012/01/what-are-generics-in-java-with-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/4579749914596473374'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/4579749914596473374'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2012/01/what-are-generics-in-java-with-example.html' title='What are Generics in Java with example. How to improve type safety of your code?'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-2459036332642862506</id><published>2012-01-09T10:51:00.000-08:00</published><updated>2012-01-09T10:58:09.199-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Sorting"/><title type='text'>Java program for Insertion sort . Arrange elements using Insertion Sort with tutorial</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;The insertion sort is a simple  comparison sort algorithm.  Each  iteration of insertion sort removes an element from the input array  and inserts it into the correct position in the array being sorted.  Insertion sort keeps  the left side of the array  is sorted . At each iteration , a[i] is inserted at the appropriate position in the sorted array and i is increased.  . The resulting array after ith  iterations will have i+1 sorted elements from the left (i.e.  a[1..i+1]) .  Insertion sort is similar to selection sort.  Advantage of Insertion sort is that it only traverse as many elements as required to find the right location , while selection sort must traverse all remaining elements to find the absolute smallest element. &lt;br /&gt;            Suppose we have the elements  a[1..n]  to be sorted. At each iteration of the algorithm,  the array has  two parts .For the i th iteration ,  first part is  a[1..i+1]  which is  sorted, the second part a[i+2..n] is still unsorted . Assume    that  a[i+2] is assigned to a temp variable. In order to insert element a[i+2] into the sorted part, it is compared with a[i+1], a[i], a[i-1], a[i-2] , etc.  Suppose a[i-1]  is the appropriate position  to insert , then the elements  a[i+1] is shifted to a[i+2] and a[i] is shifted to a[i+1]. a[i-1] is shifted to a[i] ,  and  temp value is inserted into a[i-1] . &lt;br /&gt;Insertion sort is vey simple and  efficient for small input data.   It is much less efficient on large data  . &lt;br /&gt;&lt;b&gt;Example :&lt;/b&gt; Now we have the following  elements to be sorted . For each  iterations , we can see the sorted and unsorted elements. The bold elements are sorted on each iteration.&lt;br /&gt;6, 4,2, 9,3, 1&lt;br /&gt;&lt;br /&gt;Iteration 1&lt;br /&gt;&lt;b&gt;4 6&lt;/b&gt; 2 9 3 1&lt;br /&gt;Iteration 2&lt;br /&gt;&lt;b&gt;2 4 6&lt;/b&gt; 9 3 1&lt;br /&gt;Iteration 3&lt;br /&gt;&lt;b&gt;2 4 6 9&lt;/b&gt; 3 1&lt;br /&gt;Iteration 4&lt;br /&gt;&lt;b&gt;2 3 4 6 9&lt;/b&gt; 1&lt;br /&gt;Iteration 5&lt;br /&gt;&lt;b&gt;1 2 3 4 6 9&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;The following  is the code for Inssertion Sort &lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;   &lt;br /&gt;public class InsertionSort {&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;int array[] = { 6, 4,2, 9,3, 1 };&lt;br /&gt;System.out.println(&quot;Given Elements &quot;);&lt;br /&gt;for (int k = 0; k&amp;lt; array.length; k++) {&lt;br /&gt;System.out.print(array[k] + &quot; &quot;);&lt;br /&gt;}&lt;br /&gt;int  j, temp;&lt;br /&gt;// Logic for Insertion Sort&lt;br /&gt;for (int i = 1; i &amp;lt; array.length; i++) {&lt;br /&gt;temp = array[i];&lt;br /&gt;j = i;&lt;br /&gt;while ( j&amp;gt;0 &amp;&amp; (array[j-1] &amp;gt; temp)) {&lt;br /&gt;array[j] = array[j - 1];&lt;br /&gt;j = j - 1;&lt;br /&gt;}&lt;br /&gt;array[j] = temp;&lt;br /&gt;}&lt;br /&gt;System.out.println(&quot;\nSorted Elements &quot;);&lt;br /&gt;for (int i = 0; i &amp;lt; array.length; i++) {&lt;br /&gt;System.out.print(array[i] + &quot; &quot;);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;br /&gt;D:\as2\JF4&gt;java InsertionSort&lt;br /&gt;Given Elements&lt;br /&gt;6 4 2 9 3 1&lt;br /&gt;Sorted Elements&lt;br /&gt;1 2 3 4 6 9</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/2459036332642862506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2012/01/java-program-for-insertion-sort-arrange.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2459036332642862506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2459036332642862506'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2012/01/java-program-for-insertion-sort-arrange.html' title='Java program for Insertion sort . Arrange elements using Insertion Sort with tutorial'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-2734623688704305489</id><published>2012-01-03T12:38:00.000-08:00</published><updated>2012-01-03T12:53:43.484-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><title type='text'>Java program to print Beprisque numbers between two numbers</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;A number is called a Beprisque if it is the only number between a prime and a square number ( i.e. that number should be a prime number plus one  and a square number minus one  OR  a prime number minus one and  a square number plus one).    For example , 10 is a Beprisque number because it is  the only number between 9 which is a square number and 11 which is a prime number .  24 is also a  Beprisque number   because 24 is the only number between 23 which is a prime  and 25 which is a square number .  Now the logic to find the Beprisque number will be as follows.  Take a number n . The number n is Beprisque number , if n-1 is prime and n+1  is square number  OR   n+1  is prime number and  n-1 is square number.  To implement the above logic , I have written two functions  . 1) isPrime() which  is to check the number is prime or not . 2) isSquare()  is to check the given number is square or not.  The folllowing code prints all the Beprisque numbers between  given two numbers (min and max) where min,  max is  accepted through keyboard. The logic to check the given number is square or  not will be &lt;b&gt;  (Math.sqrt(num) == (int) Math.sqrt(num)) where num is the given number &lt;/b&gt;  i.e. squareRoot of number (num) should be  an  integer &lt;br /&gt;*/&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;    &lt;br /&gt;import java.util.Scanner;&lt;br /&gt;public class BeprisqueNumbers {&lt;br /&gt; public static void main(String argv[]) {&lt;br /&gt;  Scanner sc = new Scanner(System.in);&lt;br /&gt;  System.out.println(&quot;Enter min&quot;);&lt;br /&gt;  int min = sc.nextInt();&lt;br /&gt;  System.out.println(&quot;Enter max&quot;);&lt;br /&gt;   int max = sc.nextInt();&lt;br /&gt;  System.out.println(&quot;Beprisque Numbers between &quot; + min + &quot; and &quot; + max);&lt;br /&gt;&lt;br /&gt;//Logic to print Beprisque Numbers&lt;br /&gt;   for (int n = min; n &amp;lt; max; n++) {&lt;br /&gt;          if  ( isPrime(n - 1)   &amp;&amp;  isSquare((double)(n+ 1))  ||  isPrime(n+ 1)   &amp;&amp;  isSquare((double)(n- 1)) )        System.out.println(n);     &lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;//to check prime number&lt;br /&gt;   public static boolean isPrime(int num) {&lt;br /&gt;       int i=0;&lt;br /&gt;       for ( i = 2; i &amp;lt; num ; i++) {&lt;br /&gt;          if ((num % i) == 0 ) return false;&lt;br /&gt;      }&lt;br /&gt;       return true;&lt;br /&gt;   }&lt;br /&gt;//to check square &lt;br /&gt;   public static boolean isSquare(double num) {&lt;br /&gt;     return (Math.sqrt(num) == (int) Math.sqrt(num));&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Output : &lt;/b&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibdTKeg6fM4jpAfZigIMGLrSgCS2sRT-2uBxCUddjsjIMqzB_j5-jjQd-NIkGps226AEzkcdiGO2VRXCFLbLTDS4i-SGeAd9dUmuwAvwArT7IFmWbFMhjT9sOJXWB1_XD74RS_ojr_lGc/s1600/beprisque.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 311px; height: 322px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibdTKeg6fM4jpAfZigIMGLrSgCS2sRT-2uBxCUddjsjIMqzB_j5-jjQd-NIkGps226AEzkcdiGO2VRXCFLbLTDS4i-SGeAd9dUmuwAvwArT7IFmWbFMhjT9sOJXWB1_XD74RS_ojr_lGc/s400/beprisque.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5693509209226835970&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/2734623688704305489/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2012/01/java-program-to-print-beprisque-numbers.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2734623688704305489'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2734623688704305489'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2012/01/java-program-to-print-beprisque-numbers.html' title='Java program to print Beprisque numbers between two numbers'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEibdTKeg6fM4jpAfZigIMGLrSgCS2sRT-2uBxCUddjsjIMqzB_j5-jjQd-NIkGps226AEzkcdiGO2VRXCFLbLTDS4i-SGeAd9dUmuwAvwArT7IFmWbFMhjT9sOJXWB1_XD74RS_ojr_lGc/s72-c/beprisque.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-2698429791755136066</id><published>2011-12-24T02:02:00.000-08:00</published><updated>2011-12-24T02:24:28.301-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="equal"/><category scheme="http://www.blogger.com/atom/ns#" term="HashMap"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Override"/><title type='text'>How to Override Object.equals() method in Java . Writing own equals() method   to compare two objects based on values with example code</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;This tutorial covers how to write own version of equals()  method and importance of the method. Before going to this , let us see litle bit about Object class which has equals()  and hashcode() method.  Object class is the superclass of all Java classes.  Every classes in java are inherited from this class by default that means all methods  of the Object class are inherited automatically. So by default , the classes that  we create  will have the methods equals() , hashcode() , toString(), etc ... &lt;br /&gt;This tutorial covers  the default implementation of equals method  and when and  why we need to override equals method for our class?&lt;br /&gt;In default implementation ,  two  objects are  equal only if they refer to the exact same object (i.e.  reference / memory address equality)  . The following code represents  the default equals method &lt;br /&gt;  &lt;b&gt;public boolean equals(Object obj) {    return (this == obj);  }&lt;/b&gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;When to override Object.equals() method?&lt;/b&gt;  Please go through the following code and the output. &lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;    &lt;br /&gt;class equalTest&lt;br /&gt;{&lt;br /&gt;public static void main(String arg[])&lt;br /&gt;  {&lt;br /&gt;Employee emp1=new Employee(1561, &quot;John&quot;, &quot;Smith&quot;);&lt;br /&gt;Employee emp2=new Employee(1561, &quot;John&quot; ,&quot;Smith&quot;);&lt;br /&gt;Employee emp3=emp1;&lt;br /&gt;if (emp1.equals(emp2)) System.out.println(&quot;Both Employees(emp1 , emp2)  are equal&quot;);&lt;br /&gt;else System.out.println(&quot;Both Employees(emp1 , emp2)  are not equal&quot;);&lt;br /&gt;&lt;br /&gt;if (emp3.equals(emp1)) System.out.println(&quot;Both Employees(emp1 , emp3)  are equal&quot;);&lt;br /&gt;else System.out.println(&quot;Both Employees(emp1 , emp3)  are not equal&quot;);&lt;br /&gt;&lt;br /&gt;String name1=&quot;John Smith&quot;;&lt;br /&gt;String name2=&quot;John Smith&quot;;&lt;br /&gt;&lt;br /&gt;if (name1.equals(name2)) System.out.println(&quot;Both Names(name1 , name2)  are equal&quot;);&lt;br /&gt;else System.out.println(&quot;Both Names(name1 , name2)  are not equal&quot;);&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Employee&lt;br /&gt;{&lt;br /&gt;int empCode;&lt;br /&gt;private String firstName;&lt;br /&gt;private String surname;&lt;br /&gt;&lt;br /&gt;public Employee(int empCode, String firstName, String surname)&lt;br /&gt;  {&lt;br /&gt;this.empCode=empCode;&lt;br /&gt;this.firstName=firstName;&lt;br /&gt;this.surname=surname;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Output :&lt;/b&gt;&lt;br /&gt;D:\as2\JF4&gt;java equalTest&lt;br /&gt;Both Employees(emp1 , emp2)  are not equal&lt;br /&gt;Both Employees(emp1 , emp3)  are equal&lt;br /&gt;Both Names(name1 , name2)  are equal&lt;br /&gt;&lt;br /&gt;       Before  overriding , it is very important to  keep in mind the  distinction between equality and identity .  In the above code ,   emp1 and emp2  has identical data  i.e. employee code , first name and  surname are equal for both objects .  But the output is  &lt;b&gt;Both Employees(emp1 , emp2)  are not equal&lt;/b&gt; . Because, by default the equals method of object checks  whether the two reference variables(emp1  and emp2 )  refer to the same object .  In the above code ,   both are different objects that is not identical  .  But emp1 and emp3 are  identical . So the output is &lt;b&gt;Both Employees(emp1 , emp3)  are equal &lt;/b&gt; . &lt;br /&gt; emp1 and emp2 objects are logically equal  but not identical . In this situation , we have to  override equals method sothat the it can also check the logical equality .  But  name1 and name2 are two different string objects .  But the output is &lt;b&gt;Both Names(name1 , name2)  are equal&lt;/b&gt; .  Because String class overrides the equals method to compare two string objects for equality based on the values and returns true if they  hold  the same characters in the same sequence.    &lt;br /&gt;Date class defines the equals method as follows . This  is the value-based approach to equality  (logical equality)  . Both Date objects are equal if the &lt;b&gt;getTime()&lt;/b&gt; method returns the same long value (milliseconds)  for both&lt;br /&gt;public boolean equals(Object obj) {&lt;br /&gt;        return obj instanceof Date &amp;&amp; getTime() == ((Date) obj).getTime(); &lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;     So when is it appropriate to override Object.equals?  When a class  requires logical equality ( i.e. two objects are equal if they have identical data))  that differs from  object identity, and a superclass has not already overridden equals method  to implement the desired behavior . This is generally applicable  for  value classes such as String , Integer and  Date, etc  . When we compare two objects using the equals method , we may expect  to find out  whether they are logically equivalent, not whether they refer to the same object.  In this situation , we need to override equals method. &lt;br /&gt;        &lt;b&gt;typesafe enum&lt;/b&gt; value class that does not require the equals method to be overridden . Because typesafe enum classes guarantee that at most one object exists with each value, Object’s equals method is equivalent to a logical equals method for such classes. When you override the equals method of java.lang.Object , you must adhere to its general contract which is given below&lt;br /&gt;       Let us say   a, b and  c are references of any objects   . The equals method is &lt;br /&gt;1. reflexive:  a.equals(a)  must return true   i.e. any object must be equal to itself&lt;br /&gt;2. symmetric: a.equals(b) must be true if  b.equals(a) is true.   i.e.   if first object is equal to a second and the second object is equal to first&lt;br /&gt;3. transitive:  if a.equals(b) returns true and b.equals(c) returns true, then a.equals(c) must return true . i.e.   if first object is equal to a second and the second object is equal to a third, then the first object must be equal to the third.&lt;br /&gt;4. consistent: multiple invocations of  a.equals(b) should return  the same result (i.e. either always true or always false)  unless any of the object&#39;s information is modified&lt;br /&gt;5. Non-nullity :  x.equals(null) must return false where x is non-null reference. i.e.  all objects must be unequal to null.  Allowing null value should not throw NullPointerException . &lt;br /&gt;                   Note :  It is better to check null  i.e.  if (o == null) return false;.    But ,this test is not necessary as we include another test &lt;b&gt; instanceof &lt;/b&gt; operator to check that its argument is of the correct type  i.e.  if (!(o instanceof MyType)) return false;   This take takes care of the null check also .   instanceof operator is specified to return false if its first operand is null . null objects will always fail instanceof.&lt;br /&gt; &lt;b&gt; equals() method&lt;/b&gt; will have the  following condtions and statements in the following order and applicable for the above class Employee&lt;br /&gt; public boolean equals(Object other) {&lt;br /&gt;1.  check other object  refers the same object   . use == operator . If so, return true&lt;br /&gt;  -&lt;b&gt;  if (other==this )      return true&lt;/b&gt;&lt;br /&gt;2. check that its argument is of the correct type before type casting &lt;br /&gt;            -  &lt;b&gt;if (!(other instanceof  Employee))   return false &lt;/b&gt;   // takes care of null check also .  &lt;br /&gt;3. type cast the argument to an appropriate type to access its fields  &lt;br /&gt;            - &lt;b&gt; Employee emp = (Employee) other &lt;/b&gt;  &lt;br /&gt;4. check to see if the  fields (relevent to logical equality) of the argument matches the corresponding fields of this object .  If all checks  succeed, return true. For primitive fields (except float or double) , use the == operator for comparisons; for object reference fields, invoke the equals method recursively .To avoid NullPointerException, when the fields contain null values , please include null check as given below.&lt;br /&gt;              -&lt;b&gt; return empCode == emp.empCode &amp;&amp; (firstName == emp.firstName || (firstName != null &amp;&amp; firstName.equals(emp.firstName) ))&amp;&amp; (surname  == emp.surname  ||  (surname != null &amp;&amp; surname.equals (emp.surname))) &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt; Note :  instanceof   is most suitable for immutable class or final class which can not be extended . As instanceof returns true for subclass also ,  we can use getCalss() method  for type check as given below &lt;br /&gt;    &lt;b&gt;if((other == null) || (other.getClass() != this.getClass()))&lt;/b&gt;    return false . null check also required. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Aoid common mistakes  when overriding&lt;/b&gt;&lt;br /&gt;1. You must override hashCode in every class that overrides equals. If not , it  will result in a violation of the general contract for Object.hashCode, which will prevent your class from functioning properly in conjunction with all hash-based collections, including HashMap, HashSet, and Hashtable.  For overriding hashCode() method , please gothrough my earlier tutorial  &lt;a href=http://www.techlabs4u.com/2011/12/hashcode-in-java-example-why-to.html&gt; Why and When to override Object.hashCode() method? &lt;/a&gt;&lt;br /&gt;2. Don’t use any other type other than  Object  as argument  in the equals method declaration  . &lt;br /&gt;Wrong :  public boolean equals(Employee o) {... }    - this method does not override Object.equals() , but overloads it instead.   Object.equals accepts only  the argument of  type Object , &lt;br /&gt;      Correct :  public boolean equals(Object  o) {... }   &lt;br /&gt;&lt;br /&gt;      The above Employee class with equals() and hashCode() method is given below. The equals() method of Object class is overrided in the code  to compare objects based on values.&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;    &lt;br /&gt;class equalOverride&lt;br /&gt;{&lt;br /&gt;public static void main(String arg[])&lt;br /&gt; {&lt;br /&gt;Employee emp1=new Employee(1561, &quot;John&quot;, &quot;Smith&quot;);&lt;br /&gt;Employee emp2=new Employee(1561, &quot;John&quot; ,&quot;Smith&quot;);&lt;br /&gt;Employee emp3=emp1;&lt;br /&gt;if (emp1.equals(emp2)) System.out.println(&quot;Both Employees(emp1 , emp2)  are equal&quot;);&lt;br /&gt;else System.out.println(&quot;Both Employees(emp1 , emp2)  are not equal&quot;);&lt;br /&gt;if (emp3.equals(emp1)) System.out.println(&quot;Both Employees(emp1 , emp3)  are equal&quot;);&lt;br /&gt;else System.out.println(&quot;Both Employees(emp1 , emp3)  are not equal&quot;);&lt;br /&gt;String name1=&quot;John Paul&quot;;&lt;br /&gt;String name2=&quot;John Paul&quot;;&lt;br /&gt;if (name1.equals(name2)) System.out.println(&quot;Both Names(name1 , name2)  are equal&quot;);&lt;br /&gt;else System.out.println(&quot;Both Names(name1 , name2)  are not equal&quot;);&lt;br /&gt; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;class Employee&lt;br /&gt;{&lt;br /&gt;int empCode;&lt;br /&gt;private String firstName;&lt;br /&gt;private String surname;&lt;br /&gt;&lt;br /&gt;public Employee(int empCode, String firstName, String surname)&lt;br /&gt;{&lt;br /&gt;this.empCode=empCode;&lt;br /&gt;this.firstName=firstName;&lt;br /&gt;this.surname=surname;&lt;br /&gt;}&lt;br /&gt; public boolean equals(Object other) {&lt;br /&gt; if (other==this )      return true;&lt;br /&gt; if (!(other instanceof  Employee))   return false;&lt;br /&gt;Employee emp = (Employee) other;&lt;br /&gt;return empCode == emp.empCode &amp;&amp; (firstName == emp.firstName || (firstName != null &amp;&amp; firstName.equals(emp.firstName) ))&amp;&amp; (surname  == emp.surname  ||  (surname != null &amp;&amp; surname.equals (emp.surname)))  ;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;public int hashCode() {&lt;br /&gt;        int hash = 17;&lt;br /&gt;        hash = hash * 19 + empCode;&lt;br /&gt;        hash = hash * 31 + firstName.hashCode();&lt;br /&gt;        hash = hash * 13 + (surname == null ? 0 : surname.hashCode());&lt;br /&gt;        return hash;&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Output: &lt;/b&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijJraJQZBUxZeSgRfCGAVmiHJfo42RMWMM19P9iUqZPbqxlBSsCl4YHMvYHdXGo8MhyVJs7tiv6xDe-m4arU50INM6uP4as2i2CeLkKAuEMgqYvjYP8n2H4Ppj7j17djJYemjiIjRt44g/s1600/equals+method.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 382px; height: 74px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijJraJQZBUxZeSgRfCGAVmiHJfo42RMWMM19P9iUqZPbqxlBSsCl4YHMvYHdXGo8MhyVJs7tiv6xDe-m4arU50INM6uP4as2i2CeLkKAuEMgqYvjYP8n2H4Ppj7j17djJYemjiIjRt44g/s400/equals+method.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5689634148006936466&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/2698429791755136066/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/12/how-to-override-objectequals-method-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2698429791755136066'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2698429791755136066'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/12/how-to-override-objectequals-method-in.html' title='How to Override Object.equals() method in Java . Writing own equals() method   to compare two objects based on values with example code'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEijJraJQZBUxZeSgRfCGAVmiHJfo42RMWMM19P9iUqZPbqxlBSsCl4YHMvYHdXGo8MhyVJs7tiv6xDe-m4arU50INM6uP4as2i2CeLkKAuEMgqYvjYP8n2H4Ppj7j17djJYemjiIjRt44g/s72-c/equals+method.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-2588785622827191239</id><published>2011-12-07T12:41:00.000-08:00</published><updated>2011-12-12T08:19:42.946-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="regex"/><category scheme="http://www.blogger.com/atom/ns#" term="Tricks"/><title type='text'>Java program that replaces two or more consecutive characters / blanks in a string by a single character / blank. Use Regular Expression.</title><content type='html'>&lt;div align=&quot;justify&quot;&gt; Sometimes interview questions may be little bit tricky . The following   tricky  questions may be asked &lt;br /&gt;           1. A string has  thousand or two thousand  zeros . Replace all  consecutive zeros with a single zero  without using loop&lt;br /&gt;           2  String str =  &quot;200000000000000000000000500000000000000000&quot;  . Make it to 2050 without using loop&lt;br /&gt;       The answer should be in one line  and should be a general solution to all similar problems , should not be a specific solution  to a particular problem.&lt;br /&gt;        In college / school computer practical lab , the following  program  may be given to solve.  WAP that replaces two or more consecutive characters / blanks in a string by a single character / blank. For example, if the input is &lt;b&gt;Keppplarrr-222 2b   issss 222.444    timmes the sizzzze of     the eeearth &lt;/b&gt; and the  output should be  &lt;b&gt;Keplar-2 2b is 2.4 times the size of the earth&lt;/b&gt;. &lt;br /&gt;         The solution to all  the above questions,  without using loop will be using  &lt;b&gt;Regular Expression (RegExp) &lt;/b&gt;. The answer using RegExp  is  ,  &lt;b&gt; str.replaceAll( &quot;(.)\\1+&quot;, &quot;$1&quot; ) &lt;/b&gt; &lt;br /&gt; &quot;(.)\\1+ &quot;  -  The meaning of the metacharater used in the expression is         &lt;br /&gt;    (.)  - match any charater &lt;br /&gt;    \1   - matchs text similar to that matched within the first parenthesized . &lt;br /&gt;    +   -  One or more times&lt;br /&gt;The program for the above questions is as follows . The problems are solved in two ways . One is using Regular Expressions and another is using for loop. In method 2 ,  charAt (int index), deleteCharAt(int index) are used  where &lt;b&gt; char charAt (int index) &lt;/b&gt; - Returns the char value at the specified index  and &lt;b&gt;StringBuffer deleteCharAt(int index) &lt;/b&gt;- Removes the char at the specified index*/&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;    &lt;br /&gt;class ReplaceConsecutiveChar&lt;br /&gt;{&lt;br /&gt;public static void main (String args[])&lt;br /&gt;{&lt;br /&gt;//String inputStr =&quot;Keppplarrr-222 2b   issss 222.444    timmes the sizzzze of     the eeearth&quot;;&lt;br /&gt;String inputStr  =&quot;200000000000000000000000500000000000000000&quot;;&lt;br /&gt;String newString=ReplConsectiveCharM1(inputStr);&lt;br /&gt;String newString1 = ReplConsectiveCharM2(inputStr);&lt;br /&gt;System.out.println(&quot;Input String : &quot; + inputStr);&lt;br /&gt;System.out.println(&quot;Using RegExp : &quot;+ newString  + &quot;\nUsing For Loop : &quot; +  newString1);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Method 1 : Using RegExp&lt;br /&gt;public static String ReplConsectiveCharM1(String str)&lt;br /&gt;{&lt;br /&gt;return(str.replaceAll( &quot;(.)\\1+&quot;, &quot;$1&quot; ));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Method 2 : Using For Loop&lt;br /&gt;public static String  ReplConsectiveCharM2(String s)&lt;br /&gt;{&lt;br /&gt;StringBuffer sb=new StringBuffer(s);&lt;br /&gt;for (int i=0; i&amp;lt;sb.length() ; i++)&lt;br /&gt; {&lt;br /&gt;for (int j=i+1; j&amp;lt;sb.length() ; j++)&lt;br /&gt;   {&lt;br /&gt;   if (sb.charAt(i) ==sb.charAt(j) ) &lt;br /&gt;      {&lt;br /&gt;      sb.deleteCharAt(j); &lt;br /&gt;       j--;&lt;br /&gt;     }&lt;br /&gt;   else break;&lt;br /&gt;    } &lt;br /&gt;  }&lt;br /&gt;return sb.toString();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;} &lt;br /&gt; &lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Output&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilDfPYoalu8CwA8bQdz1jAtBh9ERT13t5Qk31mnZvqL7cmdFZK7tIjmrYBnqcXKWrTRRyDE_bj9iHVaT0qWo6hdY_QrmuoOM5e0MWKd_bcvyPd_Id0k7kX8ipQRogvTA02jP8p6jNGs7A/s1600/replconsecutivechar.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 92px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilDfPYoalu8CwA8bQdz1jAtBh9ERT13t5Qk31mnZvqL7cmdFZK7tIjmrYBnqcXKWrTRRyDE_bj9iHVaT0qWo6hdY_QrmuoOM5e0MWKd_bcvyPd_Id0k7kX8ipQRogvTA02jP8p6jNGs7A/s400/replconsecutivechar.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5683490487562246930&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/2588785622827191239/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/12/java-program-that-replaces-two-or-more.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2588785622827191239'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2588785622827191239'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/12/java-program-that-replaces-two-or-more.html' title='Java program that replaces two or more consecutive characters / blanks in a string by a single character / blank. Use Regular Expression.'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilDfPYoalu8CwA8bQdz1jAtBh9ERT13t5Qk31mnZvqL7cmdFZK7tIjmrYBnqcXKWrTRRyDE_bj9iHVaT0qWo6hdY_QrmuoOM5e0MWKd_bcvyPd_Id0k7kX8ipQRogvTA02jP8p6jNGs7A/s72-c/replconsecutivechar.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-4699955010277323033</id><published>2011-10-18T12:06:00.000-07:00</published><updated>2011-10-18T12:12:30.432-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Dart"/><title type='text'>About Google Dart - a Structured Web Programming and its Features (an alternative to Javascript )</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;Good News to Web Developers .Search Engine Giant  Google has come up with a new programming language called Google Dart for building web applications. Dart is considered to be  an alternative to JavaScript . Although javascript from Netscape is a powerful scripting language used to add dynamic content to Web pages , the purpose to develop a new language is to address the core problems existing in the javascript (Please refer &lt;a href=https://gist.github.com/1208618&gt;leaked memo&lt;/a&gt; . As per leaked memo ,  the flaws exists in the  Javascript  cannot be fixed  by evolving the language . So  Google has  developed  a new language and  tried to fix the  problems in the Javascript  also aims to maintain the nature of Javascript but have a better  performance    Now let us discuss the technical aspects of Google Dart Language . &lt;br /&gt;&lt;b&gt;Technical aspects &amp; Features : &lt;/b&gt;&lt;br /&gt;              Google Dart is structured web programming language . It is a class-based, object-oriented language. Dart code is single-inheritance and  single threaded. Dart does not support shared state concurrency .   But . Concurrency is supported via isolates.  An isolate is a unit of concurrency. It has its own memory and its own thread of control.  The extention of the Dart file name will be &lt;b&gt;.dart&lt;/b&gt;.  Dart code can be executed  by a native virtual machine(VM)  or  on top of a JavaScript engine by using a compiler that translates Dart code to JavaScript.  i.e  Write code (web application)  in Dart  =&amp;gt;Compile it =&amp;gt;Run it  on any modern browser. &lt;br /&gt;&lt;b&gt;Sample program in DART to add two numbers &lt;/b&gt;&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt; &lt;br /&gt;main() {&lt;br /&gt;var a=10 , b=20;&lt;br /&gt;var c=a+b;&lt;br /&gt; print(&#39;Hello World ,Welcome to Dart  Programming! Sum of two Numbers &#39; );&lt;br /&gt;  print(a + &#39; + &#39; + b +&#39; = &#39; +c );&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;Output of the above program :&lt;/b&gt;&lt;br /&gt; Hello World ,Welcome to Dart  Programming! Sum of two Numbers &lt;br /&gt;10 + 20 = 30&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Some of the Features : &lt;/b&gt;&lt;br /&gt;1. As you see in the above sample program ,  Dart has very familiar syntax   which  is easy to learn for the programmers .&lt;br /&gt;2. Dart assures  to deliver high performance on all modern web browsers from  handheld devices to server-side execution.&lt;br /&gt;3.  More structured and flexible language for web programming.&lt;br /&gt;4. Google  believes Dart will be more suitable  for writing large web applications.&lt;br /&gt;5. The language with a set of basic libraries and  tools for checking, compiling, and running Dart code  are available online as  open source at  &lt;a href=http://www.dartlang.org&gt;dartlang.org&lt;/a&gt;&lt;br /&gt;6.  Google ensures that Dart runs  on any modern browser . How it is possible ?  . Google will integrate  Dart VM  with Chrome so that it can run Dart code  . But as far as other browser is concerned ,either  Google may encourage competitors to integrate Dart VM with their browsers  or  Google will offer a cross compiler that converts Dart code  to javascript ( ECMAScript)   for compatibility with non-Dart browsers. &lt;br /&gt;Note : Dart is originally  called Dash &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/4699955010277323033/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/10/about-google-dart-structured-web.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/4699955010277323033'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/4699955010277323033'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/10/about-google-dart-structured-web.html' title='About Google Dart - a Structured Web Programming and its Features (an alternative to Javascript )'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-8520135087426602380</id><published>2011-10-01T06:10:00.000-07:00</published><updated>2011-10-01T06:19:03.140-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><category scheme="http://www.blogger.com/atom/ns#" term="StringBuffer"/><title type='text'>String Vs String Buffer . Difference between String &amp; StringBuffer class example</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;Java &lt;b&gt;String &lt;/b&gt;object is immutable  that means the content of the String Object contents cannot be modified after they are created . There are no methods available to manipulate the contents of the string once it is created.  Methods that operate on a string returns a new string  rather than manipulating  the original copy of the string. String concatenation is done using   &quot;+&quot; operator which creates many temporary objects and increases garbage collection . So String concatenation  using &quot;+&quot; may be avoided. &lt;br /&gt;The &lt;b&gt;StringBuffer&lt;/b&gt; class represents a mutable string of characters that means their values can be changed after they are created.  There are methods like append , insert are  available which allows  to manipulate the contents of the string once it is created .  String concatenation is done using   append method  which avoids creating many temporary strings.  So it is better to use the Java StringBuffer object to concatenate strings because it is more efficient.&lt;br /&gt;&lt;b&gt;Note :&lt;/b&gt; Both String and String buffers are safe for use by multiple threads because  the operations on string will create a new string and  String buffer methods are synchronized . Every string buffer has a initial capacity. If the internal buffer used by the string buffer  overflows, it is automatically made larger.&lt;br /&gt;String buffer / StringBuilder are used by the compiler to implement the binary string concatenation operator +. StringBuilder is similar to StringBuffer except String bulider is not synchronised.  For example, the code: &lt;br /&gt;     input = &quot;StringConcatenation is done using &quot; + &quot;StringBuffer.append &quot; +4 + &quot; u&quot;  is compiled to the equivalent to the following line. &lt;br /&gt;     input = new StringBuffer().append(&quot;StringConcatenation is done using &quot;).append(&quot;StringBuffer.append &quot;).append(4).append(&quot; u&quot;).toString()&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;   &lt;br /&gt;class StringCat&lt;br /&gt;{&lt;br /&gt;public static void main(String...args)&lt;br /&gt;{&lt;br /&gt;String  input = &quot;StringConcatenation is done using &quot; + &quot;StringBuffer.append &quot; +4 + &quot; u&quot;  &lt;br /&gt;System.out.println(&quot;Output= &quot;+ input);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Compile the above program  using javac StringCat.java and  Run the using javap -c StringCat  which will display the bytecode of the StringCat.class as follows.&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;   &lt;br /&gt;Compiled from &quot;StringBuffer.java&quot;&lt;br /&gt;class StringBuffer extends java.lang.Object{&lt;br /&gt;StringBuffer();&lt;br /&gt;  Code:&lt;br /&gt;   0:   aload_0&lt;br /&gt;   1:   invokespecial   #1; //Method java/lang/Object.&quot;&amp;lt;init&amp;gt;&quot;:()V&lt;br /&gt;   4:   return&lt;br /&gt;&lt;br /&gt;public static void main(java.lang.String[]);&lt;br /&gt;  Code:&lt;br /&gt;   0:   ldc     #2; //String StringConcatenation is done using StringBuffer.append 4 u&lt;br /&gt;   2:   astore_1&lt;br /&gt;   3:   getstatic       #3; //Field java/lang/System.out:Ljava/io/PrintStream;&lt;br /&gt;   6:   new     #4; //class java/lang/StringBuilder&lt;br /&gt;   9:   dup&lt;br /&gt;   10:  invokespecial   #5; //Method java/lang/StringBuilder.&quot;&amp;lt;init&amp;gt;&quot;:()V&lt;br /&gt;   13:  ldc     #6; //String Output=&lt;br /&gt;   15:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;&lt;br /&gt;   18:  aload_1&lt;br /&gt;   19:  invokevirtual   #7; //Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder;&lt;br /&gt;   22:  invokevirtual   #8; //Method java/lang/StringBuilder.toString:()Ljava/lang/String;&lt;br /&gt;   25:  invokevirtual   #9; //Method java/io/PrintStream.println:(Ljava/lang/String;)V&lt;br /&gt;   28:  return&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/8520135087426602380/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/10/string-vs-string-buffer-difference.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8520135087426602380'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8520135087426602380'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/10/string-vs-string-buffer-difference.html' title='String Vs String Buffer . Difference between String &amp; StringBuffer class example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-752863391489492083</id><published>2011-09-30T11:53:00.000-07:00</published><updated>2011-09-30T12:32:23.208-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Serverside Validation"/><category scheme="http://www.blogger.com/atom/ns#" term="Struts"/><category scheme="http://www.blogger.com/atom/ns#" term="Struts Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Validation"/><title type='text'>Creating Own (Custom) Validations in Struts  &amp; Adding New Validation Rule ,  Definition  with Example code</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;Useful  validation methods  like  validateRequired , validateEmail , validateCreditCard , validateInteger , validateDate , ValidateMinLength and lotmore methods are already packaged with Struts Validator  Framework  . But application may require some specific validation method which may not be available with Struts validator.  But it has a flexibility to create and add new validation rules which can be reused in any form. Writing custom validation method in a action form may not be reused . So you need to write in every form. Now let us create a custom validation method and add to the validation-rules.xml. For example , an Employee  is assigned a target date to complete the work  .The employer  should not assign a date which is already  past or  sunday.  DateValidate method is available in validation-rules.xml  to check  wheather  the given date is valid (dd/MM/yyyy) or not.  Now we have to  write  code for checking the given date is already passed or  sunday . &lt;br /&gt;Major Steps to create custom  validation &lt;br /&gt;1. Writing code for validation method&lt;br /&gt;2. Adding  Validation rule for the above method in the validation-rules.xml file&lt;br /&gt;3. Adding new validation definitions to validation.xml file &lt;br /&gt;4. Adding  necessary error messages   in the  ApplicationResources.properties / MessageResources.properties.&lt;br /&gt;Now let us go through the above steps in detail&lt;br /&gt;    Ist Step is writing code for Validation Method in our example it is &lt;b&gt;sundayValidate&lt;/b&gt; to check the given date is already passed or  sunday . The vaildation method should have the arguments  / signatures as given below public static boolean validateSunday(&lt;b&gt;Object bean,ValidatorAction val_action,Field field,ActionErrors errors,HttpServletRequest request&lt;/b&gt;)  . The complete code is as follows. The validateSunday method is written in a class . In our example it is written in the class ValidationUtil under the util package. &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;package util;&lt;br /&gt;import java.util.Calendar;&lt;br /&gt;import java.util.GregorianCalendar;&lt;br /&gt;import javax.servlet.http.HttpServletRequest;&lt;br /&gt;import org.apache.commons.validator.Field;&lt;br /&gt;import org.apache.commons.validator.ValidatorAction;&lt;br /&gt;import org.apache.commons.validator.ValidatorUtil;&lt;br /&gt;import org.apache.struts.action.ActionErrors;&lt;br /&gt;import org.apache.struts.validator.Resources;&lt;br /&gt;public class ValidationUtil&lt;br /&gt;{&lt;br /&gt;public static boolean validateSunday(Object bean,ValidatorAction val_action,Field field,ActionErrors errors,HttpServletRequest request) {&lt;br /&gt; String value = ValidatorUtil.getValueAsString(bean, field.getProperty());&lt;br /&gt;    try {&lt;br /&gt;  Calendar cal = Calendar.getInstance();&lt;br /&gt;  Calendar today = Calendar.getInstance();&lt;br /&gt;  today.set(today.get(Calendar.YEAR),today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH), 12,0,0);&lt;br /&gt;  int  dd=Integer.parseInt(value.substring(0,2));&lt;br /&gt;  int mm =Integer.parseInt(value.substring(3,5));&lt;br /&gt;  int yyyy = Integer.parseInt(value.substring(6,10));&lt;br /&gt;   cal.set(yyyy ,mm-1, dd ,12,0,0);&lt;br /&gt;  int DOW = cal.get(Calendar.DAY_OF_WEEK);    // 1=Monday&lt;br /&gt;     if (cal.before(today) ||   (DOW-1)==0) {&lt;br /&gt;    errors.add(field.getKey(), Resources.getActionError(request,val_action,field));&lt;br /&gt;    return false;&lt;br /&gt;    }&lt;br /&gt;    } catch (Exception e) {&lt;br /&gt;    errors.add(field.getKey(), Resources.getActionError(request,val_action,field));&lt;br /&gt;    return false;&lt;br /&gt;    }&lt;br /&gt; return true;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;In the above method ,  Object is Form Bean object which contains the field values to be validated.  Field object retrieves the &amp;lt;field&amp;gt; definition (field name)  from the validation.xml for the field which is currently being validated.  getValueAsString() method is used to retrieve the value   for the  field name (in our example targetDate)  returned by the field.getProperty() from  the Form bean  . The logic checks the targetDate date is sunday or it is a past date . If it is sunday or old date , then the error message is added to ActionError object. &lt;br /&gt;&lt;br /&gt;      Now the 2nd step is adding Validation rule for the above method in the validation-rules.xml file&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;&amp;lt;form-validation&amp;gt;&lt;br /&gt;&amp;lt;global&amp;gt;&lt;br /&gt;.....&lt;br /&gt;......&lt;br /&gt;&amp;lt;validator name=&quot;sunday&quot; classname=&quot;util.ValidationUtil&quot;  method=&quot;validateSunday&quot; &lt;br /&gt;               methodParams=&quot;java.lang.Object,&lt;br /&gt;                       org.apache.commons.validator.ValidatorAction,&lt;br /&gt;                       org.apache.commons.validator.Field,&lt;br /&gt;                       org.apache.struts.action.ActionErrors,&lt;br /&gt;                       javax.servlet.http.HttpServletRequest&quot;&lt;br /&gt;              depends=&quot;&quot;&lt;br /&gt;       msg=&quot;errors.sunday&quot;&amp;gt;&lt;br /&gt;&amp;lt;/validator&amp;gt;&lt;br /&gt;.....&lt;br /&gt;   &amp;lt;/global&amp;gt;&lt;br /&gt;&amp;lt;/form-validation&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The validation method is given a name (in our example sunday)  by using name attribute as given above .  We have to also  mention the class name of the method using classname attribute . Method name is mentioned by method attribute .    The above validation rule can be referenced by field tag&#39;s depends attribute using name (in our example sunday)  . Note : Validation rule must be placed in &amp;lt;global&amp;gt; element in validation-rules.xml file. &lt;br /&gt;&lt;br /&gt;       Now 3rd step is adding new validation definitions to validation.xml file  whic is as follows .&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;&amp;lt;form-validation&amp;gt; &lt;br /&gt;&amp;lt;formset&amp;gt;&lt;br /&gt;.....&lt;br /&gt;&amp;lt;form name=&quot;targetFrm&quot;&amp;gt; &lt;br /&gt;&amp;lt;field property=&quot;empCode&quot; depends=&quot;required&quot;&amp;gt; &lt;br /&gt;&amp;lt;arg0 key=&quot;targetFrm.empCode&quot; /&amp;gt; &lt;br /&gt;&amp;lt;/field&amp;gt; &lt;br /&gt;&amp;lt;field property=&quot;targetDate&quot; depends=&quot;required, date, sunday&quot;&amp;gt; &lt;br /&gt;&amp;lt;arg0 key=&quot;targetFrm.targetDate&quot; /&amp;gt; &lt;br /&gt;&amp;lt;arg1 key=&quot;targetFrm.targetDate&quot; /&amp;gt; &lt;br /&gt;&amp;lt;arg2 key=&quot;targetFrm.targetDate&quot; /&amp;gt; &lt;br /&gt;&amp;lt;var&amp;gt; &lt;br /&gt;&amp;lt;var-name&amp;gt;datePatternStrict&amp;lt;/var-name&amp;gt; &lt;br /&gt;&amp;lt;var-value&amp;gt;dd/MM/yyyy&amp;lt;/var-value&amp;gt; &lt;br /&gt;&amp;lt;/var&amp;gt; &lt;br /&gt;&amp;lt;/field&amp;gt;&lt;br /&gt;&amp;lt;/form&amp;gt;&lt;br /&gt;....&lt;br /&gt;&amp;lt;/formset&amp;gt; &lt;br /&gt;&amp;lt;/form-validation&amp;gt;  &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;In the above definition field tag  mentions the  property name  to be validated .  depends attribute mentions the method  reference to be called for validation.  required calls the requiredMethod , date calls the dateValidate  . sunday calls the sundayValidate which is a custom validation method written by us and defined in the validation-rules.xml file.  required and date are already defined by default in the validation-rules.xml file with &lt;b&gt;errors.required&lt;/b&gt; and &lt;b&gt;errors.date&lt;/b&gt; message &lt;br /&gt;&lt;br /&gt;    Final and 4th step is adding  error messages to ApplicationResources.properties &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;targetFrm.empCode=Employee Code&lt;br /&gt;targetFrm.targetDate=Target Date&lt;br /&gt;# general error msgs &lt;br /&gt;errors.required={0} is required. &lt;br /&gt;errors.sunday={0} is either old date or  Sunday&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The jsp code (part)  is given below. &lt;br /&gt;targetDate.jsp&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;&amp;lt;HTML&amp;gt;&lt;br /&gt;......&lt;br /&gt;&amp;lt;TITLE&amp;gt;Employee Targetdate assign&amp;lt;/TITLE&amp;gt;&lt;br /&gt;&amp;lt;/HEAD&amp;gt;&lt;br /&gt;&amp;lt;body&amp;gt;   &amp;lt;html:errors /&amp;gt; &lt;br /&gt;&amp;lt;html:javascript formName=&quot;targetFrm&quot; /&amp;gt;  &lt;br /&gt; &amp;lt;html:form action=&quot;/targetDate&quot;&amp;gt;    &lt;br /&gt; &amp;lt;bean:message key=&quot;targetFrm.empCode&quot; /&amp;gt; &lt;br /&gt; &amp;lt;html:text property=&quot;empCode&quot;&amp;gt;&amp;lt;/html:text&amp;gt;     &amp;lt;br /&amp;gt; &lt;br /&gt; &amp;lt;bean:message key=&quot;targetFrm.targetDate&quot; /&amp;gt;  &lt;br /&gt; &amp;lt;html:text property=&quot;targetDate&quot;&amp;gt;&amp;lt;/html:text&amp;gt;     &amp;lt;br /&amp;gt;  &lt;br /&gt; &amp;lt;html:submit value=&quot;Submit&quot;&amp;gt;&amp;lt;/html:submit&amp;gt; &lt;br /&gt; &amp;lt;/html:form&amp;gt; &amp;lt;/body&amp;gt; &lt;br /&gt;....&lt;br /&gt;&amp;lt;/HTML&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The struts-config.xml (part)  code is as follows&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt; &amp;lt;form-beans&amp;gt;&lt;br /&gt;  &amp;lt;/form-bean&amp;gt;&lt;br /&gt;  &amp;lt;form-bean name=&quot;targetFrm&quot; type=&quot;strutsvalidatorweb.forms.empForm&quot;&amp;gt;&lt;br /&gt;  &amp;lt;/form-bean&amp;gt;&lt;br /&gt; &amp;lt;/form-beans&amp;gt;&lt;br /&gt;&lt;br /&gt; &amp;lt;action-mappings&amp;gt;&lt;br /&gt;    &amp;lt;action name=&quot;targetFrm&quot; path=&quot;/targetDate&quot; scope=&quot;request&quot; type=&quot;strutsvalidatorweb.actions.empEnty&quot; validate=&quot;true&quot; input=&quot;/targetDate.jsp&quot; parameter=&quot;method&quot;&amp;gt;&lt;br /&gt;     &amp;lt;forward name=&quot;target&quot; path=&quot;./target.jsp&quot;&amp;gt;&lt;br /&gt;     &amp;lt;/forward&amp;gt;&lt;br /&gt;   &amp;lt;/action&amp;gt; &lt;br /&gt; &amp;lt;/action-mappings&amp;gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;empForm code (part) is given below&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;public class empForm extends ValidatorForm {&lt;br /&gt;private String empCode = null;&lt;br /&gt;private String targetDate=null;&lt;br /&gt;public String getEmpCode() {return empCode;}&lt;br /&gt;public String getEmpName() {return empName;}&lt;br /&gt;public String getTargetDate() {return targetDate;}&lt;br /&gt;public void setTargetDate(String string) {targetDate = string;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;For more detail about Struts Validator Framework with example , please visit &lt;a href=&quot;http://www.techlabs4u.com/2011/05/struts-validator-framework-example-how.html&quot;&gt;Struts validator framework example&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Output :&lt;/b&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD-y0euON5UC4iqVC-2IMHmJhSU1ZZJw58pZZ1T_h12S5LPPQI6DqNbkkZ2dNVS6ZCuIzjpUfUpuCXSNXsx6xBB7ENf0P02CKeqoeeL4MdYBhD5DO_7g4SK8WBgPIIXqUxUQkWbRye3rE/s1600/custom+validation+in+struts.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 296px; height: 140px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD-y0euON5UC4iqVC-2IMHmJhSU1ZZJw58pZZ1T_h12S5LPPQI6DqNbkkZ2dNVS6ZCuIzjpUfUpuCXSNXsx6xBB7ENf0P02CKeqoeeL4MdYBhD5DO_7g4SK8WBgPIIXqUxUQkWbRye3rE/s400/custom+validation+in+struts.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5658230911997256210&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;b&gt;Note :&lt;/b&gt; In the above jsp ,  input date should be given as dd/MM/yyyy (total 10 digits , year should be in 4 digits)  because we have given date pattern as &lt;b&gt;datePatternStrict&lt;/b&gt; with the value dd/MM/yyyy. Other date format strictly not accepted. To accept other format you have to give &lt;b&gt;datePattern&lt;/b&gt; , but you need to change code to split the date in the  the validateSunday method .</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/752863391489492083/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/creating-own-custom-validations-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/752863391489492083'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/752863391489492083'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/creating-own-custom-validations-in.html' title='Creating Own (Custom) Validations in Struts  &amp; Adding New Validation Rule ,  Definition  with Example code'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhD-y0euON5UC4iqVC-2IMHmJhSU1ZZJw58pZZ1T_h12S5LPPQI6DqNbkkZ2dNVS6ZCuIzjpUfUpuCXSNXsx6xBB7ENf0P02CKeqoeeL4MdYBhD5DO_7g4SK8WBgPIIXqUxUQkWbRye3rE/s72-c/custom+validation+in+struts.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-103254077076218476</id><published>2011-09-25T13:03:00.000-07:00</published><updated>2011-09-25T13:10:50.729-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="regex"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>Write own code for Java String indexOf()  to find position of a given string in a source string</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;Let us first see what java String indexOf() does  . It returns position of the given character / substring in a given String from the first occurance or starting from the specified position.  IndexOf() is overloaded into four methods.&lt;br /&gt; 1.   indexOf(int ch) -  Returns the position / index of the given charater with in this string . &lt;br /&gt; 2.   indexOf(int ch, int fromIndex)  -  Same as the above method but  search  starts from the  the specified index. &lt;br /&gt; 3.  indexOf(String str) -   Returns the position / index of the given String with in this string  &lt;br /&gt; 4.   int indexOf(String str, int fromIndex) -  Same as the 3rd  method but  search  starts from the  the specified index.     &lt;br /&gt;Now let us write code for &lt;b&gt;indexOf(String source, String substr, int startPos, int occurrence, char caseSensitive) &lt;/b&gt;  which does the same work with more options using Regexp,  where &lt;b&gt;source&lt;/b&gt; is the original string to search in , &lt;b&gt;substr&lt;/b&gt; is the substring to be searched in the original string , &lt;b&gt;startPos&lt;/b&gt; is the starting position to search , &lt;b&gt;occurrence&lt;/b&gt; is used to tell which match to get. There may be more than one substring in the given string . If we give occurrence is 2 , then it will return the position of the second match. Matching can be done using CaseSensitive or Case In Sensitive . We have to pass  &#39;N&#39;  for CaseInSensitive . &lt;br /&gt;  &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;    &lt;br /&gt;import java.util.regex.Matcher;&lt;br /&gt;import java.util.regex.Pattern;&lt;br /&gt;import java.util.regex.PatternSyntaxException;&lt;br /&gt;class customIndexOf&lt;br /&gt;{&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;String source=&quot;Using Db2 UDB , Udfs can be created. DB2 UDFs  is DB2 User Defined Functions&quot;;&lt;br /&gt;String sub=&quot;db2&quot;;&lt;br /&gt;System.out.println(&quot;Position of  &quot; + sub + &quot;  = &quot;+ indexOf(source,sub,0,2,&#39;N&#39; ));&lt;br /&gt;sub=&quot;UDFs&quot;;&lt;br /&gt;System.out.println(&quot;Position of  &quot; + sub + &quot;  = &quot;+ indexOf(source,sub,0,1,&#39;Y&#39; ));&lt;br /&gt;sub=&quot;c&quot;;&lt;br /&gt;System.out.println(&quot;Position of  &quot; + sub + &quot;  = &quot;+ indexOf(source,sub,0,1,&#39;N&#39; ));&lt;br /&gt;sub=&quot;DB2&quot;;&lt;br /&gt;System.out.println(&quot;Using Java Index of ()  Position of   &quot; + sub + &quot;  = &quot;+ (source.indexOf(sub, 0)+1));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static int indexOf(String source, String substr, int startPos, int occurrence, char caseSensitive)&lt;br /&gt;{&lt;br /&gt;int position = 0;&lt;br /&gt;int flag=2;&lt;br /&gt; int occurrenceCount = 0;&lt;br /&gt;if(caseSensitive==&#39;Y&#39;)  flag=0;&lt;br /&gt;   if (  source == null || source.length()==0)  return -1; &lt;br /&gt;   Pattern pattern=null;&lt;br /&gt;  try&lt;br /&gt;   {&lt;br /&gt;      pattern = Pattern.compile(substr, flag);&lt;br /&gt;   }&lt;br /&gt;   catch (PatternSyntaxException e)&lt;br /&gt;   {&lt;br /&gt;      return 0;&lt;br /&gt;   } &lt;br /&gt;   Matcher matcher = pattern.matcher(source);&lt;br /&gt;   if (matcher.find(startPos))&lt;br /&gt;   {&lt;br /&gt;      position = matcher.start()+ 1;&lt;br /&gt;      occurrenceCount++;&lt;br /&gt;      while ((occurrenceCount &amp;lt; occurrence) &amp;&amp; (matcher.find()))&lt;br /&gt;      {&lt;br /&gt;         position = matcher.start() + 1;&lt;br /&gt;         occurrenceCount++;&lt;br /&gt;      }&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;if (occurrenceCount!=occurrence)  position =0 ;&lt;br /&gt;  return (position);&lt;br /&gt;}&lt;br /&gt; }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixPsC7w2we57H1cepZsW4WceKXJ8qckZ7A__Jybu5PpgYOZTjjessjyR3mvhRcxs1tLSXGlIXPqnnRCJA_mdfJZCYngDtKX5NYcxeX2SRZTH57USPqme-GD187hyphenhyphen1Id7wx2lSA_uqX6Rk/s1600/indexof.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 74px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixPsC7w2we57H1cepZsW4WceKXJ8qckZ7A__Jybu5PpgYOZTjjessjyR3mvhRcxs1tLSXGlIXPqnnRCJA_mdfJZCYngDtKX5NYcxeX2SRZTH57USPqme-GD187hyphenhyphen1Id7wx2lSA_uqX6Rk/s400/indexof.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5656390754576679474&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/103254077076218476/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/write-own-code-for-java-string-indexof.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/103254077076218476'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/103254077076218476'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/write-own-code-for-java-string-indexof.html' title='Write own code for Java String indexOf()  to find position of a given string in a source string'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixPsC7w2we57H1cepZsW4WceKXJ8qckZ7A__Jybu5PpgYOZTjjessjyR3mvhRcxs1tLSXGlIXPqnnRCJA_mdfJZCYngDtKX5NYcxeX2SRZTH57USPqme-GD187hyphenhyphen1Id7wx2lSA_uqX6Rk/s72-c/indexof.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-2614658539027009595</id><published>2011-09-18T04:23:00.000-07:00</published><updated>2011-10-15T05:24:55.451-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Collection"/><category scheme="http://www.blogger.com/atom/ns#" term="HashMap"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Sorting"/><category scheme="http://www.blogger.com/atom/ns#" term="TreeMap"/><title type='text'>How to Sort HashMap by Values . Write Own Compare Method in Java Example</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;We can sort the Hashmap by putting all values in a TreeMap . By default TreeMap is sorted  by Keys. To sort it by values , we have to write own  compare method  of comparator interface.  The compare method compares its  two arguments . By default two arguments are objects which is having key of Map. If both arguments are equal then it returns 0 . If first argument is less than the second argument , returns a negative integer,  If  first argument is greater than the second argument , returns a Positive integer. Now we have to  sort by values. So we have to  get the values using key  i.e  map.get(key)  .  Now we have to write our own logic to compare  the values . The return value will be 0  or 1 or -1 . If more than one elements are equal  it adds only the first value. If both arguments are equal means it returns 0 that means Hashmap  is having duplicate values. As HashMap does not allow duplicate values ,to allow duplicate values , we have to return other than 0 value. In our example , when both values are equal , we compare both keys  rather than values , thus returns other than 0.   The  following program is compatible for sorting of strings and also positive numbers. Negative numbers are sorted properly  because we are using &lt;b&gt;String.compareTo &lt;/b&gt; to compare two values. So it is always  better to pass same type of values and change the compareTo logic accordingly. For example if all the values are  integer  means , the logic will be  &lt;b&gt; Integer e1 = (Integer)thisMapValues.get(o1);  Integer e2 = (Integer) thisMapValues.get(o2);  if (e1==e2) return  return String.valueOf(o1).compareTo(String.valueOf(o2)) ; else  if (e1&amp;lt;e2) return -1 ; else return 1;  &lt;/b&gt;&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;public class HashMapSortValue{&lt;br /&gt;private static class OwnCompare implements Comparator &lt;br /&gt;                     {&lt;br /&gt;  private Map  thisMapValues = null;&lt;br /&gt;  public OwnCompare (Map mapValues)&lt;br /&gt;                                         {&lt;br /&gt;   super();&lt;br /&gt;   thisMapValues = mapValues;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  public int compare(Object o1, Object o2) &lt;br /&gt;     {&lt;br /&gt;  String    e1 = String.valueOf(thisMapValues.get(o1));&lt;br /&gt;                  String  e2 = String.valueOf( thisMapValues.get(o2));&lt;br /&gt;   if (e1.compareTo(e2)==0)   return String.valueOf(o1).compareTo(String.valueOf(o2));  else  return e1.compareTo(e2);&lt;br /&gt;  &lt;br /&gt;         }  &lt;br /&gt;}&lt;br /&gt;   &lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;Map hm = new HashMap();&lt;br /&gt;hm.put (&quot;Employee1&quot;, 60000);&lt;br /&gt;hm.put (&quot;Employee2&quot;, 50000);&lt;br /&gt;hm.put (&quot;Employee2&quot;, &quot;absent&quot;);&lt;br /&gt;hm.put (&quot;Employee3&quot;, 25000);&lt;br /&gt;hm.put (&quot;Employee4&quot;,&quot;retired&quot;);&lt;br /&gt;hm.put (&quot;Employee5&quot;,&quot;absent&quot;);&lt;br /&gt;hm.put (&quot;Employee6&quot;,20000);&lt;br /&gt;hm.put (&quot;Employee7&quot;,20000);&lt;br /&gt;hm.put (&quot;Employee8&quot;,50000);&lt;br /&gt;hm.put (&quot;Employee9&quot;,60000);&lt;br /&gt;System.out.println(&quot;List  Before Sorting : &quot; + hm); &lt;br /&gt;SortedMap sortedByValue = new TreeMap(new HashMapSortValue.OwnCompare(hm));&lt;br /&gt;sortedByValue.putAll(hm);&lt;br /&gt;System.out.println(&quot;List  After Sorting by Value &quot; +sortedByValue);&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;b&gt;Output :&lt;/b&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqfLFJyaAkOiWhxwfV-Olu4qCx7wdWBG5ZkYfmZhURVtMxgEdpMBVH_QfCtEejZldnwJFbE0xMLDtaRO3gVsrhFqKyehJNUjjHowmulXEpNJ9lcfSu643_Dxi7qPolrey3zhV1tTnZKno/s1600/hashmapsort+by+values.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 36px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqfLFJyaAkOiWhxwfV-Olu4qCx7wdWBG5ZkYfmZhURVtMxgEdpMBVH_QfCtEejZldnwJFbE0xMLDtaRO3gVsrhFqKyehJNUjjHowmulXEpNJ9lcfSu643_Dxi7qPolrey3zhV1tTnZKno/s400/hashmapsort+by+values.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5653661312197046594&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/2614658539027009595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/how-to-sort-hashmap-by-values-write-own.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2614658539027009595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2614658539027009595'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/how-to-sort-hashmap-by-values-write-own.html' title='How to Sort HashMap by Values . Write Own Compare Method in Java Example'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiqfLFJyaAkOiWhxwfV-Olu4qCx7wdWBG5ZkYfmZhURVtMxgEdpMBVH_QfCtEejZldnwJFbE0xMLDtaRO3gVsrhFqKyehJNUjjHowmulXEpNJ9lcfSu643_Dxi7qPolrey3zhV1tTnZKno/s72-c/hashmapsort+by+values.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-5453706173908374082</id><published>2011-09-12T14:27:00.000-07:00</published><updated>2012-01-12T09:39:40.576-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code"/><category scheme="http://www.blogger.com/atom/ns#" term="equal"/><category scheme="http://www.blogger.com/atom/ns#" term="hashCode"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Override"/><title type='text'>Comparing Objects by values equality by overriding equal  / hashCode() method with example.</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;Basically  ,&lt;b&gt;  Obj1.equals(Obj2)  &lt;/b&gt; returns true , if  Obj1 and Obj2 refer to the same object  that means the references of two Objects (Obj1 &amp; Obj2 )  point to the same object  which is equal to &lt;b&gt;if (Obj1==Obj2) &lt;/b&gt;.  This is the default implementation of the equal method of an Object. But we need to compare one or more values of the two Objects.  This can be done by overriding the equals method (i.e boolean equals(Object obj) )  . By overriding  equal  method , you can write your own logic  to decide wheather two objects of the same class are equal or not  that is the implementor decides which values should  be equal to consider two objects to be equal.  In our example , in Student  class, the two student objects equality is decided by  the total marks. &lt;br /&gt;In the following program,  Student class is defined with fields of name , Mark1 , Mark2, Mark3  and One constructor to assign values and public  methods to access fields.  We can create one or more  student objects  from Student  class.  Our objective is to check two student objects are equal or not based on the total value of 3 marks .So we have overrided the equal method by writing the logic to compare total marks of two objects of same class.  The hashCode method has to be  redefined as we are redefining the equals method . The concept is the Equal objects  must have equal hash codes. &lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;class Student  &lt;br /&gt;{&lt;br /&gt;  private String name;&lt;br /&gt;  private int mark1;&lt;br /&gt;  private int mark2;&lt;br /&gt;  private int mark3;&lt;br /&gt;public Student(String name, int m1, int m2, int m3)&lt;br /&gt;{&lt;br /&gt;this.name=name;&lt;br /&gt;this.mark1=m1;&lt;br /&gt;this.mark2=m2;&lt;br /&gt;this.mark3=m3;&lt;br /&gt;}&lt;br /&gt;public String getName() { return this.name; }&lt;br /&gt;public int getMark1() {return this.mark1;}&lt;br /&gt;public int getMark2() { return this.mark2; }&lt;br /&gt;public int  getMark3() { return this.mark3; }&lt;br /&gt;&lt;br /&gt;public void printValues()&lt;br /&gt;{&lt;br /&gt;System.out.println(&quot;name : &quot; + this.name);&lt;br /&gt;System.out.println(&quot;M1 : &quot; + this.mark1);&lt;br /&gt;System.out.println(&quot;M2 : &quot; + this.mark2);&lt;br /&gt;System.out.println(&quot;M3 : &quot; + this.mark3);&lt;br /&gt;}&lt;br /&gt; public boolean equals(Object obj)&lt;br /&gt;  {&lt;br /&gt;   boolean eq=false;&lt;br /&gt;    //if both object&#39;s reference is equal means , objects are equal&lt;br /&gt;    if(this == obj)    return true;&lt;br /&gt;    if (obj instanceof Student)&lt;br /&gt;    {&lt;br /&gt;      Student student = (Student) obj; &lt;br /&gt;      int tot1=student.getMark1() + student.getMark2() + student.getMark3();&lt;br /&gt;      int tot2=this.getMark1() + this.getMark2() + this.getMark3();&lt;br /&gt;       if (tot1==tot2) eq=true;&lt;br /&gt;       //the following line checks name also to decide Object&#39;s equality&lt;br /&gt;      // if ((tot1==tot2) &amp;&amp;( student.getName().equals(this.getName())) ) eq=true;&lt;br /&gt;      return eq;&lt;br /&gt;    }&lt;br /&gt;    else&lt;br /&gt;    {&lt;br /&gt;      return false;&lt;br /&gt;    }&lt;br /&gt;  } &lt;br /&gt; //hashCode() is overrided.&lt;br /&gt;public int hashCode()&lt;br /&gt;{&lt;br /&gt; int hash = 19;&lt;br /&gt; String tot1=Integer.toString(mark1+mark2+mark3);&lt;br /&gt; hash = 37 * hash + tot1.hashCode();&lt;br /&gt;                  //hashCode() of &#39;name&#39; field  is also considered in the below line. If equal method use &#39;name&#39; field  means , hashCode() also has to use. &lt;br /&gt; //hash = 37 * hash + name.hashCode() +tot1.hashCode(); &lt;br /&gt; return hash;&lt;br /&gt;} &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;//Class to create Students objects and compare two student objects for equality using overrided equal method&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;class OverrideEqual  &lt;br /&gt;{&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;Student stu1 = new Student(&quot;ABC&quot;, 50, 30, 21);&lt;br /&gt;Student stu2 = new Student(&quot;XYZ&quot;, 50, 30, 21);&lt;br /&gt;stu1.printValues();&lt;br /&gt;System.out.println(&quot;&quot;);&lt;br /&gt;stu2.printValues();&lt;br /&gt;if ( stu1.equals(stu1)  )  System.out.println(&quot;Both object references  point to  the same object&quot;);  else System.out.println(&quot;Both are different objects&quot;);  &lt;br /&gt;if ( stu1.equals(stu2 ) )  System.out.println(&quot;Both student are equal&quot;);  else System.out.println(&quot;Both students are not equal&quot;);  &lt;br /&gt;if ( stu2.equals(stu1 ) )  System.out.println(&quot;Two students are equal&quot;);  else System.out.println(&quot;Two student are not equal&quot;); &lt;br /&gt;if (stu1.hashCode()==stu2.hashCode()) System.out.println(&quot;Both Student Object Hashcode are equal&quot;);  else System.out.println(&quot;Both Student Object Hashcode are not equal&quot;); &lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;//Output :&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv97bOb5l-tTh99z8SFnu85flwZb5dnH0Yx3p9_N7vOAtND11lJAPKEnShglw8JHnjivgL2LX1NjVOt05i413NvRHL2iNyiUop4Z3WcLF5NoDgqbKTPZp1D793PsI-eKBZgIgBsVneJyk/s1600/equal+output.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 202px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv97bOb5l-tTh99z8SFnu85flwZb5dnH0Yx3p9_N7vOAtND11lJAPKEnShglw8JHnjivgL2LX1NjVOt05i413NvRHL2iNyiUop4Z3WcLF5NoDgqbKTPZp1D793PsI-eKBZgIgBsVneJyk/s400/equal+output.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5651590356253431986&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/5453706173908374082/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/comparing-objects-by-values-equality-by.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/5453706173908374082'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/5453706173908374082'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/comparing-objects-by-values-equality-by.html' title='Comparing Objects by values equality by overriding equal  / hashCode() method with example.'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiv97bOb5l-tTh99z8SFnu85flwZb5dnH0Yx3p9_N7vOAtND11lJAPKEnShglw8JHnjivgL2LX1NjVOt05i413NvRHL2iNyiUop4Z3WcLF5NoDgqbKTPZp1D793PsI-eKBZgIgBsVneJyk/s72-c/equal+output.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-7673245537065802520</id><published>2011-09-11T11:04:00.000-07:00</published><updated>2011-09-11T11:11:24.727-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="salt"/><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><title type='text'>Code to return random salt characters using java</title><content type='html'>&lt;div align=&quot;justify&quot;&gt;The salt is a sequence of characters / bytes which  is added to the password  before getting  digested by message digest algorithm, such as MD5 or SHA . It is better to send salted hash rather than sending encrypted the password alone .  This  protects against Brute Force attacks . Salt may be fixed  or variable . In fixed salt , everytime the same charaters are used for  digesting every password which is not more safe. In  variable salt, different characters (Random) are used for  each password being digested which is more secured. Now let us see how random salt is generated. For that we have to define a set of characters as a Salt. From these characters , we have to pick some number of chars (in our example it is  1 to 9 random number is generated between 1 to 9 )  randomly  which will be a  variable salt. The following code returns  random salt characters between 1 to 9 .&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 0px dashed rgb(150, 150, 150); margin: 1px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;  import java.util.*;&lt;br /&gt;  class  RandomSaltChars&lt;br /&gt;   {&lt;br /&gt;  public static void main(String args[])&lt;br /&gt;   {  &lt;br /&gt;   String  chars_salt = &quot;1234567890abcdefghijklABCDEFGHIJKLMNOPQRSTUVWXYZmnopqrstuvwxyz&quot;; &lt;br /&gt;   StringBuffer random_salt = new StringBuffer();&lt;br /&gt;   Random rnd = new Random();&lt;br /&gt;   int noc = rnd.nextInt(9) + 1;&lt;br /&gt;                     //returns a random 9 chars salt        &lt;br /&gt;   while (random_salt.length() &amp;lt; noc) {&lt;br /&gt;    int index = (int) (rnd.nextFloat() *chars_salt.length());&lt;br /&gt;                      random_salt.append(chars_salt.charAt( index));&lt;br /&gt;   }&lt;br /&gt;&lt;br /&gt;   System.out.println(&quot;salt characters : &quot; + random_salt);&lt;br /&gt;   }&lt;br /&gt;   }&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;&lt;br /&gt;Output of the program:&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhs7NZShFjVol5w2ISs1vu7VuCRIlRft4jTzKcskGxexwzoRdautYFPzWF9jHk05b5j-Ud3ALIYdNHmmZHdQmW38XdOazFZ36Z-nn2DISeFhw3sWPr7zIhdfVqgVBT3Xt08KemgRxsvlHM/s1600/salt.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 278px; height: 149px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhs7NZShFjVol5w2ISs1vu7VuCRIlRft4jTzKcskGxexwzoRdautYFPzWF9jHk05b5j-Ud3ALIYdNHmmZHdQmW38XdOazFZ36Z-nn2DISeFhw3sWPr7zIhdfVqgVBT3Xt08KemgRxsvlHM/s400/salt.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5651166186616593474&quot; /&gt;&lt;/a&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/7673245537065802520/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/code-to-return-random-salt-characters.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7673245537065802520'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7673245537065802520'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/09/code-to-return-random-salt-characters.html' title='Code to return random salt characters using java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhs7NZShFjVol5w2ISs1vu7VuCRIlRft4jTzKcskGxexwzoRdautYFPzWF9jHk05b5j-Ud3ALIYdNHmmZHdQmW38XdOazFZ36Z-nn2DISeFhw3sWPr7zIhdfVqgVBT3Xt08KemgRxsvlHM/s72-c/salt.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-1916675458309244497</id><published>2011-08-28T06:12:00.000-07:00</published><updated>2011-08-28T06:23:15.730-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Integer"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><title type='text'>Java Autoboxing / Unboxing with Example code</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;In older versions of java , int (or any other primitive value)  are not  accepted by  a collection.  Consider the following code for converting array to ArrayList  which gives the compile error due to Collection accepts  object references  not primitive  int values. The solution for this error is to box primitive values into the appropriate wrapper class . In this case we have to convert int to Integer wrapper class .  Now replace the line  lst.add(numbers[i]);  with  lst.add(new Integer(numbers[i])); here we have converted int to Integer wrapper class. &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;class listExample&lt;br /&gt;{&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;List lst = new ArrayList();&lt;br /&gt;int numbers[] ={10,30, 55, 65,33}; &lt;br /&gt;for (int i=0;i&lt;5;i++)&lt;br /&gt;{&lt;br /&gt;lst.add(numbers[i]);            // Error  listExample.java:13: cannot resolve symbol , symbol  : method add (int) , location: interface java.util.List . Solution :  Replace  lst.add(numbers[i]);  with lst.add(new Integer(numbers[i]));&lt;br /&gt;System.out.println(numbers[i]);        &lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;When we need an int, we have to unbox the Integer using the intValue method . Consider the following lines of code , where add method accepts two Object references (Not a primitive value) , when we compile the code in older version of java ,  it throws error , because we are adding two objects reference and assigning to a primitive int variable.  . Replacing the  int d=a+b;  with   int d=a.intValue()+b.intValue();  will solve the problem.&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;class Addition&lt;br /&gt;{&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;Integer k=new Integer(123);&lt;br /&gt;Integer k1=new Integer(1523);&lt;br /&gt;add (k, k1);&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;public static void add( Integer a , Integer b )    &lt;br /&gt;{   &lt;br /&gt;int d=a+b;   // using unbox    &lt;br /&gt;System.out.println(&quot;Addition = &quot; + d );   &lt;br /&gt;&lt;br /&gt;}   &lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The following example illustrates autoboxing and unboxing .   In the main function , we have declared 4 variables a, b , c, d .       a &amp; b are primitive type variables.   c &amp; d are Integer class variables.  Now in our code , we have assigned primitive type int value to  Integer c &amp;  Integer d . In older version , the this line gives compile error. But in java 1.5 , it does not give any error due to &lt;b&gt;autoboxing&lt;/b&gt; .  The  autoBoxUnbox( Integer a , Integer b ) accepts two object reference. So when we call autoBoxUnbox(a,b) where a , b are int values , in java 1.5 no error is coming due to autoboxing.  In the autoBoxUnbox method , the line   int d=a+b;  d is an int variable. but a &amp; b are Integer variable. Here a &amp; b are converted to int variable by &lt;b&gt;unboxing&lt;/b&gt; in java1.5 ,   In java 1.4 we have to use Integer.intValue()  to convert Integer to int. &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;class autoBoxUnbox&lt;br /&gt;{&lt;br /&gt;public static void main (String args[])&lt;br /&gt;{&lt;br /&gt;int a = 100;  &lt;br /&gt;int b = 100;   &lt;br /&gt;Integer c=300;&lt;br /&gt;Integer d=300;&lt;br /&gt;autoBoxUnbox(a,b);&lt;br /&gt;autoBoxUnbox(c,d);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static void autoBoxUnbox( Integer a , Integer b )    // using autoboxing&lt;br /&gt;{   &lt;br /&gt;Integer c=a+b;  &lt;br /&gt;int d=a+b;   // using unboxing&lt;br /&gt;System.out.println(&quot;Given Numbers &quot; + a + &quot; , &quot; + b);&lt;br /&gt;System.out.println(&quot;Addition  = &quot; + c );   &lt;br /&gt;System.out.println(&quot;Addition using unbox= &quot; + d );   &lt;br /&gt;System.out.println(&quot; &quot;);   &lt;br /&gt;}   &lt;br /&gt;}  &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt; &lt;/p&gt;&lt;br /&gt;&lt;b&gt;Output :&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;D:\as2\JF2&gt;java autoBoxUnbox&lt;br /&gt;Given Numbers 100 , 100&lt;br /&gt;Addition  = 200&lt;br /&gt;Addition using unbox= 200&lt;br /&gt;&lt;br /&gt;Given Numbers 300 , 300&lt;br /&gt;Addition  = 600&lt;br /&gt;Addition using unbox= 600</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/1916675458309244497/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/java-autoboxing-unboxing-with-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/1916675458309244497'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/1916675458309244497'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/java-autoboxing-unboxing-with-example.html' title='Java Autoboxing / Unboxing with Example code'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-4042051369393297958</id><published>2011-08-27T13:19:00.000-07:00</published><updated>2011-08-28T00:58:28.081-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Integer"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><title type='text'>int Vs Integer in Java Example ,Difference between Integer Wrapper &amp;  int primitive</title><content type='html'>&lt;p align=&quot;justify&quot;&gt; int  is a primitive data type . It  is a 32-bit signed two&#39;s complement integer which can have  a minimum value of -2,147,483,648 and a maximum value of 2,147,483,647 (inclusive).  Integer is a  wrapper class  for int.  The constuctor for Integer class is Integer(int num / String str) . So Integer class wraps a  primitive type int value  in an  object.  An object of Integer class contains a single field whose type is int . &lt;br /&gt; An int stores a number where as Integer points a reference of an object that contains a number .Integer class is an example  of immutable object which represents a single value that cannot change over the lifetime of the object.&lt;br /&gt;&lt;br /&gt;We may use Integer  rather than a primitive due to some of the below mentioned reasons.&lt;br /&gt;1. Some Methods of a class  (for example methods of collection class like List , Map, etc can hold object references)  accept only object reference  (not primitive  value)  as an argument . 2. Integer class has many methods &amp; constants which can be used in many ways . For example  Constants  of  the Integer  class, such as MIN_VALUE &amp;  MAX_VALUE, SIZE  which is used to define the lower ,  upper value &amp; number of bits to represent an int value   and  Methods of Integer Class su as parseInt , toString()  are useful when dealing with an int. &lt;br /&gt;&lt;br /&gt;Converting an Integer to int  vice versa   is very easy .&lt;br /&gt;To convert to Integer from int.      // box&lt;br /&gt;int number =100;&lt;br /&gt;Integer numWrap=new Integer(number);&lt;br /&gt;&lt;br /&gt;//To convert to int from Integer                 // unbox&lt;br /&gt;Integer numWrap1=new Integer(200);&lt;br /&gt;int num =numWrap1.intValue();  &lt;br /&gt;&lt;br /&gt;If you use java 1.4 , then you have to manually box , unbox using the above commands. &lt;br /&gt;&lt;br /&gt;The following example code will be compiled with java version 1.5 and above .No need to box  manually.  In Java 1.5 , new feauture autoboxing , unbox is introduced . &lt;br /&gt;&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt; &lt;br /&gt;class intVsInteger   &lt;br /&gt;{   &lt;br /&gt;public static void main(String[] args)    &lt;br /&gt;{   &lt;br /&gt;int num1 = 100;  &lt;br /&gt;int num2 = 100;   &lt;br /&gt;int num3 = 160;   &lt;br /&gt;int num4 = 160;   &lt;br /&gt;Integer num5=300; // by autoboxing available from jdk 1.5&lt;br /&gt;Integer num6=300; // by autoboxing&lt;br /&gt;int num7=num5+num6;    // by autoboxing &lt;br /&gt;&lt;br /&gt;System.out.println( &quot;num1= &quot;+num1 );   &lt;br /&gt;System.out.println(&quot;num2= &quot;+num2 );   &lt;br /&gt;System.out.println(&quot;num1==num2 &quot; + ( num1==num2) );   &lt;br /&gt;System.out.println( &quot;num3= &quot;+num3 );   &lt;br /&gt;System.out.println(&quot;num4= &quot;+num4 );   &lt;br /&gt;System.out.println(&quot;num3==num4 &quot; + (num3==num4) );   &lt;br /&gt;System.out.println( &quot;num5= &quot;+num5 );   &lt;br /&gt;System.out.println(&quot;num6= &quot;+num6 );   &lt;br /&gt;System.out.println(&quot;num5==num6 &quot; + (num5==num6) );   &lt;br /&gt;System.out.println(&quot;Below value using Integer class&quot;);&lt;br /&gt;intVsInteger(num1,num2);&lt;br /&gt;intVsInteger(num3,num4);&lt;br /&gt;intVsInteger(num5,num6);&lt;br /&gt;}   &lt;br /&gt;    &lt;br /&gt;public static void intVsInteger( Integer a , Integer b )    // using autoboxing&lt;br /&gt;{   &lt;br /&gt;System.out.println( &quot;a= &quot;+a );   &lt;br /&gt;System.out.println(&quot;b= &quot;+b );   &lt;br /&gt;Integer c=a+b;&lt;br /&gt;int d=a+b;   // using unbox&lt;br /&gt;System.out.println(&quot;c= &quot; + c );   &lt;br /&gt;System.out.println(a==b );   &lt;br /&gt;&lt;br /&gt;}   &lt;br /&gt;}  &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Output of the program&lt;br /&gt;&lt;br /&gt;D:\as2\JF2&gt;java intVsInteger&lt;br /&gt;num1= 100&lt;br /&gt;num2= 100&lt;br /&gt;num1==num2 true&lt;br /&gt;num3= 160&lt;br /&gt;num4= 160&lt;br /&gt;num3==num4 true&lt;br /&gt;num5= 300&lt;br /&gt;num6= 300&lt;br /&gt;num5==num6 false&lt;br /&gt;Below value using Integer class&lt;br /&gt;a= 100&lt;br /&gt;b= 100&lt;br /&gt;c= 200&lt;br /&gt;true&lt;br /&gt;a= 160&lt;br /&gt;b= 160&lt;br /&gt;c= 320&lt;br /&gt;false&lt;br /&gt;a= 300&lt;br /&gt;b= 300&lt;br /&gt;c= 600&lt;br /&gt;false&lt;br /&gt;&lt;br /&gt;The intVsInteger method accepts Integer object . So the object reference is passed to the method not the value.  if both are  pointing to the same reference of the object ,  then the  output will be true  otherwise the output will be false.When using primitive type , if both values are equal means , num1=num2 will be  true. &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/4042051369393297958/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/int-vs-integer-in-java-example-integer.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/4042051369393297958'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/4042051369393297958'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/int-vs-integer-in-java-example-integer.html' title='int Vs Integer in Java Example ,Difference between Integer Wrapper &amp;  int primitive'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-7098223720829394065</id><published>2011-08-21T10:53:00.000-07:00</published><updated>2011-08-21T11:06:07.379-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Password"/><category scheme="http://www.blogger.com/atom/ns#" term="Security"/><category scheme="http://www.blogger.com/atom/ns#" term="Security Audit"/><title type='text'>Rules  for Strong  Password to avoid  brute force attacks | Complexity Password Policy</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;Any  application using authentication should enforce password complexity policy in order to prevent an attacker / hacker  from easily guessing them which makes the application vulnerable to brute force attacks . &lt;br /&gt;&lt;br /&gt;So we have to design a password policy by imposing certain rules on the password.  Eventhough there is no fixed rules to design a password , atleast we should follow the following rules sothat the application should be safe from brute force attacks. &lt;br /&gt;The password must meet the following criteria&lt;br /&gt;1. Password must be minimum 8 characters. &lt;br /&gt;2. Password must contain characters from of the following 4 categories&lt;br /&gt;        a).  At least one upper case letter: (A – Z)&lt;br /&gt;        b).  At least one lower case letter: (a - z)&lt;br /&gt;        c).  At least one number: (0 - 9) &lt;br /&gt;        d) . At least one Special Characters of   !@#$%&amp;*()&lt;br /&gt;4. Password cannot contain the  user ID&lt;br /&gt;5. Password history may  be maintained in order not to use the same password again . Atleast 4 unique new passwords must be issued before an old password can be reused. &lt;br /&gt;6. Account may be locked out after the 3 number of unsuccessful attempts&lt;br /&gt;&lt;br /&gt;Eg. Passwords using the above rules    1) J6agru@F   2) jdU(g4aK2&lt;br /&gt;&lt;br /&gt;Password complexity policies are designed to deter brute force attacks by increasing the number of possible passwords. When password complexity policy is enforced, new passwords must meet the following guidelines &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/7098223720829394065/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/rules-for-strong-password-to-avoid.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7098223720829394065'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7098223720829394065'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/rules-for-strong-password-to-avoid.html' title='Rules  for Strong  Password to avoid  brute force attacks | Complexity Password Policy'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-2804449941201596151</id><published>2011-08-21T05:30:00.000-07:00</published><updated>2011-08-21T05:38:55.547-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="j2ee"/><category scheme="http://www.blogger.com/atom/ns#" term="Struts"/><category scheme="http://www.blogger.com/atom/ns#" term="Struts Interview"/><title type='text'>Configure web.xml for Struts | specify  struts-config.xml in Deployment Descriptor</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;One of benefits  using the Struts framework is that application’s configuration can be specified  in an external centralised configuration file (struts-config.xml)  instead of being hard coded into the application. This simplifies development because changes can be made to the application without having to recompile any code. Struts configure files (struts-config.xml) are specified in the web.xml as shown below&lt;br /&gt;&lt;b&gt;&amp;lt;web-app&amp;gt; &lt;br /&gt;&amp;lt;servlet&amp;gt;&lt;br /&gt;		&amp;lt;servlet-name&amp;gt;action&amp;lt;/servlet-name&amp;gt;&lt;br /&gt;		&amp;lt;servlet-class&amp;gt;org.apache.struts.action.ActionServlet&amp;lt;/servlet-class&amp;gt;&lt;br /&gt;		&amp;lt;init-param&amp;gt;&lt;br /&gt;			&amp;lt;param-name&amp;gt;config&amp;lt;/param-name&amp;gt;&lt;br /&gt;			&amp;lt;param-value&amp;gt;/WEB-INF/struts-config.xml&amp;lt;/param-value&amp;gt;&lt;br /&gt;		&amp;lt;/init-param&amp;gt;&lt;br /&gt;		&amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;	&amp;lt;/servlet&amp;gt;&lt;br /&gt;	......&lt;br /&gt;&amp;lt;/web-app&amp;gt; &lt;/b&gt;&lt;br /&gt; &lt;br /&gt;The Struts configuration file is specified by declaring  config parameter is a  servlet initialization parameter  . The config parameter must be set to your configuration file with the relative path of  Web application  , which will be the protected /WEB-INF/ directory so that it can be accessed only from server-side applications.&lt;br /&gt;Struts supports the use of multiple configuration files. Configuration settings can be broken down into 2 or more  files, which allows teams of developers to work on an application in parallel without having the configuration file be a point of contention. Multiple configuration files can be specified in web.xml as shown below seperated by comma  . If there is any overlap among the files’ settings, Struts will use the last settings specified. For example, if  struts-config.xml  specifies a setting and then struts-config2.xml specifies the same setting with a different value, the setting in struts-config2.xml will override the one in struts-config.xml,  provided that file struts-config2.xml is loaded after file struts-config.xml.&lt;br /&gt;The following configuration shows the how to configure multiple configuration files.&lt;br /&gt;&lt;b&gt;&amp;lt;init-param&amp;gt;&lt;br /&gt;  &amp;lt;param-name&amp;gt;config&amp;lt;/param-name&amp;gt;&lt;br /&gt;  &amp;lt;param-value&amp;gt;&lt;br /&gt;    /WEB-INF/struts-config.xml, /WEB-INF/struts-config2.xml, /WEB-INF/struts-config3.xml,&lt;br /&gt;  &amp;lt;/param-value&amp;gt;&lt;br /&gt;&amp;lt;/init-param&amp;gt; &lt;/b&gt;&lt;br /&gt; Struts version 1.1 added support for application modules. The module feature allows applications to be broken as  mini application inside  a large application. For this we have to create a separate configuration file for each module, as shown below&lt;br /&gt;&lt;b&gt;&amp;lt;servlet&amp;gt;&lt;br /&gt;    &amp;lt;param-name&amp;gt;config&amp;lt;/param-name&amp;gt;&lt;br /&gt;    &amp;lt;param-value&amp;gt;/WEB-INF/struts-config.xml&amp;lt;/param-value&amp;gt;&lt;br /&gt;  &amp;lt;/init-param&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;init-param&amp;gt;&lt;br /&gt;    &amp;lt;param-name&amp;gt;config/ModuleA&amp;lt;/param-name&amp;gt;&lt;br /&gt;    &amp;lt;param-value&amp;gt;/WEB-INF/struts-config-moduleB.xml&amp;lt;/param-value&amp;gt;&lt;br /&gt;  &amp;lt;/init-param&amp;gt;&lt;br /&gt;&lt;br /&gt;  &amp;lt;init-param&amp;gt;&lt;br /&gt;    &amp;lt;param-name&amp;gt;config/ModuleB&amp;lt;/param-name&amp;gt;&lt;br /&gt;    &amp;lt;param-value&amp;gt;/WEB-INF/struts-config-moduleA.xml&amp;lt;/param-value&amp;gt;&lt;br /&gt;  &amp;lt;/init-param&amp;gt;&lt;br /&gt;  &amp;lt;load-on-startup&amp;gt;1&amp;lt;/load-on-startup&amp;gt;&lt;br /&gt;&amp;lt;/servlet&amp;gt; &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;Note :  For module configuration files, parameter names must begin with config/ in order for Struts to recognize them.   eg.    config/moduleA  , config/moduleB .  Following / is the logic name for the modules. &lt;/p&gt;&lt;br /&gt;&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/2804449941201596151/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/configuring-webxml-for-struts-specify.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2804449941201596151'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/2804449941201596151'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/configuring-webxml-for-struts-specify.html' title='Configure web.xml for Struts | specify  struts-config.xml in Deployment Descriptor'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-7930427277725084967</id><published>2011-08-15T08:43:00.000-07:00</published><updated>2011-08-15T09:08:23.526-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Eclipse"/><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><category scheme="http://www.blogger.com/atom/ns#" term="j2ee"/><category scheme="http://www.blogger.com/atom/ns#" term="JBoss"/><category scheme="http://www.blogger.com/atom/ns#" term="Solution"/><title type='text'>Error : resource-ref:  has no valid JNDI binding. Check the jboss-web/resource-ref  - Solution</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;resource-ref:  has no valid JNDI binding, which means resource-ref defined in the deployment descripter has no valid binding. Assume that you have configured   JDBC Data Resources (JNDI)  in Jboss 4.0 for any database (Db2 , Postgres, etc ..) . That is  you have defined a reference lookup name (eg. jdbc/techLabsDS) to an external resource in the WEB-INF\web.xml  and also you have created / copied  *-ds.xml descriptor file with neccessary configurations like jndi-name , driver class in the  in the Jboss application server folder  Drive:\jboss-4.0.0\server\default\deploy . To use the resource adaptor defined in the deployment descripter (web.xml) ,  it must be integrated into the JBoss application server using a ds.xml descriptor file .  &lt;br /&gt; Now  when you start the Jboss server or when you try to run the application for accessing the database , you may get the error &quot;javax.naming.NamingException: resource-ref: jdbc/techLabsDS has no valid JNDI binding. Check the jboss-web/resource-ref&quot;  where techLabsDS is the JNDI name. To solve the this error , you have to copy the jboss-web.xml file with the following additional  lines  to Application&#39;s WEB-INF folder. &lt;br /&gt;&lt;br /&gt;&amp;lt;resource-ref&amp;gt; &lt;br /&gt;&amp;lt;res-ref-name&gt;jdbc/techLabsDS&amp;lt;/res-ref-name&amp;gt;&lt;br /&gt;&amp;lt;res-type&gt;javax.sql.DataSource&amp;lt;/res-type&amp;gt; &lt;br /&gt;&amp;lt;jndi-name&gt;java:/jdbc/techLabsDS&lt;/jndi-name&amp;gt;&lt;br /&gt;&amp;lt;/resource-ref&amp;gt;&lt;br /&gt;&lt;br /&gt;The following picture shows where the jboss-web.xml file to be copied.&lt;br /&gt; &lt;/p&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJ7JLjIMCRd36qNctEwyJrEhHHBj1kwDlyo50nYuZr7UYy5jLQtiOUXCJ-KkJnJIGcMr9POqy97NrF60IS1yF4ym0kAsNN4RequQD4yoh-PmxPKF4qJrIytIyS2wgMicZ3BDm0tb2pGY4/s1600/solution+to+has+no+binding.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 330px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJ7JLjIMCRd36qNctEwyJrEhHHBj1kwDlyo50nYuZr7UYy5jLQtiOUXCJ-KkJnJIGcMr9POqy97NrF60IS1yF4ym0kAsNN4RequQD4yoh-PmxPKF4qJrIytIyS2wgMicZ3BDm0tb2pGY4/s400/solution+to+has+no+binding.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5641115021480138514&quot; /&gt;&lt;/a&gt;&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/7930427277725084967/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/error-resource-ref-has-no-valid-jndi.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7930427277725084967'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7930427277725084967'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/error-resource-ref-has-no-valid-jndi.html' title='Error : resource-ref:  has no valid JNDI binding. Check the jboss-web/resource-ref  - Solution'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiJ7JLjIMCRd36qNctEwyJrEhHHBj1kwDlyo50nYuZr7UYy5jLQtiOUXCJ-KkJnJIGcMr9POqy97NrF60IS1yF4ym0kAsNN4RequQD4yoh-PmxPKF4qJrIytIyS2wgMicZ3BDm0tb2pGY4/s72-c/solution+to+has+no+binding.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-3711529665218502105</id><published>2011-08-14T06:44:00.000-07:00</published><updated>2011-08-14T08:47:27.720-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Class Path"/><category scheme="http://www.blogger.com/atom/ns#" term="Eclipse"/><category scheme="http://www.blogger.com/atom/ns#" term="Error"/><category scheme="http://www.blogger.com/atom/ns#" term="j2ee"/><category scheme="http://www.blogger.com/atom/ns#" term="JBoss"/><category scheme="http://www.blogger.com/atom/ns#" term="Solution"/><title type='text'>Solution for Unable to find a javac compiler error  in Jboss 4.0 with Eclipse when running JSP file</title><content type='html'>  &lt;p align=&quot;justify&quot;&gt;When you run a JSP with JBoss Application server  in eclipse IDE , the following error may occur  &quot;org.apache.jasper.JasperException: Unable to compile class for JSP&quot;&lt;br /&gt;&lt;br /&gt; JBoss appserver throws the following error &lt;br /&gt;Unable to find a javac compiler , com.sun.tools.javac.Main is not on the classpath. Perhaps JAVA_HOME does not point to the JDK&lt;br /&gt;&lt;br /&gt;The reason for the above error is &quot;com.sun.tools.javac.Main&quot; class which is bundled with tools.jar,  is not in the class path . So we need to include the tools.jar file in the class path.  When we add the jre to the build path , it includes all necessary jar files except tools.jar  in the class path .  Assume that  jdk1.6.0_23 is installed in d:\  .  jdk1.6.0_23 can be downloaded from java site.&lt;br /&gt; tools.jar file will be located in the installed folder of  jdk  (i.e  d:\jdk1.6.0_23\lib  OR  d:\ jdk1.6.0_23\jre\lib)   &lt;br /&gt;&lt;br /&gt;Steps to include the tools.jar file in the classpath in eclipse&lt;br /&gt;1. Select Windows-&gt;Preferences -&gt;Java -&gt; Installed JRE &lt;br /&gt;2. Highlight the selected  jre and click in edit buttor as shown in the following screen shot &lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZSv3NN74fv_5ib0vVR9hpKSxvX2NhyzdXAnvSj_2elebmbJhVPJTRuFkYHineRFfkG43f5RDWPpsJEDhmZW797sPgtPrxNVP3pBQ5JlyIqyhaRgKbGq-V7EAqKvmVmkNeo4vWrrVRdiU/s1600/Unable+to+find+a+javac+compiler.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 191px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZSv3NN74fv_5ib0vVR9hpKSxvX2NhyzdXAnvSj_2elebmbJhVPJTRuFkYHineRFfkG43f5RDWPpsJEDhmZW797sPgtPrxNVP3pBQ5JlyIqyhaRgKbGq-V7EAqKvmVmkNeo4vWrrVRdiU/s400/Unable+to+find+a+javac+compiler.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5640711126092219922&quot; /&gt;&lt;/a&gt;&lt;br /&gt;3. Click on Add External Jars and select the tools.jar file from the  jdk\lib  OR  jdk\jre\lib folder  then Finish.&lt;br /&gt;&lt;br /&gt;Now you will be able to run the JSP file.&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/3711529665218502105/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/solution-for-unable-to-find-javac.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/3711529665218502105'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/3711529665218502105'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/08/solution-for-unable-to-find-javac.html' title='Solution for Unable to find a javac compiler error  in Jboss 4.0 with Eclipse when running JSP file'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiZSv3NN74fv_5ib0vVR9hpKSxvX2NhyzdXAnvSj_2elebmbJhVPJTRuFkYHineRFfkG43f5RDWPpsJEDhmZW797sPgtPrxNVP3pBQ5JlyIqyhaRgKbGq-V7EAqKvmVmkNeo4vWrrVRdiU/s72-c/Unable+to+find+a+javac+compiler.JPG" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-8460808152473806607</id><published>2011-07-30T06:51:00.000-07:00</published><updated>2011-07-30T07:06:25.834-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Commands"/><category scheme="http://www.blogger.com/atom/ns#" term="Ports"/><category scheme="http://www.blogger.com/atom/ns#" term="Solution"/><category scheme="http://www.blogger.com/atom/ns#" term="Windows"/><title type='text'>Solution for Port already in use / Port already open. How to close a port in windows whis is in already in use or open ?</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;Sometimes you may get the error &quot;port already in use&quot; or &quot;port already open&quot;  when you install a application or when you start any server .  That means the mentioned port is used by an application.  For Example you may get the following error when you start the test server in WSAD .&lt;br /&gt;&lt;br /&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyHgGN0R_MvJJHvivOmilDw6XDDm90PetEb19X1BX4EhtxU2sDXG5B0CABwntEaDLSlDX1zIU-3JZA37nP-LY5G_E2Sz62qqbieaeftiUdWD7qoJCL4mX6Eni994WV2iK4iQA82u0UqP0/s1600/wsad+port+already+in+use.JPG&quot;&gt;&lt;img style=&quot;float:left; margin:0 10px 10px 0;cursor:pointer; cursor:hand;width: 400px; height: 249px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyHgGN0R_MvJJHvivOmilDw6XDDm90PetEb19X1BX4EhtxU2sDXG5B0CABwntEaDLSlDX1zIU-3JZA37nP-LY5G_E2Sz62qqbieaeftiUdWD7qoJCL4mX6Eni994WV2iK4iQA82u0UqP0/s400/wsad+port+already+in+use.JPG&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5635145888396232610&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt; This is due to,  we may have terminated the WSAD by using Task Center when it is not responding.  When you again load WSAD , starting the test server will the above error. This can be solved either by restarting the system or  Stopping / Killing   the process which uses the port . The same error may also occur when you install new application or when you run a java program (Socket Programming),  in this situation you can use new port  or if you want to use the same port means , you have to kill the process which uses the port. &lt;br /&gt;&lt;br /&gt;&lt;u&gt;Steps to end / stop / kill the process which uses the port&lt;/u&gt;&lt;br /&gt;        First find out the process id of process / application which is using port . To find out the process id of a process using the port no &lt;br /&gt;&lt;br /&gt;&lt;b&gt;netstat -aon | findstr &quot;&amp;lt;port number&amp;gt;&quot;&lt;/b&gt;&lt;br /&gt;For example to find the process id of the application which is using the port 9080 , use the following command&lt;br /&gt;&lt;br /&gt;&lt;b&gt;C:\&gt;netstat -aon | findstr &quot;9080&quot;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;  When you run the above command ,if the port 9080 is already used , then  the following output  is displayed&lt;br /&gt;&lt;br /&gt; TCP    0.0.0.0:9080           0.0.0.0:0              LISTENING       3800&lt;br /&gt;&lt;br /&gt; The number in the last column is the process id (PID) of the process holding the socket.  That is 3800&lt;br /&gt;&lt;br /&gt;Once PID is known, one can kill the application which corresponds to the PID using  &quot;Windows Task Manager&quot;  . (Note : to display the PID value in Task Manager, go to Processes tab, open View menu, then  Select Columns) &lt;br /&gt;&lt;br /&gt;or Using Command prompt to kill the process using process id ,  you can use the following command &lt;br /&gt;&lt;br /&gt;&lt;b&gt;taskkill /PID processid&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;    Command to kill the processid 3800 , use the following command&lt;br /&gt;&lt;br /&gt;&lt;b&gt;c:\&gt; taskkill /PID 3800&lt;/b&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/8460808152473806607/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/solution-for-port-already-in-use-port.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8460808152473806607'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8460808152473806607'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/solution-for-port-already-in-use-port.html' title='Solution for Port already in use / Port already open. How to close a port in windows whis is in already in use or open ?'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgyHgGN0R_MvJJHvivOmilDw6XDDm90PetEb19X1BX4EhtxU2sDXG5B0CABwntEaDLSlDX1zIU-3JZA37nP-LY5G_E2Sz62qqbieaeftiUdWD7qoJCL4mX6Eni994WV2iK4iQA82u0UqP0/s72-c/wsad+port+already+in+use.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-3695982926418717164</id><published>2011-07-22T12:40:00.000-07:00</published><updated>2011-07-22T12:48:25.856-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><title type='text'>Formating date in Current / any Locale in Java with Style  Example code</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;DateFormat helps  to format and parse dates for any locale . DateFormat has many  methods for obtaining default date / time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include   SHORT (23/07/11 ) ,  MEDIUM ( Jul 23, 2011 ), LONG (July 23, 2011 ) ,  FULL (Saturday, 23 July 2011 ). DateFormat has a method  getDateInstance() which is used to get the normal date format for that country &lt;br /&gt;&lt;br /&gt;To format a date in the current Locale, &lt;br /&gt;&lt;br /&gt;dateFormatStr = DateFormat.getDateInstance().format(myDate);&lt;br /&gt;&lt;br /&gt; To format a date for a different Locale, specify it in the call to getDateInstance(). &lt;br /&gt;&lt;br /&gt;  DateFormat df = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.UK);  &lt;br /&gt;&lt;br /&gt;The following code formats the current date into various different locales with different styles (SHORT, MEDIUM, LONG, FULL)  &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;&lt;br /&gt;import java.util.*;&lt;br /&gt;import java.text.*;&lt;br /&gt;&lt;br /&gt;public class DateFormatLocale&lt;br /&gt;&lt;br /&gt;{&lt;br /&gt;public static void main(String args[])&lt;br /&gt;{&lt;br /&gt;Date today = new Date();&lt;br /&gt;&lt;br /&gt;Locale[] locales ={Locale.CHINA,  Locale.UK, Locale.GERMANY, Locale.FRANCE, Locale.US };&lt;br /&gt;int[] fmt_styles={ DateFormat.SHORT,DateFormat.MEDIUM,DateFormat.LONG, DateFormat.FULL};&lt;br /&gt;DateFormat format;&lt;br /&gt;String[] fmt_style_str={ &quot;SHORT&quot;, &quot;MEDIUM&quot;, &quot;LONG&quot;, &quot;FULL&quot;};&lt;br /&gt;for (int i=0; i&amp;lt;locales.length;i++)&lt;br /&gt;{&lt;br /&gt;System.out.println(&quot;\n The date format for the country &quot; + locales[i].getDisplayCountry() );&lt;br /&gt;for (int j=0; j&amp;lt;fmt_styles.length;j++)&lt;br /&gt;{&lt;br /&gt;format = DateFormat.getDateInstance(fmt_styles[j], locales[i]);&lt;br /&gt;System.out.println(&quot;\n &quot; + fmt_style_str[j] + &quot; is &quot; + format.format(today));&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;String  dateCurrentLocale = DateFormat.getDateInstance().format(today);&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;Date format for the current locale &quot; + dateCurrentLocale); &lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;u&gt;Output of the Program:&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;java DateFormatLocale&lt;br /&gt;&lt;br /&gt; The date format for the country China&lt;br /&gt;&lt;br /&gt; SHORT is 7/23/11&lt;br /&gt;&lt;br /&gt; MEDIUM is Jul 23, 2011&lt;br /&gt;&lt;br /&gt; LONG is July 23, 2011&lt;br /&gt;&lt;br /&gt; FULL is Saturday, July 23, 2011&lt;br /&gt;&lt;br /&gt; The date format for the country United Kingd&lt;br /&gt;&lt;br /&gt; SHORT is 23/07/11&lt;br /&gt;&lt;br /&gt; MEDIUM is 23-Jul-2011&lt;br /&gt;&lt;br /&gt; LONG is 23 July 2011&lt;br /&gt;&lt;br /&gt; FULL is Saturday, 23 July 2011&lt;br /&gt;&lt;br /&gt; The date format for the country Germany&lt;br /&gt;&lt;br /&gt; SHORT is 23.07.11&lt;br /&gt;&lt;br /&gt; MEDIUM is 23.07.2011&lt;br /&gt;&lt;br /&gt; LONG is 23. Juli 2011&lt;br /&gt;&lt;br /&gt; FULL is Samstag, 23. Juli 2011&lt;br /&gt;....&lt;br /&gt;......&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/3695982926418717164/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/formating-date-in-current-any-locale-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/3695982926418717164'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/3695982926418717164'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/formating-date-in-current-any-locale-in.html' title='Formating date in Current / any Locale in Java with Style  Example code'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-3156921411867660809</id><published>2011-07-21T11:50:00.001-07:00</published><updated>2011-07-21T11:58:50.835-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Array"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>Convert Charater Arrays to String in Java |  Example code using String.copyValueOf()</title><content type='html'>&lt;p align=&quot;justify&quot;&gt; You can convert character arrays to string in two ways . &lt;br /&gt;1. String Class has a method copyValueOf()  can be used to create a String object from charater arrays. (i.e. array of type char[])  &lt;br /&gt;     a)  String str=String.copyValueOf(charArray)    -&gt; All the characters of array is converted to String&lt;br /&gt;     b)  String str = String.copyValueOf(charArray, 5, 4) - &gt; Only 4 charactes starting from 5th position , are converted to string.&lt;br /&gt;&lt;br /&gt;2. String class supports several constructors . One of the constructor accepts array of characters and  string object is created by the initialised array of charaters. String str=new String (&#39;a&#39;,&#39;e&#39;,&#39;i&#39;,&#39;o&#39;,&#39;u&#39;);&lt;br /&gt;&lt;br /&gt;The following example code explains how the character arrays are converted to string object using the above two methods.  &lt;br /&gt;&lt;br /&gt;&lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;  &lt;br /&gt;&lt;br /&gt;public class CharArrayToString {&lt;br /&gt;&lt;br /&gt; public static void main(String[] arg) {&lt;br /&gt;  char[] charArray = { &#39;V&#39;, &#39;o&#39;, &#39;w&#39;, &#39;e&#39;, &#39;l&#39;, &#39;s&#39;, &#39;:&#39;, &#39;a&#39;, &#39;e&#39;, &#39;i&#39;, &#39;o&#39;, &#39;u&#39; };&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;Given CharArray\n&quot;);&lt;br /&gt;for (char c:charArray) System.out.println(c);&lt;br /&gt;&lt;br /&gt;String str = new String(charArray);&lt;br /&gt;System.out.println(&quot;After converting to  String using Constructor : &quot; + str);&lt;br /&gt;&lt;br /&gt;String str1 = String.copyValueOf (charArray);&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;After converting to String using copyValueOf method : &quot; + str1);&lt;br /&gt;&lt;br /&gt; // converting subset of charater arrays to string &lt;br /&gt;System.out.println(&quot;\n&quot;);&lt;br /&gt;System.out.println(&quot;Converting subset of charater arrays to string\n &quot;) ;&lt;br /&gt;    String str2 = new String(charArray,0,5);  &lt;br /&gt;System.out.println(&quot;Using Constructor : &quot; + str2);&lt;br /&gt;&lt;br /&gt;String str3 = String.copyValueOf (charArray,7,5);&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;Using copyValueOf method : &quot; + str3);&lt;br /&gt;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;u&gt;After Running the above program Output&lt;/u&gt; :&lt;br /&gt;&lt;br /&gt;java CharArrayToString&lt;br /&gt;Given CharArray&lt;br /&gt;&lt;br /&gt;V&lt;br /&gt;o&lt;br /&gt;w&lt;br /&gt;e&lt;br /&gt;l&lt;br /&gt;s&lt;br /&gt;:&lt;br /&gt;a&lt;br /&gt;e&lt;br /&gt;i&lt;br /&gt;o&lt;br /&gt;u&lt;br /&gt;After converting to  String using Constructor : Vowels:aeiou&lt;br /&gt;After converting to String using copyValueOf method : Vowels:aeiou&lt;br /&gt;&lt;br /&gt;&lt;br /&gt; Converting subset of charater arrays to string&lt;br /&gt;&lt;br /&gt;Using Constructor : Vowel&lt;br /&gt;Using copyValueOf method : aeiou  &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/3156921411867660809/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/convert-charater-arrays-to-string-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/3156921411867660809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/3156921411867660809'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/convert-charater-arrays-to-string-in.html' title='Convert Charater Arrays to String in Java |  Example code using String.copyValueOf()'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-8963018078375452898</id><published>2011-07-17T11:24:00.000-07:00</published><updated>2011-07-17T11:38:08.776-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Array"/><category scheme="http://www.blogger.com/atom/ns#" term="Array List"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><title type='text'>array to ArrayList in Java with Example | Convert arrays to ArrayList in Java</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;There may be some situation where arrays needs to be converted to ArrayList . arrays are fixed length. After arrays are created , they cannot grow or shrink . So it is better to use ArrayList  where you don&#39;t know  how many elements the array will hold.    Moreover  arrays can  have  only  similar data type elements . Arraylist is a collection which can have different data type objects. ArrayList  size can  increase or decrease dynamically.&lt;br /&gt;There are lot more ways to convert array to ArrayList . &lt;br /&gt;&lt;br /&gt;1. Using Arrays.asList(array)&lt;br /&gt;2. Add all array values to  the ArrayList using ArrayList.add (value) &lt;br /&gt;3. Using Collections.addAll( ArrayList, array)  */&lt;br /&gt;&lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;import java.util.Collections; &lt;br /&gt;import java.util.Arrays;   &lt;br /&gt;import java.util.List;   &lt;br /&gt;import java.util.ArrayList;   &lt;br /&gt;public class ArrayToAL   &lt;br /&gt;{   &lt;br /&gt;   public static void main(String[] args)   &lt;br /&gt;   {   &lt;br /&gt;      String[] languages = {&quot;Java&quot;, &quot;C&quot;, &quot;C++&quot;, &quot;Pascal&quot;, &quot;Basic&quot;};   &lt;br /&gt;&lt;br /&gt;method1(languages);&lt;br /&gt;method2(languages);&lt;br /&gt;method3(languages);    &lt;br /&gt;&lt;br /&gt;   }   &lt;br /&gt;&lt;br /&gt;&lt;br /&gt;private static void method1(String[] array)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt; List&lt;String&gt; languageList = Arrays.asList(array);     //asList(array)&lt;br /&gt; System.out.println(&quot;Using Method1\n&quot;);    &lt;br /&gt;  for (String l : languageList)   System.out.print(l+&quot;\t&quot;);      &lt;br /&gt;System.out.println(&quot;\n&quot;);    &lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;private static void method2(String[] array)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;    ArrayList languageList = new ArrayList();   &lt;br /&gt;&lt;br /&gt;      for (String a : array)   languageList.add(a);       // ArrayList.add(value) &lt;br /&gt;      System.out.println(&quot;Using Method2\n&quot;);&lt;br /&gt;&lt;br /&gt;      for (Object l : languageList)   System.out.print(l+&quot;\t&quot;);   &lt;br /&gt;System.out.println(&quot;\n&quot;);    &lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;private static void method3(String[] array)&lt;br /&gt;{&lt;br /&gt;&lt;br /&gt;ArrayList languageList = new ArrayList();   &lt;br /&gt;Collections.addAll( languageList ,array);     //  Collections.addAll( ArrayList, array)  &lt;br /&gt;System.out.println(&quot;Using Method3\n&quot;);    &lt;br /&gt; for (Object l : languageList)   System.out.print(l+&quot;\t&quot;);   &lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}  &lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;After Running the program , the following output will be displayed&lt;br /&gt;&lt;br /&gt;java ArrayToAL&lt;br /&gt;Using Method1&lt;br /&gt;&lt;br /&gt;Java    C       C++     Pascal  Basic&lt;br /&gt;&lt;br /&gt;Using Method2&lt;br /&gt;&lt;br /&gt;Java    C       C++     Pascal  Basic&lt;br /&gt;&lt;br /&gt;Using Method3&lt;br /&gt;&lt;br /&gt;Java    C       C++     Pascal  Basic  &lt;br /&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/8963018078375452898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/array-to-arraylist-in-java-with-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8963018078375452898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8963018078375452898'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/array-to-arraylist-in-java-with-example.html' title='array to ArrayList in Java with Example | Convert arrays to ArrayList in Java'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-8127024101627598991</id><published>2011-07-14T12:31:00.000-07:00</published><updated>2011-07-14T12:43:45.858-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>Java String to Character Array Example Code | How to copy chars from string to char Array</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;In several ways,  we can copy the charaters of a string object into an character array. (ie. converting  the string to an array of characters.) Using the following String methods we can convert  the string to an array of characters&lt;br /&gt;charAt(i); &lt;br /&gt;toCharArray();&lt;br /&gt;getChars(int srcBegin, int srcEnd, char[] dest, int destBegin)   .  &lt;br /&gt;&lt;br /&gt;The following code illustrates how to convert String to array of charaters using above 3 methods.*/   &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;public class StringToChrArray {&lt;br /&gt;    public static void main(String[] args) {&lt;br /&gt;&lt;br /&gt;        String inputString = &quot;Techlabs4u.com&quot;;&lt;br /&gt;       int len = inputString.length();&lt;br /&gt;        char[] charArray1 = new char[len];&lt;br /&gt;      char[] charArray3 = new char[len];&lt;br /&gt;        // Converting using String to  array of chars using charAt(i);&lt;br /&gt;        for (int i = 0; i &lt; len; i++) {&lt;br /&gt;            charArray1[i] = inputString.charAt(i);&lt;br /&gt;        } &lt;br /&gt;        &lt;br /&gt; // Converting String to  array of chars using toCharArray();&lt;br /&gt;&lt;br /&gt;char[] charArray2=inputString.toCharArray();&lt;br /&gt;&lt;br /&gt; // Converting  String to  array of chars using getChars();&lt;br /&gt;&lt;br /&gt;inputString.getChars( 0,  len, charArray3,  0) ;&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;From charArray1\n&quot;);&lt;br /&gt;for (char c:charArray1) System.out.println(c);&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;From charArray2\n&quot;);&lt;br /&gt;for (char c:charArray2) System.out.println(c);&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;From charArray3\n&quot;);&lt;br /&gt;for (char c:charArray3) System.out.println(c);&lt;br /&gt;    &lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;Running the above program will output the following &lt;br /&gt;&lt;br /&gt;java StringToChrArray&lt;br /&gt;From charArray1&lt;br /&gt;&lt;br /&gt;T&lt;br /&gt;e&lt;br /&gt;c&lt;br /&gt;h&lt;br /&gt;l&lt;br /&gt;a&lt;br /&gt;b&lt;br /&gt;s&lt;br /&gt;4&lt;br /&gt;u&lt;br /&gt;.&lt;br /&gt;c&lt;br /&gt;o&lt;br /&gt;m&lt;br /&gt;From charArray2&lt;br /&gt;&lt;br /&gt;T&lt;br /&gt;e&lt;br /&gt;c&lt;br /&gt;h&lt;br /&gt;l&lt;br /&gt;a&lt;br /&gt;b&lt;br /&gt;s&lt;br /&gt;4&lt;br /&gt;u&lt;br /&gt;.&lt;br /&gt;c&lt;br /&gt;o&lt;br /&gt;m&lt;br /&gt;From charArray3&lt;br /&gt;&lt;br /&gt;T&lt;br /&gt;e&lt;br /&gt;c&lt;br /&gt;h&lt;br /&gt;l&lt;br /&gt;a&lt;br /&gt;b&lt;br /&gt;s&lt;br /&gt;4&lt;br /&gt;u&lt;br /&gt;.&lt;br /&gt;c&lt;br /&gt;o&lt;br /&gt;m</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/8127024101627598991/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/java-string-to-character-array-example.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8127024101627598991'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8127024101627598991'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/java-string-to-character-array-example.html' title='Java String to Character Array Example Code | How to copy chars from string to char Array'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-7529378078439849230</id><published>2011-07-09T13:52:00.000-07:00</published><updated>2011-07-09T13:58:58.302-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="String"/><title type='text'>String Split  in Java using Pattern.split() Example Code using Regular Expression (util.regex)</title><content type='html'>&lt;p align=&quot;justify&quot;&gt; The following program splits the given string using the split method of Pattern Calss of the package  java.util.regex available with JDK 1.4 onwards.  An instance of the Pattern class represents a regular expression which is specified in string . Pattern Class has various methods . Some of the methods are as follows.&lt;br /&gt;&lt;br /&gt;1. compile(String regex) -&gt; Compiles the given regular expression into a pattern.&lt;br /&gt;2. split(CharSequence input) -&gt;     Splits the given string using the pattern. &lt;br /&gt;3. matches(String regex, CharSequence input) -&gt;   Compiles the given regular expression (regex) and Matches the given input string against it.&lt;br /&gt;&lt;br /&gt;The following Program splits the given input string by the delimiter   #  ,  white space  and  commas &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;   &lt;br /&gt;&lt;br /&gt;import java.util.regex.*;&lt;br /&gt;import java.util.Scanner;&lt;br /&gt;public class SplitByRegex {&lt;br /&gt;    public static void main(String[] args) throws Exception {&lt;br /&gt;&lt;br /&gt;  Pattern pattern = Pattern.compile(&quot;[#,\\s]+&quot;);     // created pattern using # comma whitespace  &lt;br /&gt;&lt;br /&gt;  Scanner sc=new Scanner(System.in).useDelimiter(&quot;\n&quot;);  //Accepts Line of String &lt;br /&gt;  System.out.println(&quot;Enter Line to Split &quot;);   // &lt;br /&gt;  String inputStr=sc.next();&lt;br /&gt;  System.out.println(&quot;Enterd Line : &quot; + inputStr + &quot;\n&quot;);&lt;br /&gt;  String[] resultStr = pattern.split(inputStr);&lt;br /&gt;  System.out.println(&quot;\nResult Strings after Split\n&quot;);&lt;br /&gt;  for (int i=0; i&amp;lt;resultStr.length; i++)&lt;br /&gt;  System.out.println((i+1) +&quot;.&quot; + resultStr[i]);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;Running the Program :&lt;br /&gt;&lt;br /&gt;D:\as2\JF1&gt;java SplitByRegex&lt;br /&gt;Enter Line to Split&lt;br /&gt;Mumbai # Calcutta # Chennai # Delhi , US, Washinton , Canada  Singapore Malasia&lt;br /&gt;India&lt;br /&gt;Enterd Line : Mumbai # Calcutta # Chennai # Delhi , US, Washinton , Canada  Sing&lt;br /&gt;apore Malasia India&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Result Strings after Split&lt;br /&gt;&lt;br /&gt;1.Mumbai&lt;br /&gt;2.Calcutta&lt;br /&gt;3.Chennai&lt;br /&gt;4.Delhi&lt;br /&gt;5.US&lt;br /&gt;6.Washinton&lt;br /&gt;7.Canada&lt;br /&gt;8.Singapore&lt;br /&gt;9.Malasia&lt;br /&gt;10.India &lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/7529378078439849230/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/string-split-in-java-using-patternsplit.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7529378078439849230'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/7529378078439849230'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/string-split-in-java-using-patternsplit.html' title='String Split  in Java using Pattern.split() Example Code using Regular Expression (util.regex)'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5082823859993823215.post-8703648623844308245</id><published>2011-07-03T09:02:00.000-07:00</published><updated>2011-07-03T09:10:37.690-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Code"/><category scheme="http://www.blogger.com/atom/ns#" term="java"/><category scheme="http://www.blogger.com/atom/ns#" term="Java Interview"/><category scheme="http://www.blogger.com/atom/ns#" term="Recursion"/><title type='text'>Decimal to Binary in Java using Loop Example code</title><content type='html'>&lt;p align=&quot;justify&quot;&gt;/* The following code illustrates how to convert an Integer to Binary using loop .  We can convert an Integrer to Binary in several ways . We have converted Integet to Binary in two ways . First funtion returns binary  as String . IInd Function returns Binary as a  integer.  For returning as a string , we can easily concatenate the value of (number%2 ) in reverse order.   In the second function ,  steps to conver integer to binary&lt;br /&gt;For example, consider 10 as a integer.  &lt;br /&gt;&lt;br /&gt;1) 10 % 2 = 0   - Ist binary digit&lt;br /&gt;2) 10/2 = 5&lt;br /&gt;3) 5%2 = 1      - IInd binary digit&lt;br /&gt;4) 5/2 = 2&lt;br /&gt;5) 2%2 = 0      - IIIrd binary digit&lt;br /&gt;6) 2/2 = 1&lt;br /&gt;7) 1%2 = 1      - IV th binary digit&lt;br /&gt;&lt;br /&gt;Now convert individual digitis as single number which gives the binary number.&lt;br /&gt;0  + 1*10 + 0*100 + 1 * 1000 = 0+10+0+1000 = 1010 */&lt;br /&gt; &lt;br /&gt; &lt;pre face=&quot;arial&quot; size=&quot;12px&quot; style=&quot;border: 1px dashed rgb(150, 150, 150); margin: 5px; overflow: auto; line-height: 14px; background-color: rgb(230, 230, 230); width: auto; color: rgb(0, 0, 0);&quot;&gt;&lt;code&gt;&lt;br /&gt;&lt;br /&gt;import java.util.Scanner;&lt;br /&gt;&lt;br /&gt;class binaryLoop {&lt;br /&gt;  public static void main(String[] args) {&lt;br /&gt; &lt;br /&gt;Scanner sc = new Scanner(System.in); &lt;br /&gt;&lt;br /&gt;System.out.println(&quot;Please enter Integer&quot;);   &lt;br /&gt;&lt;br /&gt;int number = sc.nextInt();&lt;br /&gt;&lt;br /&gt;String bin=binary1(number);&lt;br /&gt;&lt;br /&gt;long bin1=binary2(number);&lt;br /&gt;&lt;br /&gt;System.out.println(&quot;Using Function 1 : Binary of   &quot;  +number + &quot; is &quot; +  bin);&lt;br /&gt;System.out.println(&quot;Using Function 2 : Binary of   &quot;  +number + &quot; is &quot; +  bin1);&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;public static String binary1 (int num)&lt;br /&gt; {    &lt;br /&gt;int sum=0;&lt;br /&gt;int temp=1;&lt;br /&gt;String bin=&quot;&quot;;&lt;br /&gt;if (num==0) return &quot;0&quot;;&lt;br /&gt;while (num!=0) &lt;br /&gt; {&lt;br /&gt; bin=(num%2)+ bin;&lt;br /&gt; num=num/2;&lt;br /&gt;  &lt;br /&gt;               }&lt;br /&gt;return bin;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;public static long binary2 (int num)&lt;br /&gt; {    &lt;br /&gt;&lt;br /&gt;long sum=0;&lt;br /&gt;long temp=1;&lt;br /&gt;&lt;br /&gt;while (num!=0) &lt;br /&gt; {&lt;br /&gt; sum = sum + temp*(num%2);&lt;br /&gt;        temp=temp*10;&lt;br /&gt; num=num/2;&lt;br /&gt;  &lt;br /&gt;               }&lt;br /&gt;&lt;br /&gt;return sum;&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Output :&lt;/u&gt;&lt;br /&gt;&lt;br /&gt;Running the above program&lt;br /&gt;&lt;br /&gt;D:\as2\JF1&gt;java binaryLoop&lt;br /&gt;Please enter Integer&lt;br /&gt;10&lt;br /&gt;Using Function 1 : Binary of   10 is 1010&lt;br /&gt;Using Function 2 : Binary of   10 is 1010&lt;br /&gt;&lt;br /&gt;D:\as2\JF1&gt;java binaryLoop&lt;br /&gt;Please enter Integer&lt;br /&gt;25&lt;br /&gt;Using Function 1 : Binary of   25 is 11001&lt;br /&gt;Using Function 2 : Binary of   25 is 11001&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://j2eeforum.blogspot.com/feeds/8703648623844308245/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/integer-to-binary-in-java-using-loop.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8703648623844308245'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5082823859993823215/posts/default/8703648623844308245'/><link rel='alternate' type='text/html' href='http://j2eeforum.blogspot.com/2011/07/integer-to-binary-in-java-using-loop.html' title='Decimal to Binary in Java using Loop Example code'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>