<?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-3953783967530253851</id><updated>2024-08-27T23:31:40.959-07:00</updated><category term="Java"/><category term="Learn java"/><category term="Learn java 5"/><category term="java 5"/><category term="ArrayList"/><category term="AspectJ"/><category term="Aspects"/><category term="Aspects in Java"/><category term="Autoboxing in Java 5"/><category term="Comparable"/><category term="Enums. enum"/><category term="Formatting with printf() and format() in Java"/><category term="Generics"/><category term="New features added to Java 5"/><category term="Pointcuts"/><category term="Serializable"/><category term="Var-Args in Java 5"/><category term="after():"/><category term="aspect"/><category term="before()"/><category term="collection"/><category term="collection in java 5"/><category term="hashCode()"/><category term="toString()"/><title type='text'>New features added to Java 5</title><subtitle type='html'>Java 5 provides Autoboxing, var args, static imports,Generics, scanners and many other useful features.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-952471864055981402</id><published>2011-06-01T09:59:00.000-07:00</published><updated>2011-06-05T21:33:27.752-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="after():"/><category scheme="http://www.blogger.com/atom/ns#" term="aspect"/><category scheme="http://www.blogger.com/atom/ns#" term="AspectJ"/><category scheme="http://www.blogger.com/atom/ns#" term="Aspects"/><category scheme="http://www.blogger.com/atom/ns#" term="Aspects in Java"/><category scheme="http://www.blogger.com/atom/ns#" term="before()"/><category scheme="http://www.blogger.com/atom/ns#" term="Java"/><category scheme="http://www.blogger.com/atom/ns#" term="java 5"/><category scheme="http://www.blogger.com/atom/ns#" term="Learn java"/><category scheme="http://www.blogger.com/atom/ns#" term="Learn java 5"/><category scheme="http://www.blogger.com/atom/ns#" term="Pointcuts"/><title type='text'>Aspects in Java</title><content type='html'>&lt;span style=&quot;font-family:arial;&quot;&gt;&lt;strong&gt;Aspects in Java&lt;br /&gt;&lt;/strong&gt;Lets take an example of a Student class. Student class is responsible for calculating marks of a Student.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:Arial;&quot;&gt;&lt;/span&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;&lt;br /&gt;class Student{&lt;br /&gt;calculateTotalMarks(){&lt;br /&gt;....&lt;br /&gt;....&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;Now, after six months, your manager tells you to add some code, which will be called before calculateTotalMarks () method will be called.&lt;br /&gt;&lt;br /&gt;One solution is to add that code above the first line of method statement. For e.g&lt;br /&gt;class Student{&lt;br /&gt;calculateTotalMarks (){&lt;br /&gt;// Do what manager told me to do&lt;br /&gt;....&lt;br /&gt;….&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;But, it might happen that this method is being called from some other places in your project. So, changing calculateTotalMarks() may break the code.&lt;br /&gt;This is where aspects come into picture. You don’t need to modify Student class. Simply create an aspect class and define pointcuts. For example:&lt;br /&gt;&lt;br /&gt;public aspect StudentAspect {&lt;br /&gt;pointcut monitorStudent()&lt;br /&gt;: execution(* Student.calculateTotalMarks (..));&lt;br /&gt;&lt;br /&gt;before() : monitorStudent (){&lt;br /&gt;getStudentDetails();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;after():monitorStudent (){&lt;br /&gt;calculateAverageMarks();&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Here, I have defined a StudentAspect aspect class and before/after pointcuts. When ever calculateTotalMarks method of Student will be called, getStudentDetails will be called before that and after execution of calculateTotalMarks method, calculateAverageMarks method will be called.&lt;br /&gt;&lt;br /&gt;-getStudentDetails method will be called&lt;br /&gt;-calculateTotalMarks method will be called&lt;br /&gt;-calculateAverageMarks method will be called&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Syntax&lt;/strong&gt;&lt;br /&gt;pointcut monitorStudent()&lt;br /&gt;: execution(* Student.calculateTotalMarks (..));&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;Pointcut monitorStudent()&lt;/span&gt; – declare pointcut with name monitorStudent&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#000099;&quot;&gt;execution(* Student.calculateTotalMarks (..))&lt;/span&gt; – Apply pointcuts to any method which resides in any package with Student as class name and calculateTotalMarks as method name with any number of arguments. &lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/952471864055981402/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2011/06/aspects-in-java.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/952471864055981402'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/952471864055981402'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2011/06/aspects-in-java.html' title='Aspects in Java'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-103020581256564488</id><published>2009-07-18T06:30:00.000-07:00</published><updated>2011-06-05T21:35:30.728-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="ArrayList"/><category scheme="http://www.blogger.com/atom/ns#" term="collection"/><category scheme="http://www.blogger.com/atom/ns#" term="collection in java 5"/><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 5"/><category scheme="http://www.blogger.com/atom/ns#" term="Learn java"/><category scheme="http://www.blogger.com/atom/ns#" term="Learn java 5"/><title type='text'>Generics in Java 5</title><content type='html'>&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;Arrays in Java have always been type safe. For example, an array declared as type integer can only accept integers (not String, Dog, Cat). But, collections are not like that. There is no syntax for declaring type safe collections in pre java 5. To create an ArrayList of integers, you would say:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;                                           &lt;span style=&quot;color:#3333ff;&quot;&gt;ArrayList lst = new ArrayList();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;color:#3333ff;&quot;  &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;Similarly, for creating ArrayList of String, you would say:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;                                          &lt;span style=&quot;color:#3333ff;&quot;&gt;ArrayList lst = new ArrayList();&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;color:#3333ff;&quot;  &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;There is no difference between the above two declarations. They are declared of type integer and String respectively, but you can put anything into it. Like, Dog object, Cat Object as so on.&lt;br /&gt;                                             &lt;br /&gt;As of java 5, you can use type safe collections. If you want to create an ArrayList of type String, you would say,&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;                        &lt;/span&gt;&lt;span style=&quot;font-family: arial;font-family:arial;color:#3333ff;&quot;  &gt;ArrayList&lt;string&gt; lst = new ArrayList&lt;string&gt;();&lt;br /&gt;                                                           OR&lt;br /&gt;                       List&lt;string&gt; lst = new ArrayList&lt;string&gt;();&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;color:#3333ff;&quot;  &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;color:#3333ff;&quot;  &gt;&lt;/span&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;How you add elements to collection in pre java 5?&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;List lst = new ArrayList ();   &lt;br /&gt;lst.add (“Danny”);&lt;br /&gt;lst.add (new Cat ());&lt;br /&gt;lst.add (new Double ());&lt;br /&gt;&lt;br /&gt;As you can see, a non- generic collection can hold any kind of Object.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;How you add elements to collection in java 5?&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;List&lt;string&gt; lst = new ArrayList&lt;string&gt; ();         &lt;br /&gt;lst.add (“Danny”);&lt;br /&gt;lst.add (“Katty”);&lt;br /&gt;lst.add (new Cat ()); &lt;strong&gt;// Compilation Error&lt;/strong&gt;&lt;/string&gt;&lt;/string&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;You can’t add Cat object in lst ArrayList – You can add only and only Strings.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;How to get objects from collection in Pre java 5?&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;In pre-java 5, the method that get objects out from collection could have only one kind of return type – java.lang.Object. If you want to get element from String list, you require a cast to do so.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                                                 &lt;span style=&quot;font-family: arial;color:#3333ff;&quot; &gt;String element = (String) lst.get (0);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;and since, you can’t guarantee that what is coming out really is a String, the cast could fail at run time.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;How to get objects from collection in java 5?&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;By using generic syntax, you can ensure that what is coming out is what you have added. So, no cast is required here.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;                                                 &lt;span style=&quot;font-family: arial;color:#3333ff;&quot; &gt;String element = lst.get (0);&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;You are sure that lst List holds only Strings, so no cast is required here.&lt;/span&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/103020581256564488/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/07/generics-in-java-5.html#comment-form' title='9 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/103020581256564488'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/103020581256564488'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/07/generics-in-java-5.html' title='Generics in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>9</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-2131657853960187595</id><published>2009-07-16T08:20:00.000-07:00</published><updated>2011-06-05T21:37:19.952-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Comparable"/><category scheme="http://www.blogger.com/atom/ns#" term="Enums. enum"/><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 5"/><category scheme="http://www.blogger.com/atom/ns#" term="Learn java"/><category scheme="http://www.blogger.com/atom/ns#" term="Learn java 5"/><category scheme="http://www.blogger.com/atom/ns#" term="Serializable"/><category scheme="http://www.blogger.com/atom/ns#" term="toString()"/><title type='text'>Enums in Java 5</title><content type='html'>&lt;span style=&quot;font-family:arial;&quot;&gt;Following are the advantages of using Enums:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;Enums are type safe.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;Enums are Serializable and Comparable by default&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;Programmers doesn’t require to do extra work of implementing toString(), equals() and hashCode(). Enums provide implementation of these by default.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;Programmers can use Enums in switch-case statements.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;Enums are permitted to implement to interfaces.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:arial;&quot;&gt;Java 5.0 lets you to confine a variable to have only a fixed set of predefined values. For example, a cold drink shop may want to serve cold drinks in just three sizes- SMALL, MEDIUM, and LARGE. With this simple declaration, it can be guaranteed that the compiler will stop you from assigning anything to cold drinks except SMALL, MEDIUM, and LARGE.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family:Arial;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;&lt;strong&gt;Syntax&lt;/strong&gt; enum DrinkSize {SMALL, MEDIUM, LARGE};&lt;/span&gt;&lt;/p&gt;&lt;p&gt;Now, if you want to get DrinkSize, you will do something like this&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;DrinkSize ds = DrinkSize.SMALL;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;According to sun coding convention, all constants should be in caps, although you can give these in small letters also.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;One important &lt;strong&gt;rule&lt;/strong&gt; you need to remember about Enums is:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;They must not be declared within a method.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;How to declare Enum outside a class?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;enum DrinkSize { SMALL, MEDIUM, LARGE } // this cannot be &lt;/p&gt;&lt;p&gt;// private or protected&lt;br /&gt;&lt;br /&gt;class Drink {&lt;br /&gt;DrinkSize size;&lt;br /&gt;}&lt;br /&gt;public class DrinkTest {&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;Drink drink = new Drink ();&lt;br /&gt;drink.size = DrinkSize.BIG; // enum outside class&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;Key point to remember here is that the enum can be declared with only public or default modifier.&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;strong&gt;How to declare Enum inside a class?&lt;/strong&gt;&lt;/p&gt;&lt;p&gt;class Drink {&lt;br /&gt;enum DrinkSize { SMALL, MEDIUM, LARGE }&lt;br /&gt;DrinkSize size;&lt;br /&gt;}&lt;br /&gt;public class DrinkTest {&lt;br /&gt;public static void main(String[] args) {&lt;br /&gt;Drink drink = new Drink();&lt;br /&gt;drink.size = Drink. DrinkSize.SMALL; // enclosing class&lt;br /&gt;// name required&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;At the end of the enum, putting a semicolon is optional&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;enum DrinkSize { SMALL, MEDIUM, LARGE };&lt;/span&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/2131657853960187595/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/07/enums-in-java.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/2131657853960187595'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/2131657853960187595'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/07/enums-in-java.html' title='Enums in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-1689699476146397779</id><published>2009-07-14T08:30:00.000-07:00</published><updated>2011-06-05T21:37:32.557-07:00</updated><title type='text'>Static Imports in Java 5</title><content type='html'>&lt;span style=&quot;font-family: arial;&quot;&gt;One reason of introducing static imports in java is to reduce keystrokes. Although, this is not the only reason, static imports are used when you want to use static members of a class. For example, if you want to use static fields and methods of Math class, you use it as Math, a dot operator (.), followed by a field or method name. Static import declarations enable programmers to use static members of a class as if they were declared in the same class. Class name and dot operator are not required in this case.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;Forms of static imports&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;Static Imports in java comes in two forms:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;ul style=&quot;font-family: arial;&quot;&gt;&lt;li&gt;Import a particular static member of a class.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;import static packageName.className.staticMemberName;&lt;br /&gt;for ex. java.lang.System.out;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;Import all static members of a class.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;import static packageName.className.*;&lt;br /&gt;for ex. java.lang.Integer.*;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;strong&gt;Let’s look at the static imports with a simple example:&lt;/strong&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;Without using static imports&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;public class WithoutStaticImportProg {&lt;br /&gt;    public static void main(String[] pmt) {&lt;br /&gt;         System.out.println(Integer.MAX_VALUE);&lt;br /&gt;         System.out.println(Integer.toHexString(42));&lt;br /&gt;    }&lt;br /&gt;}&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;Using static imports&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;&lt;/span&gt; &lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;import static java.lang.System.out;&lt;br /&gt;import static java.lang.Integer.*;&lt;br /&gt;public class StaticImportProg {&lt;br /&gt;    public static void main(String[] pmt) {&lt;br /&gt;         out.println(MAX_VALUE);&lt;br /&gt;         out.println(toHexString(42));&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;Both classes produce the same output:&lt;br /&gt;&lt;br /&gt;2147483647&lt;br /&gt;2a&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt; &lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;strong&gt;Points to remember for static imports:&lt;/strong&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;strong&gt;&lt;/strong&gt; &lt;/p&gt;&lt;ul style=&quot;font-family: arial;&quot;&gt;&lt;li&gt;If you use static import for MAX_VALUE member for both Integer and Long class in the same program, you will get a compilation error, since both have a MAX_VALUE constant. Java won’t come to know which class MAX_VALUE you are talking about.&lt;/li&gt;&lt;li&gt;Static imports can only be used with static object references, constants (as they are static and final), and static methods.&lt;/li&gt;&lt;li&gt;You can use import static; you can’t say static import.&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/1689699476146397779/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/07/static-imports-in-java-5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/1689699476146397779'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/1689699476146397779'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/07/static-imports-in-java-5.html' title='Static Imports in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-651379363076262183</id><published>2009-07-13T09:32:00.000-07:00</published><updated>2011-06-05T21:37:48.096-07:00</updated><title type='text'>Scanners in Java 5</title><content type='html'>&lt;span style=&quot;font-family: arial;font-family:arial;&quot; &gt;Java.util.Scanner class in java 5 is mainly used for tokenizing data and finding stuff. Although, Scanners in java 5 doesn’t provide location information or search and replace functionality, it can be used to source data by applying regular expressions to find out how many instances of an expression exists in source data.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:Arial;&quot; &gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:Arial;&quot; &gt;&lt;strong&gt;Tokenizing&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:Arial;&quot; &gt;Tokenizing is the process of taking big pieces of source data, breaking them into pieces and storing the pieces into variables. Besides scanners in java 5, tokenizing functionality is provided by String (with the help of split() method). &lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:Arial;&quot; &gt;For the purpose of Tokenizing, source data comprises of tokens and delimiters. Tokens are the actual pieces of data, and delimiters are the expressions that separate tokens from each other. &lt;/span&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;For Example:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;Source: “123,23,india,delhi”&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;If we say that our delimiter is a comma, then our four tokens would be&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;123&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;23&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;india&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;delhi&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;Tokenizing with Scanner&lt;/strong&gt;&lt;br /&gt;&lt;strong style=&quot;font-family: arial;&quot;&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;Let’s look at it using an example. Scanner’s default delimiter is white space.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;&quot;&gt;Scanner methods:&lt;/span&gt;&lt;br /&gt;&lt;ol style=&quot;font-family: arial;&quot;&gt;&lt;li&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;hasNextXxx()&lt;/span&gt; - tests the value of the next token but do not actually get the token. For example, hasNextDouble() checks if the next token is of double data type.&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;nextXxx()&lt;/span&gt; - returns the next token&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;useDelimeter()&lt;/span&gt; - allows you to set the delimiter to be any valid regular expression.&lt;/li&gt;&lt;/ol&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;strong&gt;Code Snippet:&lt;/strong&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;import java.util.Scanner;&lt;br /&gt;class ScanProg {&lt;br /&gt;public static void main(String [] args) {&lt;br /&gt;boolean b1, b2;&lt;br /&gt;int i;&lt;br /&gt;String s, hitsCntr = &quot; &quot;;&lt;br /&gt;Scanner s1 = new Scanner(args[0]);&lt;br /&gt;Scanner s2 = new Scanner(args[0]);&lt;br /&gt;while(b1 = s1.hasNext()) {&lt;br /&gt;s = s1.next();&lt;br /&gt;hitsCntr += &quot;s&quot;;&lt;br /&gt;}&lt;br /&gt;while(b1 = s2.hasNext()) {&lt;br /&gt;if (s2.hasNextInt()) {&lt;br /&gt;i = s2.nextInt();hitsCntr += &quot;i&quot;;&lt;br /&gt;}&lt;br /&gt;else if (s2.hasNextBoolean()) {&lt;br /&gt;b2 = s2.nextBoolean();hitsCntr += &quot;b1&quot;;&lt;br /&gt;}&lt;br /&gt;else {&lt;br /&gt;s2.next();&lt;br /&gt;hitsCntr += &quot;s2&quot;;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;System.out.println(&quot;hitsCntr &quot; + hitsCntr);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;If this program is invoked with&lt;br /&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;java ScanNext &quot;2 false 46 tty&quot;&lt;/span&gt;&lt;br /&gt;it produces&lt;/p&gt;&lt;p style=&quot;font-family: arial;&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;hitsCntr ssssibis2&lt;/span&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/651379363076262183/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/07/scanners-in-java-5.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/651379363076262183'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/651379363076262183'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/07/scanners-in-java-5.html' title='Scanners in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-5079948885001771814</id><published>2009-07-12T07:05:00.000-07:00</published><updated>2011-06-05T21:38:04.508-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Formatting with printf() and format() in Java"/><title type='text'>printf() method in Java 5</title><content type='html'>&lt;span style=&quot;font-family: arial;color:#333333;&quot; &gt;The format() and printf() methods were added to java.io.PrintStream with java 5.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family: arial;font-family:Arial;color:#3333ff;&quot;  &gt;Syntax of printf() method in java 5:&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;center&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;printf(&quot;format string&quot;, argument (s));&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;The point to remember here is that, formatting data will always start with a percent sign (%). &lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;For Example: &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;center&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;System.out.printf(&quot;%1$d + %2$d&quot;, 10, 20);&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;Output: &lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;center&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;10 + 20&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;Here, as we can see, inside the double quotes, is a format string. 1$ represents the first argument. Similarly, 2$ represents the second argument. + sign is used to add a + symbol. d character is a conversion character.&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;span style=&quot;color:#3333ff;&quot;&gt;Now, lets watch out for complete syntax:&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;left&quot;&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;center&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;% [arg_index$] [flags] [width] [.precision] conversion char&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;center&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;font-family: arial;&quot; align=&quot;center&quot;&gt;&lt;/div&gt;&lt;ol style=&quot;font-family: arial;&quot;&gt;&lt;br /&gt;&lt;li&gt;&lt;div align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;arg_index &lt;/strong&gt;An integer value followed by $. arg_index indicates which argument should be printed in this position.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;/li&gt;&lt;li&gt;&lt;div align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;flags &lt;/strong&gt;Following is the list of some of the flags:&lt;br /&gt;&quot;-&quot; Left justify the argument&lt;br /&gt;&quot;+&quot; Include a (+ or -) sign with the argument&lt;br /&gt;&quot;0&quot; Pad the argument with zeros&lt;br /&gt;&quot;,&quot; Use comma in argument (like 100,678)&lt;br /&gt;&quot;(&quot; Enclose nagitive numbers in parentheses&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;div align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;width&lt;/strong&gt; Determines minimum of characters to be printed&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;div align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;precision &lt;/strong&gt;Used for formatting with floating point numbers. precision determines the number of digits to be printed after decimal point.&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;div align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;conversion &lt;/strong&gt;Type of argument to be formatted. For Example:&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;/ol&gt;&lt;ul style=&quot;font-family: arial;&quot;&gt;&lt;li&gt;&lt;div align=&quot;left&quot;&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;b &lt;/strong&gt;boolean&lt;/span&gt;&lt;/div&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;c&lt;/strong&gt; char&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;d&lt;/strong&gt; integer&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;f&lt;/strong&gt; floating point&lt;/span&gt;&lt;/li&gt;&lt;br /&gt;&lt;li&gt;&lt;span style=&quot;color:#333333;&quot;&gt;&lt;strong&gt;s&lt;/strong&gt; string&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/5079948885001771814/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/07/printf-method-in-java-5.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/5079948885001771814'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/5079948885001771814'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/07/printf-method-in-java-5.html' title='printf() method in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-3976225529371597202</id><published>2009-02-08T01:03:00.000-08:00</published><updated>2011-06-05T21:38:17.409-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Var-Args in Java 5"/><title type='text'>Var-Args in Java 5</title><content type='html'>&lt;span style=&quot;font-family: arial;font-size:100%;&quot; &gt;Java 5 allows methods to take variable number of arguments&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;Pre Java 5 Example&lt;/strong&gt;&lt;br /&gt;void doSomething(int i, int j){&lt;/span&gt;&lt;a onclick=&quot;if (this.className.indexOf(&amp;quot;ubtn-disabled&amp;quot;) == -1) {var e = document[&#39;stuffform&#39;].saveDraft;(e.length) ? e[0].click() : e.click(); if (window.event) window.event.cancelBubble = true; return false;}&quot; style=&quot;font-family: arial;&quot; class=&quot;cssButton&quot; id=&quot;saveButton&quot; href=&quot;javascript:void(0)&quot; target=&quot;&quot;&gt; &lt;div class=&quot;cssButtonOuter&quot;&gt;&lt;div class=&quot;cssButtonMiddle&quot;&gt;&lt;div class=&quot;cssButtonInner&quot;&gt;Save as Draft&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/a&gt;&lt;span style=&quot;font-family: arial;font-size:100%;&quot; &gt;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;This method will be called as follows:&lt;br /&gt;doSomething(10, 20);&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using Java 5&lt;/strong&gt;, you can declare doSomething method by giving variable arguments as follows:&lt;br /&gt;&lt;br /&gt;void doSomething(int... i){&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;And at the time of calling the method, pass any number of arguments as you want.&lt;br /&gt;&lt;br /&gt;doSomething(10);&lt;br /&gt;doSomething(10,20);&lt;br /&gt;and so on....&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Declaration rules for var-args:&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;ol style=&quot;font-family: arial;font-family:arial;&quot; &gt;&lt;li&gt;&lt;span style=&quot;COLOR: rgb(51,51,255);font-size:100%;&quot; &gt;&lt;strong&gt;Var-arg Type: &lt;/strong&gt;&lt;/span&gt;&lt;span style=&quot;COLOR: rgb(0,0,0);font-size:100%;&quot; &gt;When you declare var-args in method, you must specify type of arguments method can receive.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;COLOR: rgb(51,51,255);font-size:100%;&quot; &gt;&lt;strong&gt;Syntax: &lt;/strong&gt;&lt;/span&gt;&lt;span style=&quot;COLOR: rgb(0,0,0);font-size:100%;&quot; &gt;Var-args should be declared with type followed by three dots (...) and then name of array that will hold the parameters&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;COLOR: rgb(51,51,255);font-size:100%;&quot; &gt;&lt;strong&gt;Positioning and number of var-args parameters: &lt;/strong&gt;&lt;/span&gt;&lt;span style=&quot;COLOR: rgb(0,0,0);font-size:100%;&quot; &gt;Var-args must be the last argument in a method signature and you can have only one var-arg in a method.&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;p style=&quot;font-family: arial;font-family:arial;&quot; &gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/3976225529371597202/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/02/discussion-on-var-args.html#comment-form' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/3976225529371597202'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/3976225529371597202'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/02/discussion-on-var-args.html' title='Var-Args in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-7462453615986317766</id><published>2009-02-02T23:12:00.000-08:00</published><updated>2011-06-05T21:38:28.868-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Autoboxing in Java 5"/><title type='text'>Autoboxing in Java 5</title><content type='html'>&lt;div  align=&quot;left&quot; style=&quot;font-family:arial;&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;Hi, &lt;/span&gt;&lt;/div&gt;&lt;div  align=&quot;left&quot; style=&quot;font-family:arial;&quot;&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;In this post, I am going to discuss about Autoboxing but before that let me explain Wrapper classes. The main purpose of wrapper classes:&lt;br /&gt;Java programmers often need some mechanism to wrap primitive values in an object so that the primitives can be used in activities reserved for objects, like adding into and retreiving from the collections, returning object from a method etc. Wrapper classes serves this.&lt;br /&gt;With the help of autoboxing and unboxing (features provided by Java 5), most of the wrapping features, that user needs to do manually are now handled automatically.&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using Java 4, you may want to use wrapper classes as follows:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Integer x = new Integer(100); // make it&lt;br /&gt;int y = x.intValue(); // unwrap it&lt;br /&gt;y++; // increment the value&lt;br /&gt;x = new Integer(y); // re-wrap it&lt;br /&gt;System.out.println(y); // print it&lt;br /&gt;&lt;br /&gt;&lt;strong&gt;Using Java 5 (Autoboxing feature), you may want to use wrapper classes as follows:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;Integer y = new Integer(100);&lt;br /&gt;y++;&lt;br /&gt;System.out.println(y);&lt;br /&gt;&lt;strong&gt;Both examples produce the output:&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;101&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/7462453615986317766/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/02/discussion-on-autoboxing.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/7462453615986317766'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/7462453615986317766'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/02/discussion-on-autoboxing.html' title='Autoboxing in Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3953783967530253851.post-5500232236185290713</id><published>2009-01-31T00:24:00.000-08:00</published><updated>2009-07-12T10:53:41.601-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="New features added to Java 5"/><title type='text'>New features added to Java 5</title><content type='html'>&lt;span style=&quot;COLOR: rgb(51,102,255);font-family:arial;font-size:100%;&quot;  &gt;Some of the new features that have been added to Java 5 are:&lt;/span&gt;&lt;span style=&quot;font-size:100%;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;ol&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;Autoboxing&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;Var-args&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;printf Method (to make C programmers happy:))&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;Scanners&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;Static Imports&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;Enumerated Types&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;font-family:Arial;font-size:100%;&quot;&gt;Generics&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;</content><link rel='replies' type='application/atom+xml' href='http://learnjava5.blogspot.com/feeds/5500232236185290713/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://learnjava5.blogspot.com/2009/01/introduction-to-java-5.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/5500232236185290713'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3953783967530253851/posts/default/5500232236185290713'/><link rel='alternate' type='text/html' href='http://learnjava5.blogspot.com/2009/01/introduction-to-java-5.html' title='New features added to Java 5'/><author><name>Gaurav Seth</name><uri>http://www.blogger.com/profile/17440685985369378900</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='18' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjCaUiTGNmlsEqnRFlN0yZS2WR-mpVP7ijSXxNfOO42B4exAbPK63HVXOAzq6fd5lWfN-jRpnA9wnTjsMA_WmlGBbxB3wPr_FBM-d5WO6uXYWLkYaUA6yS1v1eK53w6wdvZLl5BtG7kmjQFOh7GEz9eWtOF4KsOX1HoGBHFMMiPY0kP/s220/Mypic.jpg'/></author><thr:total>2</thr:total></entry></feed>