<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7735704189187983707</id><updated>2026-03-08T00:24:17.752-08:00</updated><category term="Articles - Java"/><category term="Java Interview Questions"/><category term="Articles - Different Topics"/><category term="Core Java"/><category term="Java Applet"/><category term="Articles - Java Basics"/><category term="Java - Applet JLabel"/><category term="Java Servlet Interview Questions"/><category term="Jsp Interview Questions"/><category term="Collection-ArrayList"/><category term="Articles - Advance Java"/><category term="Java - Applet JTextField"/><category term="JDBC"/><category term="OOPS"/><category term="Java 2D Programming"/><category term="Java File"/><category term="Java Servlet"/><category term="Collections"/><category term="java Basic"/><category term="Collections - Hashset"/><category term="Runtime JVM"/><category term="Collection-LinkedHashMap"/><category term="Java Security"/><category term="Java Stream Objects"/><category term="Java Threads"/><category term="Articles - Java EJB"/><category term="Java Networking"/><category term="Java Reflexion"/><category term="EJB"/><category term="Java Extration"/><category term="Java XML Parsing"/><category term="Articles - Collections"/><category term="Articles - Java Installation"/><category term="Collection-Data Structure"/><category term="System Utility Commands"/><category term="JAVA SOAP"/><category term="Java BuzzWords"/><category term="Java Mail"/><title type='text'>Java Sample Codes and Articles</title><subtitle type='html'>Best Java Article and Sample codes that Needed.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>489</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-7574594057664851984</id><published>2012-02-13T00:14:00.000-08:00</published><updated>2012-05-31T01:22:50.996-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Observing garbage collector behavior&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
I encourage readers to experiment with the behavior of the garbage collector. Use jconsole (comes with the JDK) or &lt;a href=&quot;http://visualvm.java.net/&quot;&gt;VisualVM&lt;/a&gt;
 (which produced the graph earlier on in this post) to visualize 
behavior on a running JVM. But, in particular, start getting familiar 
with garbage collection log output, by running your JVM with (updated 
with jbellis’ feedback – thanks!):&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintGC&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintGCDetails&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintGCDateStamps&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintGCApplicationStoppedTime&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintPromotionFailure&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
Also useful but verbose (meaning explained in later posts):&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintHeapAtGC&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-XX:+PrintTenuringDistribution&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-XX:PrintFLSStatistics=1&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
The output is pretty easy to read for the throughput collector. For CMS 
and G1, the output is more opaque to analysis without an introduction. I
 hope to cover this in a later update.&lt;br /&gt;
&lt;br /&gt;
In the mean time, the take-away is that those options above are probably
 the first things you want to use whenever you suspect that you have a 
GC related problem. It is almost always the first thing I tell people 
when they start to hypothesize GC issues; &lt;em&gt;have you looked at GC logs?&lt;/em&gt; If you have not, you are probably wasting your time speculating about GC.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/7574594057664851984/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/7574594057664851984?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7574594057664851984'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7574594057664851984'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_13.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-2791096219207461450</id><published>2012-02-12T09:13:00.000-08:00</published><updated>2012-05-31T01:13:54.570-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Available Hotspot garbage collectors&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The default choice of garbage collector in Hotspot is the throughput 
collector, which is a generational, parallel, compacting collector. It 
is entirely optimized for throughput; total amount of work achieved by 
the application in a given time period.&lt;br /&gt;
&lt;br /&gt;
The traditional alternative for situations where latency/pause times are a concern, is the &lt;em&gt;CMS&lt;/em&gt; collector. CMS stands for &lt;em&gt;Concurrent Mark &amp;amp; Sweep&lt;/em&gt;
 and refers to the mechanism used by the collector. The purpose of the 
collector is to minimize or even eliminate long stop-the-world pauses, 
limiting garbage collection work to shorter stop-the-world (often 
parallel) pauses, in combination with longer work performed concurrently
 with the application. An important property of the CMS collector is 
that it is &lt;em&gt;not&lt;/em&gt; compacting, and thus suffers from fragmentation concerns (more on this in a later blog post).&lt;br /&gt;
&lt;br /&gt;
As of later versions of JDK 1.6 and JDK 1.7, there is a new garbage collector available which is called &lt;em&gt;G1&lt;/em&gt; (which stands for &lt;em&gt;Garbage First&lt;/em&gt;).
 It’s aim, like the CMS collector, is to try to mitigate or eliminate 
the need for long stop-the-world pauses and it does most of it’s work in
 parallel in short stop-the-world incremental pauses, with some work 
also being done concurrently with the application. Contrary to CMS, G1 &lt;em&gt;is&lt;/em&gt;
 a compacting collector and does not suffer from fragmentation concerns –
 but has other trade-offs instead (again, more on this in a later blog 
post).&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/2791096219207461450/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/2791096219207461450?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2791096219207461450'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2791096219207461450'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_12.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-1797291076815546506</id><published>2012-02-11T11:12:00.000-08:00</published><updated>2012-05-31T01:12:57.169-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Concurrent collection&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;Concurrent&lt;/em&gt; in a garbage collection context refers to performing garbage collection work &lt;em&gt;concurrently with&lt;/em&gt;
 the application (mutator). For example, on an 8 core system, a garbage 
collector might keep two background threads that do garbage collection 
work while the application is running. This allows significant amounts 
of work to be done without incurring an application pause, usually at 
some cost of throughput and implementation complexity (for the garbage 
collector implementor).&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/1797291076815546506/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/1797291076815546506?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1797291076815546506'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1797291076815546506'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_11.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-4106155964875000161</id><published>2012-02-10T05:11:00.000-08:00</published><updated>2012-05-31T01:12:14.278-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Incremental collection&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;em&gt;Incremental&lt;/em&gt; in a garbage collection context refers to dividing 
up the work that needs to be done in smaller chunks, often with the aim 
of pausing the applications for multiple brief periods instead of a 
single long pause. The behavior of the generational collector described 
above is partially incremental in the sense that the young generation 
collectors constitute incremental work.  However, as a whole, the 
collection process is not incremental because of the full heap 
collections incurred when the old generation becomes full.&lt;br /&gt;
Other forms of incremental collections are possible; for example, a 
collector can do a tiny bit of garbage collection work for every 
allocation performed by the application. The concept is not tied to a 
particular implementation strategy.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/4106155964875000161/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/4106155964875000161?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/4106155964875000161'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/4106155964875000161'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_10.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-1543215812008930655</id><published>2012-02-09T01:10:00.000-08:00</published><updated>2012-05-31T01:11:25.188-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Parallel collection&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
The point of having a generational collector is to optimize for &lt;em&gt;throughput&lt;/em&gt;;
 in other words, the total amount of work the application gets to do in a
 particular amount of time. As a side-effect, most of the pauses 
