<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-4575263909641298207</id><updated>2012-02-16T20:41:52.681+05:00</updated><category term='analyses pattern'/><category term='Catching Multiple Exception'/><category term='Language Enhancements'/><category term='Simple Chart'/><category term='Rethrowing Exceptions'/><category term='try-with-resources'/><category term='Charts'/><category term='Decorator'/><category term='Core Plot'/><category term='String in switch'/><category term='s7graphview'/><category term='Chart API'/><category term='Decorator Pattern'/><category term='Design Pattern by Example'/><category term='Design Pattern'/><category term='dynamic responsibility'/><category term='Java 7'/><category term='software design'/><category term='Underscores in Numeric'/><category term='iOS'/><category term='Binary Literals'/><category term='Java'/><category term='GOF'/><category term='Type Inference'/><title type='text'>TechGuy's Blog</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://zishanbilal.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://zishanbilal.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>TechGuy</name><uri>http://www.blogger.com/profile/10187290112762198659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://1.bp.blogspot.com/-V-CQBKVZ7Zg/TsIErMoEE0I/AAAAAAAAAC8/GzLjknclF-Q/s220/DSC08838.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>4</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-4575263909641298207.post-3837500893911169530</id><published>2011-11-15T11:12:00.001+05:00</published><updated>2011-11-15T14:59:06.563+05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Language Enhancements'/><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='Rethrowing Exceptions'/><category scheme='http://www.blogger.com/atom/ns#' term='try-with-resources'/><category scheme='http://www.blogger.com/atom/ns#' term='Catching Multiple Exception'/><category scheme='http://www.blogger.com/atom/ns#' term='Java 7'/><title type='text'>Java 7 - Language Enhancements (Part 2)</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;Previously (in &lt;a href="http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-1.html" title="Java 7 – Language Enhancements (Part 1)"&gt;Part 1&lt;/a&gt;), we had learned the basic enhancements of Java 7, which includes Binary Literals, Underscores in Numeric Literals, Strings in switch Statements and Type Inference for Generic Instance Creation. Here we will take a look at exception handling updates like &lt;em&gt;try-with-resources&lt;/em&gt; statement, catching multiple exceptions in single catch and more. Lets continue our topic:&lt;/span&gt; &lt;br /&gt;&lt;br /&gt;&lt;h1 style="color: #336699; font-family: Lucida Sans Unicode; font-size: 12pt;"&gt;The try-with-resources Statement&lt;/h1&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The &lt;em&gt;try-with-resources&lt;/em&gt; statement is a &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; statement that declares one or more resources. A &lt;em&gt;resource&lt;/em&gt; is as an object that must be closed after the program is finished with it. The &lt;em&gt;try-with-resources&lt;/em&gt; statement ensures that each resource is closed at the end of the statement. Any object that implements &lt;code&gt;java.lang.AutoCloseable&lt;/code&gt;, which includes all objects which implement &lt;code&gt;java.io.Closeable&lt;/code&gt;, can be used as a resource.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The following example reads the first line from a file. It uses an instance of &lt;code&gt;BufferedReader&lt;/code&gt; to read data from the file. &lt;code&gt;BufferedReader&lt;/code&gt; is a resource that must be closed after the program is finished with it:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;code style="color: #7f0055; font-size: 10pt;"&gt;static&lt;/code&gt; String readFirstLineFromFileWithFinallyBlock(String path) &lt;code style="color: #7f0055; font-size: 10pt;"&gt;throws&lt;/code&gt; IOException {&lt;br /&gt;  BufferedReader br = &lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; BufferedReader(&lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; FileReader(path));&lt;br /&gt;  &lt;code style="color: #7f0055;"&gt;try&lt;/code&gt; {&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;return&lt;/code&gt; br.readLine();&lt;br /&gt;  } &lt;code style="color: #7f0055;"&gt;finally&lt;/code&gt; {&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;if&lt;/code&gt; (br != &lt;code style="color: #7f0055;"&gt;null&lt;/code&gt;) br.close();&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;As we can use a &lt;code style="color: #7f0055; font-size: 10pt;"&gt;finally&lt;/code&gt; block to ensure that a resource is closed regardless of whether the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; statement completes normally or abruptly. Java SE 7, provides a new &lt;em&gt;try-with-resources&lt;/em&gt; statement to perform the same operation with better managibility:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;code style="color: #7f0055; font-size: 10pt;"&gt;static&lt;/code&gt; String readFirstLineFromFile(String path) &lt;code style="color: #7f0055; font-size: 10pt;"&gt;throws&lt;/code&gt; IOException {&lt;br /&gt;  &lt;strong&gt;&lt;code style="color: #7f0055;"&gt;try&lt;/code&gt; (BufferedReader br = &lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; BufferedReader(&lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; FileReader(path)))&lt;/strong&gt; {&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;return&lt;/code&gt; br.readLine();&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;In this example, the resource declared in the &lt;em&gt;try-with-resources&lt;/em&gt; statement is a &lt;code&gt;BufferedReader&lt;/code&gt;. The declaration statement appears within parentheses immediately after the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; keyword. The class &lt;code&gt;BufferedReader&lt;/code&gt;, in Java SE 7 and later, implements the interface &lt;code&gt;java.lang.AutoCloseable&lt;/code&gt;. Because the &lt;code&gt;BufferedReader&lt;/code&gt; instance is declared in a &lt;em&gt;try-with-resources&lt;/em&gt; statement, it will be closed regardless of whether the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; statement completes normally or abruptly (as a result of the method &lt;code&gt;BufferedReader.readLine&lt;/code&gt; throwing an &lt;code&gt;IOException&lt;/code&gt;).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;However, in the first example, if the methods &lt;code&gt;readLine&lt;/code&gt; and &lt;code&gt;close&lt;/code&gt; both throw exceptions, then the method &lt;code&gt;readFirstLineFromFileWithFinallyBlock&lt;/code&gt; throws the exception thrown from the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;finally&lt;/code&gt; block; the exception thrown from the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block is suppressed. In contrast, in the example &lt;code&gt;readFirstLineFromFile&lt;/code&gt;, if exceptions are thrown from both the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block and the &lt;em&gt;try-with-resources&lt;/em&gt; statement, then the method &lt;code&gt;readFirstLineFromFile&lt;/code&gt; throws the exception thrown from the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block; the exception thrown from the &lt;em&gt;try-with-resources&lt;/em&gt; block is suppressed. In Java SE 7 and later, you can retrieve suppressed exceptions.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;You may declare one or more resources in a &lt;em&gt;try-with-resources&lt;/em&gt; statement. The following example retrieves the names of the files packaged in the zip file &lt;code&gt;zipFileName&lt;/code&gt; and creates a text file that contains the names of these files:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &lt;code style="color: #7f0055; font-size: 10pt;"&gt;public static void&lt;/code&gt; writeToFileZipFileContents(String zipFileName, String outputFileName)&lt;br /&gt;    &lt;code style="color: #7f0055; font-size: 10pt;"&gt;throws&lt;/code&gt; java.io.IOException {&lt;br /&gt;&lt;br /&gt;    java.nio.charset.Charset charset = java.nio.charset.Charset.forName(&lt;span style="color: #2a00ff;"&gt;"US-ASCII"&lt;/span&gt;);&lt;br /&gt;    java.nio.file.Path outputFilePath = java.nio.file.Paths.get(outputFileName);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #4f6228; font-size: 9pt;"&gt;// Open zip file and create output file with try-with-resources statement&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;&lt;code style="color: #7f0055;"&gt;try&lt;/code&gt; ( java.util.zip.ZipFile zf = &lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; java.util.zip.ZipFile(zipFileName);&lt;br /&gt;     java.io.BufferedWriter writer = java.nio.file.Files.newBufferedWriter(outputFilePath, charset) &lt;br /&gt;    )&lt;/strong&gt; {&lt;br /&gt;&lt;br /&gt;      &lt;span style="color: #4f6228; font-size: 9pt;"&gt;// Enumerate each entry&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;      for (java.util.Enumeration entries = zf.entries(); entries.hasMoreElements();) {&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #4f6228; font-size: 9pt;"&gt;// Get the entry name and write it to the output file&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        String newLine = System.getProperty(&lt;span style="color: #2a00ff;"&gt;"line.separator"&lt;/span&gt;);&lt;br /&gt;        String zipEntryName = ((java.util.zip.ZipEntry)entries.nextElement()).getName() + newLine;&lt;br /&gt;        writer.write(zipEntryName, 0, zipEntryName.length());&lt;br /&gt;      }&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;In this example, the &lt;em&gt;try-with-resources&lt;/em&gt; statement contains two declarations that are separated by a semicolon: &lt;code&gt;ZipFile&lt;/code&gt; and &lt;code&gt;BufferedWriter&lt;/code&gt;. When the block of code that directly follows it terminates, either normally or because of an exception, the &lt;code&gt;close&lt;/code&gt; methods of the &lt;code&gt;BufferedWriter&lt;/code&gt; and &lt;code&gt;ZipFile&lt;/code&gt; objects are automatically called in this order. Note that the &lt;code&gt;close&lt;/code&gt; methods of resources are called in the &lt;em&gt;opposite&lt;/em&gt; order of their creation.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The following example uses a &lt;em&gt;try-with-resources&lt;/em&gt; statement to automatically close a &lt;code&gt;java.sql.Statement&lt;/code&gt; object:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &lt;code style="color: #7f0055; font-size: 10pt;"&gt;public static void&lt;/code&gt; viewTable(Connection con) &lt;code style="color: #7f0055; font-size: 10pt;"&gt;throws&lt;/code&gt; SQLException {&lt;br /&gt;&lt;br /&gt;    String query = &lt;span style="color: #2a00ff;"&gt;"select COF_NAME, SUP_ID, PRICE, SALES, TOTAL from COFFEES"&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    &lt;strong&gt;&lt;code style="color: #7f0055;"&gt;try&lt;/code&gt; (Statement stmt = con.createStatement())&lt;/strong&gt; {&lt;br /&gt;&lt;br /&gt;      ResultSet rs = stmt.executeQuery(query);&lt;br /&gt;&lt;br /&gt;      &lt;code style="color: #7f0055;"&gt;while&lt;/code&gt; (rs.next()) {&lt;br /&gt;        String coffeeName = rs.getString(&lt;span style="color: #2a00ff;"&gt;"COF_NAME"&lt;/span&gt;);&lt;br /&gt;        &lt;code style="color: #7f0055;"&gt;int&lt;/code&gt; supplierID = rs.getInt(&lt;span style="color: #2a00ff;"&gt;"SUP_ID"&lt;/span&gt;);&lt;br /&gt;        &lt;code style="color: #7f0055;"&gt;float&lt;/code&gt; price = rs.getFloat(&lt;span style="color: #2a00ff;"&gt;"PRICE"&lt;/span&gt;);&lt;br /&gt;        &lt;code style="color: #7f0055;"&gt;int&lt;/code&gt; sales = rs.getInt(&lt;span style="color: #2a00ff;"&gt;"SALES"&lt;/span&gt;);&lt;br /&gt;        &lt;code style="color: #7f0055;"&gt;int&lt;/code&gt; total = rs.getInt(&lt;span style="color: #2a00ff;"&gt;"TOTAL"&lt;/span&gt;);&lt;br /&gt;        System.out.println(coffeeName + &lt;span style="color: #2a00ff;"&gt;", "&lt;/span&gt; + supplierID + &lt;span style="color: #2a00ff;"&gt;", "&lt;/span&gt; + price +&lt;br /&gt;                           &lt;span style="color: #2a00ff;"&gt;", "&lt;/span&gt; + sales + &lt;span style="color: #2a00ff;"&gt;", "&lt;/span&gt; + total);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;    } &lt;code style="color: #7f0055;"&gt;catch&lt;/code&gt; (SQLException e) {&lt;br /&gt;      JDBCTutorialUtilities.printSQLException(e);&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The resource &lt;code&gt;java.sql.Statement&lt;/code&gt; used in this example is part of the JDBC 4.1 and later API.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;&lt;strong&gt;Note&lt;/strong&gt;: A &lt;em&gt;try-with-resources&lt;/em&gt; statement can have &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; and &lt;code style="color: #7f0055; font-size: 10pt;"&gt;finally&lt;/code&gt; blocks just like an ordinary &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; statement. In a &lt;em&gt;try-with-resources&lt;/em&gt; statement, any &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; or &lt;code style="color: #7f0055; font-size: 10pt;"&gt;finally&lt;/code&gt; block is run after the resources declared have been closed.&lt;/span&gt;&lt;br /&gt;&lt;h1 style="color: #336699; font-family: Lucida Sans Unicode; font-size: 12pt;"&gt;Handling More Than One Type of Exception&lt;/h1&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;In Java SE 7 and later, a single &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block can handle more than one type of exception. This feature can reduce code duplication and lessen the temptation to catch an overly broad exception.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;Consider the following example, which contains duplicate code in each of the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; blocks:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;code style="color: #7f0055;"&gt;catch&lt;/code&gt; (IOException ex) {&lt;br /&gt;     logger.log(ex);&lt;br /&gt;     &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; ex;&lt;br /&gt;&lt;code style="color: #7f0055;"&gt;catch&lt;/code&gt; (SQLException ex) {&lt;br /&gt;     logger.log(ex);&lt;br /&gt;     &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; ex;&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;In releases prior to Java SE 7, it is difficult to create a common method to eliminate the duplicated code because the variable &lt;code&gt;ex&lt;/code&gt; has different types.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The following example, which is valid in Java SE 7 and later, eliminates the duplicated code:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;&lt;code style="color: #7f0055;"&gt;catch&lt;/code&gt; (IOException|SQLException ex) {&lt;br /&gt;    logger.log(ex);&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; ex;&lt;br /&gt;}&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; clause specifies the types of exceptions that the block can handle, and each exception type is separated with a vertical bar (&lt;code&gt;|&lt;/code&gt;).&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;&lt;strong&gt;Note&lt;/strong&gt;: If a &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block handles more than one exception type, then the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; parameter is implicitly &lt;code&gt;final&lt;/code&gt;. In this example, the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; parameter &lt;code&gt;ex&lt;/code&gt; is &lt;code&gt;final&lt;/code&gt; and therefore you cannot assign any values to it within the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;Bytecode generated by compiling a &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block that handles multiple exception types will be smaller (and thus superior) than compiling many &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; blocks that handle only one exception type each. A &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block that handles multiple exception types creates no duplication in the bytecode generated by the compiler; the bytecode has no replication of exception handlers.&lt;/span&gt;&lt;br /&gt;&lt;h1 style="color: #336699; font-family: Lucida Sans Unicode; font-size: 12pt;"&gt;Rethrowing Exceptions with More Inclusive Type Checking&lt;/h1&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The Java SE 7 compiler performs more precise analysis of rethrown exceptions than earlier releases of Java SE. This enables you to specify more specific exception types in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause of a method declaration.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;Consider the following example:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &lt;code style="color: #7f0055;"&gt;static class&lt;/code&gt; FirstException &lt;code style="color: #7f0055;"&gt;extends&lt;/code&gt; Exception { }&lt;br /&gt;  &lt;code style="color: #7f0055;"&gt;static class&lt;/code&gt; SecondException &lt;code style="color: #7f0055;"&gt;extends&lt;/code&gt; Exception { }&lt;br /&gt;&lt;br /&gt;  &lt;code style="color: #7f0055;"&gt;public void&lt;/code&gt; rethrowException(String exceptionName) &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; Exception {&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;try&lt;/code&gt; {&lt;br /&gt;      &lt;code style="color: #7f0055;"&gt;if&lt;/code&gt; (exceptionName.equals(&lt;span style="color: #2a00ff;"&gt;"First"&lt;/span&gt;)) {&lt;br /&gt;        &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; &lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; FirstException();&lt;br /&gt;      } else {&lt;br /&gt;        &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; &lt;code style="color: #7f0055;"&gt;new&lt;/code&gt; SecondException();&lt;br /&gt;      }&lt;br /&gt;    } &lt;code style="color: #7f0055;"&gt;catch&lt;/code&gt; (Exception e) {&lt;br /&gt;      &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; e;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;This examples's &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block could throw either &lt;code&gt;FirstException&lt;/code&gt; or &lt;code&gt;SecondException&lt;/code&gt;. Suppose you want to specify these exception types in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause of the &lt;code&gt;rethrowException&lt;/code&gt; method declaration. In releases prior to Java SE 7, you cannot do so. Because the exception parameter of the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; clause, &lt;code&gt;e&lt;/code&gt;, is type &lt;code&gt;Exception&lt;/code&gt;, and the catch block rethrows the exception parameter &lt;code&gt;e&lt;/code&gt;, you can only specify the exception type &lt;code&gt;Exception&lt;/code&gt; in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause of the &lt;code&gt;rethrowException&lt;/code&gt; method declaration.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;However, in Java SE 7, you can specify the exception types &lt;code&gt;FirstException&lt;/code&gt; and &lt;code&gt;SecondException&lt;/code&gt; in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause in the &lt;code&gt;rethrowException&lt;/code&gt; method declaration. The Java SE 7 compiler can determine that the exception thrown by the statement &lt;code&gt;&lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; e&lt;/code&gt; must have come from the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block, and the only exceptions thrown by the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block can be &lt;code&gt;FirstException&lt;/code&gt; and &lt;code&gt;SecondException&lt;/code&gt;. Even though the exception parameter of the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; clause, &lt;code&gt;e&lt;/code&gt;, is type &lt;code&gt;Exception&lt;/code&gt;, the compiler can determine that it is an instance of either &lt;code&gt;FirstException&lt;/code&gt; or &lt;code&gt;SecondException&lt;/code&gt;:&lt;/span&gt;&lt;br /&gt;&lt;pre style="background-color: #d9d9d9; font-size: 10pt; padding-left: 30px;"&gt;&lt;br /&gt;&amp;nbsp;&lt;br /&gt;  &lt;code style="color: #7f0055;"&gt;public void&lt;/code&gt; rethrowException(String exceptionName)&lt;br /&gt;  &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; &lt;strong&gt;FirstException, SecondException&lt;/strong&gt; {&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;try&lt;/code&gt; {&lt;br /&gt;      &lt;span style="color: #4f6228; font-size: 9pt;"&gt;// ...&lt;/span&gt;&lt;br /&gt;    }&lt;br /&gt;    &lt;code style="color: #7f0055;"&gt;catch&lt;/code&gt; (Exception e) {&lt;br /&gt;      &lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; e;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;&amp;nbsp;&lt;/pre&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;This analysis is disabled if the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; parameter is assigned to another value in the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block. However, if the catch parameter is assigned to another value, you must specify the exception type &lt;code&gt;Exception&lt;/code&gt; in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause of the method declaration.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;In detail, in Java SE 7 and later, when you declare one or more exception types in a &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; clause, and rethrow the exception handled by this &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; block, the compiler verifies that the type of the rethrown exception meets the following conditions:&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The &lt;code style="color: #7f0055; font-size: 10pt;"&gt;try&lt;/code&gt; block is able to throw it.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;There are no other preceding &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; blocks that can handle it.&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;It is a subtype or supertype of one of the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; clause's exception parameters.&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;The Java SE 7 compiler allows you to specify the exception types &lt;code&gt;FirstException&lt;/code&gt; and &lt;code&gt;SecondException&lt;/code&gt; in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause in the &lt;code&gt;rethrowException&lt;/code&gt; method declaration because you can rethrow an exception that is a supertype of any of the types declared in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-family: 'Lucida Sans Unicode'; font-size: 9pt;"&gt;In releases prior to Java SE 7, you cannot throw an exception that is a supertype of one of the &lt;code style="color: #7f0055; font-size: 10pt;"&gt;catch&lt;/code&gt; clause's exception parameters. A compiler from a release prior to Java SE 7 generates the error, "unreported exception &lt;code&gt;Exception&lt;/code&gt;; must be caught or declared to be thrown" at the statement &lt;code&gt;&lt;code style="color: #7f0055;"&gt;throw&lt;/code&gt; e&lt;/code&gt;. The compiler checks if the type of the exception thrown is assignable to any of the types declared in the &lt;code style="color: #7f0055;"&gt;throws&lt;/code&gt; clause of the &lt;code&gt;rethrowException&lt;/code&gt; method declaration. However, the type of the catch parameter &lt;code&gt;e&lt;/code&gt; is &lt;code&gt;Exception&lt;/code&gt;, which is a supertype, not a subtype, of &lt;code&gt;FirstException&lt;/code&gt; and&lt;code&gt;SecondException&lt;/code&gt;.&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4575263909641298207-3837500893911169530?l=zishanbilal.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zishanbilal.blogspot.com/feeds/3837500893911169530/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-2.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/3837500893911169530'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/3837500893911169530'/><link rel='alternate' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-2.html' title='Java 7 - Language Enhancements (Part 2)'/><author><name>TechGuy</name><uri>http://www.blogger.com/profile/10187290112762198659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://1.bp.blogspot.com/-V-CQBKVZ7Zg/TsIErMoEE0I/AAAAAAAAAC8/GzLjknclF-Q/s220/DSC08838.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4575263909641298207.post-3004889887657346043</id><published>2011-11-15T11:11:00.001+05:00</published><updated>2011-11-15T14:56:55.036+05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Language Enhancements'/><category scheme='http://www.blogger.com/atom/ns#' term='Underscores in Numeric'/><category scheme='http://www.blogger.com/atom/ns#' term='Java'/><category scheme='http://www.blogger.com/atom/ns#' term='try-with-resources'/><category scheme='http://www.blogger.com/atom/ns#' term='Catching Multiple Exception'/><category scheme='http://www.blogger.com/atom/ns#' term='Binary Literals'/><category scheme='http://www.blogger.com/atom/ns#' term='String in switch'/><category scheme='http://www.blogger.com/atom/ns#' term='Java 7'/><category scheme='http://www.blogger.com/atom/ns#' term='Type Inference'/><title type='text'>Java 7 - Language Enhancements (Part 1)</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;After nearly five year (on July 2011), Java SE 7 has been released! With Project Coin, the new Fork/Framework, the New File System API (NIO.2), and more. Java SE 7 is an important step in Java's evolution. There are number of enhancements in the programming language, listed below.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Binary Literals&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Underscores in Numeric Literals&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Strings in switch Statements&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Type Inference for Generic Instance Creation&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Improved Compiler Warnings and Errors When Using Non-Reifiable Formal Parameters with Varargs Methods&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;The try-with-resources Statement&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Catching Multiple Exception Types and Rethrowing Exceptions with Improved Type Checking&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;span style="color:#336699;font-family:Lucida Sans Unicode;font-size:12pt;"&gt;Binary Literals&lt;/span&gt;&lt;span style="color:black;font-family:Arial;font-size:9pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;In Java SE 7, the integral types&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt; &lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;&lt;span style="font-size:9pt;"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;byte&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;, &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;, &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;, &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;and&lt;/span&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Arial;font-size:7pt;"&gt; &lt;/span&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;) &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#222222;"&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;can also be expressed using the binary number system. To specify a binary literal, add the prefix &lt;/span&gt;&lt;span style="font-family:Courier New;font-size:10pt;background-color:white;"&gt;&lt;strong&gt;0b&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt; or &lt;/span&gt;&lt;span style="font-family:Courier New;font-size:10pt;background-color:white;"&gt;&lt;strong&gt;0B&lt;/strong&gt;&lt;/span&gt;&lt;span style="font-family:Lucida Sans Unicode;"&gt;&lt;span style="font-size:9pt;background-color:white;"&gt; to the number. Declarative uses of binary literals are demonstrated below:&lt;/span&gt;&lt;span style="background-color:white;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;An 8-bit &lt;/span&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;byte&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Courier New;background-color:white;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;value:&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;byte&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; ab = (&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;byte&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b00100001;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;An 16-bit &lt;/span&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Courier New;background-color:white;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;value:&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;span class="Apple-style-span" style="font-weight:900;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; as = (&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1010000101000101;&lt;strong&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Few 32-bit &lt;/span&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Courier New;background-color:white;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;values:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; ai = 0b10100001010001011010000101000101;&lt;br /&gt;&lt;span class="Apple-style-span" style="color:#000000;"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; ai2 = 0B101; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// The B can be upper or lower case.&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;An 64-bit &lt;/span&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#7f0055;font-family:Courier New;background-color:white;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Courier New;background-color:white;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;value. Note the L suffix:&lt;/span&gt;&lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; al = 0b1010000101000101101000010100010110100001010001011010000101000101L;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Binary literals can make relationships among data more apparent than they would be in hexadecimal or octal. For example, each successive number in the following array is rotated by one bit:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;public static final int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;[] phases = {&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b00110001,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b01100010,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b11000100,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b10001001,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b00010011,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b00100110,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b01001100,&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0b10011000&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;}&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;In hexadecimal, the relationship among the numbers is not readily apparent:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;public static final int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;[] phases = {&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;   0x31, 0x62, 0xC4, 0x89, 0x13, 0x26, 0x4C, 0x98&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;}&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;You can use binary literals to make a bitmap more readable:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;public static final short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;[] HAPPY_FACE = {&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#444444;"&gt;   (&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0000011111100000;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0000100000010000;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0001000000001000;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0010000000000100;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0100000000000010;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1000011001100001;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1000011001100001;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1000000000000001;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1000000000000001;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1001000000001001;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b1000100000010001;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0100011111100010;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0010000000000100;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0001000000001000;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0000100000010000;&lt;br /&gt;(&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;short&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0000011111100000;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;}&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Uses of binary literals in bit wise operations also help to understand the logic and program structure.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h1&gt;&lt;span style="color:#336699;font-family:Lucida Sans Unicode;font-size:12pt;"&gt;Underscores in Numeric Literals&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Any number of underscore characters (_) can appear anywhere between digits in a numerical literal. This feature enables you, for example, to separate groups of digits in numeric literals, which can improve the readability of your code. For instance, if your code contains numbers with many digits, you can use an underscore character to separate digits in groups of three, similar to how you would use a punctuation mark like a comma, or a space, as a separator. The following example shows other ways you can use the underscore in numeric literals:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; creditCardNumber = 1234_5678_9012_3456L;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; socialSecurityNumber = 999_99_9999L;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;float&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; pi = 3.14_15F;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; hexBytes = 0xFF_EC_DE_5E;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; hexWords = 0xCAFE_BABE;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; maxLong = 0x7fff_ffff_ffff_ffffL;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;byte&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; nybbles = (&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;byte&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;)0b0010_0101;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; bytes = 0b11010010_01101001_10010100_10010010;&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;You can place underscores only between digits; you cannot place underscores in the following places:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;At the beginning or end of a number&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Adjacent to a decimal point in a floating point literal&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Prior to an F or L suffix&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;In positions where a string of digits is expected&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;The following examples demonstrate valid and invalid underscore placements (which are highlighted) in numeric literals:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;float&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; pi1 = 3_.1415F; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores adjacent to a decimal point&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;float&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; pi2 = 3._1415F; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores adjacent to a decimal point&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;long&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; socialSecurityNumber1 &lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#444444;font-size:10pt;"&gt;= 999_99_9999_L; &lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores prior to an L suffix&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x1 = _52; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// This is an identifier, not a numeric literal&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x2 = 5_2; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// OK (decimal literal)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x3 = 52_; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores at the end of a literal&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x4 = 5_______2; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// OK (decimal literal)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x5 = 0_x52; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores in the 0x radix prefix&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x6 = 0x_52; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores at the beginning of a number&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x7 = 0x5_2; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// OK (hexadecimal literal)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x8 = 0x52_; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores at the end of a number&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x9 = 0_52; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// OK (octal literal)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x10 = 05_2; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// OK (octal literal)&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;int&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; x11 = 052_; &lt;/span&gt;&lt;/span&gt;&lt;span style="color:#4f6228;font-size:9pt;"&gt;// Invalid; cannot put underscores at the end of a number&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;br /&gt;&lt;h1&gt;&lt;span style="color:#336699;font-family:Lucida Sans Unicode;font-size:12pt;"&gt;Strings in switch Statements&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;You can use the&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;"&gt;String&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;class in the expression of a&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;"&gt;&lt;strong&gt;switch&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;statement.&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="font-family:Courier New;font-size:10pt;"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;public &lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;String getTypeOfDayWithSwitchStatement(String dayOfWeekArg) {&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;String typeOfDay;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;switch&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; (dayOfWeekArg) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;strong&gt;&lt;span style="color:#7f0055;"&gt;case&lt;/span&gt;&lt;span style="color:#444444;"&gt; &lt;/span&gt;&lt;/strong&gt;&lt;span style="color:#2a00ff;"&gt;"Monday"&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#444444;"&gt;typeOfDay = &lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Start of work week"&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;strong&gt;&lt;span style="color:#7f0055;"&gt;case&lt;/span&gt;&lt;span style="color:#444444;"&gt; &lt;/span&gt;&lt;/strong&gt;&lt;span style="color:#2a00ff;"&gt;"Tuesday"&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;strong&gt;&lt;span style="color:#7f0055;"&gt;case&lt;/span&gt;&lt;span style="color:#444444;"&gt; &lt;/span&gt;&lt;/strong&gt;&lt;span style="color:#2a00ff;"&gt;"Wednesday"&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;case &lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Thursday&lt;/span&gt;&lt;span style="color:#444444;"&gt;":&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#444444;"&gt;typeOfDay = &lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Midweek"&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;strong&gt;&lt;span style="color:#7f0055;"&gt;case&lt;/span&gt;&lt;span style="color:#444444;"&gt; &lt;/span&gt;&lt;/strong&gt;&lt;span style="color:#2a00ff;"&gt;"Friday"&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#444444;"&gt;typeOfDay = &lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"End of work week"&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;case &lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Saturday"&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;strong&gt;&lt;span style="color:#7f0055;"&gt;case&lt;/span&gt;&lt;span style="color:#444444;"&gt; &lt;/span&gt;&lt;/strong&gt;&lt;span style="color:#2a00ff;"&gt;"Sunday"&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#444444;"&gt;typeOfDay = &lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;"Weekend"&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;break&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;default&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt;:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;throw &lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; IllegalArgumentException&lt;/span&gt;&lt;span style="color:#2a00ff;"&gt;("Invalid day of the week: "&lt;/span&gt;&lt;span style="color:#444444;"&gt; + dayOfWeekArg);&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;}&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-family:'Courier New';"&gt;&lt;span style="color:#7f0055;"&gt;&lt;strong&gt;return&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#444444;"&gt; typeOfDay;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;}&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="font-size:9pt;"&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;The &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;"&gt;&lt;strong&gt;switch&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt; statement compares the&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;"&gt;String&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;object in its expression with the expressions associated with each&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;"&gt;&lt;strong&gt;case&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt; label as if it were using the&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;"&gt;String.equals&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;method; consequently, the comparison of&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;"&gt;String&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;objects in&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;"&gt;&lt;strong&gt;switch&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt; statements is case sensitive. The Java compiler generates generally more efficient bytecode from&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;"&gt;&lt;strong&gt;switch&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt; statements that use&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#444444;font-family:Courier New;"&gt;String&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt;objects than from chained&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt; &lt;/span&gt;&lt;span style="color:#7f0055;font-family:Courier New;"&gt;&lt;strong&gt;if-else-then&lt;/strong&gt;&lt;/span&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;background-color:white;"&gt; statements.&lt;/span&gt;&lt;span style="color:black;font-family:Arial;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;h1&gt;&lt;span style="color:#336699;font-family:Lucida Sans Unicode;font-size:12pt;"&gt;Type Inference for Generic Instance Creation&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;You can replace the type arguments required to invoke the constructor of a generic class with an empty set of type parameters (&amp;lt;&amp;gt;) as long as the compiler can infer the type arguments from the context. This pair of angle brackets is informally called the &lt;em&gt;diamond&lt;/em&gt;. For example, consider the following variable declaration:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; myMap = new HashMap&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt;();&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;In Java SE 7, you can substitute the parameterized type of the constructor with an empty set of type parameters (&amp;lt;&amp;gt;):&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; myMap = new HashMap&amp;lt;&amp;gt;();&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;"&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Note that to take advantage of automatic type inference during generic class instantiation, you must specify the diamond. In the following example, the compiler generates an unchecked conversion warning because the &lt;/span&gt;&lt;span style="font-family:Courier New;font-size:10pt;background-color:white;"&gt;HashMap()&lt;/span&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt; constructor refers to the &lt;/span&gt;&lt;span style="font-family:Courier New;font-size:10pt;background-color:white;"&gt;HashMap&lt;/span&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt; raw type, not the &lt;/span&gt;&lt;span style="font-family:Courier New;font-size:10pt;background-color:white;"&gt;Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt;&lt;/span&gt;&lt;span style="font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt; type:&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;Map&amp;lt;String, List&amp;lt;String&amp;gt;&amp;gt; myMap = new HashMap(); // unchecked conversion warning&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Java SE 7 supports limited type inference for generic instance creation; you can only use type inference if the parameterized type of the constructor is obvious from the context. For example, the following example does not compile:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;List&amp;lt;String&amp;gt; list = new ArrayList&amp;lt;&amp;gt;();&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;list.add("A");&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;// The following statement should fail since addAll expects Collection&amp;lt;? extends String&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;list.addAll(new ArrayList&amp;lt;&amp;gt;());&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;Note that the diamond often works in method calls; however, it is suggested that you use the diamond primarily for variable declarations.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;In comparison, the following example compiles:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;p style="background-image:initial;background-attachment:initial;background-color:#d9d9d9;padding-left:30px;background-position:initial initial;background-repeat:initial initial;"&gt;&lt;span style="color:#444444;font-family:Courier New;font-size:10pt;"&gt;&lt;br /&gt;// The following statements compile:&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;List&amp;lt;? extends String&amp;gt; list2 = new ArrayList&amp;lt;&amp;gt;();&lt;br /&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="color:#444444;font-family:'Courier New';"&gt;list.addAll(list2);&lt;br /&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/p&gt;&lt;span style="color:#222222;font-family:Lucida Sans Unicode;font-size:9pt;background-color:white;"&gt;There are some other examples and uses of Type Interface of Generic Instance Creation that we will cover in our next part (&lt;a href="http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-2.html" title="Java 7 – Language Enhancements (Part 2)"&gt;Part 2&lt;/a&gt;) with remaining three language enhancements.&lt;/span&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4575263909641298207-3004889887657346043?l=zishanbilal.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zishanbilal.blogspot.com/feeds/3004889887657346043/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-1.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/3004889887657346043'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/3004889887657346043'/><link rel='alternate' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/java-7-language-enhancements-part-1.html' title='Java 7 - Language Enhancements (Part 1)'/><author><name>TechGuy</name><uri>http://www.blogger.com/profile/10187290112762198659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://1.bp.blogspot.com/-V-CQBKVZ7Zg/TsIErMoEE0I/AAAAAAAAAC8/GzLjknclF-Q/s220/DSC08838.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4575263909641298207.post-4135960756683632167</id><published>2011-11-15T11:09:00.001+05:00</published><updated>2011-11-15T14:53:48.896+05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Simple Chart'/><category scheme='http://www.blogger.com/atom/ns#' term='Chart API'/><category scheme='http://www.blogger.com/atom/ns#' term='s7graphview'/><category scheme='http://www.blogger.com/atom/ns#' term='Charts'/><category scheme='http://www.blogger.com/atom/ns#' term='iOS'/><category scheme='http://www.blogger.com/atom/ns#' term='Core Plot'/><title type='text'>Charts in iOS</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;Last day i was working on client requirments to provide prototype for an iOS based application. I was looking for some chart API or some framework that helps me to draw chart in native or as alternate in UIWebView. I found some and want to share with you guys.&lt;br /&gt;&lt;br /&gt;1) http://code.google.com/p/core-plot/&lt;br /&gt;&lt;br /&gt;2) http://code.google.com/p/s7graphview/&lt;br /&gt;&lt;br /&gt;3) http://github.com/duivesteyn-enterprises/deSimpleChart&lt;br /&gt;&lt;br /&gt;4) http://www.rgraph.net/ (Will work on UIWebView only)&lt;br /&gt;&lt;br /&gt;5) http://sebkade.wordpress.com/2010/05/06/basic-graph-class-for-iphone/ (a blog where demo is given to create a graph)&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4575263909641298207-4135960756683632167?l=zishanbilal.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zishanbilal.blogspot.com/feeds/4135960756683632167/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/charts-in-ios.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/4135960756683632167'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/4135960756683632167'/><link rel='alternate' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/charts-in-ios.html' title='Charts in iOS'/><author><name>TechGuy</name><uri>http://www.blogger.com/profile/10187290112762198659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://1.bp.blogspot.com/-V-CQBKVZ7Zg/TsIErMoEE0I/AAAAAAAAAC8/GzLjknclF-Q/s220/DSC08838.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-4575263909641298207.post-2692596324581225071</id><published>2011-11-15T10:52:00.001+05:00</published><updated>2011-11-21T09:06:45.026+05:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='Decorator'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='software design'/><category scheme='http://www.blogger.com/atom/ns#' term='Design Pattern by Example'/><category scheme='http://www.blogger.com/atom/ns#' term='Decorator Pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='analyses pattern'/><category scheme='http://www.blogger.com/atom/ns#' term='GOF'/><category scheme='http://www.blogger.com/atom/ns#' term='dynamic responsibility'/><title type='text'>Design Pattern by Example – Decorator Pattern</title><content type='html'>&lt;div dir="ltr" style="text-align: left;" trbidi="on"&gt;&lt;h1&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="font-size: 10pt;"&gt;This series of articles will help you to build a good understanding of design patterns using different examples from real life and some from well-known frameworks or APIs. There are many articles around the web that discuss design patterns but they sometimes lack appropriate examples to quote. So, either their purpose stays unclear or we cannot memorize the patterns longer and sooner they slip out of mind. More importantly, we understand them but to employ within our problem domain is again out of the quest.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;Expert designers reuse solutions that they had worked on in the past and they re-engage them whenever they faced with the same problems. Rookie designers can also use design patterns to solve their problems efficiently. This article is intended to help both naive and expert developers by making them understand the application of patterns and opening up new dimensions in solutions domain.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h1&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;&lt;span style="font-size: 12pt;"&gt;Decorator Pattern&lt;/span&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: #4f81bd; font-size: 12pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="font-size: 10pt;"&gt;For the sake of continuity, let's look at the formal definition first.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #4f81bd; font-size: 10pt;"&gt;&lt;strong&gt;&lt;em&gt;Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;img alt="" src="http://zishanbilal.files.wordpress.com/2011/04/042811_2030_designpatte11.png" /&gt;&lt;span style="color: #4f81bd;"&gt;&lt;strong&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-size: 10pt;"&gt;At this stage you might not understand it clearly but as you go through the whole article you will gain better understanding of the pattern and would defiantly want to use it to solve your own domain problems.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;In this part of the article I totally concerned about dynamic behaviors that most readers expect from pattern, decorator is not just design as well as analysis pattern too, we will discuss this in next part. So ignore &lt;strong&gt;NewState&lt;/strong&gt; field in &lt;strong&gt;ConcreteDecoratorA&lt;/strong&gt; for this time.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h2&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Problem Domain&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-size: 10pt;"&gt;To keep things simpler we take an example of a subsystem, where employees are working with different responsibilities (such as team members, team leads and a manager). A team member is responsible to do his assigned tasks and to coordinate with other members for the completion of group tasks. On the other hand, a team lead has to manage and collaborate with his team members and plan their tasks. Similarly, a manager has some extra responsibility over a team lead such as employee profiling, work assignment.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;Following are the system entities and their behaviors:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;strong&gt;&lt;em&gt;Employee: &lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;calculate salary, join, terminate.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;strong&gt;&lt;em&gt;Team member: &lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;perform task, coordinate with others.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;span style="color: #4f81bd;"&gt;&lt;strong&gt;&lt;em&gt;Team lead: &lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;planning, motivate.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #4f81bd; font-size: 13px;"&gt;&lt;strong&gt;&lt;em&gt;Manager: &lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;assign task, employee profiling, create requirements.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;There may be some other entities like Team, Task, etc. but my focus here is to convey the idea of a Decorator while keeping things simple and paying attention on root of pattern.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h2&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Traditional Approach&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-size: 10pt;"&gt;In such a system, the most followed approach is to make an employee super class and extend it to all three classes. Even this is an object oriented approach but it has some drawbacks.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;img alt="" src="http://zishanbilal.files.wordpress.com/2011/04/042811_2030_designpatte21.png" /&gt;&lt;/div&gt;&lt;span style="font-size: 10pt;"&gt;Whenever a team member becomes a team lead, we have to create a new object of team lead and the previous object that points to that employee (team member) may be destroyed or archived. That's not a recommended approach when employee is still a part of your organization. Same is the case with manager, when an employee turns into a manager from a team lead/team member.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;Another case is when an employee can perform responsibilities of a team member as well as those of a team lead or a manager can perform team leads responsibilities. In that case you need to create two objects for the same employee which is totally wrong.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;em&gt;In these scenarios a team member/team lead can have extra responsibilities at run time. And their responsibilities can be assigned/revoked at run time.&lt;br /&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;h2&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Decorator Pattern &lt;strong&gt;&lt;strong&gt;Approach&lt;/strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-size: 10pt;"&gt;Let's see how design patterns help in these cases. If we look at Decorator Pattern it says:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #4f81bd; font-size: 10pt;"&gt;&lt;strong&gt;&lt;em&gt;Attach additional responsibilities to an object dynamically.&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;Wow, can it really solve my problem, let's see.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;img alt="" src="http://zishanbilal.files.wordpress.com/2011/04/042811_2030_designpatte31.png" /&gt;&lt;/div&gt;&lt;span style="font-size: 10pt;"&gt;At this moment we defector our previous approach in such a way that we always register just an &lt;strong&gt;Employee&lt;/strong&gt; and &lt;strong&gt;TeamMember&lt;/strong&gt;, &lt;strong&gt;TeamLead&lt;/strong&gt; and &lt;strong&gt;Manager&lt;/strong&gt; will be decorator of that employee object.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;Now, if we want to change responsibilities of an employee to manager we just need a new Manager (Decorator) and assigning that employee to it will solve our problem. Same is the case when a team lead's responsibilities are revoked, and some other member becomes team lead, we just need to swap employee objects within &lt;strong&gt;TeamMember&lt;/strong&gt; and &lt;strong&gt;TeamLead&lt;/strong&gt; decorators.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;As for the case where an employee can perform the responsibilities of a team member as well as those of a team lead, we just need &lt;strong&gt;TeamMember&lt;/strong&gt; and &lt;strong&gt;TeamLead&lt;/strong&gt; decorators that are pointing to same employee object.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;h4&gt;&lt;span style="font-size: 10pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Employee (Decorated/Component)&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/h4&gt;&lt;h4&gt;&lt;span class="Apple-style-span" style="font-size: 13px; font-weight: normal;"&gt;Implementation of Employee is as follows:&lt;/span&gt;&lt;/h4&gt;&lt;div style="background-attachment: scroll; background-clip: initial; background-color: #d9d9d9; background-image: none; background-origin: initial; background-position: 0px 0px; background-repeat: repeat repeat; padding-left: 30px; text-align: left;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;public interface&lt;/strong&gt;&lt;span style="color: black;"&gt; Employee {&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; join(Date joinDate);    &lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; terminate(Date terminateDate);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #4f6228;"&gt;&amp;nbsp; // other behaviors may reside (see sample code)&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;strong style="color: #336699; font-size: 13px;"&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong style="color: #336699; font-size: 13px;"&gt;EmployeeImpl&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;This is the core implementation class of Employee interface, EmployeeImpl is given below:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="background: none repeat scroll 0 0 #d9d9d9; padding-left: 30px;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;public class&lt;/strong&gt;&lt;span style="color: black;"&gt; EmployeeImpl &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;implements&lt;/strong&gt;&lt;span style="color: black;"&gt; Employee { &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="color: #4f6228;"&gt;&amp;nbsp; // other behaviors and properties may reside (see sample code)&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; join(Date joinDate){&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" joined on "&lt;span style="color: black;"&gt; + joinDate);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;span style="color: black;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; terminate(Date terminateDate){&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: black;"&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" terminate on "&lt;span style="color: black;"&gt; + terminateDate);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; }&lt;br /&gt;&lt;/span&gt;}&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;strong style="color: #336699; font-size: 13px;"&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong style="color: #336699; font-size: 13px;"&gt;EmployeeDecorator (Abstract Decorator)&lt;/strong&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;This is an abstraction of Employee decorator, EmployeeDecorator is given below:&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="font-size: 13px;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="background: none repeat scroll 0 0 #d9d9d9; padding-left: 30px;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;public abstract class&lt;/strong&gt;&lt;span style="color: black;"&gt; EmployeeDecorator &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;implements&lt;/strong&gt;&lt;span style="color: black;"&gt; Employee {&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; protected&lt;/strong&gt;&lt;span style="color: black;"&gt; Employee &lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt;;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; protected&lt;/strong&gt;&lt;span style="color: black;"&gt; EmployeeDecorator(Employee employee) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; this&lt;/strong&gt;&lt;span style="color: black;"&gt;.&lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt; = employee;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; }&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;span style="color: #4f6228;"&gt;&amp;nbsp; // other behaviors may reside (see sample code)&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;strong&gt;void &lt;/strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;join(Date joinDate) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: black;"&gt;&amp;nbsp; &amp;nbsp; employee&lt;span style="color: black;"&gt;.join(joinDate);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&amp;nbsp; }&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; terminate(Date terminateDate) {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000c0;"&gt;&amp;nbsp; &amp;nbsp; employee&lt;span style="color: black;"&gt;.terminate(terminateDate);&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: black;"&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;strong style="color: #336699; font-size: 13px;"&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong style="color: #336699; font-size: 13px;"&gt;TeamMember/TeamLead/Manager (Concrete Decorator)&lt;/strong&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;These are concrete implementations of EmployeeDecorator, classes are given below:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="background: none repeat scroll 0 0 #d9d9d9; padding-left: 30px;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;public class&lt;/strong&gt;&lt;span style="color: black;"&gt; TeamMember &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;extends&lt;/strong&gt;&lt;span style="color: black;"&gt; EmployeeDecorator {&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; protected&lt;/strong&gt;&lt;span style="color: black;"&gt; TeamMember(Employee employee) {&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; super&lt;/strong&gt;&lt;span style="color: black;"&gt;(employee);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #7f0055;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; performTask() {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: black;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is performing his assigned tasks."&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; }&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public &lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;void&lt;/strong&gt;&lt;span style="color: black;"&gt; coordinateWithOthers() {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: black;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is coordinating with other members of his team."&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;public class&lt;/strong&gt;&lt;span style="color: black;"&gt; TeamLead &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;extends&lt;/strong&gt;&lt;span style="color: black;"&gt; EmployeeDecorator {&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; protected&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt; TeamLead(Employee employee) {&lt;/span&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: #7f0055;"&gt;&amp;nbsp; &amp;nbsp; super&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;(employee);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; }&lt;/span&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public void&lt;/strong&gt;&lt;span style="color: black;"&gt; planing() {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: black;"&gt;&lt;strong&gt;&lt;span style="color: #7f0055;"&gt;this&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;.&lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is planing."&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; }&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public void&lt;/strong&gt;&lt;span style="color: black;"&gt; motivate() {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: black;"&gt;&lt;strong&gt;&lt;span style="color: #7f0055;"&gt;this&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;.&lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is motivating his members."&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; }&lt;/span&gt;&lt;br /&gt;}&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;public class&lt;/strong&gt;&lt;span style="color: black;"&gt; Manager &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;extends&lt;/strong&gt;&lt;span style="color: black;"&gt; EmployeeDecorator {&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; protected&lt;/strong&gt;&lt;/span&gt;&lt;span style="color: black;"&gt; Manager(Employee employee) {&lt;/span&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: #7f0055;"&gt;&amp;nbsp; &amp;nbsp; super&lt;/span&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;(employee);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; }&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public void&lt;/strong&gt;&lt;span style="color: black;"&gt; assignTask() {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: black;"&gt;.&lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is assigning tasks."&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #7f0055;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #7f0055;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public void&lt;/strong&gt;&lt;span style="color: black;"&gt; profileEmployee() {&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: black;"&gt;.&lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is profiling employees."&lt;span style="color: black;"&gt;);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;&lt;span class="Apple-style-span" style="color: #7f0055;"&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;strong&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/strong&gt;&lt;span style="color: black;"&gt;&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;&amp;nbsp; public void&lt;/strong&gt;&lt;span style="color: black;"&gt; createRequirments() {&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&amp;nbsp; &amp;nbsp; print(&lt;span style="color: #7f0055;"&gt;&lt;strong&gt;this&lt;/strong&gt;&lt;span style="color: black;"&gt;.&lt;span style="color: #0000c0;"&gt;employee&lt;span style="color: black;"&gt;.getName() + &lt;span style="color: #2a00ff;"&gt;" is creating requirement documents."&lt;span style="color: black;"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp; &lt;/span&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;br /&gt;&lt;h2&gt;&lt;span style="font-size: 11pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Some Example from APIs&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-size: 10pt;"&gt;There are many application of decorator in our routine APIs, like in I/O Streams and Readers use decorator pattern extensively.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="text-align: center;"&gt;&lt;img alt="" src="http://zishanbilal.files.wordpress.com/2011/04/042811_2030_designpatte41.png" /&gt;&lt;/div&gt;&lt;span style="font-size: 10pt;"&gt;In this example &lt;strong&gt;FilterInputStream&lt;/strong&gt;, &lt;strong&gt;BufferedInputStream&lt;/strong&gt;, &lt;strong&gt;DataInputStream&lt;/strong&gt; and &lt;strong&gt;PushbackInputStream&lt;/strong&gt; are decorators and &lt;strong&gt;FileInputStream&lt;/strong&gt; and &lt;strong&gt;ByteArrayInputStream&lt;/strong&gt; are decorated objects.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="background: none repeat scroll 0 0 #d9d9d9; padding-left: 30px;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;br /&gt;InputStream inStream = &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;span style="color: black;"&gt; FileInputStream(&lt;span style="color: #2a00ff;"&gt;"data.txt"&lt;span style="color: black;"&gt;);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-size: 10pt;"&gt;Here &lt;strong&gt;inStream&lt;/strong&gt; is a simple file input stream and&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;div style="background: none repeat scroll 0 0 #d9d9d9; padding-left: 30px;"&gt;&lt;span style="color: black; font-family: 'Courier New'; font-size: 9pt;"&gt;&lt;br /&gt;inStream = &lt;span style="color: #7f0055;"&gt;&lt;strong&gt;new&lt;/strong&gt;&lt;span style="color: black;"&gt; BufferedInputStream(inStream);&lt;br /&gt;&lt;strong&gt;&lt;/strong&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;span style="font-size: 10pt;"&gt;Now &lt;strong&gt;inStream&lt;/strong&gt; is decorated by &lt;strong&gt;BufferedInputStream&lt;/strong&gt; decorator. At runtime the &lt;strong&gt;BufferedInputStream&lt;/strong&gt;, which is a decorator, forwards the method call to its decorated object &lt;strong&gt;FileInputStream&lt;/strong&gt;. The decorator will apply the additional functionality of buffering around &lt;strong&gt;FileInputStream&lt;/strong&gt;.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;strong&gt;SiteMash&lt;/strong&gt; is also one of the examples of decorator pattern in java web frameworks to decorate page.&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;h2&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Basic steps to use Decorator&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;span style="font-size: 10pt;"&gt;To implement the decorator pattern you can just follow these steps:&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;ol&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Create an abstract class or interface that you want to decorate. (e.g; Employee) and provide concrete implementation of that class/interface by extending it.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Create an abstract decorator (e.g; EmployeeDecorator) that contains pointer field of decorated class, decorator must extend same decorated class/interface. (e.g; Employee)&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Pass the object that you want to decorate in the constructor of decorator.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Redirect methods of decorator to decorated class's core implementation.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Override methods where you need to change behavior.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ol&gt;&lt;h2&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Conclusions&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h2&gt;&lt;ul&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Decorators are very flexible alternative of inheritance.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;Decorators enhance (or in some cases restrict) the functionality of decorated objects.&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style="font-size: 10pt;"&gt;They work dynamically to extend class responsibilities, even inheritance does same but in a static fashion (means compile time).&lt;br /&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;h1&gt;&lt;span style="font-size: 12pt;"&gt;&lt;span style="color: #336699;"&gt;&lt;strong&gt;Design Patterns&lt;/strong&gt;&lt;/span&gt;&lt;br /&gt;&lt;/span&gt;&lt;/h1&gt;&lt;span style="font-size: 10pt;"&gt;The key to any solution is to understand the problem. When we have complete requirements of our problem domain, the proper analyses of those requirements will facilitate us when incorporating any possible changes in the future. We have to suggest some design that can support current requirements and any possible changes. Therefor we use our past experience or shared thoughts of experts (Design Patterns) to design an elegant, flexible and reusable solution, and try to avoid redesigning or at least minimize it.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;&lt;em&gt;&lt;span style="color: #4f81bd;"&gt;&lt;strong&gt;Design patterns are solutions to design problems you find again and again in real world application development.&lt;br /&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/em&gt;&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style="font-size: 10pt;"&gt;Design patterns are not actually APIs that can be programmed in classes and reused as they are, neither are they are not domain-specific designs for any application or module. Design patterns actually are guidelines for communicating objects and classes that are customized to solve a general design problem in a particular context.&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/4575263909641298207-2692596324581225071?l=zishanbilal.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://zishanbilal.blogspot.com/feeds/2692596324581225071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/design-pattern-by-example-decorator.html#comment-form' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/2692596324581225071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/4575263909641298207/posts/default/2692596324581225071'/><link rel='alternate' type='text/html' href='http://zishanbilal.blogspot.com/2011/11/design-pattern-by-example-decorator.html' title='Design Pattern by Example – Decorator Pattern'/><author><name>TechGuy</name><uri>http://www.blogger.com/profile/10187290112762198659</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='http://1.bp.blogspot.com/-V-CQBKVZ7Zg/TsIErMoEE0I/AAAAAAAAAC8/GzLjknclF-Q/s220/DSC08838.JPG'/></author><thr:total>7</thr:total></entry></feed>