incurred due to garbage collection also become shorter. However, no 
attempt is made to eliminate the periodic full collections which will 
imply a pause time of whatever is necessary to complete a full 
collection.&lt;br /&gt;
&lt;br /&gt;
The throughput collector does do one thing which is worth mentioning in order to mitigate this: It is &lt;em&gt;parallel&lt;/em&gt;,
 meaning it uses multiple CPU cores simultaneously to speed up garbage 
collection. This does lead to shorter pause times, but there is a limit 
to how far you can go – even in an unrealistic perfect situation of a 
linear speed-up (meaning, double CPU count -&amp;gt; half collection time) 
you are limited by the number of CPU cores on your system. If you are 
collecting a 30 GB heap, that is going to take some significant time 
even if you do so with 16 parallel threads.&lt;br /&gt;
&lt;br /&gt;
In garbage collection parlance, the word parallel is used to refer to a 
collector that does work on multiple CPU cores at the same time.&lt;br /&gt;
&lt;div style=&quot;background-color: white; border: medium none; color: black; overflow: hidden; text-align: left; text-decoration: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/1543215812008930655/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/1543215812008930655?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1543215812008930655'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1543215812008930655'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_09.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-7579348828565420143</id><published>2012-02-08T04:09:00.000-08:00</published><updated>2012-05-31T01:10:42.925-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Generational garbage collection&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Most real-world applications tend to perform a lot allocation of 
short-lived objects (in other words, objects that are allocated, used 
for a brief period, and then no longer referenced). A generational 
garbage collector attempts to exploit this observation in order to be 
more CPU efficient (in other words, have &lt;em&gt;higher throughput&lt;/em&gt;). (More formally, the hypothesis that most applications have this behavior is known as the &lt;em&gt;weak generational hypothesis&lt;/em&gt;.)&lt;br /&gt;
&lt;br /&gt;
It is called “generational” because objects are divided up into &lt;em&gt;generations&lt;/em&gt;.
 The details will vary between collectors, but a reasonable 
approximation at this point is to say that objects are divided into two 
generations:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;young generation&lt;/em&gt; is where objects are initially allocated. In other words, all objects start off being in the young generation.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;old generation&lt;/em&gt; is where objects “graduate” to when they have spent some time in the young generation.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
The reason why generational collectors are typically more efficient, is that they &lt;em&gt;collect the young generation separately&lt;/em&gt;
 from the old generation. Typical behavior of an application in steady 
state doing allocation, is frequent short pauses as the young generation
 is being collected – punctuated by infrequent but longer pauses as the 
old generation fills up and triggers a full collection of the entire 
heap (old and new). If you look at a heap usage graph of a typical 
application, it will look similar to this:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;wp-caption alignnone&quot; id=&quot;attachment_81&quot; style=&quot;width: 310px;&quot;&gt;

&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;http://worldmodscode.files.wordpress.com/2011/12/sawtooth_typical_throughput_collector.png&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img alt=&quot;Typical sawtooth behavior of heap usage&quot; class=&quot;size-medium wp-image-81&quot; height=&quot;358&quot; src=&quot;http://worldmodscode.files.wordpress.com/2011/12/sawtooth_typical_throughput_collector.png?w=300&amp;amp;h=168&quot; title=&quot;Typical sawtooth behavior of heap usage&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: x-small;&quot;&gt;Typical sawtooth behavior of heap usage with the throughput collector&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;wp-caption-text&quot;&gt;

&lt;/div&gt;
&lt;/div&gt;
The ongoing sawtooth look is a result of young generation garbage 
collections. The large dip towards the end is when the old generation 
became full and the JVM did a complete collection of the entire heap. 
The amount of heap usage at the end of that dip is a reasonable 
approximation of the actual &lt;em&gt;live set at that point in time&lt;/em&gt;. 
(Note: This is a graph from running a stress test against a Cassandra 
instance configured to use the default JVM throughput collector; it does
 not reflect out-of-the-box behavior of Cassandra.)&lt;br /&gt;
&lt;br /&gt;
Note that simply picking the “current heap usage” at an arbitrary point in time on that graph &lt;em&gt;will not give you an idea of the memory usage of the application&lt;/em&gt;. I cannot stress that point enough. What is typically considered the memory “usage” is the &lt;em&gt;live set&lt;/em&gt;,
 not the heap usage at any particular time. The heap usage is much more a
 function of the implementation details of the garbage collector; the 
only effect on heap usage from the memory usage of the application is 
that it provides a &lt;em&gt;lower bound&lt;/em&gt; on the heap usage.&lt;br /&gt;
Now, back to why generational collectors are typically more efficient.&lt;br /&gt;
&lt;br /&gt;
Suppose our hypothetical application is such that 90% of all objects &lt;em&gt;die young&lt;/em&gt;;
 in other words, they never survive long enough to be promoted to the 
old generation. Further, suppose that our collection of the young 
generation is compacting (see previous sections) in nature. The cost of 
collecting the young generation is now roughly that of tracing and 
copying 10% of the objects it contains. The cost associated with the 
remaining 90% was quite small. Collection of the young generation 
happens when it becomes full, and is a stop-the-world pause.&lt;br /&gt;
&lt;br /&gt;
The 10% of objects that survived may be promoted to the old generation 
immediately, or they may survive for another round or two in young 
generation (depending on various factors). The important overall 
behavior to understand however, is that objects start off in the young 
generation, and are &lt;em&gt;promoted&lt;/em&gt; to the old generation as a result of &lt;em&gt;surviving&lt;/em&gt; in the young generation.&lt;br /&gt;
&lt;br /&gt;
(Astute readers may have noticed that collecting the young generation 
completely separately is not possible – what if an object in the old 
generation has a reference to an object in the new generation? This is 
indeed something a garbage collector must deal with; a future post will 
talk about this.)&lt;br /&gt;
&lt;br /&gt;
The optimization is quite dependent on the &lt;em&gt;size&lt;/em&gt; of the young 
generation. If the size is too large, it may be so large that the pause 
times associated with collecting it is a noticeable problem. If the size
 is too small, it may be that even objects that die young do not die 
quite quickly &lt;em&gt;enough&lt;/em&gt; to still be in the young generation when they die.&lt;br /&gt;
&lt;br /&gt;
Recall that the young generation is collected when it becomes full; this
 means that the smaller it is, the more often it will be collected. 
Further recall that when objects survive the young generation, they get 
promoted to the old generation. If most objects, despite dying young, 
never have a chance to die in the young generation because it is too 
small – they will get promoted to the old generation and the 
optimization that the generational garbage collector is trying to make 
will fail, and you will take the full cost of collecting the object 
later on in the old generation (plus the up-front cost of having copied 
it from the young generation).&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/7579348828565420143/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/7579348828565420143?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7579348828565420143'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7579348828565420143'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_08.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-5457604981577092356</id><published>2012-02-07T03:09:00.000-08:00</published><updated>2012-05-31T01:09:17.363-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Compacting vs. non-compacting garbage collection&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
An important distinction between garbage collectors is whether or not they are &lt;em&gt;compacting&lt;/em&gt;.
 Compacting refers to moving objects around (in memory) to as to collect
 them in one dense region of memory, instead of being spread out 
sparsely over a larger region.&lt;br /&gt;
&lt;br /&gt;
Real-world analogy: Consider a room full of things on the floor in 
random places. Taking all these things and stuffing them tightly in a 
corner is essentially compacting them; freeing up floor space. Another 
way to remember what compaction is, is to envision one of those machines
 that take something like a car and compact it together into a block of 
metal, thus taking less space than the original car by eliminating all 
the space occupied by air (but as someone has pointed out, while the car
 id destroyed, objects on the heap are not!).&lt;br /&gt;
&lt;br /&gt;
By contrast a non-compacting collector never moves objects around. Once 
an object has been allocated in a particular location in memory, it 
remains there forever or until it is freed.&lt;br /&gt;
There are some interesting properties of both:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The cost of performing a compacting collection is a function of the 
amount of live data on the heap. If only 1% of data is live, only 1% of 
data needs to be compacted (copied in memory).&lt;/li&gt;
&lt;li&gt;By contrast, in a non-compacting collector objects that are no 
longer reachable still imply book keeping overhead as their memory 
locations must be kept track of as being freed, to be used in future 
allocations.&lt;/li&gt;
&lt;li&gt;In a compacting collector, allocation is usually done via a &lt;em&gt;bump-the-pointer&lt;/em&gt;
 approach. You have some region of space, and maintain your current 
allocation pointer. If you allocate an object of n bytes, you simply 
bump that pointer by n (I am eliding complications like multi-threading 
and optimizations that implies).&lt;/li&gt;
&lt;li&gt;In a non-compacting collector, allocation involves finding &lt;em&gt;where&lt;/em&gt;
 to allocate using some mechanism that is dependent on the exact 
mechanism used to track the availability of free memory. In order to 
satisfy an allocation of n bytes, a contiguous region of n bytes free 
space must be found. If one cannot be found (because the heap is &lt;em&gt;fragmented&lt;/em&gt;, meaning it consists of a mixed bag of free and allocated space), the allocation will fail.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Real-world analogy: Consider your room again. Suppose you are a 
compacting collector. You can move things around on the floor freely at 
your leisure. When you need to make room for that big sofa in the middle
 of the floor, you move other things around to free up an appropriately 
sized chunk of space for the sofa. On the other hand, if you are a 
non-compacting collector, everything on the floor is nailed to it, and 
cannot be moved. A large sofa might not fit, despite the fact that you 
have plenty of floor space available – there is just no &lt;em&gt;single&lt;/em&gt; space large enough to fit the sofa.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/5457604981577092356/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/5457604981577092356?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/5457604981577092356'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/5457604981577092356'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_07.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-7173726659773457172</id><published>2012-02-06T11:08:00.000-08:00</published><updated>2012-05-31T01:08:37.184-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Basic stop-the-world, mark, sweep, resume&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
A very simple tracing garbage collector works using the following process:&lt;br /&gt;
&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Pause the application completely.&lt;/li&gt;
&lt;li&gt;Mark all objects that are reachable (from the root set, see above) 
by tracing the object graph (i.e., following references recursively).&lt;/li&gt;
&lt;li&gt;Free all objects that were not reachable.&lt;/li&gt;
&lt;li&gt;Resume the application.&lt;/li&gt;
&lt;/ol&gt;
In a single-threaded world, this is pretty easy to imagine: The call 
that is responsible for allocating a new object will either return the 
new object immediately, or, if the heap is full, initiate the above 
process to free up space, followed by completing the allocation and 
returning the object.&lt;br /&gt;
None of the JVM garbage collectors work like this. However, it is good 
to understand this basic form of a garbage collector, as the available 
garbage collectors are essentially optimizations of the above process.&lt;br /&gt;
The two main reasons why the JVM does not implement garbage collection like this are:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Every single garbage collection pause will be long enough to collect the entire heap; in other words, it has very poor latency.&lt;/li&gt;
&lt;li&gt;For almost all real-world applications, it is by far not the most 
efficient way to perform garbage &lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/7173726659773457172/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/7173726659773457172?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7173726659773457172'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7173726659773457172'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_06.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-3220961564114463666</id><published>2012-02-05T05:07:00.000-08:00</published><updated>2012-05-31T01:07:43.530-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Tracing vs. reference counting&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
You may have heard of &lt;em&gt;reference counting&lt;/em&gt; being used (for 
example, cPython uses a reference counting scheme for most of it’s 
garbage collection work). I am not going to talk much about it because 
it is not relevant to JVM:s, except to say two things:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;One property that reference counting garbage collection has is that 
an object will be known to be unreachable  immediately at the point 
where the last reference is removed.&lt;/li&gt;
&lt;li&gt;Reference counting will not detect as unreachable cyclic data 
structures, and has some other problems that cause it to not be the 
end-all be-all garbage collection choice.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
The JVM instead uses what is known as a &lt;em&gt;tracing&lt;/em&gt; garbage 
collector. It is called tracing because, at least at an abstract level, 
the process of identifying garbage involves taking the &lt;em&gt;root set&lt;/em&gt;
 (things like your local variables on your stack or global variables) 
and tracing a path from those objects to all objects that are directly 
or indirectly reachable from said root set. Once all reachable (live) 
objects have been identified, the objects eligible for being freed by 
the garbage collector have been identified by a proces of elimination.&lt;div style=&quot;background-color: white; border: medium none; color: black; overflow: hidden; text-align: left; text-decoration: none;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/3220961564114463666/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/3220961564114463666?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/3220961564114463666'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/3220961564114463666'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_05.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-2036053947540940830</id><published>2012-02-04T13:06:00.000-08:00</published><updated>2012-05-31T01:07:02.645-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: medium;&quot;&gt;Why should anyone have to care about the garbage collector?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
That is a good question. The perfect garbage collector would do its job 
without a human ever noticing that it exists. Unfortunately, there 
exists no known perfect (whatever perfection means) garbage collection 
algorithm. Further, the selection of garbage collectors practically 
available to most people is additionally limited to a subset of garbage 
collection algorithms that are in fact implemented. (Similarly, &lt;code&gt;malloc&lt;/code&gt;
 is not perfect either and has its issues, with multiple implementations
 available with different characteristics. However, this post is not 
trying to contrast automatic and explicit memory management, although 
that is an interesting topic.)&lt;br /&gt;
&lt;br /&gt;
The reality is that, as with many technical problems, there are some 
trade-offs involved. As a rule of thumb, if you’re using the freely 
available Hotspot based JVM:s (Oracle/Sun, OpenJDK), &lt;em&gt;you mostly notice the garbage collector if you care about latency&lt;/em&gt;.
 If you do not, chances are the garbage collector will not be a bother –
 other than possibly to select a maximum heap size different from the 
default.&lt;br /&gt;
&lt;br /&gt;
By latency, in the context of garbage collection, I mean &lt;em&gt;pause times&lt;/em&gt;.
 The garbage collector needs to pause the application sometimes in order
 to do some of its work; this is often referred to as a &lt;em&gt;stop-the-world&lt;/em&gt; pause (the “world” being the observable universe from the perspective of the Java application, or &lt;em&gt;mutator&lt;/em&gt; in GC speak (because it is &lt;em&gt;mutating&lt;/em&gt;
 the heap while the garbage collector is trying to collect it). It is 
important to note that while all practically available garbage 
collectors impose stop-the-world pauses on the application, the 
frequency and duration of these pauses vary greatly with the choice of 
garbage collector, garbage collector settings, and application behavior&lt;em&gt;.&lt;/em&gt;&lt;br /&gt;
&lt;br /&gt;
As we shall see, there exists garbage collection algorithms that attempt
 to avoid the need to ever collect the entire heap in a stop-the-world 
pause. The reason this is an important property is that if at any point 
(even if infrequent), you stop the application for a complete collection
 of the heap, the pause times suffered by the application scale &lt;em&gt;proportionally to the heap size&lt;/em&gt;.
 This is typically the main thing you want to avoid when you care about 
latency. There are other concerns as well, but this is usually the big 
one.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/2036053947540940830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/2036053947540940830?isPopup=true' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2036053947540940830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2036053947540940830'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection_04.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-8003957624528965958</id><published>2012-02-03T01:05:00.000-08:00</published><updated>2012-05-31T01:05:26.537-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Practical Garbage Collection</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
This is the first part of a series of blog posts I intend to write, 
whose aim will be to explain how garbage collection works in the real 
world (in particular, with the &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_virtual_machine&quot;&gt;JVM&lt;/a&gt;).
 I will cover some theory that I believe is necessary to understand 
garbage collection enough for practical purposes, but will keep it to a 
minimum.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;&quot; name=&quot;more&quot;&gt;&lt;/a&gt;The motivation is that garbage collection related 
questions keeps coming up in variety of circumstances, including (for 
example) on the &lt;a href=&quot;http://cassandra.apache.org/&quot;&gt;Cassandra&lt;/a&gt; 
mailing list. The problem when trying to help is that explaining the 
nuances of garbage collection is too much of an effort to do ad-hoc in a
 mailing list reply tailored to that specific situation, and you rarely 
have enough information about the situation to tell someone what &lt;em&gt;their&lt;/em&gt; particular problem is caused by.&lt;br /&gt;
&lt;br /&gt;
I hope that this guide will be something I can point to in answering 
these questions. I hope that it will be detailed enough to be useful, 
yet easy to digest and non-academic enough for a broad audience.&amp;nbsp;
I very much appreciate any feedback on what I need to clarify, improve, rip out completely, etc.Much of the information here is not specific to Java. However, in order 
to avoid constantly invoking generic and abstract terminology, I am 
going to speak in concrete terms of the Hotspot JVM wherever possible.&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/8003957624528965958/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/8003957624528965958?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/8003957624528965958'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/8003957624528965958'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/02/practical-garbage-collection.html' title='Practical Garbage Collection'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-495644526541824638</id><published>2012-01-22T06:18:00.000-08:00</published><updated>2012-05-18T01:18:39.606-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;Java8
Lambda Expression&lt;/strong&gt;&lt;br /&gt;




Java has
finally realized the power of writing code in functional style and is going to
support the concept of closures starting from Java8. JSR 335 - Lambda
Expressions for the JavaTM Programming Language aims to support programming in
a multicore environment by adding closures and related features to the Java
language. So it will finally be possible to pass around functions similar to
variables in pure Java code.&lt;br /&gt;




Currently
if someone wants to try out and play around lambda expressions, Project Lambda
of OpenJDK provides prototype implementation of JSR-335. Following code snippet
should run fine with OpenJDK Project Lambda compiler.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;ExecutorService executor = Executors.newCachedThreadPool();

executor.submit(() -&amp;gt; {System.out.println(&quot;I am running&quot;)})&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
As can be
seen above, a closure(function) has been passed to executor&#39;s submit method. It
does not take any argument and hence empty brackets () have been placed. This
function just prints &quot;I am running&quot; when executed. Just as we can
pass functions to function, it will also be possible to create closure within
functions and return closure from function. I would recommend to try out
OpenJDK to get a feel of lambda expressions which is going to be part of Java8.&lt;br /&gt;
&lt;br /&gt;




&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;




So this was all about functional
programming, it&#39;s concepts, benefits and options available on JVM to write
function code. Functional programming requires a different mind-set and can be
very useful if used correctly. Functional Programming along with Object
Oriented Programming can be a jewel in crown. As discussed there are various
options available to write code in functional style that can be executed on
JVM. Choice depends on various factors and there is no one language that can be
considered best in all aspects. However one thing is for sure, going forward we
are going to see more and more usage of functional programming.&lt;br /&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/495644526541824638/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/495644526541824638?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/495644526541824638'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/495644526541824638'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm_22.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-2809500613560017812</id><published>2012-01-21T00:16:00.000-08:00</published><updated>2012-05-18T01:17:31.804-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Groovy&lt;/b&gt;&lt;br /&gt;
Groovy is
again a dynamic language with some support for functional programming. Amongst
the 3 languages, Groovy can be considered weakest in terms of functional
programming features. However because of it&#39;s dynamic nature and close
resemblance to Java, it has been widely accepted and considered good
alternative to Java. Groovy does not provide immutable objects out of the box
but has excellent support for higher order functions. Immutable objects can be
created with annotation @Immutation, but it&#39;s far less flexible than
immutablity support in Scala and Clojure. In Groovy functions can be passed
around just as any other variable in the form of Closures. The same example in
Groovy can be written as follows&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;def numbers = [1,2,3,4,5,6,7,8,9,10]

def total = numbers.inject(0){a,b -&amp;gt;

    a+b
}&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
However the point to be noted is that variables &quot;numbers&quot; and &quot;total&quot; are not immutable and can be modified at any point of time. Hence writing multithreaded code can be a bit challenging. But Groovy does provide the concept of Actors, Agents and DataFlow variables via library called GPars(Groovy Parallel System) which reduces the challenges associated with multithreaded code to a greater extent. &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/2809500613560017812/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/2809500613560017812?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2809500613560017812'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2809500613560017812'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/groovy-groovy-isagain-dynamic-language.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-3136469318437250302</id><published>2012-01-20T01:14:00.000-08:00</published><updated>2012-05-18T01:15:43.903-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Clojure&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Clojure
is a dynamic language with an excellent support for writing code in functional
style. It is a dialect of &quot;lisp&quot; programming language with an
efficient and robust infrastructure for multithreaded programming. Clojure is
predominantly a functional programming language, and features a rich set of
immutable, persistent data structures. When mutable state is needed, Clojure
offers a software transactional memory system and reactive Agent system that
ensure clean, correct multithreaded designs. Apart from this since Clojure is a
dynamic language, it allows to modify class definition at run time by adding
new methods or modifying existing one at run time. This makes it different from
Scala which is a statically typed language.&lt;br /&gt;
Immutability
is in the root of Clojure. To create immutable list just following needs to be
done. By default list in Clojure is immutable, so does not require any extra
effort.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;(def numbers (list 1 2 3 4 5 6 7 8 9 10))&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
To add numbers without maintaining state, reduce function can be used as mentioned below&lt;br /&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;(reduce + 0 &#39;(1 2 3 4 5 6 7 8 9 10))&lt;/pre&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
As can be seen, adding list of numbers just requires one line of code without mutating
any state. This is the beauty about functional programming languages and plays
an important role for parallel execution. &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/3136469318437250302/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/3136469318437250302?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/3136469318437250302'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/3136469318437250302'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm_20.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-4706550881811018708</id><published>2012-01-19T03:12:00.000-08:00</published><updated>2012-05-18T01:13:29.588-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;b&gt;Scala&lt;/b&gt;&lt;br /&gt;
Scala is
a statically typed multi-paradigm programming language designed to integrate
features of object oriented programming and functional programming. Since it is
static, one cannot change class definition at run time i.e. one cannot add new
methods or variables at run-time. However Scala does provide functional
programming concepts i.e. immutability, higher-order functions, nested
functions etc. Apart from supporting Java&#39;s concurrency model, it also provides
concept of Actor model out of the box for event based asynchronous message
passing between objects. The code written in Scala gets compiled into very
efficient bytecode which can then be executed on JVM.&lt;br /&gt;
Creating
immutable list in Scala is very simple and doesn&#39;t require any extra effort.
&quot;val&quot; keyword does the trick.&lt;br /&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;val numbers = List(1,2,3,4)&lt;/pre&gt;
&lt;br /&gt;
Functions
can be passed as arguments. Let&#39;s see this with an example. &lt;br /&gt;
Suppose
we have a list of 10 numbers and we want to calculate sum of all the numbers in
list.&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;val numbers = List(1,2,3,4,5,6,7,8,9,10)

val total = numbers.foldLeft(0){(a,b) =&amp;gt;

    a+b

}&lt;/pre&gt;
As can be seen in above example, we are passing a function to add two variables &quot;a&quot; and &quot;b&quot; to another function &quot;foldLeft&quot; which is provided by Scala library on collections. We have also not used any iteration logic and temporary variable to calculate the sum. &quot;foldLeft&quot; method eliminates the need to maintain state in temporary variable which would have otherwise be required if we were to write this code in pure Java way (as mentioned below). &lt;br /&gt;&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
&lt;pre&gt;int total = 0;

for(int number in numbers){

    total+=number;

}&lt;/pre&gt;
Scala
function can easily be executed in parallel without any need for
synchronization since it does not mutate state.&lt;br /&gt;
This was
just a small example to showcase the power of Scala as functional programming
language. There are whole lot of features available in Scala to write code in
functional style.&lt;br /&gt;
&lt;pre&gt;&amp;nbsp;&lt;/pre&gt;
&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/4706550881811018708/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/4706550881811018708?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/4706550881811018708'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/4706550881811018708'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm_19.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-82713220276203481</id><published>2012-01-18T01:08:00.000-08:00</published><updated>2012-05-18T01:08:19.941-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;JVM based
Functional Programming Languages&lt;/strong&gt;&lt;br /&gt;




There are
many JVM based languages which supports functional programming paradigm.
However I intend to limit discussion around following. &lt;br /&gt;


&lt;ul class=&quot;unIndentedList&quot;&gt;
&lt;li&gt;
Scala&lt;/li&gt;
&lt;li&gt;
Clojure&lt;/li&gt;
&lt;li&gt;
Groovy&lt;/li&gt;
&lt;li&gt;
Lambda
Expressions in Java 8&lt;/li&gt;
&lt;/ul&gt;
Lambda
Expressions is not a programming language but a feature that will be supported
in Java8. The reason for including it in this article is to emphasize on the
fact that going forward Java will also support writing code in functional
style.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/82713220276203481/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/82713220276203481?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/82713220276203481'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/82713220276203481'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm_18.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-1671951981400889285</id><published>2012-01-17T11:07:00.000-08:00</published><updated>2012-05-18T01:07:42.951-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;What is
Functional Programming?&lt;/strong&gt;&lt;br /&gt;




Functional
Programming is a concept which treats functions as first class citizens. At the
core of functional programming is immutability. It emphasizes on application of
functions in contrast to imperative programming style which emphasizes on
change in state. Functional programming has no side effects whereas programming
in imperative style can result in side-effects. Let&#39;s elaborate more on each of
these characteristics to understand the concept behind functional programming.&lt;br /&gt;


&lt;ul class=&quot;unIndentedList&quot;&gt;
&lt;li&gt;
&lt;strong&gt;Immutable state&lt;/strong&gt; - The state of an object doesn&#39;t change and hence need not be protected
or synchronized. That might sound a bit awkward at first, since if nothing
changes, one might think that we are not writing a useful program. However
that&#39;s not what immutable state means. In functional programming, change in
state occurs via series of transformations which keeps the object immutable and
yet achieves change in state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Functions as first class citizens&lt;/strong&gt; - There was a major shift in the way programs were
written when Object oriented concepts came into picture. Everything was
conceptualized as object and any action to be performed was treated as method
call on objects. Hence there is a series of method calls executed on objects to
get the desired work done. In functional programming world, it&#39;s more about
thinking in terms of communication chain between functions than method calls on
objects. This makes functions as first class citizens of functional programming
since everything is modelled around functions. &lt;/li&gt;
&lt;/ul&gt;
&lt;ul class=&quot;unIndentedList&quot;&gt;
&lt;li&gt;
&lt;strong&gt;Higher-order functions&lt;/strong&gt; - Functions in functional programming are higher
order functions since following actions can be performed with them.&lt;/li&gt;
&lt;/ul&gt;
1.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Functions can be passed within
functions as arguments.&lt;br /&gt;


2.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Functions can be created within
functions just as objects can be created in functions&lt;br /&gt;


3.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Functions can be returned from
functions&lt;br /&gt;




&lt;ul class=&quot;unIndentedList&quot;&gt;
&lt;li&gt;
&lt;strong&gt;Functions with no side-effects - &lt;/strong&gt;In functional programming, function execution has
no side-effects. In other words a function code will always return same result
for same argument when called multiple times. It doesn&#39;t change anything
outside its boundaries and is also not affected by any external change outside
it&#39;s boundary. It doesn&#39;t change input value and can only produce new output.
However once the output has been produced and returned by function, it also
becomes immutable and cannot be modified by any other function. In other words,
they support referential transparency i.e. if a function takes an input and
returns some output, multiple invocation of that function at different point of
time will always return same output as long as input remains same. This is one
of the main motivations behind using functional language as it makes easy to
understand and predict behaviour of program.&lt;/li&gt;
&lt;/ul&gt;
Characteristics
like immutability and no side-effects are extremely helpful while writing
multi-threaded code and developers need not to worry for synchronizing the
state. Hence functional code is very easy to distribute across multiple cores
as they don&#39;t have any side effects.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/1671951981400889285/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/1671951981400889285?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1671951981400889285'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1671951981400889285'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm_17.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-5319715254891800476</id><published>2012-01-16T02:06:00.000-08:00</published><updated>2012-05-18T01:06:55.965-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;Why
Functional Programming?&lt;/strong&gt;&lt;br /&gt;




Computers
of current era are shipped with multicore processors. Going forward the number
of processors in a machine is only going to increase. The code we write today
and tomorrow will probably never run on a single processor system. In order to
get best out of this, software must be designed to make more and more use of
concurrency and hence keep all available processors busy. Java does provide
concurrency concepts like threads, synchronization, locks etc. to execute code
in parallel. But shared memory multi-threading approach in Java causes more
trouble than solving the problem. &lt;br /&gt;










Java
based functional programming languages like Scala, Clojure, Groovy etc. looks
into these problems with a different angle and provides less complex and less
error-prone solutions as compared to imperative programming. They provide
immutability concepts out of the box and hence eliminate need of
synchronization and associated risk of deadlocks or livelocks. Concepts like
Actors, Agents and DataFlow variables provide high level concurrency
abstraction and makes very easy to write concurrent programs&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/5319715254891800476/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/5319715254891800476?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/5319715254891800476'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/5319715254891800476'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm_16.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-4526849777280267959</id><published>2012-01-15T05:05:00.000-08:00</published><updated>2012-05-18T01:06:08.863-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Different Topics"/><title type='text'>Functional Programming on the JVM</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;Introduction&lt;/strong&gt;&lt;br /&gt;


In recent times, many programming languages that run on JVM have emerged. Many of these
languages support the concept of writing code in a functional style. Programmers
have started realizing the benefits of functional programming and are beginning
to rediscover the powerful style of this programming paradigm. The emergence of
multiple languages on JVM have only helped to reignite the strong interest in
this paradigm.&lt;br /&gt;




Java at
its core is an imperative programming language. However in recent past many new
languages like Scala, Clojure, Groovy etc. have become popular which supports
functional programming style and yet run on JVM. However none of these
languages can be considered as pure functional language since all of them allow
Java code to be called from within them and Java on its own is not a functional
language. Still they have different degree of support for writing code in
functional style and have their own benefits. Functional programming requires
different kind of thinking and has its own advantages as compared to imperative
programming.&lt;br /&gt;




It seems
that Java has also realized functional programming advantages and is slowly
inching towards it. First sign of this can be seen in the form of Lambda
Expressions that will be supported in Java 8. Although it&#39;s too early to
comment on this as the draft for Java 8 is still under review and is expected
to be released next year, but it does show that Java has plans of supporting
functional programming style going forward.&lt;br /&gt;




In this
article we will first discuss what functional programming is and how it is
different from imperative programming. Later we will see where does each of the
above mentioned Java based programming languages i.e. Scala, Clojure and Groovy
fits in the world of functional programming and what each of them has to offer.
And at the last we will sneak-peak into Java 8&#39;s lambda expressions.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/4526849777280267959/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/4526849777280267959?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/4526849777280267959'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/4526849777280267959'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/functional-programming-on-jvm.html' title='Functional Programming on the JVM'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-6644687130446996071</id><published>2012-01-14T02:54:00.000-08:00</published><updated>2012-05-18T00:58:49.644-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Advance Java"/><title type='text'>Authentication in Web.xml</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h2&gt;
 &lt;b&gt;Authentication&lt;/b&gt;&lt;/h2&gt;
A web container can authenticate a web client/user using either HTTP BASIC, HTTP DIGEST, HTTPS CLIENT or FORM based authentication schemes. &lt;br /&gt;&lt;br /&gt;&lt;b&gt;Use Case:&lt;/b&gt; We would like to utilize the browser authentication mechanism, HTTP BASIC as defined in the HTTP 1.0 specification. &lt;br /&gt;&lt;br /&gt;The login-config.xml element in web.xml would look like the following:&lt;br /&gt;
&amp;lt;login-config&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;auth-method&amp;gt;BASIC&amp;lt;/auth-method&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;realm-name&amp;gt;default&amp;lt;/realm-name&amp;gt;&lt;br /&gt;
&amp;lt;/login-config&amp;gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Use Case: &lt;/b&gt;We would like to utilize HTTPS Client authentication mechanism that is based on digital certificates. The authentication is based on the user&#39;s X509 certificate.

The login-config.xml element in web.xml would look like the following:&lt;br /&gt; &lt;br /&gt;&amp;lt;login-config&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;auth-method&amp;gt;CLIENT-CERT&amp;lt;/auth-method&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;realm-name&amp;gt;JMX Console&amp;lt;/realm-name&amp;gt;&lt;br /&gt;
&amp;lt;/login-config&amp;gt;&lt;br /&gt; &lt;br /&gt;&lt;b&gt;Use Case:&lt;/b&gt; We would like to utilize FORM based authentication mechanism.
FORM based mechanism provides flexibility in defining a custom jsp/html page
 for login and another page to direct for errors during login.

The login-config xml element in web.xml would look like the following: &lt;br /&gt; &lt;br /&gt;&amp;lt;login-config&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;auth-method&amp;gt;FORM&amp;lt;/auth-method&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;form-login-config&amp;gt;&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; &amp;lt;form-login-page&amp;gt;/login.html&amp;lt;/form-login-page&amp;gt;
 &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;lt;form-error-page&amp;gt;/error.html&amp;lt;/form-error-page&amp;gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;lt;/form-login-config&amp;gt;&lt;br /&gt;
&amp;lt;/login-config&amp;gt;&lt;br /&gt; &lt;br /&gt;In this case, the html page for login is the login.html and if any 
errors are encountered (including failed login), the user is directed to
 error.html &lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/6644687130446996071/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/6644687130446996071?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/6644687130446996071'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/6644687130446996071'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/authentication-in-webxml.html' title='Authentication in Web.xml'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-7976513903988065267</id><published>2012-01-13T00:52:00.000-08:00</published><updated>2012-05-18T00:52:28.593-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Advance Java"/><title type='text'>Implement Security in Web.xml</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;:3 We would like to &lt;strong&gt;&lt;em&gt;exclude&lt;/em&gt;&lt;/strong&gt;
 a set of web resources from any access. This can arise when a certain 
portion of the web application needs to undergo some form of maintenance
 or is not applicable for a particular physical deployment of a generic 
web application. We will achieve this with authorization constraints 
that specify &lt;em&gt;no roles.&lt;/em&gt;&lt;br /&gt;


&lt;br /&gt;


&lt;br /&gt;
&lt;pre&gt;&amp;lt;security-constraint&amp;gt;
   &amp;lt;display-name&amp;gt;excluded&amp;lt;/display-name&amp;gt;
   &amp;lt;web-resource-collection&amp;gt;
      &amp;lt;web-resource-name&amp;gt;No Access&amp;lt;/web-resource-name&amp;gt;
      &amp;lt;url-pattern&amp;gt;/excluded/*&amp;lt;/url-pattern&amp;gt;
      &amp;lt;url-pattern&amp;gt;/restricted/employee/excluded/*&amp;lt;/url-pattern&amp;gt;
      &amp;lt;url-pattern&amp;gt;/restricted/partners/excluded/*&amp;lt;/url-pattern&amp;gt;
   &amp;lt;/web-resource-collection&amp;gt;
   &amp;lt;web-resource-collection&amp;gt;
      &amp;lt;web-resource-name&amp;gt;No Access&amp;lt;/web-resource-name&amp;gt;
      &amp;lt;url-pattern&amp;gt;/restricted/*&amp;lt;/url-pattern&amp;gt;
      &amp;lt;http-method&amp;gt;DELETE&amp;lt;/http-method&amp;gt;
      &amp;lt;http-method&amp;gt;PUT&amp;lt;/http-method&amp;gt;
      &amp;lt;http-method&amp;gt;HEAD&amp;lt;/http-method&amp;gt;
      &amp;lt;http-method&amp;gt;OPTIONS&amp;lt;/http-method&amp;gt;
      &amp;lt;http-method&amp;gt;TRACE&amp;lt;/http-method&amp;gt;
      &amp;lt;http-method&amp;gt;GET&amp;lt;/http-method&amp;gt;
      &amp;lt;http-method&amp;gt;POST&amp;lt;/http-method&amp;gt;
   &amp;lt;/web-resource-collection&amp;gt;
   &amp;lt;auth-constraint /&amp;gt;
   &amp;lt;user-data-constraint&amp;gt;
      &amp;lt;transport-guarantee&amp;gt;NONE&amp;lt;/transport-guarantee&amp;gt;
   &amp;lt;/user-data-constraint&amp;gt;
&amp;lt;/security-constraint&amp;gt;&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/7976513903988065267/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/7976513903988065267?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7976513903988065267'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7976513903988065267'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/implement-security-in-webxml_13.html' title='Implement Security in Web.xml'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-2611472765957956086</id><published>2012-01-12T12:51:00.000-08:00</published><updated>2012-05-18T00:51:49.143-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Advance Java"/><title type='text'>Implement Security in Web.xml</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;div class=&quot;syntaxhighlighter &quot; id=&quot;highlighter_922412&quot;&gt;

&lt;/div&gt;
&lt;strong&gt;Use Case&lt;/strong&gt;2:
 HTTP GET operation on a set of web resources should be accessible only 
by an user with the role &quot;Employee&quot;. We will achieve this with the 
specification of authorization constraints (&lt;em&gt;auth-constraint&lt;/em&gt; element with the &lt;em&gt;role-name&lt;/em&gt; element).&lt;br /&gt;

&lt;div class=&quot;syntaxhighlighter &quot; id=&quot;highlighter_13256&quot;&gt;
&lt;div class=&quot;lines&quot;&gt;

  &lt;pre&gt;&amp;lt;security-constraint&amp;gt;
   &amp;lt;display-name&amp;gt;Restricted GET To Employees&amp;lt;/display-name&amp;gt;
   &amp;lt;web-resource-collection&amp;gt;
      &amp;lt;web-resource-name&amp;gt;Restricted Access - Get Only&amp;lt;/web-resource-name&amp;gt;
      &amp;lt;url-pattern&amp;gt;/restricted/employee/*&amp;lt;/url-pattern&amp;gt;
      &amp;lt;http-method&amp;gt;GET&amp;lt;/http-method&amp;gt;
   &amp;lt;/web-resource-collection&amp;gt;
   &amp;lt;auth-constraint&amp;gt;
      &amp;lt;role-name&amp;gt;Employee&amp;lt;/role-name&amp;gt;
   &amp;lt;/auth-constraint&amp;gt;
   &amp;lt;user-data-constraint&amp;gt;
      &amp;lt;transport-guarantee&amp;gt;NONE&amp;lt;/transport-guarantee&amp;gt;
   &amp;lt;/user-data-constraint&amp;gt;
&amp;lt;/security-constraint&amp;gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;strong&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/2611472765957956086/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/2611472765957956086?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2611472765957956086'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/2611472765957956086'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/implement-security-in-webxml_12.html' title='Implement Security in Web.xml'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-1586945392039339606</id><published>2012-01-11T00:50:00.000-08:00</published><updated>2012-05-18T00:51:02.220-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Advance Java"/><title type='text'>Implement Security in Web.xml</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
In this section, we will look at specifying the security constraints for multiple use cases.&lt;br /&gt;

&lt;br /&gt;
&lt;strong&gt;Use Case1&lt;/strong&gt;: We would like to define a set of web resources that will have &lt;strong&gt;&lt;em&gt;unchecked&lt;/em&gt;&lt;/strong&gt; access. We will achieve this by omitting the authorization constrainsts (auth-constraint element).&lt;br /&gt;
&lt;div class=&quot;syntaxhighlighter &quot; id=&quot;highlighter_922412&quot;&gt;

  &lt;pre&gt;&amp;lt;security-constraint&amp;gt;
   &amp;lt;web-resource-collection&amp;gt;
     &amp;lt;web-resource-name&amp;gt;All Access&amp;lt;/web-resource-name&amp;gt;
     &amp;lt;url-pattern&amp;gt;/unchecked/*&amp;lt;/url-pattern&amp;gt;
     &amp;lt;http-method&amp;gt;DELETE&amp;lt;/http-method&amp;gt;
     &amp;lt;http-method&amp;gt;PUT&amp;lt;/http-method&amp;gt;
     &amp;lt;http-method&amp;gt;HEAD&amp;lt;/http-method&amp;gt;
     &amp;lt;http-method&amp;gt;OPTIONS&amp;lt;/http-method&amp;gt;
     &amp;lt;http-method&amp;gt;TRACE&amp;lt;/http-method&amp;gt;
     &amp;lt;http-method&amp;gt;GET&amp;lt;/http-method&amp;gt;
     &amp;lt;http-method&amp;gt;POST&amp;lt;/http-method&amp;gt;
   &amp;lt;/web-resource-collection&amp;gt;
   &amp;lt;user-data-constraint&amp;gt;
     &amp;lt;transport-guarantee&amp;gt;NONE&amp;lt;/transport-guarantee&amp;gt;
   &amp;lt;/user-data-constraint&amp;gt;
&amp;lt;/security-constraint&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/1586945392039339606/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/1586945392039339606?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1586945392039339606'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1586945392039339606'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/implement-security-in-webxml_11.html' title='Implement Security in Web.xml'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-1712997172355960909</id><published>2012-01-10T00:49:00.000-08:00</published><updated>2012-05-18T00:49:47.709-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Advance Java"/><title type='text'>Implement Security in web.xml</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
The deployment descriptor, web.xml is the most important Java EE 
configuration piece of Java EE Web applications. The security 
configuration in this descriptor drives the semantics and operation of 
web container security. Hence it is very critical that web developers 
and administrators understand the various combinations possible in the 
security configuration in this descriptor.&lt;br /&gt;
&lt;h2&gt;
Security Constraints&lt;/h2&gt;
Security
 Constraints are least understood by web developers, even though they 
are critical for the security of Java EE Web applications. Specifying a 
combination of URL patterns, HTTP methods, roles and transport 
constraints can be daunting to a programmer or administrator. It is 
important to realize that any combination that was intended to be secure
 but was not specified via security constraints, will mean that the web 
container will allow those requests. Security Constraints consist of Web
 Resource Collections (URL patterns, HTTP methods), Authorization 
Constraint (role names) and User Data Constraints (whether the web 
request needs to be received over a protected transport such as TLS). We will told you using 3 use cases.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/1712997172355960909/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/1712997172355960909?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1712997172355960909'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/1712997172355960909'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/implement-security-in-webxml.html' title='Implement Security in web.xml'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7735704189187983707.post-7052914251384585632</id><published>2012-01-09T02:57:00.000-08:00</published><updated>2012-05-11T02:57:54.225-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Articles - Java"/><title type='text'>Downcasting in Java</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;h3 class=&quot;title&quot;&gt;
Downcasting&lt;/h3&gt;
&lt;div class=&quot;itemizedlist&quot;&gt;
&lt;ul class=&quot;itemizedlist&quot; type=&quot;disc&quot;&gt;
&lt;li class=&quot;listitem&quot;&gt;&lt;div class=&quot;simpara&quot;&gt;

An upcast reference can be &lt;span class=&quot;emphasis&quot;&gt;&lt;em&gt;downcast&lt;/em&gt;&lt;/span&gt; to a subclass through explicit casting. For example:
&lt;/div&gt;
&lt;pre class=&quot;programlisting&quot;&gt;Employee e = new Manager(...);
// ...
Manager m = (Manager) e;&lt;/pre&gt;
&lt;div class=&quot;itemizedlist&quot;&gt;
&lt;ul class=&quot;itemizedlist&quot; type=&quot;circle&quot;&gt;
&lt;li class=&quot;listitem&quot;&gt;
The object referenced must actually be a member of the downcast type, or else a &lt;code class=&quot;literal&quot;&gt;ClassCastException&lt;/code&gt; run-time exception occurs.
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/li&gt;
&lt;li class=&quot;listitem&quot;&gt;&lt;div class=&quot;simpara&quot;&gt;

You can test if an object is a member of a specific type using the &lt;code class=&quot;literal&quot;&gt;instanceof&lt;/code&gt; operator, for example:
&lt;/div&gt;
&lt;pre class=&quot;literallayout&quot;&gt;if (obj instanceof Manager) { // We&#39;ve got a Manager object }&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;pre class=&quot;programlisting&quot;&gt;public class EmployeeDemo {
    public static void main(String[] args) {
        final Employee e1 = new Employee(&quot;John&quot;, &quot;555-12-345&quot;, &quot;john@company.com&quot;);
        final Employee e2 = new Employee(&quot;456-78-901&quot;, 1974);
        e2.setName(&quot;Tom&quot;);
        Employee em = new Manager(&quot;Bob&quot;, &quot;345-11-987&quot;, &quot;Development&quot;);

        Employee.setBaseVacationDays(15);
        e2.setExtraVacationDays(5);
        em.setExtraVacationDays(10);

        if (em instanceof Manager) {
            Manager m = (Manager) em;
            m.setResponsibility(&quot;Operations&quot;);
        }

        e1.print(&quot;COOL EMPLOYEE&quot;);
        e2.print(&quot;START OF EMPLOYEE&quot;, &quot;END OF EMPLOYEE&quot;);
        em.print(&quot;BIG BOSS&quot;);
    }
}&lt;/pre&gt;
This would print:&lt;br /&gt;
&lt;pre class=&quot;screen&quot;&gt;BIG BOSS
Name: Bob
SSN: 345-11-987
Email Address: null
Year Of Birth: 0
Vacation Days: 25
Responsibility: Operations&lt;/pre&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bestjavapractices.blogspot.com/feeds/7052914251384585632/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7735704189187983707/7052914251384585632?isPopup=true' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7052914251384585632'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7735704189187983707/posts/default/7052914251384585632'/><link rel='alternate' type='text/html' href='http://bestjavapractices.blogspot.com/2012/01/downcasting-in-java.html' title='Downcasting in Java'/><author><name>Amit Kumar Mangal</name><uri>http://www.blogger.com/profile/16621590399073412249</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='26' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcuFtnSTAsdIv6TCrRAeHFPEJ0b9War6N-TT3uESNtExFzUTJhn-e6BH6G2IuMl0YcDeWMV0GQD8KN91gWRjrrA3zBVc4YU9dkjENPURKhF3VR8F8MckCq1gJY3h7TeA/s220/MyOffice.JPG'/></author><thr:total>0</thr:total></entry></feed>