tag:blogger.com,1999:blog-81935828553232023352024-03-12T19:07:51.224-07:00What I learnt today!!!It's all about the techi stuff i'll be dealing with on day to day basis.kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.comBlogger35125tag:blogger.com,1999:blog-8193582855323202335.post-19826859357882443502017-02-08T06:50:00.000-08:002019-11-18T21:50:30.675-08:00The Garbage Collector [JAVA]<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Java Memory Management, with its built-in garbage collection, is one of the language’s finest achievements. It allows developers to create new objects without worrying explicitly about memory allocation and deallocation, because the garbage collector automatically reclaims memory for reuse. This enables faster development with less boilerplate code, while eliminating memory leaks and other memory-related problems. At least in theory.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Ironically, Java garbage collection seems to work too well, creating and removing too many objects. Most memory-management issues are solved, but often at the cost of creating serious performance problems. Making garbage collection adaptable to all kinds of situations has led to a complex and hard-to-optimize system. In order to wrap your head around garbage collection, you need first to understand how memory management works in a Java Virtual Machine (JVM).</div>
<h2 id="how-garbage-collection-really-works" style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 2rem; font-stretch: inherit; font-variant-numeric: inherit; font-weight: 300; line-height: 1.5; margin: 0px 0px 1.5rem; padding: 0px; vertical-align: baseline;">
How Garbage Collection Really Works</h2>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Many people think garbage collection collects and discards dead objects. In reality, Java garbage collection is doing the opposite! Live objects are tracked and everything else designated garbage. As you’ll see, this fundamental misunderstanding can lead to many performance problems.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Let’s start with the heap, which is the area of memory used for dynamic allocation. In most configurations the operating system allocates the heap in advance to be managed by the JVM while the program is running. This has a couple of important ramifications:</div>
<ul style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; list-style: none; margin: 0px 0px 1.5rem 1rem; padding: 0px 0px 0px 0.25rem; vertical-align: baseline;">
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; position: relative; vertical-align: baseline;">Object creation is faster because global synchronization with the operating system is not needed for every single object. An allocation simply claims some portion of a memory array and moves the offset pointer forward (see Figure 2.1). The next allocation starts at this offset and claims the next portion of the array.</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 1.25rem 0px 0px; padding: 0px; position: relative; vertical-align: baseline;">When an object is no longer used, the garbage collector reclaims the underlying memory and reuses it for future object allocation. This means there is no explicit deletion and no memory is given back to the operating system.</li>
</ul>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
<img alt="New objects are simply allocated at the end of the used heap" src="https://assets.dynatrace.com/content/dam/dynatrace/javabook/allocation.png" style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; max-width: 100%; padding: 0px; vertical-align: baseline;" /></div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Figure 2.1: New objects are simply allocated at the end of the used heap.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
All objects are allocated on the heap area managed by the JVM. Every item that the developer uses is treated this way, including class objects, static variables, and even the code itself. As long as an object is being referenced, the JVM considers it alive. Once an object is no longer referenced and therefore is not reachable by the application code, the garbage collector removes it and reclaims the unused memory. As simple as this sounds, it raises a question: what is the first reference in the tree?</div>
<h2 id="garbage-collection-rootsthe-source-of-all-object-trees" style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 2rem; font-stretch: inherit; font-variant-numeric: inherit; font-weight: 300; line-height: 1.5; margin: 0px 0px 1.5rem; padding: 0px; vertical-align: baseline;">
Garbage-Collection Roots—The Source of All Object Trees</h2>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Every object tree must have one or more root objects. As long as the application can reach those roots, the whole tree is reachable. But when are those root objects considered reachable? Special objects called garbage-collection roots (GC roots; see Figure 2.2) are always reachable and so is any object that has a garbage-collection root at its own root.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
There are four kinds of GC roots in Java:</div>
<ul style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; list-style: none; margin: 0px 0px 1.5rem 1rem; padding: 0px 0px 0px 0.25rem; vertical-align: baseline;">
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; position: relative; vertical-align: baseline;"><span style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Local variables</span> are kept alive by the stack of a thread. This is not a real object virtual reference and thus is not visible. For all intents and purposes, local variables are GC roots.</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 1.25rem 0px 0px; padding: 0px; position: relative; vertical-align: baseline;"><span style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Active Java threads</span> are always considered live objects and are therefore GC roots. This is especially important for thread local variables.</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 1.25rem 0px 0px; padding: 0px; position: relative; vertical-align: baseline;"><span style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">Static variables</span> are referenced by their classes. This fact makes them de facto GC roots. Classes themselves can be garbage-collected, which would remove all referenced static variables. This is of special importance when we use application servers, <a href="http://www.wikipedia.com/osgi" style="border: 0px; box-sizing: inherit; color: #00a1b2; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">OSGi containers</a> or class loaders in general. We will discuss the related problems in the Problem Patterns section.</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 1.25rem 0px 0px; padding: 0px; position: relative; vertical-align: baseline;"><span style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">JNI References</span> are Java objects that the native code has created as part of a JNI call. Objects thus created are treated specially because the JVM does not know if it is being referenced by the native code or not. Such objects represent a very special form of GC root, which we will examine in more detail in the Problem Patterns section below.</li>
</ul>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
<img alt="GC Roots are objects that are themselves referenced by the JVM and thus keep every other object from being garbage collected." src="https://assets.dynatrace.com/content/dam/dynatrace/javabook/gc-roots.png" style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; max-width: 100%; padding: 0px; vertical-align: baseline;" /></div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Figure 2.2: GC roots are objects that are themselves referenced by the JVM and thus keep every other object from being garbage-collected.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Therefore, a simple Java application has the following GC roots:</div>
<ul style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; list-style: none; margin: 0px 0px 1.5rem 1rem; padding: 0px 0px 0px 0.25rem; vertical-align: baseline;">
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; position: relative; vertical-align: baseline;">Local variables in the main method</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 1.25rem 0px 0px; padding: 0px; position: relative; vertical-align: baseline;">The main thread</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 1.25rem 0px 0px; padding: 0px; position: relative; vertical-align: baseline;">Static variables of the main class</li>
</ul>
<h2 id="marking-and-sweeping-away-garbage" style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 2rem; font-stretch: inherit; font-variant-numeric: inherit; font-weight: 300; line-height: 1.5; margin: 0px 0px 1.5rem; padding: 0px; vertical-align: baseline;">
Marking and Sweeping Away Garbage</h2>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
To determine which objects are no longer in use, the JVM intermittently runs what is very aptly called a <a href="https://en.wikipedia.org/wiki/Garbage_collection_(computer_science)" style="border: 0px; box-sizing: inherit; color: #00a1b2; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">mark-and-sweep algorithm</a>. As you might intuit, it’s a straightforward, two-step process:</div>
<ol style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; list-style-image: initial; list-style-position: initial; margin: 0px 0px 1.5rem 1rem; padding: 0px 0px 0px 0.25rem; vertical-align: baseline;">
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">The algorithm traverses all object references, starting with the GC roots, and marks every object found as alive.</li>
<li style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;">All of the heap memory that is not occupied by marked objects is reclaimed. It is simply marked as free, essentially swept free of unused objects.</li>
</ol>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Garbage collection is intended to remove the cause for classic memory leaks: unreachable-but-not-deleted objects in memory. However, this works only for memory leaks in the original sense. It’s possible to have unused objects that are still reachable by an application because the developer simply forgot to dereference them. Such objects cannot be garbage-collected. Even worse, such a logical memory leak cannot be detected by any software (see Figure 2.3). Even the best analysis software can only highlight suspicious objects. We will examine memory leak analysis in the Analyzing the Performance Impact of Memory Utilization and Garbage Collection section, below.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
<img alt="When objects are no longer referenced directly or indirectly by a GC root, they will be removed. There are no classic memory leaks. Analysis cannot really identify memory leaks, it can only hint at suspicious objects" src="https://assets.dynatrace.com/content/dam/dynatrace/javabook/gc-roots-with-memory-leak.png" style="border: 0px; box-sizing: inherit; font-family: inherit; font-size: inherit; font-stretch: inherit; font-style: inherit; font-variant: inherit; font-weight: inherit; line-height: inherit; margin: 0px; max-width: 100%; padding: 0px; vertical-align: baseline;" /></div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
Figure 2.3: When objects are no longer referenced directly or indirectly by a GC root, they will be removed. There are no classic memory leaks. Analysis cannot really identify memory leaks; it can only point out suspicious objects.</div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"><b>Reference: </b></span><span style="font-family: "Courier New", Courier, monospace; font-size: x-small;">http://www.journaldev.com/2856/java-jvm-memory-model-memory-management-in-java</span></div>
<div style="background-color: white; border: 0px; box-sizing: inherit; color: #454646; font-family: BerninaSans, Arial, sans-serif; font-size: 16px; font-stretch: inherit; font-variant-numeric: inherit; line-height: inherit; margin-bottom: 1.5rem; padding: 0px; vertical-align: baseline;">
<br /></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-52295594320000628292017-02-02T22:34:00.000-08:002017-02-02T22:34:14.740-08:00Compile Time Constants in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<h1 class="entry-title" itemprop="headline" style="box-sizing: border-box; color: black; font-size: 30px; font-weight: normal; line-height: 1.4; margin: 0px 0px 16px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Compile Time Constants in Java</span></h1>
<h1 style="background-color: white; margin-bottom: 0.67em; margin-top: 0.67em; text-shadow: white 1px 1px 0px;">
<header class="entry-header" style="box-sizing: border-box; font-family: Raleway, sans-serif; font-size: 16px; font-weight: normal;"></header><section class="widget widget_text" id="text-6" style="box-sizing: border-box; font-family: Raleway, sans-serif; font-size: 16px; font-weight: normal; word-wrap: break-word;"><div class="widget-wrap" style="box-sizing: border-box;">
<div class="textwidget" style="box-sizing: border-box;">
<div id="titlead" style="box-sizing: border-box; margin-bottom: 15px; margin-top: 5px;">
</div>
</div>
</div>
</section><div class="entry-content" itemprop="text" style="box-sizing: border-box; font-size: 16px; font-weight: normal;">
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">In this tutorial we’ll get a basic understanding of Compile Time Constants in Java programming language. This is not an exhaustive tutorial about compile time constants, but this should be enough for most people. We’ll understand what compile time constants are and how are they special. First lets go through the general rules regarding compile time constants.</span></div>
<ul style="text-align: left;">
<li><span style="font-family: Courier New, Courier, monospace;">They must be declared final</span></li>
<li><span style="font-family: Courier New, Courier, monospace;">They are of primitive data types or String</span></li>
<li><span style="font-family: Courier New, Courier, monospace;">They must be initialized with their declaration.</span></li>
<li><span style="font-family: Courier New, Courier, monospace;">Their value must be constant expression.</span></li>
</ul>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you don’t understand all of this, don’t worry, we’ll go through these rules now.</span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Final variables of primitive data types and Strings can be compile time constants. No other type of variables are compile time constants, not even the wrapper classes. They must also be initialized with their declaration, otherwise they’ll not be compile time constants. Lets take a few examples. All of the following are compile time constants,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> j <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">20</span>, k <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">30</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">String</span> s <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="st0" style="box-sizing: border-box; color: #036a07; font-stretch: normal; line-height: 1.4em;">"Hello"</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">float</span> f <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> 10.5f<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The following are not compile time constants,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">Integer</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//not a primitive or String</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> j <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//not final</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> k<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//not initialized with declaration</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" />k <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> l, m <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> l <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">20</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//both l and m are not compile time constants</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The last example might not be intuitive. We are initializing <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">l</span></code> and <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">m</span></code> in the same statement as their declaration, but they will not be compile time constants. Compile time constants have to be initialized right with their declaration and you can’t use a different variable in the middle of their declaration.</span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">The last rule about compile time constants is that the compiler must be able to deduce their value. So their value can be any expression which contains literals and other compile time constants. Lets have a look at some valid values for compile time constants,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span> <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">*</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">20</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//uses only literals</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> j <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> i<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//uses another compile time constant</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> k <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">*</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">20</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//mixing compile time constant and literal</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">If you use a variable in the expression to assign value to a final variable, then the compiler won’t be able to figure out the value of the final variable, so it will not be a compile time constant. The final variables given below are not compile time constants, because their value is not a constant expression,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> j <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> i<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//using a variable</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> k <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">Math</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">round</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">(</span><span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10.2</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">)</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//method call</span></span></div>
</div>
</div>
</h1>
<h2 style="box-sizing: border-box; font-size: 30px; font-weight: normal; line-height: 1.2; margin: 0px 0px 26px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></h2>
<h2 style="box-sizing: border-box; font-size: 30px; font-weight: normal; line-height: 1.2; margin: 0px 0px 26px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Compile Time Constants in Java! What’s So special About Them?</span></h2>
<h1 style="background-color: white; margin-bottom: 0.67em; margin-top: 0.67em; text-shadow: white 1px 1px 0px;">
<div class="entry-content" itemprop="text" style="box-sizing: border-box; font-size: 16px; font-weight: normal;">
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now that we know what compile time constants are, we’ll now see why are they special. As the name implies, compile time constants get special treatment by the compiler. The first example of the special treatment is that they are implicit downcasted (just like literals). If you assign an int variable with value 10 to a short variable, you’ll get an error, but if you assign a int which is a compile time constant with the value 10 to a short variable, that will compile fine. Here is an example,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> a <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">short</span> s1 <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> a<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//error</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> b <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">short</span> s2 <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> b<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//fine</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Since the compiler knows the value of compile time constants, the compler knows that the value of <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">b</span></code> is 10, so the compiler knows that the value of <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">b</span></code> is within the range of short data type. You can also use compile time constants as case values in switch-case statements as shown in the following example,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> a <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> b <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">switch</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">(</span>b<span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">)</span> <span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">{</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">case</span> a<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">:</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//some code</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">case</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">20</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">:</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="co1" style="box-sizing: border-box; color: #0066ff; font-stretch: normal; font-style: italic; line-height: 1.4em;">//some code</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">}</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">But remember, case values cannot be String or floating point numbers, so you can’t use compile time constants of float, double or String data types as case values.</span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Both the behaviors that we observed above i.e. implicit downcast and case labels are because of a common reason. Wherever you use a compile time constant, the compiler replaces their use with their actual value. So if we write this code,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">short</span> j <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> i<span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">System</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">out</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">println</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">(</span>i<span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">)</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">After compilation it will become,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> i <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="xtra ln-xtra" style="background-color: #ffff66; box-sizing: border-box; font-stretch: normal; line-height: 1.4em;"><span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">short</span> j <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span></span><span class="xtra ln-xtra" style="background-color: #ffff66; box-sizing: border-box; font-stretch: normal; line-height: 1.4em;"><span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">System</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">out</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">println</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">(</span><span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">)</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span></span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">As you can see, at both the places where we used <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">i</span></code>, the compiler replaced it with its value 10. This is the reason why they can be used as case values and that is why they are implicitly downcasted. This replacing of value has serious implications in some cases.</span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Suppose we have two classes in two different files like this,</span></div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">public</span> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">class</span> ConstantClass <span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">{</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">public</span> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">static</span> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">final</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">int</span> MY_CONST <span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">=</span> <span class="nu0" style="box-sizing: border-box; color: mediumblue; font-stretch: normal; line-height: 1.4em;">10</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">}</span></span></div>
</div>
<div class="codecolorer-container java mac-classic" style="border: 1px solid rgb(159, 159, 159); box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; margin-bottom: 10px; overflow: auto; white-space: nowrap; width: 435px;">
<div class="java codecolorer" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em; padding: 5px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">public</span> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">class</span> Main <span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">{</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">public</span> <span class="kw1" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">static</span> <span class="kw4" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">void</span> main<span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">(</span><span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">String</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">[</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">]</span> args<span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">)</span> <span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">{</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="kw3" style="box-sizing: border-box; color: blue; font-stretch: normal; line-height: 1.4em;">System</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">out</span>.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">println</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">(</span>ConstantClass.<span class="me1" style="box-sizing: border-box; color: #0000a2; font-stretch: normal; line-height: 1.4em;">MY_CONST</span><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">)</span><span class="sy0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">;</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /> <span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">}</span><br style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;" /><span class="br0" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">}</span></span></div>
</div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">Now if we compile both classes and run <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">Main</span></code> class, the output will be 10. Since <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">MY_CONST</span></code> is a compile time constant, so the compiler replaces it with its value in the main method. Now we go and change the value of <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">MY_CONST</span></code> to 20 and compile only <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">ConstantClass</span></code> class. Then we run the <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">Main</span></code> class (without compiling it again), the output will still be 10 i.e. the value of <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">MY_CONST</span></code> when <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">Main</span></code> class was compiled. We’ll have to recompile <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">Main</span></code> class to see the new value of <code class="codecolorer java default" style="background-color: #f1f1f1; box-sizing: border-box; font-size: 13px; font-stretch: normal; line-height: 1.4em; padding: 2px;"><span class="java" style="box-sizing: border-box; font-stretch: normal; line-height: 1.4em;">MY_CONST</span></code>as the output.</span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;">One more special treatment that compile time constants receive is that you can declare static compile time constants inside non-static inner classes. As you might know, non-static inner classes cannot have static members. But you are allowed to add static compile time constants to non-static inner classes.</span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
<div style="box-sizing: border-box; margin-bottom: 26px; padding: 0px;">
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><br /></span></div>
</div>
</h1>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-47726012908087468642016-07-27T08:25:00.005-07:002016-07-27T08:49:40.634-07:00Try out with Swagger and Jersey Rest <div dir="ltr" style="text-align: left;" trbidi="on">
Recently I had to try out few tools that can generate richer documentation for a Rest API.<br />
I have created a basic demo project that shows off many features of Swagger.<br />
I'm using Tomcat 7.0 , jdk 7.0 , Jersey 2.0 , Swagger 1.5x and eclipse as editor.<br />
<br />
Follow the steps and also you can copy the project from my <a href="https://github.com/thekant/jaxrs_swagger_rest_example">github</a> project.<br />
<h4 style="text-align: left;">
Step 1: Dependencies </h4>
<div>
<pre style="background: #ffffff; color: black;"><span style="color: #7f0055;"> <</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>javax.servlet<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>javax.servlet-api<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>3.1.0<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #3f7f59;"><!--</span><span style="color: #3f7f59;"> JAX-RS </span><span style="color: #3f7f59;">--></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>javax.ws.rs<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>javax.ws.rs-api<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>${jaxrs.version}<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #3f7f59;"><!--</span><span style="color: #3f7f59;"> Jersey 2.19 </span><span style="color: #3f7f59;">--></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>org.glassfish.jersey.containers<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>jersey-container-servlet<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>${jersey2.version}<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>org.glassfish.jersey.core<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>jersey-server<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>${jersey2.version}<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>org.glassfish.jersey.core<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>jersey-client<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>${jersey2.version}<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>org.glassfish.jersey.media<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>jersey-media-json-jackson<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>${jersey2.version}<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #3f7f59;"><!--</span><span style="color: #3f7f59;"> Swagger dependencies </span><span style="color: #3f7f59;">--></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>io.swagger<span style="color: #7f0055;"></</span><span style="color: #7f0055;">groupId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>swagger-jersey2-jaxrs<span style="color: #7f0055;"></</span><span style="color: #7f0055;">artifactId</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>1.5.0<span style="color: #7f0055;"></</span><span style="color: #7f0055;">version</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">dependency</span><span style="color: #7f0055;">></span>
</pre>
</div>
<h4 style="text-align: left;">
Step 2: Web.xml config</h4>
<div>
<pre style="background: #ffffff; color: black;"><span style="color: #7f0055;"> <</span><span style="color: #7f0055;">init-param</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">param-name</span><span style="color: #7f0055;">></span>jersey.config.server.provider.packages<span style="color: #7f0055;"></</span><span style="color: #7f0055;">param-name</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">param-value</span><span style="color: #7f0055;">></span>com.kant.rest,io.swagger.jaxrs.listing<span style="color: #7f0055;"></</span><span style="color: #7f0055;">param-value</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">init-param</span><span style="color: #7f0055;">></span>
Bootstrap class added to configure Swagger
<span style="color: #7f0055;"> <</span><span style="color: #7f0055;">servlet</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">servlet-name</span><span style="color: #7f0055;">></span>SwaggerBootstrap<span style="color: #7f0055;"></</span><span style="color: #7f0055;">servlet-name</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">servlet-class</span><span style="color: #7f0055;">></span>com.kant.rest.servlet.Bootstrap<span style="color: #7f0055;"></</span><span style="color: #7f0055;">servlet-class</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">load-on-startup</span><span style="color: #7f0055;">></span>2<span style="color: #7f0055;"></</span><span style="color: #7f0055;">load-on-startup</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">servlet</span><span style="color: #7f0055;">></span>
</pre>
<pre style="background: #ffffff; color: black;">Filter to enable CORS support for tomcat
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">filter</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">filter-name</span><span style="color: #7f0055;">></span>CorsFilter<span style="color: #7f0055;"></</span><span style="color: #7f0055;">filter-name</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">filter-class</span><span style="color: #7f0055;">></span>org.apache.catalina.filters.CorsFilter<span style="color: #7f0055;"></</span><span style="color: #7f0055;">filter-class</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">filter</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">filter-mapping</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">filter-name</span><span style="color: #7f0055;">></span>CorsFilter<span style="color: #7f0055;"></</span><span style="color: #7f0055;">filter-name</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"><</span><span style="color: #7f0055;">url-pattern</span><span style="color: #7f0055;">></span>/*<span style="color: #7f0055;"></</span><span style="color: #7f0055;">url-pattern</span><span style="color: #7f0055;">></span>
<span style="color: #7f0055;"></</span><span style="color: #7f0055;">filter-mapping</span><span style="color: #7f0055;">></span>
</pre>
</div>
<h4 style="text-align: left;">
Step 3: Use of Swagger Annotations</h4>
<div>
<pre style="background: #ffffff; color: black;">/**Service endpoint**/</pre>
<pre style="background: #ffffff; color: black;">@Api(value = "hello", description = "Endpoint for user management")
@Path("/hello")
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">class</span> HelloWorldService <span style="color: purple;">{</span>
<span style="color: #808030;">@</span>GET
<span style="color: #808030;">@</span>Path<span style="color: #808030;">(</span><span style="color: #0000e6;">"/{param}"</span><span style="color: #808030;">)</span>
<span style="color: #808030;">@</span>Produces<span style="color: #808030;">(</span>MediaType<span style="color: #808030;">.</span>TEXT_PLAIN<span style="color: #808030;">)</span>
<span style="color: #808030;">@</span>ApiOperation<span style="color: #808030;">(</span>value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Returns welcome message."</span><span style="color: #808030;">,</span> notes <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Returns a welcome message for the param name passed."</span><span style="color: #808030;">,</span> response <span style="color: #808030;">=</span> <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>class<span style="color: #808030;">)</span>
<span style="color: #808030;">@</span>ApiResponses<span style="color: #808030;">(</span>value <span style="color: #808030;">=</span> <span style="color: purple;">{</span>
<span style="color: #808030;">@</span>ApiResponse<span style="color: #808030;">(</span>code <span style="color: #808030;">=</span> <span style="color: #008c00;">200</span><span style="color: #808030;">,</span> message <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Successful retrieval of response"</span><span style="color: #808030;">,</span> response <span style="color: #808030;">=</span> <span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">.</span>class<span style="color: #808030;">)</span><span style="color: #808030;">,</span>
<span style="color: #808030;">@</span>ApiResponse<span style="color: #808030;">(</span>code <span style="color: #808030;">=</span> <span style="color: #008c00;">404</span><span style="color: #808030;">,</span> message <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Param passed is not good"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span>
<span style="color: #808030;">@</span>ApiResponse<span style="color: #808030;">(</span>code <span style="color: #808030;">=</span> <span style="color: #008c00;">500</span><span style="color: #808030;">,</span> message <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Internal server error"</span><span style="color: #808030;">)</span> <span style="color: purple;">}</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">public</span> Response getMsg<span style="color: #808030;">(</span><span style="color: #808030;">@</span>PathParam<span style="color: #808030;">(</span><span style="color: #0000e6;">"param"</span><span style="color: #808030;">)</span> <span style="color: #bb7977; font-weight: bold;">String</span> msg<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #0000e6;">"bad"</span><span style="color: #808030;">.</span>equalsIgnoreCase<span style="color: #808030;">(</span>msg<span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> Response<span style="color: #808030;">.</span>status<span style="color: #808030;">(</span>Status<span style="color: #808030;">.</span>BAD_REQUEST<span style="color: #808030;">)</span><span style="color: #808030;">.</span>build<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: #bb7977; font-weight: bold;">String</span> output <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Jersey say : "</span> <span style="color: #808030;">+</span> msg<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> Response<span style="color: #808030;">.</span>status<span style="color: #808030;">(</span><span style="color: #008c00;">200</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span>entity<span style="color: #808030;">(</span>output<span style="color: #808030;">)</span><span style="color: #808030;">.</span>build<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: #808030;">@</span>GET
<span style="color: #808030;">@</span>Path<span style="color: #808030;">(</span><span style="color: #0000e6;">"/person/{id}"</span><span style="color: #808030;">)</span>
<span style="color: #808030;">@</span>Produces<span style="color: #808030;">(</span>MediaType<span style="color: #808030;">.</span>APPLICATION_JSON<span style="color: #808030;">)</span>
<span style="color: #808030;">@</span>ApiOperation<span style="color: #808030;">(</span>value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Returns Person."</span><span style="color: #808030;">,</span> notes <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Returns a person details for value passed."</span><span style="color: #808030;">,</span> response <span style="color: #808030;">=</span> Person<span style="color: #808030;">.</span>class<span style="color: #808030;">)</span>
<span style="color: #808030;">@</span>ApiResponses<span style="color: #808030;">(</span>value <span style="color: #808030;">=</span> <span style="color: purple;">{</span>
<span style="color: #808030;">@</span>ApiResponse<span style="color: #808030;">(</span>code <span style="color: #808030;">=</span> <span style="color: #008c00;">200</span><span style="color: #808030;">,</span> message <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Successful retrieval of response"</span><span style="color: #808030;">,</span> response <span style="color: #808030;">=</span> Person<span style="color: #808030;">.</span>class<span style="color: #808030;">)</span><span style="color: #808030;">,</span>
<span style="color: #808030;">@</span>ApiResponse<span style="color: #808030;">(</span>code <span style="color: #808030;">=</span> <span style="color: #008c00;">404</span><span style="color: #808030;">,</span> message <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Param passed is not good"</span><span style="color: #808030;">)</span><span style="color: #808030;">,</span>
<span style="color: #808030;">@</span>ApiResponse<span style="color: #808030;">(</span>code <span style="color: #808030;">=</span> <span style="color: #008c00;">500</span><span style="color: #808030;">,</span> message <span style="color: #808030;">=</span> <span style="color: #0000e6;">"Internal server error"</span><span style="color: #808030;">)</span> <span style="color: purple;">}</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">public</span> Response getPerson<span style="color: #808030;">(</span><span style="color: #808030;">@</span>PathParam<span style="color: #808030;">(</span><span style="color: #0000e6;">"id"</span><span style="color: #808030;">)</span> <span style="color: #bb7977; font-weight: bold;">String</span> id<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
Person person <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">new</span> Person<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #0000e6;">"1"</span><span style="color: #808030;">.</span>equals<span style="color: #808030;">(</span>id<span style="color: #808030;">)</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
person<span style="color: #808030;">.</span>setAge<span style="color: #808030;">(</span><span style="color: #008c00;">27</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setEmailId<span style="color: #808030;">(</span><span style="color: #0000e6;">"itsmeshashi@outlook.com"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setFirstName<span style="color: #808030;">(</span><span style="color: #0000e6;">"shashi"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setLastName<span style="color: #808030;">(</span><span style="color: #0000e6;">"kant"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setPhone<span style="color: #808030;">(</span><span style="color: #0000e6;">"532600021"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span> <span style="color: maroon; font-weight: bold;">else</span> <span style="color: maroon; font-weight: bold;">if</span> <span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #808030;">.</span>parseInt<span style="color: #808030;">(</span>id<span style="color: #808030;">)</span> <span style="color: #808030;"><</span> <span style="color: #008c00;">5</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
person<span style="color: #808030;">.</span>setAge<span style="color: #808030;">(</span><span style="color: #008c00;">22</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setEmailId<span style="color: #808030;">(</span><span style="color: #0000e6;">"dummayMail@fake.com"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setFirstName<span style="color: #808030;">(</span><span style="color: #0000e6;">"foo"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setLastName<span style="color: #808030;">(</span><span style="color: #0000e6;">"bar"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
person<span style="color: #808030;">.</span>setPhone<span style="color: #808030;">(</span><span style="color: #0000e6;">"2343-243-234"</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span> <span style="color: maroon; font-weight: bold;">else</span>
<span style="color: maroon; font-weight: bold;">return</span> Response<span style="color: #808030;">.</span>status<span style="color: #808030;">(</span>Status<span style="color: #808030;">.</span>BAD_REQUEST<span style="color: #808030;">)</span><span style="color: #808030;">.</span>build<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">return</span> Response<span style="color: #808030;">.</span>status<span style="color: #808030;">(</span><span style="color: #008c00;">200</span><span style="color: #808030;">)</span><span style="color: #808030;">.</span>entity<span style="color: #808030;">(</span>person<span style="color: #808030;">)</span><span style="color: #808030;">.</span>build<span style="color: #808030;">(</span><span style="color: #808030;">)</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span>
</pre>
<br />
/**Model class**/<br />
<pre style="background: #ffffff; color: black;">@ApiModel(value = "Person", description = "stores person details")
@XmlRootElement
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">class</span> Person <span style="color: purple;">{</span>
<span style="color: #808030;">@</span>XmlElement
<span style="color: #808030;">@</span>ApiModelProperty<span style="color: #808030;">(</span>position <span style="color: #808030;">=</span> <span style="color: #008c00;">1</span><span style="color: #808030;">,</span> required <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">true</span><span style="color: #808030;">,</span> value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"user's firstname containing only lowercase letters"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">String</span> firstName<span style="color: purple;">;</span>
<span style="color: #808030;">@</span>XmlElement
<span style="color: #808030;">@</span>ApiModelProperty<span style="color: #808030;">(</span>position <span style="color: #808030;">=</span> <span style="color: #008c00;">2</span><span style="color: #808030;">,</span> required <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">true</span><span style="color: #808030;">,</span> value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"user's last name containing only lowercase letters"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">String</span> lastName<span style="color: purple;">;</span>
<span style="color: #808030;">@</span>XmlElement
<span style="color: #808030;">@</span>ApiModelProperty<span style="color: #808030;">(</span>position <span style="color: #808030;">=</span> <span style="color: #008c00;">3</span><span style="color: #808030;">,</span> required <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">true</span><span style="color: #808030;">,</span> value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"email id "</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">String</span> emailId<span style="color: purple;">;</span>
<span style="color: #808030;">@</span>XmlElement
<span style="color: #808030;">@</span>ApiModelProperty<span style="color: #808030;">(</span>position <span style="color: #808030;">=</span> <span style="color: #008c00;">4</span><span style="color: #808030;">,</span> required <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">true</span><span style="color: #808030;">,</span> value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"phone number"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">String</span> phone<span style="color: purple;">;</span>
<span style="color: #808030;">@</span>XmlElement
<span style="color: #808030;">@</span>ApiModelProperty<span style="color: #808030;">(</span>position <span style="color: #808030;">=</span> <span style="color: #008c00;">5</span><span style="color: #808030;">,</span> required <span style="color: #808030;">=</span> <span style="color: maroon; font-weight: bold;">true</span><span style="color: #808030;">,</span> value <span style="color: #808030;">=</span> <span style="color: #0000e6;">"user's age"</span><span style="color: #808030;">)</span>
<span style="color: maroon; font-weight: bold;">private</span> <span style="color: #bb7977; font-weight: bold;">Integer</span> age<span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977; font-weight: bold;">String</span> getFirstName<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> firstName<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977;">void</span> setFirstName<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> firstName<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">this</span><span style="color: #808030;">.</span>firstName <span style="color: #808030;">=</span> firstName<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977; font-weight: bold;">String</span> getLastName<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> lastName<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977;">void</span> setLastName<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> lastName<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">this</span><span style="color: #808030;">.</span>lastName <span style="color: #808030;">=</span> lastName<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977; font-weight: bold;">String</span> getEmailId<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> emailId<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977;">void</span> setEmailId<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> emailId<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">this</span><span style="color: #808030;">.</span>emailId <span style="color: #808030;">=</span> emailId<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977; font-weight: bold;">String</span> getPhone<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> phone<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977;">void</span> setPhone<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> phone<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">this</span><span style="color: #808030;">.</span>phone <span style="color: #808030;">=</span> phone<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977; font-weight: bold;">Integer</span> getAge<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">return</span> age<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977;">void</span> setAge<span style="color: #808030;">(</span><span style="color: #bb7977; font-weight: bold;">Integer</span> age<span style="color: #808030;">)</span> <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">this</span><span style="color: #808030;">.</span>age <span style="color: #808030;">=</span> age<span style="color: purple;">;</span>
<span style="color: purple;">}</span>
<span style="color: purple;">}</span>
</pre>
<br />
<br /></div>
<h4 style="text-align: left;">
Screenshots:</h4>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-C5zI2Uvdyjw/V5jRpVNXWEI/AAAAAAAAEIU/DUQL4jCP7hMZuBRk0eLKqcI7yipqoN2XgCLcB/s1600/Capture1.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="332" src="https://3.bp.blogspot.com/-C5zI2Uvdyjw/V5jRpVNXWEI/AAAAAAAAEIU/DUQL4jCP7hMZuBRk0eLKqcI7yipqoN2XgCLcB/s640/Capture1.PNG" width="640" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://3.bp.blogspot.com/-v-oZExA8m3E/V5jRpTkmJDI/AAAAAAAAEIQ/i-NCkrWT4-0mLdNlFhDROQd3lIq5DrZJwCLcB/s1600/Capture2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="396" src="https://3.bp.blogspot.com/-v-oZExA8m3E/V5jRpTkmJDI/AAAAAAAAEIQ/i-NCkrWT4-0mLdNlFhDROQd3lIq5DrZJwCLcB/s640/Capture2.PNG" width="640" /></a></div>
<br />
<br />
<br />
<h4 style="text-align: left;">
Notes:</h4>
1. <a href="https://github.com/swagger-api/swagger-ui">Swagger-ui </a>: copy zipped version, unzip and open ./dist/index.html to launch Swagger UI in a browser. Now Explore your local api. Make sure CORS filter is added to your web.xml according to the server.<br />
2. <a href="http://editor.swagger.io/#/">Swagger editor</a> : try out live demo version and copy your swagger.json to test the editor right away<br />
3. Other <a href="http://swagger.io/tools/">Swagger tools</a> .<br />
<br />
<br />
<h4 style="text-align: left;">
References:</h4>
<span style="font-size: x-small;">1. https://github.com/swagger-api/swagger-core/wiki/Swagger-Core-Jersey-2.X-Project-Setup-1.5</span><br />
<span style="font-size: x-small;">2. <a href="http://stackoverflow.com/questions/16296145/set-cors-header-in-tomcat/18850438#18850438">http://stackoverflow.com/questions/16296145/set-cors-header-in-tomcat/18850438#18850438</a> </span><br />
<br />
<h4 style="text-align: left;">
Project Page: <a href="https://github.com/thekant/jaxrs_swagger_rest_example"><span style="font-family: "courier new" , "courier" , monospace;">https://github.com/thekant/jaxrs_swagger_rest_example</span></a></h4>
<br />
<br />
<br /></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-65998080752450715922016-03-30T10:09:00.002-07:002016-03-30T10:09:23.106-07:00Statistics : calculating mean median mode and standard deviation<div dir="ltr" style="text-align: left;" trbidi="on">
<pre style="background: rgb(0, 0, 0); color: #d1d1d1;"><span style="color: #90b0e0;">/**</span></pre>
<pre style="background: rgb(0, 0, 0); color: #d1d1d1;"><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">package</span><span style="color: #008073;"> com</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">kant</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">hackerrank</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">statistics</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">ArrayList</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">HashMap</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">Iterator</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">List</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">Map</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">Scanner</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">util</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">TreeMap</span><span style="color: #b060b0;">;</span>
<span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">@author</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">shaskant</span><span style="color: #90b0e0;"></span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"></span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">public</span> <span style="color: #e66170; font-weight: bold;">class</span> Challenge1 <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">double</span> mean <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">double</span> median <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">int</span> mode <span style="color: #d2cd86;">=</span> <span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">.</span>MAX_VALUE<span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">double</span> deviationStd <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">@param</span><span style="color: #90b0e0;"> args</span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">public</span> <span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">void</span> main<span style="color: #d2cd86;">(</span><span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #d2cd86;">[</span><span style="color: #d2cd86;">]</span> args<span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #d2cd86;">@</span>SuppressWarnings<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"resource"</span><span style="color: #d2cd86;">)</span>
Scanner scanner <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">new</span> Scanner<span style="color: #d2cd86;">(</span><span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>in<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span> inputSize <span style="color: #d2cd86;">=</span> scanner<span style="color: #d2cd86;">.</span>nextInt<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
Map<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">,</span> <span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span> store <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">new</span> TreeMap<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">,</span> <span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span><span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span> count <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">for</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977;">int</span> inputC <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span> inputC <span style="color: #d2cd86;"><</span> inputSize<span style="color: #b060b0;">;</span> inputC<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977;">int</span> inputN <span style="color: #d2cd86;">=</span> scanner<span style="color: #d2cd86;">.</span>nextInt<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
mean <span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">=</span> inputN<span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>store<span style="color: #d2cd86;">.</span>containsKey<span style="color: #d2cd86;">(</span>inputN<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
count <span style="color: #d2cd86;">=</span> store<span style="color: #d2cd86;">.</span>get<span style="color: #d2cd86;">(</span>inputN<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>intValue<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
store<span style="color: #d2cd86;">.</span>put<span style="color: #d2cd86;">(</span>inputN<span style="color: #d2cd86;">,</span> count <span style="color: #d2cd86;">+</span> <span style="color: #008c00;">1</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span> <span style="color: #e66170; font-weight: bold;">else</span> <span style="color: #b060b0;">{</span>
store<span style="color: #d2cd86;">.</span>put<span style="color: #d2cd86;">(</span>inputN<span style="color: #d2cd86;">,</span> <span style="color: #008c00;">1</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span>
mean <span style="color: #d2cd86;">=</span> mean <span style="color: #d2cd86;">/</span> inputSize<span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%.1f</span><span style="color: teal;">\n</span><span style="color: #00c4c4;">"</span><span style="color: #d2cd86;">,</span> mean<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
calculateModeAndDeviation<span style="color: #d2cd86;">(</span>store<span style="color: #d2cd86;">,</span> inputSize<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%.1f</span><span style="color: teal;">\n</span><span style="color: #00c4c4;">"</span><span style="color: #d2cd86;">,</span> median<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>println<span style="color: #d2cd86;">(</span>mode<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%.1f</span><span style="color: teal;">\n</span><span style="color: #00c4c4;">"</span><span style="color: #d2cd86;">,</span> deviationStd<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">@param</span><span style="color: #90b0e0;"> store</span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">private</span> <span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">void</span> calculateModeAndDeviation<span style="color: #d2cd86;">(</span>Map<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">,</span> <span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span> store<span style="color: #d2cd86;">,</span>
<span style="color: #bb7977;">int</span> inputSize<span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
Map<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">,</span> <span style="color: #bb7977; font-weight: bold;">List</span><span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span><span style="color: #d2cd86;">></span> map <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">new</span> HashMap<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">,</span> <span style="color: #bb7977; font-weight: bold;">List</span><span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span><span style="color: #d2cd86;">></span><span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
Iterator<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span> iterator <span style="color: #d2cd86;">=</span> store<span style="color: #d2cd86;">.</span>keySet<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>iterator<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span> maxKey <span style="color: #d2cd86;">=</span> <span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">.</span>MIN_VALUE<span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">boolean</span> isOdd <span style="color: #d2cd86;">=</span> inputSize <span style="color: #d2cd86;">%</span> <span style="color: #008c00;">2</span> <span style="color: #d2cd86;">!</span><span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span> inputC <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span><span style="color: #d2cd86;">[</span><span style="color: #d2cd86;">]</span> data <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">new</span> <span style="color: #bb7977;">int</span><span style="color: #d2cd86;">[</span>inputSize<span style="color: #d2cd86;">]</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">while</span> <span style="color: #d2cd86;">(</span>iterator<span style="color: #d2cd86;">.</span>hasNext<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977;">int</span> next <span style="color: #d2cd86;">=</span> iterator<span style="color: #d2cd86;">.</span>next<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>intValue<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">Integer</span> nextVal <span style="color: #d2cd86;">=</span> store<span style="color: #d2cd86;">.</span>get<span style="color: #d2cd86;">(</span>next<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
deviationStd <span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">=</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977;">double</span><span style="color: #d2cd86;">)</span> nextVal <span style="color: #d2cd86;">*</span> <span style="color: #bb7977; font-weight: bold;">Math</span><span style="color: #d2cd86;">.</span>pow<span style="color: #d2cd86;">(</span>next <span style="color: #d2cd86;">-</span> mean<span style="color: #d2cd86;">,</span> <span style="color: #008c00;">2</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>map<span style="color: #d2cd86;">.</span>containsKey<span style="color: #d2cd86;">(</span>nextVal<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
map<span style="color: #d2cd86;">.</span>get<span style="color: #d2cd86;">(</span>nextVal<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>add<span style="color: #d2cd86;">(</span>next<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span> <span style="color: #e66170; font-weight: bold;">else</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977; font-weight: bold;">List</span><span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span> list <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">new</span> ArrayList<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span><span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
list<span style="color: #d2cd86;">.</span>add<span style="color: #d2cd86;">(</span>next<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
map<span style="color: #d2cd86;">.</span>put<span style="color: #d2cd86;">(</span>nextVal<span style="color: #d2cd86;">,</span> list<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>maxKey <span style="color: #d2cd86;"><</span> nextVal<span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
maxKey <span style="color: #d2cd86;">=</span> nextVal<span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #e66170; font-weight: bold;">for</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977;">int</span> i <span style="color: #d2cd86;">=</span> inputC<span style="color: #b060b0;">;</span> i <span style="color: #d2cd86;"><</span> inputC <span style="color: #d2cd86;">+</span> nextVal<span style="color: #b060b0;">;</span> i<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">)</span>
data<span style="color: #d2cd86;">[</span>i<span style="color: #d2cd86;">]</span> <span style="color: #d2cd86;">=</span> next<span style="color: #b060b0;">;</span>
inputC <span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">=</span> nextVal<span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">!</span>isOdd<span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
median <span style="color: #d2cd86;">=</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977;">double</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">(</span>data<span style="color: #d2cd86;">[</span>inputSize <span style="color: #d2cd86;">/</span> <span style="color: #008c00;">2</span><span style="color: #d2cd86;">]</span> <span style="color: #d2cd86;">+</span> data<span style="color: #d2cd86;">[</span>inputSize <span style="color: #d2cd86;">/</span> <span style="color: #008c00;">2</span> <span style="color: #d2cd86;">-</span> <span style="color: #008c00;">1</span><span style="color: #d2cd86;">]</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">/</span><span style="color: #008c00;">2</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span> <span style="color: #e66170; font-weight: bold;">else</span> <span style="color: #b060b0;">{</span>
median <span style="color: #d2cd86;">=</span> data<span style="color: #d2cd86;">[</span>inputSize <span style="color: #d2cd86;">/</span> <span style="color: #008c00;">2</span><span style="color: #d2cd86;">]</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
Iterator<span style="color: #d2cd86;"><</span><span style="color: #bb7977; font-weight: bold;">Integer</span><span style="color: #d2cd86;">></span> iterator2 <span style="color: #d2cd86;">=</span> map<span style="color: #d2cd86;">.</span>get<span style="color: #d2cd86;">(</span>maxKey<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>iterator<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">while</span> <span style="color: #d2cd86;">(</span>iterator2<span style="color: #d2cd86;">.</span>hasNext<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977;">int</span> next <span style="color: #d2cd86;">=</span> iterator2<span style="color: #d2cd86;">.</span>next<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>intValue<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>mode <span style="color: #d2cd86;">></span> next<span style="color: #d2cd86;">)</span>
mode <span style="color: #d2cd86;">=</span> next<span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
deviationStd <span style="color: #d2cd86;">=</span> <span style="color: #bb7977; font-weight: bold;">Math</span><span style="color: #d2cd86;">.</span>sqrt<span style="color: #d2cd86;">(</span>deviationStd <span style="color: #d2cd86;">/</span> inputSize<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span></pre>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-19585108719404971522016-03-18T01:50:00.003-07:002016-03-18T02:00:03.694-07:00Custom Annotations <div dir="ltr" style="text-align: left;" trbidi="on">
<pre style="background: rgb(255, 255, 255);"><span style="color: maroon; font-weight: bold;">
</span></pre>
<h3 style="background: rgb(255, 255, 255); text-align: left;">
<span style="color: maroon; font-weight: bold;">1. @Test</span></h3>
<h4 style="background: rgb(255, 255, 255); text-align: left;">
<span style="color: maroon;">package</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">kant</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotations</span><span style="color: #808030;">.</span><span style="color: #004a43;">custom</span><span style="color: purple;">;</span></h4>
<pre style="background: rgb(255, 255, 255);"><span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">ElementType</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">Retention</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">RetentionPolicy</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">Target</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> NOTE</span><span style="color: #7f9fbf; font-weight: bold;">:</span><span style="color: #3f5fbf;"> Method declarations must not have any parameters or a throws clause.</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Return types are restricted to primitives</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> String</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> Class</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> enums</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"> annotations</span><span style="color: #7f9fbf; font-weight: bold;">,</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> and arrays of the preceding types.</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@author</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">shaskant</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
<span style="color: maroon; font-weight: bold;">public</span> @<span style="color: maroon; font-weight: bold;">interface</span> Test <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: #bb7977;">boolean</span> enabled<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">default</span> <span style="color: maroon; font-weight: bold;">true</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre>
<pre style="background: rgb(255, 255, 255);"><span style="color: purple;">
</span></pre>
<pre style="background: rgb(255, 255, 255);"><span style="color: purple;">
</span></pre>
<h4 style="background: rgb(255, 255, 255); text-align: left;">
<span style="color: purple;"><br /></span></h4>
<h3 style="text-align: left;">
<span style="color: purple;">2. @TesterInfo</span></h3>
<pre style="background: rgb(255, 255, 255);"><pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;"><span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: maroon; font-weight: bold;">package</span><span style="color: #004a43;"> com</span><span style="color: #808030;">.</span><span style="color: #004a43;">kant</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotations</span><span style="color: #808030;">.</span><span style="color: #004a43;">custom</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">ElementType</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">Retention</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">RetentionPolicy</span><span style="color: purple;">;</span>
<span style="color: maroon; font-weight: bold;">import</span><span style="color: #004a43;"> java</span><span style="color: #808030;">.</span><span style="color: #004a43;">lang</span><span style="color: #808030;">.</span><span style="color: #004a43;">annotation</span><span style="color: #808030;">.</span><span style="color: #004a43;">Target</span><span style="color: purple;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@author</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">shaskant</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
<span style="color: maroon; font-weight: bold;">public</span> @<span style="color: maroon; font-weight: bold;">interface</span> TesterInfo <span style="color: purple;">{</span>
<span style="color: maroon; font-weight: bold;">public</span> <span style="color: maroon; font-weight: bold;">enum</span> Priority <span style="color: purple;">{</span>
LOW<span style="color: #808030;">,</span> MEDIUM<span style="color: #808030;">,</span> HIGH
<span style="color: purple;">}</span>
Priority priority<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">default</span> Priority<span style="color: #808030;">.</span>MEDIUM<span style="color: purple;">;</span>
<span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #808030;">[</span><span style="color: #808030;">]</span> tags<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">default</span> <span style="color: #0000e6;">""</span><span style="color: purple;">;</span>
<span style="color: #bb7977; font-weight: bold;">String</span> createdBy<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">default</span> <span style="color: #0000e6;">"kantsh"</span><span style="color: purple;">;</span>
<span style="color: #bb7977; font-weight: bold;">String</span> lastModified<span style="color: #808030;">(</span><span style="color: #808030;">)</span> <span style="color: maroon; font-weight: bold;">default</span> <span style="color: #0000e6;">"03/01/2016"</span><span style="color: purple;">;</span>
<span style="color: purple;">}</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;"><span style="color: purple;">
</span></pre>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;"><span style="color: purple;">
</span></pre>
<h3 style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; text-align: left;">
<span style="color: purple;">3. TestExample [Annotated with above custom annotation]</span></h3>
<div>
<pre style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial;"><pre style="background: rgb(0, 0, 0); color: #d1d1d1;"><span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">package</span><span style="color: #008073;"> com</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">kant</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">annotations</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">custom</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> com</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">kant</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">annotations</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">custom</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">TesterInfo</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">Priority</span><span style="color: #b060b0;">;</span>
<span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">@author</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">shaskant</span><span style="color: #90b0e0;"></span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"></span>
<span style="color: #90b0e0;"> */</span>
@TesterInfo(priority = Priority.HIGH, createdBy = "kantsh", tags = <span style="color: #b060b0;">{</span> <span style="color: #00c4c4;">"sales"</span><span style="color: #d2cd86;">,</span>
<span style="color: #00c4c4;">"test"</span> <span style="color: #b060b0;">}</span>, lastModified = "18/03/2016")
<span style="color: #e66170; font-weight: bold;">public</span> <span style="color: #e66170; font-weight: bold;">class</span> TestExample <span style="color: #b060b0;">{</span>
<span style="color: #d2cd86;">@</span>Test
<span style="color: #bb7977;">void</span> testA<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span><span style="color: #e66170; font-weight: bold;">true</span><span style="color: #d2cd86;">)</span>
<span style="color: #e66170; font-weight: bold;">throw</span> <span style="color: #e66170; font-weight: bold;">new</span> <span style="color: #bb7977; font-weight: bold;">RuntimeException</span><span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"This test always failed"</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #d2cd86;">@</span>Test<span style="color: #d2cd86;">(</span>enabled <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">false</span><span style="color: #d2cd86;">)</span>
<span style="color: #bb7977;">void</span> testB<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span><span style="color: #e66170; font-weight: bold;">false</span><span style="color: #d2cd86;">)</span>
<span style="color: #e66170; font-weight: bold;">throw</span> <span style="color: #e66170; font-weight: bold;">new</span> <span style="color: #bb7977; font-weight: bold;">RuntimeException</span><span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"This test always passed"</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #d2cd86;">@</span>Test<span style="color: #d2cd86;">(</span>enabled <span style="color: #d2cd86;">=</span> <span style="color: #e66170; font-weight: bold;">true</span><span style="color: #d2cd86;">)</span>
<span style="color: #bb7977;">void</span> testC<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span><span style="color: #008c00;">10</span> <span style="color: #d2cd86;">></span> <span style="color: #008c00;">1</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #9999a9;">// do nothing, this test always passed.</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span></pre>
<div>
<span style="color: #b060b0;">
</span></div>
<div>
<span style="color: purple;">
</span>
<h3 style="text-align: left;">
<span style="color: purple;">4. RunTests</span></h3>
</div>
</pre>
</div>
<div>
<span style="color: purple;">Parse the class using Java reflections and process based on meta info provided from Annotations.</span></div>
<div>
<pre style="background: rgb(0, 0, 0); color: #d1d1d1;"><span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">package</span><span style="color: #008073;"> com</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">kant</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">annotations</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">custom</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">lang</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">annotation</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">Annotation</span><span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">import</span><span style="color: #008073;"> java</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">lang</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">reflect</span><span style="color: #d2cd86;">.</span><span style="color: #008073;">Method</span><span style="color: #b060b0;">;</span>
<span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> Now read Annotations.</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">@author</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">shaskant</span><span style="color: #90b0e0;"></span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"></span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">public</span> <span style="color: #e66170; font-weight: bold;">class</span> RunTest <span style="color: #b060b0;">{</span>
<span style="color: #90b0e0;">/**</span>
<span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">*</span><span style="color: #90b0e0;"> </span><span style="color: #8695b8; font-weight: bold;">@param</span><span style="color: #90b0e0;"> args</span>
<span style="color: #90b0e0;"> */</span>
<span style="color: #e66170; font-weight: bold;">public</span> <span style="color: #e66170; font-weight: bold;">static</span> <span style="color: #bb7977;">void</span> main<span style="color: #d2cd86;">(</span><span style="color: #bb7977; font-weight: bold;">String</span><span style="color: #d2cd86;">[</span><span style="color: #d2cd86;">]</span> args<span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>println<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"Testing..."</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span> passed <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #d2cd86;">,</span> failed <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #d2cd86;">,</span> count <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #d2cd86;">,</span> ignore <span style="color: #d2cd86;">=</span> <span style="color: #008c00;">0</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">Class</span><span style="color: #d2cd86;"><</span>TestExample<span style="color: #d2cd86;">></span> obj <span style="color: #d2cd86;">=</span> TestExample<span style="color: #d2cd86;">.</span>class<span style="color: #b060b0;">;</span>
<span style="color: #9999a9;">// Process TesterInfo annotation</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>obj<span style="color: #d2cd86;">.</span>isAnnotationPresent<span style="color: #d2cd86;">(</span>TesterInfo<span style="color: #d2cd86;">.</span>class<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
Annotation annotation <span style="color: #d2cd86;">=</span> obj<span style="color: #d2cd86;">.</span>getAnnotation<span style="color: #d2cd86;">(</span>TesterInfo<span style="color: #d2cd86;">.</span>class<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
TesterInfo testerInfo <span style="color: #d2cd86;">=</span> <span style="color: #d2cd86;">(</span>TesterInfo<span style="color: #d2cd86;">)</span> annotation<span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%nPriority :%s"</span><span style="color: #d2cd86;">,</span> testerInfo<span style="color: #d2cd86;">.</span>priority<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%nCreatedBy :%s"</span><span style="color: #d2cd86;">,</span> testerInfo<span style="color: #d2cd86;">.</span>createdBy<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%nTags :"</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977;">int</span> tagLength <span style="color: #d2cd86;">=</span> testerInfo<span style="color: #d2cd86;">.</span>tags<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">.</span>length<span style="color: #b060b0;">;</span>
<span style="color: #e66170; font-weight: bold;">for</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977; font-weight: bold;">String</span> tag <span style="color: #d2cd86;">:</span> testerInfo<span style="color: #d2cd86;">.</span>tags<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>tagLength <span style="color: #d2cd86;">></span> <span style="color: #008c00;">1</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>print<span style="color: #d2cd86;">(</span>tag <span style="color: #d2cd86;">+</span> <span style="color: #00c4c4;">", "</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span> <span style="color: #e66170; font-weight: bold;">else</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>print<span style="color: #d2cd86;">(</span>tag<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
tagLength<span style="color: #d2cd86;">-</span><span style="color: #d2cd86;">-</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%nLastModified :%s%n%n"</span><span style="color: #d2cd86;">,</span>
testerInfo<span style="color: #d2cd86;">.</span>lastModified<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #9999a9;">// Process @Test [applicable on methods only]</span>
<span style="color: #e66170; font-weight: bold;">for</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977; font-weight: bold;">Method</span> method <span style="color: #d2cd86;">:</span> obj<span style="color: #d2cd86;">.</span>getDeclaredMethods<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>method<span style="color: #d2cd86;">.</span>isAnnotationPresent<span style="color: #d2cd86;">(</span>Test<span style="color: #d2cd86;">.</span>class<span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
Annotation annotation <span style="color: #d2cd86;">=</span> method<span style="color: #d2cd86;">.</span>getAnnotation<span style="color: #d2cd86;">(</span>Test<span style="color: #d2cd86;">.</span>class<span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
Test test <span style="color: #d2cd86;">=</span> <span style="color: #d2cd86;">(</span>Test<span style="color: #d2cd86;">)</span> annotation<span style="color: #b060b0;">;</span>
<span style="color: #9999a9;">// if enabled = true (default)</span>
<span style="color: #e66170; font-weight: bold;">if</span> <span style="color: #d2cd86;">(</span>test<span style="color: #d2cd86;">.</span>enabled<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #e66170; font-weight: bold;">try</span> <span style="color: #b060b0;">{</span>
method<span style="color: #d2cd86;">.</span>invoke<span style="color: #d2cd86;">(</span>obj<span style="color: #d2cd86;">.</span>newInstance<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%s - Test '%s' - passed %n"</span><span style="color: #d2cd86;">,</span>
<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span>count<span style="color: #d2cd86;">,</span> method<span style="color: #d2cd86;">.</span>getName<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
passed<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span> <span style="color: #e66170; font-weight: bold;">catch</span> <span style="color: #d2cd86;">(</span><span style="color: #bb7977; font-weight: bold;">Throwable</span> ex<span style="color: #d2cd86;">)</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%s - Test '%s' - failed: %s %n"</span><span style="color: #d2cd86;">,</span>
<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span>count<span style="color: #d2cd86;">,</span> method<span style="color: #d2cd86;">.</span>getName<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">,</span> ex<span style="color: #d2cd86;">.</span>getCause<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
failed<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span> <span style="color: #e66170; font-weight: bold;">else</span> <span style="color: #b060b0;">{</span>
<span style="color: #bb7977; font-weight: bold;">System</span><span style="color: #d2cd86;">.</span>out<span style="color: #d2cd86;">.</span>printf<span style="color: #d2cd86;">(</span><span style="color: #00c4c4;">"%s - Test '%s' - ignored%n"</span><span style="color: #d2cd86;">,</span> <span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span>count<span style="color: #d2cd86;">,</span>
method<span style="color: #d2cd86;">.</span>getName<span style="color: #d2cd86;">(</span><span style="color: #d2cd86;">)</span><span style="color: #d2cd86;">)</span><span style="color: #b060b0;">;</span>
ignore<span style="color: #d2cd86;">+</span><span style="color: #d2cd86;">+</span><span style="color: #b060b0;">;</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span>
<span style="color: #b060b0;">}</span></pre>
</div>
<div>
</div>
</pre>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-55704610477038132762016-02-24T02:50:00.002-08:002016-03-18T02:01:15.671-07:00Start and End dates for Week or Month provided with any date within <div dir="ltr" style="text-align: left;" trbidi="on">
<pre style="background: rgb(255, 255, 255);"><span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">package</span><span style="color: #7f0055;"> com</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">kant</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">calendertest</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">text</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">DateFormat</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">text</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">ParseException</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">text</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">SimpleDateFormat</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">util</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">Calendar</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">util</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">Date</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">util</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">GregorianCalendar</span><span style="color: #7f0055;">;</span>
<span style="color: #7f0055; font-weight: bold;">import</span><span style="color: #7f0055;"> java</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">util</span><span style="color: #7f0055;">.</span><span style="color: #7f0055;">Locale</span><span style="color: #7f0055;">;</span>
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@author</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">shaskant</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">public</span> <span style="color: #7f0055; font-weight: bold;">class</span> Mycalender {
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #3f3fbf;">http://www.java2s.com/Tutorial/Java/</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> 0120__Development</span><span style="color: #7f9fbf; font-weight: bold;">/</span><span style="color: #3f5fbf;">FulldaynameSimpleDateFormatEEEE</span><span style="color: #3f5fbf;">.</span><span style="color: #3f5fbf;">htm</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> args</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@throws</span><span style="color: #3f5fbf;"> ParseException</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">public</span> <span style="color: #7f0055; font-weight: bold;">static</span> <span style="color: #7f0055; font-weight: bold;">void</span> main(<span style="color: #7f0055; font-weight: bold;">String</span>[] args) <span style="color: #7f0055; font-weight: bold;">throws</span> <span style="color: #7f0055; font-weight: bold;">ParseException</span> {
<span style="color: #7f0055; font-weight: bold;">Date</span> date = parseDate(<span style="color: #2a00ff;">"Wed dec 24 20:29:30 JST 2016"</span>);
<span style="color: #7f0055; font-weight: bold;">System</span>.out.println(<span style="color: #2a00ff;">"This Week: "</span> + getThisWeek(date).toString());
;
<span style="color: #7f0055; font-weight: bold;">System</span>.out.println(<span style="color: #2a00ff;">"This Month: "</span> + getThisMonth(date).toString());
}
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Parse date from target string</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> target</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@throws</span><span style="color: #3f5fbf;"> ParseException</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">private</span> <span style="color: #7f0055; font-weight: bold;">static</span> <span style="color: #7f0055; font-weight: bold;">Date</span> parseDate(<span style="color: #7f0055; font-weight: bold;">String</span> target) <span style="color: #7f0055; font-weight: bold;">throws</span> <span style="color: #7f0055; font-weight: bold;">ParseException</span> {
<span style="color: #7f0055; font-weight: bold;">DateFormat</span> df = <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #7f0055; font-weight: bold;">SimpleDateFormat</span>(<span style="color: #2a00ff;">"EEE MMM dd kk:mm:ss z yyyy"</span>,
<span style="color: #7f0055; font-weight: bold;">Locale</span>.ENGLISH);
<span style="color: #7f0055; font-weight: bold;">Date</span> result = df.parse(target);
<span style="color: #7f0055; font-weight: bold;">System</span>.out.println(result);
<span style="color: #7f0055; font-weight: bold;">return</span> result;
}
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> Get month start and end dates</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> date</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">private</span> <span style="color: #7f0055; font-weight: bold;">static</span> DateRange getThisMonth(<span style="color: #7f0055; font-weight: bold;">Date</span> date) {
<span style="color: #7f0055; font-weight: bold;">Calendar</span> cal = <span style="color: #7f0055; font-weight: bold;">GregorianCalendar</span>.getInstance();
cal.setTime(date);
cal.set(<span style="color: #7f0055; font-weight: bold;">Calendar</span>.DATE, cal.getActualMinimum(<span style="color: #7f0055; font-weight: bold;">Calendar</span>.DATE));
<span style="color: #7f0055; font-weight: bold;">Date</span> startDate = cal.getTime();
cal.set(<span style="color: #7f0055; font-weight: bold;">Calendar</span>.DATE, cal.getActualMaximum(<span style="color: #7f0055; font-weight: bold;">Calendar</span>.DATE));
<span style="color: #7f0055; font-weight: bold;">Date</span> endDate = cal.getTime();
<span style="color: #7f0055; font-weight: bold;">return</span> <span style="color: #7f0055; font-weight: bold;">new</span> DateRange(startDate, endDate);
}
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">private</span> <span style="color: #7f0055; font-weight: bold;">static</span> DateRange getThisWeek(<span style="color: #7f0055; font-weight: bold;">Date</span> date) {
<span style="color: #7f0055; font-weight: bold;">Calendar</span> cal = <span style="color: #7f0055; font-weight: bold;">Calendar</span>.getInstance();
cal.setTime(date);
cal.set(<span style="color: #7f0055; font-weight: bold;">Calendar</span>.DAY_OF_WEEK, 2);
<span style="color: #7f0055; font-weight: bold;">Date</span> startDate = cal.getTime();
cal.add(<span style="color: #7f0055; font-weight: bold;">Calendar</span>.DAY_OF_WEEK, 6); <span style="color: #3f7f59;">// adds 6 days to initial day set.</span>
<span style="color: #7f0055; font-weight: bold;">Date</span> endDate = cal.getTime();
<span style="color: #7f0055; font-weight: bold;">return</span> <span style="color: #7f0055; font-weight: bold;">new</span> DateRange(startDate, endDate);
}
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">* Placeholder to return two date values.</span></pre>
<pre style="background: rgb(255, 255, 255);"> *
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@author</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">shaskant</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"></span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">static</span> <span style="color: #7f0055; font-weight: bold;">class</span> DateRange {
<span style="color: #7f0055; font-weight: bold;">private</span> <span style="color: #7f0055; font-weight: bold;">Date</span> startDate = <span style="color: #7f0055; font-weight: bold;">null</span>;
<span style="color: #7f0055; font-weight: bold;">private</span> <span style="color: #7f0055; font-weight: bold;">Date</span> endDate = <span style="color: #7f0055; font-weight: bold;">null</span>;
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> startDate</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@param</span><span style="color: #3f5fbf;"> endDate</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">public</span> DateRange(<span style="color: #7f0055; font-weight: bold;">Date</span> startDate, <span style="color: #7f0055; font-weight: bold;">Date</span> endDate) {
<span style="color: #7f0055; font-weight: bold;">super</span>();
<span style="color: #7f0055; font-weight: bold;">this</span>.startDate = startDate;
<span style="color: #7f0055; font-weight: bold;">this</span>.endDate = endDate;
}
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"> the startDate</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">public</span> <span style="color: #7f0055; font-weight: bold;">Date</span> getStartDate() {
<span style="color: #7f0055; font-weight: bold;">return</span> startDate;
}
<span style="color: #3f5fbf;">/**</span>
<span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">*</span><span style="color: #3f5fbf;"> </span><span style="color: #7f9fbf; font-weight: bold;">@return</span><span style="color: #3f5fbf;"> the endDate</span>
<span style="color: #3f5fbf;"> */</span>
<span style="color: #7f0055; font-weight: bold;">public</span> <span style="color: #7f0055; font-weight: bold;">Date</span> getEndDate() {
<span style="color: #7f0055; font-weight: bold;">return</span> endDate;
}
@Override
<span style="color: #7f0055; font-weight: bold;">public</span> <span style="color: #7f0055; font-weight: bold;">String</span> toString() {
<span style="color: #7f0055; font-weight: bold;">SimpleDateFormat</span> formatter = <span style="color: #7f0055; font-weight: bold;">new</span> <span style="color: #7f0055; font-weight: bold;">SimpleDateFormat</span>(<span style="color: #2a00ff;">"yyyy/MM/dd EEEE"</span>);
<span style="color: #7f0055; font-weight: bold;">return</span> <span style="color: #2a00ff;">"Starts on: "</span> + formatter.format(startDate) + <span style="color: #2a00ff;">" Ends on: "</span>
+ formatter.format(endDate);
}
}
}</pre>
<div>
<br /></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-15572679070179615432015-06-11T04:03:00.001-07:002015-06-11T04:03:23.814-07:00The Bot Cleaner [AI spot cleaning bot] <div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b>Link to Complete problem:</b><br />
<a href="https://www.hackerrank.com/challenges/botclean"><span style="color: red;">https://www.hackerrank.com/challenges/botclean</span></a><br />
<br />
Not the perfect AI Bot wrt score [17.73 (max 17.82)]<br />
<br />
<br />
<b>Algorithm:</b><br />
1. make a list of spots.<br />
2. find the closest spot [greedy approach]<br />
3. move in that direction.<br />
<br />
<b>To Improve</b>:<br />
1. find closest cluster of points.<br />
2. or find a path that covers more than 1 point [not exactly close to TSP but path containing 2 points should be attainable]<br />
3. or http://www.geeksforgeeks.org/travelling-salesman-problem-set-2-approximate-using-mst/<br />
<br />
<b>Source Code is below</b>:<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">kant</span><span style="color: #333333;">.</span><span style="color: #0000cc;">hackerrank</span><span style="color: #333333;">.</span><span style="color: #0000cc;">ai</span><span style="color: #333333;">.</span><span style="color: #0000cc;">challenge</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Arrays</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Iterator</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Scanner</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * @author shaskant</span>
<span style="color: #888888;"> *</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">BotCleaner</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> * @param args</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">main</span><span style="color: #333333;">(</span>String<span style="color: #333333;">[]</span> args<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Scanner in <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Scanner<span style="color: #333333;">(</span>System<span style="color: #333333;">.</span><span style="color: #0000cc;">in</span><span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[]</span> pos <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> <span style="color: #333399; font-weight: bold;">int</span><span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">];</span>
String board<span style="color: #333333;">[]</span> <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> String<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">];</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> i <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">2</span><span style="color: #333333;">;</span> i<span style="color: #333333;">++)</span>
pos<span style="color: #333333;">[</span>i<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> in<span style="color: #333333;">.</span><span style="color: #0000cc;">nextInt</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> i <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">;</span> i<span style="color: #333333;">++)</span>
board<span style="color: #333333;">[</span>i<span style="color: #333333;">]</span> <span style="color: #333333;">=</span> in<span style="color: #333333;">.</span><span style="color: #0000cc;">next</span><span style="color: #333333;">();</span>
next_move<span style="color: #333333;">(</span>pos<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">],</span> pos<span style="color: #333333;">[</span><span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">],</span> board<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> List<span style="color: #333333;"><</span>Point<span style="color: #333333;">></span> spots <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>BotCleaner<span style="color: #333333;">.</span><span style="color: #0000cc;">Point</span><span style="color: #333333;">>();</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> * @param posr</span>
<span style="color: #888888;"> * @param posc</span>
<span style="color: #888888;"> * @param board</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">next_move</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> posr<span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> posc<span style="color: #333333;">,</span> String<span style="color: #333333;">[]</span> board<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> row <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> row <span style="color: #333333;"><</span> board<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">;</span> row<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
String<span style="color: #333333;">[]</span> cols <span style="color: #333333;">=</span> board<span style="color: #333333;">[</span>row<span style="color: #333333;">].</span><span style="color: #0000cc;">split</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"(?!^)"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> col <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> col <span style="color: #333333;"><</span> cols<span style="color: #333333;">.</span><span style="color: #0000cc;">length</span><span style="color: #333333;">;</span> col<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">switch</span> <span style="color: #333333;">(</span>cols<span style="color: #333333;">[</span>col<span style="color: #333333;">])</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">case</span> <span style="background-color: #fff0f0;">"d"</span><span style="color: #333333;">:</span>
spots<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Point<span style="color: #333333;">(</span>row<span style="color: #333333;">,</span> col<span style="color: #333333;">));</span>
<span style="color: #008800; font-weight: bold;">break</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
Point neighbor <span style="color: #333333;">=</span> findNextClosestNeighbour<span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> Point<span style="color: #333333;">(</span>posr<span style="color: #333333;">,</span> posc<span style="color: #333333;">));</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span> <span style="color: #333333;">></span> posc<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span> <span style="color: #333333;">></span> posr<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span><span style="color: #333333;">,</span> posr<span style="color: #333333;">)</span> <span style="color: #333333;">></span> diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span><span style="color: #333333;">,</span> posc<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"DOWN"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"RIGHT"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span><span style="color: #333333;">,</span> posr<span style="color: #333333;">)</span> <span style="color: #333333;">></span> diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span><span style="color: #333333;">,</span> posc<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"UP"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"RIGHT"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span> <span style="color: #333333;"><</span> posc<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span> <span style="color: #333333;">></span> posr<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span><span style="color: #333333;">,</span> posr<span style="color: #333333;">)</span> <span style="color: #333333;">></span> diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span><span style="color: #333333;">,</span> posc<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"DOWN"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"LEFT"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span><span style="color: #333333;">,</span> posr<span style="color: #333333;">)</span> <span style="color: #333333;">></span> diff<span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span><span style="color: #333333;">,</span> posc<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"UP"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"LEFT"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span> <span style="color: #333333;">==</span> posr <span style="color: #333333;">&&</span> neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span> <span style="color: #333333;">==</span> posc<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"CLEAN"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span><span style="color: #008800; font-weight: bold;">else</span><span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>neighbor<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span> <span style="color: #333333;">></span> posr<span style="color: #333333;">){</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"DOWN"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">else</span> System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"UP"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> Point <span style="color: #0066bb; font-weight: bold;">findNextClosestNeighbour</span><span style="color: #333333;">(</span>Point start<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
Point neigh <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> Point<span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">1000</span><span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">1000</span><span style="color: #333333;">);</span>
Iterator<span style="color: #333333;"><</span>Point<span style="color: #333333;">></span> points <span style="color: #333333;">=</span> spots<span style="color: #333333;">.</span><span style="color: #0000cc;">iterator</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">while</span> <span style="color: #333333;">(</span>points<span style="color: #333333;">.</span><span style="color: #0000cc;">hasNext</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
Point next <span style="color: #333333;">=</span> points<span style="color: #333333;">.</span><span style="color: #0000cc;">next</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>dist<span style="color: #333333;">(</span>start<span style="color: #333333;">,</span> neigh<span style="color: #333333;">)</span> <span style="color: #333333;">>=</span> dist<span style="color: #333333;">(</span>next<span style="color: #333333;">,</span> start<span style="color: #333333;">))</span> <span style="color: #333333;">{</span>
neigh <span style="color: #333333;">=</span> next<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span>neigh<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span><span style="color: #333333;">+</span><span style="background-color: #fff0f0;">" "</span><span style="color: #333333;">+</span>neigh<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">return</span> neigh<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">dist</span><span style="color: #333333;">(</span>Point start<span style="color: #333333;">,</span> Point neigh<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> Math<span style="color: #333333;">.</span><span style="color: #0000cc;">abs</span><span style="color: #333333;">(</span>neigh<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span> <span style="color: #333333;">-</span> start<span style="color: #333333;">.</span><span style="color: #0000cc;">r</span><span style="color: #333333;">)</span> <span style="color: #333333;">+</span> Math<span style="color: #333333;">.</span><span style="color: #0000cc;">abs</span><span style="color: #333333;">(</span>neigh<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span> <span style="color: #333333;">-</span> start<span style="color: #333333;">.</span><span style="color: #0000cc;">c</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">static</span> <span style="color: #333399; font-weight: bold;">int</span> <span style="color: #0066bb; font-weight: bold;">diff</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> x1<span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> x2<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">return</span> Math<span style="color: #333333;">.</span><span style="color: #0000cc;">abs</span><span style="color: #333333;">(</span>x1 <span style="color: #333333;">-</span> x2<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">static</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">Point</span> <span style="color: #333333;">{</span>
<span style="color: #333399; font-weight: bold;">int</span> r<span style="color: #333333;">;</span>
<span style="color: #333399; font-weight: bold;">int</span> c<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">Point</span><span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> s<span style="color: #333333;">,</span> <span style="color: #333399; font-weight: bold;">int</span> p<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
r <span style="color: #333333;">=</span> s<span style="color: #333333;">;</span>
c <span style="color: #333333;">=</span> p<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-44261129773581642222015-04-11T09:19:00.005-07:002015-04-11T09:43:14.369-07:00theWebcrawler<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Before you begin review following topics:<br />
<div style="text-align: left;">
</div>
<ul style="text-align: left;">
<li><a href="http://jsoup.org/">Jsoup Library</a> </li>
<li>http://www.mkyong.com/java/jsoup-html-parser-hello-world-examples/</li>
<li><a href="http://www.javacodegeeks.com/2013/01/java-thread-pool-example-using-executors-and-threadpoolexecutor.html">ThreadExecutorService </a>basics</li>
<li><a href="http://www.tutorialspoint.com/design_pattern/strategy_pattern.htm">Strategy pattern refresher</a></li>
</ul>
<br />
and finally the design i nearly followed <a href="http://andreas-hess.info/programming/webcrawler/webqueue.png" imageanchor="1" style="clear: right; display: inline !important; margin-bottom: 1em; margin-left: 1em; text-align: center;"><img border="0" src="http://andreas-hess.info/programming/webcrawler/webqueue.png" /></a><br />
<span style="font-family: Georgia, Times New Roman, serif;">{ref: http://andreas-hess.info/programming/webcrawler/}</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;"><br /><br />I decided to use strategy from start as i knew i will be looking for better n efficient ways to parse web page for href links.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">Below is a Jsoup based strategy that extends interface "UrlParserStrategy".</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">kant</span><span style="color: #333333;">.</span><span style="color: #0000cc;">web</span><span style="color: #333333;">.</span><span style="color: #0000cc;">crawler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">strategy</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.io.IOException</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.ArrayList</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.jsoup.Jsoup</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.jsoup.nodes.Document</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.jsoup.nodes.Element</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">org.jsoup.select.Elements</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * @author shashi</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">JsoupExecuter</span> <span style="color: #008800; font-weight: bold;">implements</span> UrlParserStrategy <span style="color: #333333;">{</span>
<span style="color: #888888;">/*</span>
<span style="color: #888888;"> * (non-Javadoc)</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> * @see</span>
<span style="color: #888888;"> * com.kant.web.crawler.strategy.ExecuterStrategy#execute(java.lang.String)</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> <span style="color: #0066bb; font-weight: bold;">fetchLinks</span><span style="color: #333333;">(</span>String url<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>url <span style="color: #333333;">==</span> <span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">||</span> url<span style="color: #333333;">.</span><span style="color: #0000cc;">isEmpty</span><span style="color: #333333;">())</span>
<span style="color: #008800; font-weight: bold;">return</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
Document doc <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> urls <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// if (isValid(url)) {</span>
doc <span style="color: #333333;">=</span> Jsoup<span style="color: #333333;">.</span><span style="color: #0000cc;">connect</span><span style="color: #333333;">(</span>url<span style="color: #333333;">).</span><span style="color: #0000cc;">timeout</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">3000</span><span style="color: #333333;">).</span><span style="color: #0000cc;">get</span><span style="color: #333333;">();</span>
urls <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> ArrayList<span style="color: #333333;"><</span>String<span style="color: #333333;">>();</span>
Elements links <span style="color: #333333;">=</span> doc<span style="color: #333333;">.</span><span style="color: #0000cc;">select</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"a[href]"</span><span style="color: #333333;">);</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span>Element link <span style="color: #333333;">:</span> links<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
urls<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>link<span style="color: #333333;">.</span><span style="color: #0000cc;">attr</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"abs:href"</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">// }</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>IOException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span>
<span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"[Skipping]:Non-textual webcontent or timeout has occured "</span>
<span style="color: #333333;">+</span> url<span style="color: #333333;">);</span>
urls <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
doc <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">return</span> urls<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * for valid media types</span>
<span style="color: #888888;"> */</span>
<span style="color: #888888;">// private boolean isValid(String webUrl) throws IOException {</span>
<span style="color: #888888;">// URL url = null;</span>
<span style="color: #888888;">// HttpURLConnection connection = null;</span>
<span style="color: #888888;">// try {</span>
<span style="color: #888888;">// url = new URL(webUrl);</span>
<span style="color: #888888;">// connection = (HttpURLConnection) url.openConnection();</span>
<span style="color: #888888;">// connection.setRequestMethod("HEAD");</span>
<span style="color: #888888;">// connection.connect();</span>
<span style="color: #888888;">// String contentType = connection.getContentType();</span>
<span style="color: #888888;">// return contentType.contains("text/html;")</span>
<span style="color: #888888;">// || contentType.contains("application/xml");</span>
<span style="color: #888888;">//</span>
<span style="color: #888888;">// } catch (MalformedURLException e) {</span>
<span style="color: #888888;">// e.printStackTrace();</span>
<span style="color: #888888;">// } finally {</span>
<span style="color: #888888;">// connection.disconnect();</span>
<span style="color: #888888;">// }</span>
<span style="color: #888888;">//</span>
<span style="color: #888888;">// return false;</span>
<span style="color: #888888;">// }</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span><span style="font-family: Georgia, Times New Roman, serif;">Jsoup will parse the webpage and finds all the 'HREF' elements for inks defined on the page and store it a list and returns.</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">Then comes the JobController that contains main logic .</span><br />
<br />
<ul style="text-align: left;">
<li><span style="font-family: Georgia, 'Times New Roman', serif;">A blockingQueue is used as a shared resource [thread safe java impl]</span></li>
<li><span style="font-family: Georgia, 'Times New Roman', serif;">ExecutorService is used to create and maintain thread worker pool [java impl]</span></li>
<li>Each Job or queueMessage contains a url to fetch next level of links from and also stores current level/depth of search.</li>
<li>I added HashSet [java impl] to prevent duplicate jobs and loops.</li>
</ul>
<br />
<br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">kant</span><span style="color: #333333;">.</span><span style="color: #0000cc;">web</span><span style="color: #333333;">.</span><span style="color: #0000cc;">crawler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">controller</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.HashSet</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.Set</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.BlockingQueue</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.ExecutorService</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.TimeUnit</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.kant.web.crawler.model.JobFactory</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.kant.web.crawler.model.JobRunnable</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.kant.web.crawler.model.QueueMessage</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.kant.web.crawler.strategy.UrlParserStrategy</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * @author shashi</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">JobController</span> <span style="color: #008800; font-weight: bold;">implements</span> CrawlerController <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> ExecutorService executorService<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> BlockingQueue<span style="color: #333333;"><</span>QueueMessage<span style="color: #333333;">></span> sharedQueue<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> UrlParserStrategy parser<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> <span style="color: #008800; font-weight: bold;">final</span> <span style="color: #333399; font-weight: bold;">int</span> endLevel <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">5</span><span style="color: #333333;">;</span> <span style="color: #888888;">// TODO make it configurable</span>
<span style="color: #008800; font-weight: bold;">private</span> Set<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> urlsCache<span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * @param executorService</span>
<span style="color: #888888;"> * @param sharedQueue</span>
<span style="color: #888888;"> * @param executor</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">JobController</span><span style="color: #333333;">(</span>ExecutorService executorService<span style="color: #333333;">,</span>
BlockingQueue<span style="color: #333333;"><</span>QueueMessage<span style="color: #333333;">></span> sharedQueue<span style="color: #333333;">,</span> UrlParserStrategy parser<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">executorService</span> <span style="color: #333333;">=</span> executorService<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">sharedQueue</span> <span style="color: #333333;">=</span> sharedQueue<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">parser</span> <span style="color: #333333;">=</span> parser<span style="color: #333333;">;</span>
urlsCache <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">new</span> HashSet<span style="color: #333333;"><</span>String<span style="color: #333333;">>();</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * Processes only 3000 urls.</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> * TODO make processing power configurable</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> * @param baseUrl</span>
<span style="color: #888888;"> * @throws InterruptedException</span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">processWebSite</span><span style="color: #333333;">(</span>String baseUrl<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
sharedQueue<span style="color: #333333;">.</span><span style="color: #0000cc;">put</span><span style="color: #333333;">(</span>JobFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">createJob</span><span style="color: #333333;">(</span>baseUrl<span style="color: #333333;">,</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">));</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span><span style="color: #333399; font-weight: bold;">int</span> i <span style="color: #333333;">=</span> <span style="color: #0000dd; font-weight: bold;">0</span><span style="color: #333333;">;</span> i <span style="color: #333333;"><</span> <span style="color: #0000dd; font-weight: bold;">3000</span><span style="color: #333333;">;</span> i<span style="color: #333333;">++)</span> <span style="color: #333333;">{</span>
QueueMessage message<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
message <span style="color: #333333;">=</span> sharedQueue<span style="color: #333333;">.</span><span style="color: #0000cc;">poll</span><span style="color: #333333;">(</span><span style="color: #0000dd; font-weight: bold;">400</span><span style="color: #333333;">,</span> TimeUnit<span style="color: #333333;">.</span><span style="color: #0000cc;">MILLISECONDS</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
message <span style="color: #333333;">=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>message <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>message<span style="color: #333333;">.</span><span style="color: #0000cc;">getLevel</span><span style="color: #333333;">()</span> <span style="color: #333333;"><</span> endLevel<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(!</span>urlsCache<span style="color: #333333;">.</span><span style="color: #0000cc;">contains</span><span style="color: #333333;">(</span>message<span style="color: #333333;">.</span><span style="color: #0000cc;">getUrl</span><span style="color: #333333;">()))</span> <span style="color: #333333;">{</span>
urlsCache<span style="color: #333333;">.</span><span style="color: #0000cc;">add</span><span style="color: #333333;">(</span>message<span style="color: #333333;">.</span><span style="color: #0000cc;">getUrl</span><span style="color: #333333;">());</span>
executorService<span style="color: #333333;">.</span><span style="color: #0000cc;">execute</span><span style="color: #333333;">(</span><span style="color: #008800; font-weight: bold;">new</span> JobRunnable<span style="color: #333333;">(</span>message<span style="color: #333333;">,</span>
parser<span style="color: #333333;">,</span> sharedQueue<span style="color: #333333;">));</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">else</span> <span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>message<span style="color: #333333;">.</span><span style="color: #0000cc;">getLevel</span><span style="color: #333333;">()</span> <span style="color: #333333;">==</span> endLevel<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"[Processed/completed]: "</span>
<span style="color: #333333;">+</span> message<span style="color: #333333;">.</span><span style="color: #0000cc;">getUrl</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">" [At depth]: "</span> <span style="color: #333333;">+</span> endLevel<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"\n-------------"</span> <span style="color: #333333;">+</span> i <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">"-------------\n"</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<br />
<br />
and finally my Thread worker Job<br />
<br />
uses the strategy to fetch urls and then adds the urls to shared Queue for further searching by JobCrawler<br />
<br />
<br />
<!-- HTML generated using hilite.me --><br />
<div style="background: #ffffff; border-width: .1em .1em .1em .8em; border: solid gray; overflow: auto; padding: .2em .6em; width: auto;">
<table><tbody>
<tr><td><pre style="line-height: 125%; margin: 0;"> 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55</pre>
</td><td><pre style="line-height: 125%; margin: 0;"><span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">package</span> com<span style="color: #333333;">.</span><span style="color: #0000cc;">kant</span><span style="color: #333333;">.</span><span style="color: #0000cc;">web</span><span style="color: #333333;">.</span><span style="color: #0000cc;">crawler</span><span style="color: #333333;">.</span><span style="color: #0000cc;">model</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.List</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.BlockingQueue</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">java.util.concurrent.TimeUnit</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">import</span> <span style="color: #0e84b5; font-weight: bold;">com.kant.web.crawler.strategy.UrlParserStrategy</span><span style="color: #333333;">;</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * @author shashi</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #008800; font-weight: bold;">class</span> <span style="color: #bb0066; font-weight: bold;">JobRunnable</span> <span style="color: #008800; font-weight: bold;">implements</span> Runnable <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">private</span> UrlParserStrategy parser<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> QueueMessage job<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">private</span> BlockingQueue<span style="color: #333333;"><</span>QueueMessage<span style="color: #333333;">></span> sharedQ<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #0066bb; font-weight: bold;">JobRunnable</span><span style="color: #333333;">(</span>QueueMessage job<span style="color: #333333;">,</span> UrlParserStrategy parser<span style="color: #333333;">,</span>
BlockingQueue<span style="color: #333333;"><</span>QueueMessage<span style="color: #333333;">></span> queueRef<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">parser</span> <span style="color: #333333;">=</span> parser<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">job</span> <span style="color: #333333;">=</span> job<span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">this</span><span style="color: #333333;">.</span><span style="color: #0000cc;">sharedQ</span> <span style="color: #333333;">=</span> queueRef<span style="color: #333333;">;</span>
<span style="color: #333333;">}</span>
<span style="color: #888888;">/**</span>
<span style="color: #888888;"> * </span>
<span style="color: #888888;"> */</span>
<span style="color: #008800; font-weight: bold;">public</span> <span style="color: #333399; font-weight: bold;">void</span> <span style="color: #0066bb; font-weight: bold;">run</span><span style="color: #333333;">()</span> <span style="color: #333333;">{</span>
String url <span style="color: #333333;">=</span> job<span style="color: #333333;">.</span><span style="color: #0000cc;">getUrl</span><span style="color: #333333;">();</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>url <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">&&</span> <span style="color: #333333;">!</span>url<span style="color: #333333;">.</span><span style="color: #0000cc;">isEmpty</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"[Processing]: "</span> <span style="color: #333333;">+</span> url <span style="color: #333333;">+</span> <span style="background-color: #fff0f0;">" [At depth]: "</span>
<span style="color: #333333;">+</span> job<span style="color: #333333;">.</span><span style="color: #0000cc;">getLevel</span><span style="color: #333333;">());</span>
List<span style="color: #333333;"><</span>String<span style="color: #333333;">></span> result <span style="color: #333333;">=</span> parser<span style="color: #333333;">.</span><span style="color: #0000cc;">fetchLinks</span><span style="color: #333333;">(</span>url<span style="color: #333333;">);</span>
<span style="color: #333399; font-weight: bold;">int</span> newLevel <span style="color: #333333;">=</span> job<span style="color: #333333;">.</span><span style="color: #0000cc;">getLevel</span><span style="color: #333333;">()</span> <span style="color: #333333;">+</span> <span style="color: #0000dd; font-weight: bold;">1</span><span style="color: #333333;">;</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>result <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span><span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">for</span> <span style="color: #333333;">(</span>String item <span style="color: #333333;">:</span> result<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">if</span> <span style="color: #333333;">(</span>item <span style="color: #333333;">!=</span> <span style="color: #008800; font-weight: bold;">null</span> <span style="color: #333333;">&&</span> <span style="color: #333333;">!</span>item<span style="color: #333333;">.</span><span style="color: #0000cc;">isEmpty</span><span style="color: #333333;">())</span> <span style="color: #333333;">{</span>
<span style="color: #008800; font-weight: bold;">try</span> <span style="color: #333333;">{</span>
<span style="color: #888888;">// sharedQ.put(JobFactory.createJob(item,</span>
<span style="color: #888888;">// newLevel));</span>
sharedQ<span style="color: #333333;">.</span><span style="color: #0000cc;">offer</span><span style="color: #333333;">(</span>JobFactory<span style="color: #333333;">.</span><span style="color: #0000cc;">createJob</span><span style="color: #333333;">(</span>item<span style="color: #333333;">,</span> newLevel<span style="color: #333333;">),</span>
<span style="color: #0000dd; font-weight: bold;">100</span><span style="color: #333333;">,</span> TimeUnit<span style="color: #333333;">.</span><span style="color: #0000cc;">MILLISECONDS</span><span style="color: #333333;">);</span>
<span style="color: #333333;">}</span> <span style="color: #008800; font-weight: bold;">catch</span> <span style="color: #333333;">(</span>InterruptedException e<span style="color: #333333;">)</span> <span style="color: #333333;">{</span>
e<span style="color: #333333;">.</span><span style="color: #0000cc;">printStackTrace</span><span style="color: #333333;">();</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
System<span style="color: #333333;">.</span><span style="color: #0000cc;">out</span><span style="color: #333333;">.</span><span style="color: #0000cc;">println</span><span style="color: #333333;">(</span><span style="background-color: #fff0f0;">"[Completed]: "</span> <span style="color: #333333;">+</span> url<span style="color: #333333;">);</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
<span style="color: #333333;">}</span>
</pre>
</td></tr>
</tbody></table>
</div>
<br />
<br />
<br />
<span style="font-family: Helvetica Neue, Arial, Helvetica, sans-serif;"><b>Download source code here:</b></span><br />
<a href="https://github.com/thekant/theWebCrawler">https://github.com/thekant/theWebCrawler</a><br />
<br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;">Note: Used <a href="http://hilite.me/">http://hilite.me/</a> for highlighting code [almost effortless to use]</span><br />
<br />
<b>FYI:</b> <span style="font-family: Courier New, Courier, monospace; font-size: x-small;">http://java-source.net/open-source/crawlers</span><br />
<br /></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-23225126317720109742015-02-13T06:40:00.002-08:002015-02-13T06:45:55.025-08:00Annotations in Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div id="articleBody" style="color: #333333; font-family: Helvetica; font-size: 16px;">
Java annotations are used to provide meta data for your Java code. Being meta data, the annotations do not directly affect the execution of your code, although some types of annotations can actually be used for that purpose.<br />
Java annotations were added to Java from Java 5. This text covers Java annotations as they look in Java 6. As far as I know, Java annotations have not changed in Java 7, so this text should be valid for Java 7 programmers too.<br />
<a href="https://www.blogger.com/null" name="java-annotation-purposes" style="color: #551a8b; font-weight: 700;"></a><br />
<h2 style="margin-top: 30px;">
Java Annotation Purposes</h2>
Java annotations are typically used for the following purposes:<br />
<ul>
<li>Compiler instructions</li>
<li>Build-time instructions</li>
<li>Runtime instructions</li>
</ul>
Java has 3 built-in annotations that you can use to give instructions to the Java compiler. These annotations are explained in more detail later in this text.<br />
Java annotations can be be used at build-time, when you build your software project. The build process includes generating source code, compiling the source, generating XML files (e.g. deployment descriptors), packaging the compiled code and files into a JAR file etc. Building the software is typically done by an automatic build tool like Apache Ant or Apache Maven. Build tools may scan your Java code for specific annotations and generate source code or other files based on these annotations.<br />
Normally, Java annotations are not present in your Java code after compilation. It is possible, however, to define your own annotations that are available at runtime. These annotations can then be accessed via <a href="http://tutorials.jenkov.com/java-reflection/index.html" style="color: #551a8b; font-weight: 700; text-decoration: none;">Java Reflection</a>, and used to give instructions to your program, or some third party API.<br />
<a href="https://www.blogger.com/null" name="annotation-basics" style="color: #551a8b; font-weight: 700;"></a><br />
<h2 style="margin-top: 30px;">
Annotation Basics</h2>
An annotation in its shortest form looks like this:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@Entity
</pre>
The <code style="font-family: Courier; font-size: 1em;">@</code> character signals to the compiler that this is an annotation. The name following the <code style="font-family: Courier; font-size: 1em;">@</code> character is the name of the annotation. In the example above the annotation name is <code style="font-family: Courier; font-size: 1em;">Entity</code>.<br />
<a href="https://www.blogger.com/null" name="annotation-elements" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
Annotation Elements</h3>
Annotations can have elements for which you can set values. An element is like an attribute or parameter. Here is an example of an annotation with an element:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@Entity(tableName = "vehicles")
</pre>
The annotation in this example contains a single element named <code style="font-family: Courier; font-size: 1em;">tableName</code>, with the value set to <code style="font-family: Courier; font-size: 1em;">vehicles</code>. Elements are enclosed inside the parentheses after the annotation name. Annotations without elements do not need the parentheses.<br />
An annotation can contain multiple elements. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@Entity(tableName = "vehicles", primaryKey = "id")
</pre>
In case an annotation contains just a single element, it is convention to name that element <code style="font-family: Courier; font-size: 1em;">value</code>, like this:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@InsertNew(value = "yes")
</pre>
When an annotation just contains a single element named <code style="font-family: Courier; font-size: 1em;">value</code>, you can leave out the element name, and just provide the value. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@InsertNew("yes")
</pre>
<a href="https://www.blogger.com/null" name="annotation-placement" style="color: #551a8b; font-weight: 700;"></a><br />
<h2 style="margin-top: 30px;">
Annotation Placement</h2>
You can put Java annotations above classes, interfaces, methods, method parameters, fields and local variables. Here is an example annotation added above a class definition:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@Entity
public class Vehicle {
}
</pre>
The annotation starts with the <code style="font-family: Courier; font-size: 1em;">@</code> character, followed by the name of the annotation. In this case, the annotation name is<code style="font-family: Courier; font-size: 1em;">Entity</code>. The <code style="font-family: Courier; font-size: 1em;">Entity</code> annotation is an annotation I have made up. It doesn't have any meaning in Java.<br />
Here is a bigger example with annotations above both the class, fields, methods, parameters and local variables:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;"><b>@Entity</b>
public class Vehicle {
<b>@Persistent</b>
protected String vehicleName = null;
<b>@Getter</b>
public String getVehicleName() {
return this.vehicleName;
}
public void setVehicleName(<b>@Optional</b> vehicleName) {
this.vehicleName = vehicleName;
}
public List addVehicleNameToList(List names) {
<b>@Optional</b>
List localNames = names;
if(localNames == null) {
localNames = new ArrayList();
}
localNames.add(getVehicleName());
return localNames;
}
}
</pre>
The annotations are again just annotations I have made up. They have no specific meaning in Java.<br />
<a href="https://www.blogger.com/null" name="javas-builtin-annotations" style="color: #551a8b; font-weight: 700;"></a><br />
<h2 style="margin-top: 30px;">
Java's Builtin Annotations</h2>
Java comes with three annotations which are used to give the Java compiler instructions. These annotations are:<br />
<ul>
<li>@Deprecated</li>
<li>@Override</li>
<li>@SuppressWarnings</li>
</ul>
Each of these annotations are explained in the following sections.<br />
<br />
<h3 style="margin-top: 30px;">
@Deprecated</h3>
The <code style="font-family: Courier; font-size: 1em;">@Deprecated</code> annotation is used to mark a class, method or field as deprecated, meaning it should no longer be used. If your code uses deprecated classes, methods or fields, the compiler will give you a warning. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@Deprecated
public class MyComponent {
}
</pre>
The use of the <code style="font-family: Courier; font-size: 1em;">@Deprecated</code> annotation above the class declaration marks the class as deprecated.<br />
You can also use the <code style="font-family: Courier; font-size: 1em;">@Deprecated</code> annotation above method and field declarations, to mark the method or field as deprecated.<br />
When you use the <code style="font-family: Courier; font-size: 1em;">@Deprecated</code> annotation, it is a good idea to also use the corresponding <code style="font-family: Courier; font-size: 1em;">@deprecated</code> JavaDoc symbol, and explain why the class, method or field is deprecated, and what the programmer should use instead.<br />
<a href="https://www.blogger.com/null" name="override" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
@Override</h3>
The <code style="font-family: Courier; font-size: 1em;">@Override</code> annotation is used above methods that override methods in a superclass. If the method does not match a method in the superclass, the compiler will give you an error.<br />
The <code style="font-family: Courier; font-size: 1em;">@Override</code> annotation is not necessary in order to override a method in a superclass. It is a good idea to use it still, though. In case someone changed the name of the overridden method in the superclass, your subclass method would no longer override it. Without the <code style="font-family: Courier; font-size: 1em;">@Override</code> annotation you would not find out. With the <code style="font-family: Courier; font-size: 1em;">@Override</code>annotation the compiler would tell you that the method in the subclass is not overriding any method in the superclass.<br />
Here is an example use of the <code style="font-family: Courier; font-size: 1em;">@Override</code> annotation:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">public class MySuperClass {
public void doTheThing() {
System.out.println("Do the thing");
}
}
public class MySubClass extends MySuperClass{
@Override
public void doTheThing() {
System.out.println("Do it differently");
}
}
</pre>
In case the method <code style="font-family: Courier; font-size: 1em;">doTheThing()</code> in <code style="font-family: Courier; font-size: 1em;">MySuperClass</code> changes signature so that the same method in the supclass no longer overrides it, the compiler will generate an error.<br />
<a href="https://www.blogger.com/null" name="suppress-warnings" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
@SuppressWarnings</h3>
The <code style="font-family: Courier; font-size: 1em;">@SuppressWarnings</code> annotation makes the compiler suppress warnings for a given method. For instance, if a method calls a deprecated method, or makes an insecure type cast, the compiler may generate a warning. You can suppress these warnings by annotating the method containing the code with the <code style="font-family: Courier; font-size: 1em;">@SuppressWarnings</code> annotation.<br />
Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@SuppressWarnings
public void methodWithWarning() {
}
</pre>
<a href="https://www.blogger.com/null" name="creating-your-own-annotations" style="color: #551a8b; font-weight: 700;"></a><br />
<h2 style="margin-top: 30px;">
Creating Your Own Annotations</h2>
It is possible to create your own annotations. Annotations are defined in their own file, just like a Java class or interface. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@interface MyAnnotation {
String value();
String name();
int age();
String[] newNames();
}
</pre>
This example defines an annotation called <code style="font-family: Courier; font-size: 1em;">MyAnnotation</code> which has four elements.<br />
Notice that each element is defined similarly to a method definition in an interface. It has a data type and a name. You can use all primitive data types as element data types. You can also use arrays as data type. You cannot use complex objects as data type.<br />
To use the above annotation, you do like this:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@MyAnnotation(
value="123",
name="Jakob",
age=37,
newNames={"Jenkov", "Peterson"}
)
public class MyClass {
}
</pre>
As you can see, I have to specify values for all elements of the <code style="font-family: Courier; font-size: 1em;">MyAnnotation</code> annotation.<br />
<h3 style="margin-top: 30px;">
Element Default Values</h3>
You can specify default values for an element. That way the element becomes optional and can be left out. Here is an example of how the annotation definition looks with a default value for an element:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@interface MyAnnotation {
String value() default "";
String name();
int age();
String[] newNames();
}
</pre>
The <code style="font-family: Courier; font-size: 1em;">value</code> element can now be left out when using the annotation. If you leave it out, it will be considered as if you had used the default value for the <code style="font-family: Courier; font-size: 1em;">value</code> element. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@MyAnnotation(
name="Jakob",
age=37,
newNames={"Jenkov", "Peterson"}
)
public class MyClass {
}
</pre>
Notice that the <code style="font-family: Courier; font-size: 1em;">value</code> element is no longer present.<br />
<a href="https://www.blogger.com/null" name="retention" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
@Retention</h3>
You can specify for your custom annotation if it should be available at runtime, for inspection via reflection. You do so by annotating your annotation definition with the <code style="font-family: Courier; font-size: 1em;">@Retention</code> annotation. Here is how that is done:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value() default "";
}
</pre>
Notice the annotation added above the <code style="font-family: Courier; font-size: 1em;">MyAnnotation</code> definition:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@Retention(RetentionPolicy.RUNTIME)
</pre>
This is what signals to the compiler and JVM that the annotation should be available via reflection at runtime.<br />
<br />
The <code style="font-family: Courier; font-size: 1em;">RetentionPolicy</code> class contains two more values you can use:<br />
<code style="font-family: Courier; font-size: 1em;">RetentionPolicy.CLASS</code> means that the annotation is stored in the .class file, but not available at runtime. This is the default retention policy, if you do not specify any retention policy at all.<br />
<code style="font-family: Courier; font-size: 1em;">RetentionPolicy.SOURCE</code> means that the annotation is only available in the source code, and not in the .class files and not a runtime. If you create your own annotations for use with build tools that scan the code, you can use this retention policy. That way the .class files are not poluted unnecessarily.<br />
<a href="https://www.blogger.com/null" name="target" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
@Target</h3>
You can specify which Java elements your custom annotation can be used to annotate. You do so by annotating your annotation definition with the <code style="font-family: Courier; font-size: 1em;">@Target</code> annotation. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Target({ElementType.METHOD})
public @interface MyAnnotation {
String value();
}
</pre>
This example shows an annotation that can only be used to annotate methods.<br />
The <code style="font-family: Courier; font-size: 1em;">ElementType</code> class contains the following possible targets:<br />
<ul>
<li>ElementType.ANNOTATION_TYPE</li>
<li>ElementType.CONSTRUCTOR</li>
<li>ElementType.FIELD</li>
<li>ElementType.LOCAL_VARIABLE</li>
<li>ElementType.METHOD</li>
<li>ElementType.PACKAGE</li>
<li>ElementType.PARAMETER</li>
<li>ElementType.TYPE</li>
</ul>
Most of these are self explaining, but a few are not. Therefore is here an explanation of the few that are not obvious.<br />
The <code style="font-family: Courier; font-size: 1em;">ANNOTATION_TYPE</code> target means annotation definitions. Thus, the annotation can only be used to annotate other annotations. Like the <code style="font-family: Courier; font-size: 1em;">@Target</code> and <code style="font-family: Courier; font-size: 1em;">@Retention</code> annotations.<br />
The <code style="font-family: Courier; font-size: 1em;">TYPE</code> target means any type. A type is either a class, interface, enum or annotation.<br />
<a href="https://www.blogger.com/null" name="inherited" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
@Inherited</h3>
The <code style="font-family: Courier; font-size: 1em;">@Inherited</code> annotation signals that a custom annotation used in a class should be inherited by subclasses inheriting from that class. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">java.lang.annotation.Inherited
@Inherited
public @interface MyAnnotation {
}
</pre>
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@MyAnnotation
public class MySuperClass { ... }
</pre>
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">public class MySubClass extends MySuperClass { ... }
</pre>
In this example the class <code style="font-family: Courier; font-size: 1em;">MySubClass</code> inherits the annotation <code style="font-family: Courier; font-size: 1em;">@MyAnnotation</code> because <code style="font-family: Courier; font-size: 1em;">MySubClass</code> inherits from<code style="font-family: Courier; font-size: 1em;">MySuperClass</code>, and <code style="font-family: Courier; font-size: 1em;">MySuperClass</code> has a <code style="font-family: Courier; font-size: 1em;">@MyAnnotation</code> annotation.<br />
<a href="https://www.blogger.com/null" name="documented" style="color: #551a8b; font-weight: 700;"></a><br />
<h3 style="margin-top: 30px;">
@Documented</h3>
The <code style="font-family: Courier; font-size: 1em;">@Documented</code> annotation is used to signal to the JavaDoc tool that your custom annotation should be visible in the JavaDoc for classes using your custom annotation. Here is an example:<br />
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">java.lang.annotation.Documented
@Documented
public @interface MyAnnotation {
}
</pre>
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">@MyAnnotation
public class MySuperClass { ... }
</pre>
When generating JavaDoc for the <code style="font-family: Courier; font-size: 1em;">MySuperClass</code> class, the <code style="font-family: Courier; font-size: 1em;">@MyAnnotation</code> is now included in the JavaDoc.<br />
You will not use the <code style="font-family: Courier; font-size: 1em;">@Documented</code> annotation often, but now you know it exists, if you should need it.<br />
<br />
References:<br />
1. http://tutorials.jenkov.com/java/annotations.html<br />
<br />
<div>
<br /></div>
</div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-81992873599279013782015-02-04T10:27:00.002-08:002015-02-05T07:49:16.805-08:00The Scrabble Algorithm<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"><br /></span>
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">If you are playing scrabble and you have n letters, what are all the possible words that you can make from these n letters? </span><br />
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">It looks as if it would involve </span><b>permutations and combinations</b><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"> </span><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"><i>both</i></span><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"> of the letters or a union of permutations and combinations for n letters. Surprisingly, it is much simpler. </span><br />
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">Below are the possible words for letters 'abc'. I have highlighted the patterns in them.</span><br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 546.022705078125px;"><code><span style="color: #cc0000;">a</span>
<span style="color: #38761d;">ab</span>
<span style="color: blue;">abc</span>
<span style="color: #38761d;">ac</span>
<span style="color: blue;">acb</span>
<span style="color: red;">b</span>
<span style="color: #38761d;">ba</span>
<span style="color: blue;">bac</span>
<span style="color: #38761d;">bc</span>
<span style="color: blue;">bca</span>
<span style="color: red;">c</span>
<span style="color: #38761d;">ca</span>
<span style="color: blue;">cab</span>
<span style="color: #38761d;">cb</span>
<span style="color: blue;">cba</span>
</code></pre>
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">For n letters, it looks like we have to get </span><span style="background-color: white; color: blue; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">permutations of n letters</span><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"> plus </span><span style="background-color: white; color: #38761d; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">permutations of n-1 letters </span><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">and so on till </span><span style="background-color: white; color: red; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">permutations of each letter</span><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"> which is O(nPn) + O(n-1Pn-1) + ... O(1P1). The time complexity is O(n!) + O(n-1!) + ... + O(1!) = O(n!).</span><br />
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">All possible words for n letters can be obtained by using the permutations algorithm of n letters with a </span><i style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">very simple</i><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"> mod in O(n!) time itself. The changes are in blue.</span><br />
<br />
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 546.022705078125px;"><code>permutation(char[] in):
//initially, used[] set to false and depth is 0
permuteWords(in, out, used, 0)
permuteWords(char[] in, buffer out, bool[] used, int depth):
if depth = in.length:
<span style="color: blue;"><strike>print out</strike></span>
return
for (int i = 0; i < in.length; i++):
if used[i] = true:
continue
used[i] = true
out.append(in[i])
<span style="color: blue;">print out</span>
permuteWords(in, used, out, depth +1)
out.length = out.length -1
used[i] = false
</code></pre>
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">Time complexity is the same as that of permutation algorithm O(n!).</span><br />
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">A better way is to have each word added to a list (the list would be created in </span><code style="background-color: white; font-size: 13.63636302948px; line-height: 16.7999992370605px;">permutation</code><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">method and passed as parameter to </span><code style="background-color: white; font-size: 13.63636302948px; line-height: 16.7999992370605px;">permute<span style="color: blue;">Words</span></code><span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"> method) for extensions of the problem. </span><br />
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">Possible extensions to this scrabble problem are: </span><br />
<ul style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px; margin: 0.5em 0px; padding: 0px 2.5em;">
<li style="margin: 0px 0px 0.25em; padding: 0px;">Find the longest word. </li>
<li style="margin: 0px 0px 0.25em; padding: 0px;">Find the word with maximum points given a map of points to each letter.</li>
<li style="margin: 0px 0px 0.25em; padding: 0px;">You have a dictionary, how would you use it to get the valid words? For this, I would assume that the dictionary has a function like boolean isValidWord(String s) which would indicate if a particular word is valid or not. Here is another interesting thought with the dictionary extension which requires a <b>trie</b> structure. I could use a dictionary, to see if I can bust out of permuting further as <i>mostly</i> not all permutations are going to be valid words. For example, if you have a dictionary implemented as a trie with a function boolean hasPrefix(String s) or hasPrefix(char c) and letters 'aabc' and suppose that the dictionary does not have a trieNode structure a-a, you do not have to permute further for the unused characters 'bc'. Below is the changed code that I came up with (not tested). </li>
</ul>
<pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); font-family: 'Andale Mono', 'Lucida Console', Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 546.022705078125px;"><code>Set getValidWordsPermutation(char[] in):
Set validWords = new HashSet();
//initially, used[] set to false and depth is 0
permute<span style="color: blue;">Words</span>(in, out, used, 0, <span style="color: blue;">validWords</span>);
return validWords;
permute<span style="color: blue;">Words</span>(char[] in, buffer out, bool[] used, int depth, <span style="color: blue;">Set validWords</span>):
// if the current prefix is not present in the dictionary, then do not permute further with the rest of the unused letters
<span style="color: blue;">if !dict.hasPrefix(out): //or dict.hasPrefix(out[out.length()])
return</span>
if depth = in.length:
return
for (int i = 0; i < in.length; i++):
if used[i] = true:
continue
used[i] = true
out.append(in[i])
//if current prefix is a word, add to the set
<span style="color: blue;">if dict.isValidWord(out):
validWords.add(out)</span>
permute<span style="color: blue;">Words</span>(in, used, out, <b>depth +1</b>, <span style="color: blue;">validWords</span>)
out.length = out.length -1
used[i] = false
</code></pre>
<br style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;" />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;">Using the dictionary, only those trieNodes forming valid words possible from the n letters are traversed in the dictionary trie [O(|V|+|E|) time] and not all permutations [O(n!) time]. If all permutations are valid words OR if there is no such hasPrefix(String s) function for trie, then it takes O(n!) time - which is better than O(nCn * n!) + O(nCn-1 * n-1!) + ... O(nC1 * 1!). </span><br />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"><br /></span>
<br />
Checkout below for a trie based dictionary which hasValidPrefix() and findWordMeaning() methods implemented
<br />
<script src="http://pastebin.com/embed_js.php?i=FGFwkpsS"></script>
<br />
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"><br /></span>
<span style="background-color: white; font-family: Arial, Tahoma, Helvetica, FreeSans, sans-serif; font-size: 13.63636302948px; line-height: 16.7999992370605px;"><br /></span></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-72134156849841099602015-02-03T22:32:00.004-08:002015-02-18T02:00:15.439-08:00JVM internals<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<b><br /></b>
<b>T</b>his article explains the internal architecture of the Java Virtual Machine (JVM). The following diagram show the key internal components of a typical JVM that conforms to <i><a href="http://www.amazon.co.uk/Virtual-Machine-Specification-Edition-Series/dp/0133260445" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">The Java Virtual Machine Specification Java SE 7 Edition</a>.</i></div>
<a class="image_link" href="http://blog.jamesdbloom.com/images_2013_11_17_17_56/JVM_Internal_Architecture.png" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; display: block; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em; outline: 0px; padding: 0px; position: relative; text-align: justify; text-decoration: none; vertical-align: baseline;"><img alt="internal architecture of Java Virtual Machine (JVM)" src="http://blog.jamesdbloom.com/images_2013_11_17_17_56/JVM_Internal_Architecture_small.png" id="JVM_Internal_Architecture_image" itemprop="image" style="background: transparent; border: 0px; clear: both; display: block; margin: 0px auto; max-width: 100%; outline: 0px; padding: 1em 0px; vertical-align: baseline; width: 617.796875px;" /></a><br />
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The components shown on this diagram are each explained below in two sections. First section covers the components that are created for each thread and the second section covers the components that are created independently of threads.</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<h3 style="text-align: left;">
<b><u><span style="color: black;">Threads</span></u></b></h3>
<ol style="text-align: left;">
<li><b><span style="color: black;">JVM System Threads</span></b></li>
<li><b><span style="color: black;">Per Thread</span></b></li>
<li><b><span style="color: black;">program Counter (PC)</span></b></li>
<li><b><span style="color: black;">Stack</span></b></li>
<li><b><span style="color: black;">Native Stack</span></b></li>
<li><b><span style="color: black;">Stack Restrictions</span></b></li>
<li><b><span style="color: black;">Frame</span></b></li>
<li><b><span style="color: black;">Local Variables Array</span></b></li>
<li><b><span style="color: black;">Operand Stack</span></b></li>
<li><b><span style="color: black;">Dynamic Linking</span></b></li>
</ol>
</div>
<span style="color: black;"><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#per_thread" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#program_counter" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#stack" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#native_stack" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#stack_restrictions" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#frame" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#local_variables_array" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#operand_stack" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a><a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#dynamic_linking" style="background-attachment: initial; background-clip: initial; background-image: initial; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; border: 0px; line-height: 1.5em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"></a></span><br />
<h3 style="text-align: left;">
<u><span style="color: black;">Shared Between Threads</span></u></h3>
<ol style="text-align: left;">
<li><b><span style="color: black;">Heap</span></b></li>
<li><b><span style="color: black;">Memory Management</span></b></li>
<li><b><span style="color: black;">Non-Heap Memory</span></b></li>
<li><b><span style="color: black;">Just In Time (JIT) Compilation</span></b></li>
<li><b><span style="color: black;">Method Area</span></b></li>
<li><b><span style="color: black;">Class File Structure</span></b></li>
<li><b><span style="color: black;">Classloader</span></b></li>
<li><b><span style="color: black;">Faster Class Loading</span></b></li>
<li><b><span style="color: black;">Where Is The Method Area</span></b></li>
<li><b><span style="color: black;">Classloader Reference</span></b></li>
<li><b><span style="color: black;">Run Time Constant Pool</span></b></li>
<li><b><span style="color: black;">Exception Table</span></b></li>
<li><b><span style="color: black;">Symbol Table</span></b></li>
<li><b><span style="color: black;">Interned Strings (String Table)</span></b></li>
</ol>
<h2 id="threads" style="background: rgb(255, 255, 255); border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 2.17em; line-height: 1.15em; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<u>
Thread</u></h2>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
A thread is a thread of execution in a program. The JVM allows an application to have multiple threads of execution running concurrently. In the Hotspot JVM there is a direct mapping between a Java Thread and a native operating system Thread. After preparing all of the state for a Java thread such as thread-local storage, allocation buffers, synchronization objects, stacks and the program counter, the native thread is created. The native thread is reclaimed once the Java thread terminates. The operating system is therefore responsible for scheduling all threads and dispatching them to any available CPU. Once the native thread has initialized it invokes the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">run()</span> method in the Java thread. When the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">run()</span> method returns, uncaught exceptions are handled, then the native thread confirms if the JVM needs to be terminated as a result of the thread terminating (i.e. is it the last non-deamon thread). When the thread terminates all resources for both the native and Java thread are released.</div>
<h3 id="jvm_system_threads" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
JVM System Threads</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
If you use jconsole or any debugger it is possible to see there are numerous threads running in the background. These background threads run in addition to the main thread, which is created as part of invoking <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;"><span class="keyword" style="background: transparent; border: 0px; color: #0000a3; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public static void </span>main(String[])</span>, and any threads created by the main thread. The main background system threads in the Hotspot JVM are:</div>
<div class="columns" style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline; width: 684.09375px;">
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
VM thread</div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This thread waits for operations to appear that require the JVM to reach a safe-point. The reason these operations have to happen on a separate thread is because they all require the JVM to be at a safe point where modifications to the heap can not occur. The type of operations performed by this thread are "stop-the-world" garbage collections, thread stack dumps, thread suspension and biased locking revocation.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
Periodic task thread</div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This thread is responsible for timer events (i.e. interrupts) that are used to schedule execution of periodic operations</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
GC threads</div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
These threads support the different types of garbage collection activities that occur in the JVM</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
Compiler threads</div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
These threads compile byte code to native code at runtime</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
Signal dispatcher thread</div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This thread receives signals sent to the JVM process and handle them inside the JVM by calling the appropriate JVM methods.</div>
</div>
</div>
<h2 id="per_thread" style="background: rgb(255, 255, 255); border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 2.17em; line-height: 1.15em; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<u>
Per Thread</u></h2>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Each thread of execution has the following components:</div>
<h3 id="program_counter" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Program Counter (PC)</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Address of the current instruction (or opcode) unless it is native. If the current method is native then the PC is undefined. All CPUs have a PC, typically the PC is incremented after each instruction and therefore holds the address of the next instruction to be executed. The JVM uses the PC to keep track of where it is executing instructions, the PC will in fact be pointing at a memory address in the Method Area.</div>
<h3 id="stack" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Stack</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Each thread has its own stack that holds a frame for each method executing on that thread. The stack is a Last In First Out (LIFO) data structure, so the currently executing method is at the top of the stack. A new frame is created and added (pushed) to the top of stack for every method invocation. The frame is removed (popped) when the method returns normally or if an uncaught exception is thrown during the method invocation. The stack is not directly manipulated, except to push and pop frame objects, and therefore the frame objects may be allocated in the Heap and the memory does not need to be contiguous.</div>
<h3 id="native_stack" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Native Stack</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Not all JVMs support native methods, however, those that do typically create a per thread native method stack. If a JVM has been implemented using a C-linkage model for Java Native Invocation (JNI) then the native stack will be a C stack. In this case the order of arguments and return value will be identical in the native stack to typical C program. A native method can typically (depending on the JVM implementation) call back into the JVM and invoke a Java method. Such a native to Java invocation will occur on the stack (normal Java stack); the thread will leave the native stack and create a new frame on the stack (normal Java stack).</div>
<h3 id="stack_restrictions" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Stack Restrictions</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
A stack can be a dynamic or fixed size. If a thread requires a larger stack than allowed a StackOverflowError is thrown. If a thread requires a new frame and there isn’t enough memory to allocate it then an OutOfMemoryError is thrown.</div>
<h3 id="frame" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Frame</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
A new frame is created and added (pushed) to the top of stack for every method invocation. The frame is removed (popped) when the method returns normally or if an uncaught exception is thrown during the method invocation. For more detail on exception handling <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#exception_table" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">see the section on Exception Tables below</a>.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Each frame contains:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Local variable array</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Return value</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Operand stack</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Reference to runtime constant pool for class of the current method</li>
</ul>
</div>
<h3 id="local_variables_array" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Local Variables Array</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The array of local variables contains all the variables used during the execution of the method, including a reference to <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">this</span>, all method parameters and other locally defined variables. For class methods (i.e. static methods) the method parameters start from zero, however, for instance method the zero slot is reserved for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">this</span>.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
A local variable can be:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">boolean</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">byte</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">char</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">long</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">short</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">int</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">float</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">double</span></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">reference</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">returnAddress</li>
</ul>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
All types take a single slot in the local variable array except <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">long</span> and <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">double</span> which both take two consecutive slots because these types are double width (64-bit instead of 32-bit).</div>
<h3 id="operand_stack" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Operand Stack</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The operand stack is used during the execution of byte code instructions in a similar way that general-purpose registers are used in a native CPU. Most JVM byte code spends its time manipulating the operand stack by pushing, popping, duplicating, swapping, or executing operations that produce or consume values. Therefore, instructions that move values between the array of local variables and the operand stack are very frequent in byte code. For example, a simple variable initialization results in two byte codes that interact with the operand stack.</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;"><span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">int</span> i;</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Gets compiled to the following byte code:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;"><span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"> 0</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">iconst_0</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Push 0 to top of the operand stack</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">1</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">istore_1</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Pop value from top of operand stack and store as local variable 1</span></pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
For more detail explaining interactions between the local variables array, operand stack and run time constant pool <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#class_file_structure" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">see the section on Class File Structure below</a>.</div>
<h3 id="dynamic_linking" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Dynamic Linking</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Each frame contains a reference to the runtime constant pool. The reference points to the constant pool for the class of the method being executed for that frame. This reference helps to support dynamic linking.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
C/C++ code is typically compiled to an object file then multiple object files are linked together to product a usable artifact such as an executable or dll. During the linking phase symbolic references in each object file are replaced with an actual memory address relative to the final executable. In Java this linking phase is done dynamically at runtime.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
When a Java class is compiled, all references to variables and methods are stored in the class's constant pool as a symbolic reference. A symbolic reference is a logical reference not a reference that actually points to a physical memory location. The JVM implementation can choose when to resolve symbolic references, this can happen when the class file is verified, after being loaded, called eager or static resolution, instead this can happen when the symbolic reference is used for the first time called lazy or late resolution. However the JVM has to behave as if the resolution occurred when each reference is first used and throw any resolution errors at this point. Binding is the process of the field, method or class identified by the symbolic reference being replaced by a direct reference, this only happens once because the symbolic reference is completely replaced. If the symbolic reference refers to a class that has not yet been resolved then this class will be loaded. Each direct reference is stored as an offset against the storage structure associated with the runtime location of the variable or method.</div>
<h2 id="shared_between_threads" style="background: rgb(255, 255, 255); border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 2.17em; line-height: 1.15em; margin: 0.5em 0px 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<u>
Shared Between Threads</u></h2>
<h3 id="heap" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 2em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Heap</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The Heap is used to allocate class instances and arrays at runtime. Arrays and objects can never be stored on the stack because a frame is not designed to change in size after it has been created. The frame only stores references that point to objects or arrays on the heap. Unlike primitive variables and references in the local variable array (in each frame) objects are always stored on the heap so they are not removed when a method ends. Instead objects are only removed by the garbage collector.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
To support garbage collection the heap is divided into three sections:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Young Generation</strong><div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Often split between Eden and Survivor</li>
</ul>
</div>
</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Old Generation</strong> (also called Tenured Generation)</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Permanent Generation</strong></li>
</ul>
</div>
<h3 id="memory_management" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Memory Management</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Objects and Arrays are never explicitly de-allocated instead the garbage collector automatically reclaims them.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Typically this works as follows:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ol class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">New objects and arrays are created into the young generation</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Minor garbage collection will operate in the young generation. Objects, that are still alive, will be moved from the eden space to the survivor space.</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Major garbage collection, which typically causes the application threads to pause, will move objects between generations. Objects, that are still alive, will be moved from the young generation to the old (tenured) generation.</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">The permanent generation is collected every time the old generation is collected. They are both collected when either becomes full.</li>
</ol>
</div>
<h3 id="non_heap_memory" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Non-Heap Memory</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Objects that are logically considered as part of the JVM mechanics are not created on the Heap.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The non-heap memory includes:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Permanent Generation</strong> that contains<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">the method area</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">interned strings</li>
</ul>
</div>
</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Code Cache</strong> used for compilation and storage of methods that have been compiled to native code by the JIT compiler</li>
</ul>
</div>
<h3 id="jit_compilation" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Just In Time (JIT) Compilation</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
Java byte code is interpreted however this is not as fast as directly executing native code on the JVM’s host CPU. To improve performance the Oracle Hotspot VM looks for “hot” areas of byte code that are executed regularly and compiles these to native code. The native code is then stored in the code cache in non-heap memory. In this way the Hotspot VM tries to choose the most appropriate way to trade-off the extra time it takes to compile code verses the extra time it take to execute interpreted code.</div>
<h3 id="method_area" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Method Area</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The method area stores per-class information such as:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Classloader Reference</strong></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Run Time Constant Pool</strong><div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Numeric constants</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Field references</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Method References</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Attributes</li>
</ul>
</div>
</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Field data</strong><div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Per field<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Name</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Type</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Modifiers</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Attributes</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Method data</strong><div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Per method<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Name</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Return Type</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Parameter Types (in order)</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Modifiers</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Attributes</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Method code</strong><div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Per method<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Bytecodes</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Operand stack size</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Local variable size</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Local variable table</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Exception table<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Per exception handler<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Start point</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">End point</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">PC offset for handler code</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Constant pool index for exception class being caught</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
</li>
</ul>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
All threads share the same method area, so access to the method area data and the process of dynamic linking must be thread safe. If two threads attempt to access a field or method on a class that has not yet been loaded it must only be loaded once and both threads must not continue execution until it has been loaded.</div>
<h3 id="class_file_structure" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Class File Structure</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
A compiled class file consists of the following structure:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;">ClassFile {
u4 magic;
u2 minor_version;
u2 major_version;
u2 constant_pool_count;
cp_info contant_pool[constant_pool_count – 1];
u2 access_flags;
u2 this_class;
u2 super_class;
u2 interfaces_count;
u2 interfaces[interfaces_count];
u2 fields_count;
field_info fields[fields_count];
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}</pre>
<div class="columns" style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline; width: 684.09375px;">
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">magic, minor_version, major_version</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
specifies information about the version of the class and the version of the JDK this class was compiled for.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">constant_pool</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
similar to a symbol table although it contains more data <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#constant_pool" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">this is described in more detail below.</a></div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">access_flags</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
provides the list of modifiers for this class.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">this_class</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
index into the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">constant_pool</span> providing the fully qualified name of this class i.e.<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">org/jamesdbloom/foo/Bar</span></div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">super_class</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
index into the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">constant_pool</span> providing a symbolic reference to the super class i.e.<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">java/lang/Object</span></div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">interfaces</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
array of indexes into the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">constant_pool</span>providing a symbolic references to all interfaces that have been implemented.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">fields</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
array of indexes into the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">constant_pool</span>giving a complete description of each field.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">methods</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
array of indexes into the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">constant_pool</span>giving a complete description of each method signature, if the method is not abstract or native then the bytecode is also present.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">attributes</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
array of different value that provide additional information about the class including any annotations with <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">RetentionPolicy.<span class="final_value" style="background: transparent; border: 0px; color: darkred; font-style: italic; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">CLASS</span></span> or<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">RetentionPolicy.<span class="final_value" style="background: transparent; border: 0px; color: darkred; font-style: italic; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">RUNTIME</span></span></div>
</div>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
It is possible to view the byte code in a compiled Java class by using the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">javap</span>command.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
If you compile the following simple class:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;"><span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">package</span> org.jvminternals;
<span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class</span> SimpleClass {
<span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">void</span> sayHello() {
System.out.println(<span class="constant_value" style="background: transparent; border: 0px; color: green; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Hello"</span>);
}
}</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px 0.25em; position: relative; text-align: justify; vertical-align: baseline;">
Then you get the following output if you run:</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px 1em; position: relative; vertical-align: baseline;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">javap -v -p -s -sysinfo -constants classes/org/jvminternals/SimpleClass.class</span></div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;"><span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class</span> org.jvminternals.SimpleClass
SourceFile: <span class="constant_value" style="background: transparent; border: 0px; color: green; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"SimpleClass.java"</span>
minor version: 0
major version: 51
flags: <span class="final_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; font-style: italic; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ACC_PUBLIC</span>, <span class="final_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; font-style: italic; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ACC_SUPER</span>
Constant pool:
#1 = Methodref #6.#17 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/Object.<span class="constant_value" style="background: transparent; border: 0px; color: green; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"<init>"</span>:()V</span>
#2 = Fieldref #18.#19 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/System.out:Ljava/io/PrintStream;</span>
#3 = String #20 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// </span><span class="constant_value" style="background: transparent; border: 0px; color: green; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Hello"</span>
#4 = Methodref #21.#22 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/io/PrintStream.println:(Ljava/lang/String;)V</span>
#5 = Class #23 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// org/jvminternals/SimpleClass</span>
#6 = Class #24 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/Object</span>
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lorg/jvminternals/SimpleClass;
#14 = Utf8 sayHello
#15 = Utf8 SourceFile
#16 = Utf8 SimpleClass.java
#17 = NameAndType #7:#8 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// <span class="constant_value" style="background: transparent; border: 0px; color: green; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"<init>"</span>:()V</span>
#18 = Class #25 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/System</span>
#19 = NameAndType #26:#27 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// out:Ljava/io/PrintStream;</span>
#20 = Utf8 Hello
#21 = Class #28 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/io/PrintStream</span>
#22 = NameAndType #29:#30 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// println:(Ljava/lang/String;)V</span>
#23 = Utf8 org/jvminternals/SimpleClass
#24 = Utf8 java/lang/Object
#25 = Utf8 java/lang/System
#26 = Utf8 out
#27 = Utf8 Ljava/io/PrintStream;
#28 = Utf8 java/io/PrintStream
#29 = Utf8 println
#30 = Utf8 (Ljava/lang/String;)V
{
<span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> org.jvminternals.SimpleClass();
Signature: ()V
flags: <span class="final_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; font-style: italic; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ACC_PUBLIC</span>
Code:
stack=1, locals=1, args_size=1
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">aload_0</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">1</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">invokespecial</span> <span class="this_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">#1</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Method java/lang/Object.<span class="constant_value" style="background: transparent; border: 0px; color: green; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"<init>"</span>:()V</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">4</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">return</span>
LineNumberTable:
line 3: <span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>
LocalVariableTable:
Start Length Slot Name Signature
0 5 0 <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">this</span> Lorg/jvminternals/SimpleClass;
<span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">void</span> sayHello();
Signature: ()V
flags: <span class="final_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; font-style: italic; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ACC_PUBLIC</span>
Code:
stack=2, locals=1, args_size=1
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">getstatic </span> <span class="this_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">#2</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Field java/lang/System.out:Ljava/io/PrintStream;</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">3</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">ldc </span> <span class="this_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">#3</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// String </span><span class="constant_value" style="background: transparent; border: 0px; color: green; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Hello"</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">5</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">invokevirtual </span> <span class="this_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">#4</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Method java/io/PrintStream.println:(Ljava/lang/String;)V</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">8</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">return</span>
LineNumberTable:
line 6: <span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>
line 7: <span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">8</span>
LocalVariableTable:
Start Length Slot Name Signature
0 9 0 <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">this</span> Lorg/jvminternals/SimpleClass;
}</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
This class file shows three main sections the constant pool, the constructor and the sayHello method.</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Constant Pool</strong> – this provides the same information that a symbol table typically provides and is <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#constant_pool" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">described in more detail below.</a></li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;"><strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Methods</strong> – each containing four areas:<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ul style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">signature and access flags</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">byte code</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">LineNumberTable – this provides information to a debugger to indicate which line corresponds to which byte code instruction, for example line 6 in the Java code corresponds to byte code 0 in the sayHello method and line 7 corresponds to byte code 8.</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">LocalVariableTable – this lists all local variables provided in the frame, in both examples the only local variable is <span clas="code inline keyword" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">this</span>.</li>
</ul>
</div>
</li>
</ul>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The following byte code operands are used in this class file</div>
<div class="columns" style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline; width: 684.09375px;">
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">aload_0</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This opcode is one of a group of opcodes with the format <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">aload_<n></span>. They all load an object reference into the operand stack. The <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;"><n></span>refers to the location in the local variable array that is being accessed but can only be 0, 1, 2 or 3. There are other similar opcodes for loading values that are not an object reference<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">iload_<n></span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">lload_<n></span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">float_<n></span> and<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">dload_<n></span> where i is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">int</span>, l is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">long</span>, f is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">float</span> and d is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">double</span>. Local variables with an index higher than 3 can be loaded using <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">iload</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">lload</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">float</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">dload</span> and<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">aload</span>. These opcodes all take a single operand that specifies the index of local variable to load.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">ldc</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This opcode is used to push a constant from the run time constant pool into the operand stack.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">getstatic</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This opcode is used to push a static value from a static field listed in the run time constant pool into the operand stack.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokespecial, invokevirtual</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
These opcodes are in a group of opcodes that invoke methods these are <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokedynamic</span>,<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokeinterface</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokespecial</span>,<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokestatic</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokevirtual</span>. In this class file <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokespecial</span> and <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokevirutal</span> are both used the difference between these is that<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokevirutal</span> invokes a method based on the class of the object. The <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokespecial</span>instruction is used to invoke instance initialization methods as well as private methods and methods of a superclass of the current class.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">return</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
This opcode is in a group of opcodes <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">ireturn</span>,<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">lreturn</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">freturn</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">dreturn</span>, <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">areturn</span> and<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">return</span>. Each of these opcodes are a typed return statement that returns a different type where i is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">int</span>, l is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">long</span>, f is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">float</span>, d is for <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">double</span> and a is for an object reference. The opcode with no leading type letter <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">return</span>only returns <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">void</span>.</div>
</div>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
As in any typical byte code the majority of the operands interact with the local variables, operand stack and run time constant pool as follows.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The constructor has two instructions first <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">this</span> is pushed onto the operand stack, next the constructor for the super class is invoked which consumes the value off<span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">this</span> and therefore pops it off the operand stack.</div>
<a class="image_link" href="http://blog.jamesdbloom.com/images_2013_11_17_17_56/bytecode_explanation_SimpleClass.png" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; display: block; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em; outline: 0px; padding: 0px; position: relative; text-align: justify; text-decoration: none; vertical-align: baseline;"><img alt="local variables, operand stack and run time constant pool changes when invoking a static method in Java Virtual Machine (JVM)" src="http://blog.jamesdbloom.com/images_2013_11_17_17_56/bytecode_explanation_SimpleClass.png" id="bytecode_explanation_SimpleClass_image" itemprop="image" style="background: transparent; border: 0px; clear: both; display: block; margin: 0px auto; max-width: 100%; outline: 0px; padding: 1em 0px; vertical-align: baseline; width: 617.796875px;" /></a><br />
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">sayHello()</span> method is more complex as it has to resolve symbolic references to actual references using the run time constant pool, <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#dynamic_linking" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">as explained in more detail above</a>. The first operand <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">getstatic</span> is used to push a reference to the static field<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">out</span> of the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">System</span> class on to the operand stack. The next operand <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">ldc</span> pushes the string <span class="code inline constant_value" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: green; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">"Hello"</span> onto the operand stack. The final operand <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokevirtual</span>invokes the <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">println</span> method of <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">System.out</span> which pops <span class="code inline constant_value" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: green; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">"Hello"</span> off the operand stack as an argument and creates a new frame for the current thread.</div>
<a class="image_link" href="http://blog.jamesdbloom.com/images_2013_11_17_17_56/bytecode_explanation_sayHello.png" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; display: block; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em; outline: 0px; padding: 0px; position: relative; text-align: justify; text-decoration: none; vertical-align: baseline;"><img alt="local variables, operand stack and run time constant pool changes when calling System.out in Java Virtual Machine (JVM)" src="http://blog.jamesdbloom.com/images_2013_11_17_17_56/bytecode_explanation_sayHello_smaller.png" id="bytecode_explanation_sayHello_image" itemprop="image" style="background: transparent; border: 0px; clear: both; display: block; margin: 0px auto; max-width: 100%; outline: 0px; padding: 1em 0px; vertical-align: baseline; width: 617.796875px;" /></a><br />
<h3 id="classloader" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
<u>
Classloader</u></h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The JVM starts up by loading an initial class using the bootstrap classloader. The class is then linked and initialized before <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;"><span class="keyword" style="background: transparent; border: 0px; color: #0000a3; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public static void</span> main(String[])</span> is invoked. The execution of this method will in turn drive the loading, linking and initialization of additional classes and interfaces as required.<br />
<br /></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Loading</strong> is the process of finding the class file that represents the class or interface type with a particular name and reading it into a byte array. Next the bytes are parsed to confirm they represent a <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Class</span> object and have the correct major and minor versions. Any class or interface named as a direct superclass is also loaded. Once this is completed a class or interface object is created from the binary representation.<br />
<br /></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Linking</strong> is the process of taking a class or interface verifying and preparing the type and its direct superclass and superinterfaces. Linking consists of three steps verifying, preparing and optionally resolving.</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px auto; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline; width: 649.875px;">
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Verifying</em></strong> is the process of confirming the class or interface representation is structurally correct and obeys the semantic requirements of the Java programming language and JVM, for example the following checks are performed:</div>
<div style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<ol style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">consistent and correctly formatted symbol table</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">final methods / classes not overridden</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">methods respect access control keywords</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">methods have correct number and type of parameters</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">bytecode doesn't manipulate stack incorrectly</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">variables are initialized before being read</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: decimal; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">variables are a value of the correct type</li>
</ol>
</div>
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
Performing these checks during the verifying stages means these checks do not need to be performed at runtime. Verification during linking slows down class loading however it avoids the need to perform these checks multiple when executing the bytecode.</div>
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Preparing</em></strong> involves allocation of memory for static storage and any data structures used by the JVM such as method tables. Static fields are created and initialized to their default values, however, no initializers or code is executed at this stage as that happens as part of initialization.</div>
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Resolving</em></strong> is an optional stage which involves checking symbolic references by loading the referenced classes or interfaces and checking the references are correct. If this does not take place at this point the resolution of symbolic references can be deferred until just prior to their use by a byte code instruction.</div>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Initialization</strong> of a class or interface consists of executing the class or interface initialization method <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;"><clinit></span></div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://blog.jamesdbloom.com/images_2013_11_17_17_56/Class_Loading_Linking_Initializing.png" id="Class_Loading_Linking_Initializing_image" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; display: block; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 0px auto; outline: 0px; padding: 0px; position: relative; text-align: justify; text-decoration: none; vertical-align: baseline; width: 239.421875px;"><img alt="class loading, linking and initialization in the Java Virtual Machine (JVM)" src="http://blog.jamesdbloom.com/images_2013_11_17_17_56/Class_Loading_Linking_Initializing.png" height="320" itemprop="image" style="background: transparent; border: 0px; margin: 0px; max-width: 100%; outline: 0px; padding: 1em 0px; vertical-align: baseline;" width="224" /></a></div>
<br />
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
In the JVM there are multiple classloaders with different roles. Each classloader delegates to its parent classloader (that loaded it) except the <strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">bootstrap classloader</strong>which is the top classloader.</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px auto; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline; width: 649.875px;">
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Bootstrap Classloader</em></strong> is usually implemented as native code because it is instantiated very early as the JVM is loaded. The bootstrap classloader is responsible for loading the basic Java APIs, including for example rt.jar. It only loads classes found on the boot classpath which have a higher level of trust; as a result it skips much of the validation that gets done for normal classes.</div>
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Extension Classloader</em></strong> loads classes from standard Java extension APIs such as security extension functions.</div>
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">System Classloader</em></strong> is the default application classloader, which loads application classes from the classpath.</div>
<div style="background: transparent; border-radius: 0.25em; border: 0px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; vertical-align: baseline;">
<strong style="background: transparent; border: 0px; font-family: Helvetica, arial, freesans, clean, sans-serif; font-size: 0.9em; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;"><em style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">User Defined Classloaders</em></strong> can alternatively be used to load application classes. A user defined classloader is used for a number of special reasons including run time reloading of classes or separation between different groups of loaded classes typically required by web servers such as Tomcat.</div>
</div>
<div class="separator" style="clear: both; text-align: center;">
<a href="http://blog.jamesdbloom.com/images_2013_11_17_17_56/class_loader_hierarchy.png" id="class_loader_hierarchy_image" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; display: block; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 0px auto; outline: 0px; padding: 0px; position: relative; text-align: justify; text-decoration: none; vertical-align: baseline; width: 615.671875px;"><img alt="classloader hierarchy in the Java Virtual Machine (JVM)" src="http://blog.jamesdbloom.com/images_2013_11_17_17_56/class_loader_hierarchy.png" height="254" itemprop="image" style="background: transparent; border: 0px; margin: 0px; max-width: 100%; outline: 0px; padding: 1em 0px; vertical-align: baseline;" width="400" /></a></div>
<br />
<h3 id="faster_class_loading" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Faster Class Loading</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
A feature called Class Data Sharing (CDS) was introduce in HotSpot JMV from version 5.0. During the installation process of the JVM the installer loads a set of key JVM classes, such as rt.jar, into a memory-mapped shared archive. CDS reduces the time it takes to load these classes improving JVM start-up speed and allows these classes to be shared between different instances of the JVM reducing the memory footprint.</div>
<h3 id="where_is_the_method_area" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Where Is The Method Area</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
<a href="http://www.amazon.co.uk/Virtual-Machine-Specification-Edition-Series/dp/0133260445" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">The Java Virtual Machine Specification Java SE 7 Edition</a> clearly states: “Although the method area is logically part of the heap, simple implementations may choose not to either garbage collect or compact it.” In contradiction to this jconsole for the Oracle JVM shows the method area (and code cache) as being non-heap. The <a href="http://openjdk.java.net/" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">OpenJDK</a> code shows that the CodeCache is a separate field of the VM to the ObjectHeap.</div>
<h3 id="classloader_reference" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Classloader Reference</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
All classes that are loaded contain a reference to the classloader that loaded them. In turn the classloader also contains a reference to all classes that it has loaded.</div>
<h3 id="constant_pool" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Run Time Constant Pool</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The JVM maintains a per-type constant pool, a run time data structure that is similar to a symbol table although it contains more data. Byte codes in Java require data, often this data is too large to store directly in the byte codes, instead it is stored in the constant pool and the byte code contains a reference to the constant pool. The run time constant pool is used in dynamic linking <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#dynamic_linking" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">as described above</a></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Several types of data is stored in the constant pool including</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">numeric literals</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">string literals</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">class references</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">field references</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">method references</li>
</ul>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
For example the following code:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;">Object foo = <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">new</span> Object();</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Would be written in byte code as follows:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;"> <span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">0</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">new</span> <span class="this_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">#2</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Class java/lang/Object</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">1</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">dup</span>
<span class="annotation" style="background: transparent; border: 0px; color: darkkhaki; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">2</span>: <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">invokespecial</span> <span class="this_value" style="background: transparent; border: 0px; color: darkred; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">#3</span> <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// Method java/ lang/Object <span class="constant_value" style="background: transparent; border: 0px; color: green; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"<init>"</span>( ) V</span></pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">new</span> opcode (operand code) is followed by the <span class="code inline this_value" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: darkred; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">#2</span> operand. This operand is an index into the constant pool and therefore is referencing the second entry in the constant pool. The second entry is a class reference, this entry in turn references another entry in the constant pool containing the name of the class as a constant UTF8 string with the value <span class="code inline attribute_name" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #b2b2b2; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">// Class java/lang/Object</span>. This symbolic link can then be used to lookup the class for java.lang.Object. The <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">new</span> opcode creates a class instance and initializes its variables. A reference to the new class instance is then added to the operand stack. The <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">dup</span> opcode then creates an extra copy of the top reference on the operand stack and adds this to the top of the operand stack. Finally an instance initialization method is called on line 2 by <span class="code inline keyword" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); color: #0000a3; display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">invokespecial</span>. This operand also contains a reference to the constant pool. The initialization method consumes (pops) the top reference off the operand pool as an argument to the method. At the end there is one reference to the new object that has been both created and initialized.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
If you compile the following simple class:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;"><span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">package</span> org.jvminternals;
<span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">class</span> SimpleClass {
<span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">public</span> <span class="keyword" style="background: transparent; border: 0px; color: #0000a3; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">void</span> sayHello() {
System.out.println(<span class="constant_value" style="background: transparent; border: 0px; color: green; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Hello"</span>);
}
}</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The constant pool in the generated class file would look like:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;">Constant pool:
#1 = Methodref #6.#17 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/Object.<span class="constant_value" style="background: transparent; border: 0px; color: green; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"<init>"</span>:()V</span>
#2 = Fieldref #18.#19 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/System.out:Ljava/io/PrintStream;</span>
#3 = String #20 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// </span><span class="constant_value" style="background: transparent; border: 0px; color: green; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"Hello"</span>
#4 = Methodref #21.#22 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/io/PrintStream.println:(Ljava/lang/String;)V</span>
#5 = Class #23 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// org/jvminternals/SimpleClass</span>
#6 = Class #24 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/Object</span>
#7 = Utf8 <init>
#8 = Utf8 ()V
#9 = Utf8 Code
#10 = Utf8 LineNumberTable
#11 = Utf8 LocalVariableTable
#12 = Utf8 this
#13 = Utf8 Lorg/jvminternals/SimpleClass;
#14 = Utf8 sayHello
#15 = Utf8 SourceFile
#16 = Utf8 SimpleClass.java
#17 = NameAndType #7:#8 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// <span class="constant_value" style="background: transparent; border: 0px; color: green; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">"<init>"</span>:()V</span>
#18 = Class #25 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/lang/System</span>
#19 = NameAndType #26:#27 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// out:Ljava/io/PrintStream;</span>
#20 = Utf8 Hello
#21 = Class #28 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// java/io/PrintStream</span>
#22 = NameAndType #29:#30 <span class="attribute_name" style="background: transparent; border: 0px; color: #b2b2b2; font-size: 14px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">// println:(Ljava/lang/String;)V</span>
#23 = Utf8 org/jvminternals/SimpleClass
#24 = Utf8 java/lang/Object
#25 = Utf8 java/lang/System
#26 = Utf8 out
#27 = Utf8 Ljava/io/PrintStream;
#28 = Utf8 java/io/PrintStream
#29 = Utf8 println
#30 = Utf8 (Ljava/lang/String;)V
</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
The constant pool contains the following types:</div>
<div class="columns" style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline; width: 684.09375px;">
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Integer</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A 4 byte int constant</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Long</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
An 8 byte long constant</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Float</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A 4 byte float constant</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Double</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A 8 byte double constant</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">String</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A String constant that points at another Utf8 entry in the constant pool which contains the actual bytes</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Utf8</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A stream of bytes representing a Utf8 encoded sequence of characters</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Class</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A Class constant that points at another Utf8 entry in the constant pool which contains the fully qualified class name in the internal JVM format (this is used by the <a href="https://www.blogger.com/blogger.g?blogID=8193582855323202335#dynamic_linking" style="background: transparent; border: 0px; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;">dynamic linking process</a>)</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">NameAndType</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A colon separated pair of values each pointing at other entries in the constant pool. The first value (before the colon) points at a Utf8 string entry that is the method or field name. The second value points at a Utf8 entry that represents the type, in the case of a field this is the fully qualified class name, in the case of a method this is a list of fully qualified class names one per parameter.</div>
</div>
<div class="row" style="background: transparent; border: 0px; line-height: 1.25em; margin: 17.09375px auto; outline: 0px; overflow: hidden; padding: 0px; vertical-align: middle; width: 649.875px;">
<div class="keyword_column" style="background: transparent; border: 0px; float: left; margin: 0px; outline: 0px; padding: 0px; text-align: left; vertical-align: middle; width: 194.953125px;">
<span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Fieldref, Methodref, InterfaceMethodref</span></div>
<div class="description_column" style="background: transparent; border: 0px; float: right; margin: 0px; outline: 0px; padding: 0px; vertical-align: middle; width: 389.921875px;">
A dot separated pair of values each pointing at other entries in the constant pool. The first value (before the dot) points at a Class entry. The second value points at a NameAndType entry.</div>
</div>
</div>
<h3 id="exception_table" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Exception Table</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The exception table stores per-exception handler information such as:</div>
<div style="background: rgb(255, 255, 255); border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 14.9499998092651px; margin: 0px; outline: 0px; padding: 0px; text-align: justify; vertical-align: baseline;">
<ul class="list" style="background: transparent; border: 0px; line-height: 1.5em; list-style: none; margin: 0px; outline: 0px; padding: 0.125em 0px 0.25em; vertical-align: baseline;">
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Start point</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">End point</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">PC offset for handler code</li>
<li style="background: transparent; border: 0px; line-height: 1.5em; list-style-image: none; list-style-type: none; margin: 0px 0px 0px 2em; outline: 0px; padding: 0px; vertical-align: baseline;">Constant pool index for exception class being caught</li>
</ul>
</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
If a method has defined a try-catch or a try-finally exception handler then an Exception Table will be created. This contains information for each exception handler or finally block including the range over which the handler applies, what type of exception is being handled and where the handler code is.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
When an exception is thrown the JVM looks for a matching handler in the current method, if none is found the method ends abruptly popping the current stack frame and the exception is re-thrown in the calling method (the new current frame). If no exception handler is found before all frames have been popped then the thread is terminated. This can also cause the JVM itself to terminate if the exception is thrown in the last non-daemon thread, for example if the thread is the main thread.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Finally exception handlers match all types of exceptions and so always execute whenever an exception is thrown. In the case when no exception is thrown a finally block is still executed at the end of a method, this is achieved by jumping to the finally handler code immediately before the return statement is executed.</div>
<h3 id="symbol_table" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Symbol Table</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
In addition to per-type run-time constant pools the Hotspot JVM has a symbol table held in the permanent generation. The symbol table is a Hashtable mapping symbol pointers to symbols (i.e. <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Hashtable<Symbol*, Symbol></span>) and includes a pointer to all symbols including those held in run time constant pools in each class.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
Reference counting is used to control when a symbol is removed from the symbol table. For example when a class is unloaded the reference count of all symbols held in its run time constant pool are decremented. When the reference count of a symbol in the the symbol table goes to zero then the symbol table knows that symbol is not being referenced anymore and the symbol is unloaded from the symbol table. For both the symbol table and the string table (see below) all entries are held in a canonicalized form to improve efficiency and ensure each entry only appears once.</div>
<h3 id="string_table" style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: ComicSCFRegular, BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; margin: 1em 0px 0px; outline: 0px; padding: 0px; position: relative; text-align: justify; vertical-align: baseline;">
Interned Strings (String Table)</h3>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.25em 0px 0.75em; position: relative; text-align: justify; vertical-align: baseline;">
The Java Language Specification requires that identical string literals, that contain the same sequence of Unicode code points, must refer to the same instance of String. In addition if <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">String.intern()</span> is called on an instance of String a reference must be returned that would be identical to the reference return if the string was a literal. The following therefore holds true:</div>
<pre class="code" style="background: rgb(255, 255, 255); border-radius: 0.5em; border: 1px solid rgb(203, 203, 203); font-family: 'Courier New', Courier, monospace !important; font-size: 0.8em; line-height: 1.5em; outline: 0px; overflow: auto; padding: 1em; text-align: justify; vertical-align: baseline;">("j" + "v" + "m").intern() == "jvm"</pre>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
In the Hotspot JVM interned string are held in the string table, which is a Hashtable mapping object pointers to symbols (i.e. <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">Hashtable<oop, Symbol></span>), and is held in the permanent generation. For both the symbol table (see above) and the string table all entries are held in a canonicalized form to improve efficiency and ensure each entry only appears once.</div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
String literals are automatically interned by the compiler and added into the symbol table when the class is loaded. In addition instances of the String class can be explicitly interned by calling <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">String.intern()</span>. When <span class="code inline" style="background: transparent; border-radius: 0.5em; border: 0px solid rgb(203, 203, 203); display: inline; font-family: 'Courier New', Courier, monospace !important; font-size: 1em; line-height: 0.9em; margin: 0px; outline: 0px; overflow: auto; padding: 0.1em; vertical-align: baseline; white-space: pre-line;">String.intern()</span> is called, if the symbol table already contains the string then a reference to this is returned, if not the string is added to the string table and its reference is returned.<br />
<br /></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<b>References:</b></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<span style="font-family: Courier New, Courier, monospace;">1. http://blog.jamesdbloom.com/JVMInternals.html</span></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<span style="font-family: Courier New, Courier, monospace; line-height: 1.5em;">2. http://www.artima.com/insidejvm/ed2/jvmP.html</span></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<br /></div>
<div style="background: rgb(255, 255, 255); border-radius: 0.25em; border: 0px; font-family: BandaRegular, Helvetica, arial, freesans, clean, sans-serif; font-size: 17px; line-height: 1.5em; outline: 0px; padding: 0.75em 0px; position: relative; text-align: justify; vertical-align: baseline;">
<br /></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-85041061261037556722015-02-03T03:29:00.005-08:002015-02-18T02:02:50.527-08:00Memory Model Java<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The Java memory model specifies how the Java virtual machine works with the computer's memory (RAM). The Java virtual machine is a model of a whole computer so this model naturally includes a memory model - AKA the Java memory model.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
It is very important to understand the Java memory model if you want to design correctly behaving concurrent programs. The Java memory model specifies how and when different threads can see values written to shared variables by other threads, and how to synchronize access to shared variables when necessary.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The original Java memory model was insufficient, so the Java memory model was revised in Java 1.5. This version of the Java memory model is still in use in Java 8.</div>
<h2 style="color: #333333; font-family: Helvetica; margin-top: 30px;">
The Internal Java Memory Model</h2>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The Java memory model used internally in the JVM divides memory between thread stacks and the heap. This diagram illustrates the Java memory model from a logic perspective:</div>
<a href="https://www.blogger.com/null" name="javas-logic-memory-model" style="color: #551a8b; font-family: Helvetica; font-size: 16px; font-weight: 700;"></a><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><img alt="The Java Memory Model From a Logic Perspective" src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-1.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Each thread running in the Java virtual machine has its own thread stack. The thread stack contains information about what methods the thread has called to reach the current point of execution. I will refer to this as the "call stack". As the thread executes its code, the call stack changes.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The thread stack also contains all local variables for each method being executed (all methods on the call stack). A thread can only access it's own thread stack. Local variables created by a thread are invisible to all other threads than the thread who created it. Even if two threads are executing the exact same code, the two threads will still create the local variables of that code in each their own thread stack. Thus, each thread has its own version of each local variable.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
All local variables of primitive types ( <code style="font-family: Courier; font-size: 1em;">boolean</code>, <code style="font-family: Courier; font-size: 1em;">byte</code>, <code style="font-family: Courier; font-size: 1em;">short</code>, <code style="font-family: Courier; font-size: 1em;">char</code>, <code style="font-family: Courier; font-size: 1em;">int</code>, <code style="font-family: Courier; font-size: 1em;">long</code>, <code style="font-family: Courier; font-size: 1em;">float</code>, <code style="font-family: Courier; font-size: 1em;">double</code>) are fully stored on the thread stack and are thus not visible to other threads. One thread may pass a copy of a pritimive variable to another thread, but it cannot share the primitive local variable itself.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The heap contains all objects created in your Java application, regardless of what thread created the object. This includes the object versions of the primitive types (e.g. <code style="font-family: Courier; font-size: 1em;">Byte</code>, <code style="font-family: Courier; font-size: 1em;">Integer</code>, <code style="font-family: Courier; font-size: 1em;">Long</code> etc.). It does not matter if an object was created and assigned to a local variable, or created as a member variable of another object, the object is still stored on the heap.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Here is a diagram illustrating the call stack and local variables stored on the thread stacks, and objects stored on the heap:</div>
<img alt="The Java Memory Model showing where local variables and objects are stored in memory." src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-2.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
A local variable may be of a primitive type, in which case it is totally kept on the thread stack.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
A local variable may also be a reference to an object. In that case the reference (the local variable) is stored on the thread stack, but the object itself if stored on the heap.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
An object may contain methods and these methods may contain local variables. These local variables are also stored on the thread stack, even if the object the method belongs to is stored on the heap.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
An object's member variables are stored on the heap along with the object itself. That is true both when the member variable is of a primitive type, and if it is a reference to an object.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Static class variables are also stored on the heap along with the class definition.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Objects on the heap can be accessed by all threads that have a reference to the object. When a thread has access to an object, it can also get access to that object's member variables. If two threads call a method on the same object at the same time, they will both have access to the object's member variables, but each thread will have its own copy of the local variables.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Here is a diagram illustrating the points above:</div>
<img alt="The Java Memory Model showing references from local variables to objects, and from object to other objects." src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-3.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Two threads have a set of local variables. One of the local variables (<code style="font-family: Courier; font-size: 1em;">Local Variable 2</code>) point to a shared object on the heap (Object 3). The two threads each have a different reference to the same object. Their references are local variables and are thus stored in each thread's thread stack (on each). The two different references point to the same object on the heap, though.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Notice how the shared object (Object 3) has a reference to Object 2 and Object 4 as member variables (illustrated by the arrows from Object 3 to Object 2 and Object 4). Via these member variable references in Object 3 the two threads can access Object 2 and Object 4.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The diagram also shows a local variable which point to two different objects on the heap. In this case the references point to two different objects (Object 1 and Object 5), not the same object. In theory both threads could access both Object 1 and Object 5, if both threads had references to both objects. But in the diagram above each thread only has a reference to one of the two objects.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
So, what kind of Java code could lead to the above memory graph? Well, code as simple as the code below:</div>
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); color: #333333; font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">public class MyRunnable implements Runnable() {
public void run() {
methodOne();
}
public void methodOne() {
int localVariable1 = 45;
MySharedObject localVariable2 =
MySharedObject.sharedInstance;
//... do more with local variables.
methodTwo();
}
public void methodTwo() {
Integer localVariable1 = new Integer(99);
//... do more with local variable.
}
}
</pre>
<pre class="codeBox" style="background-color: #eeeeee; border: 1px solid rgb(204, 204, 204); color: #333333; font-family: Courier; font-size: 0.8em; margin-bottom: 20px; margin-top: 20px; padding: 10px;">public class MySharedObject {
//static variable pointing to instance of MySharedObject
public static final MySharedObject sharedInstance =
new MySharedObject();
//member variables pointing to two objects on the heap
public Integer object2 = new Integer(22);
public Integer object4 = new Integer(44);
public long member1 = 12345;
public long member1 = 67890;
}
</pre>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
If two threads were executing the <code style="font-family: Courier; font-size: 1em;">run()</code> method then the diagram shown earlier would be the outcome. The <code style="font-family: Courier; font-size: 1em;">run()</code>method calls <code style="font-family: Courier; font-size: 1em;">methodOne()</code> and <code style="font-family: Courier; font-size: 1em;">methodOne()</code> calls <code style="font-family: Courier; font-size: 1em;">methodTwo()</code>.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
<code style="font-family: Courier; font-size: 1em;">methodOne()</code> declares a primitive local variable (<code style="font-family: Courier; font-size: 1em;">localVariable1</code> of type <code style="font-family: Courier; font-size: 1em;">int</code>) and an local variable which is an object reference (<code style="font-family: Courier; font-size: 1em;">localVariable2</code>).</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Each thread executing <code style="font-family: Courier; font-size: 1em;">methodOne()</code> will create its own copy of <code style="font-family: Courier; font-size: 1em;">localVariable1</code> and <code style="font-family: Courier; font-size: 1em;">localVariable2</code> on their respective thread stacks. The <code style="font-family: Courier; font-size: 1em;">localVariable1</code> variables will be completely separated from each other, only living on each thread's thread stack. One thread cannot see what changes another thread makes to its copy of<code style="font-family: Courier; font-size: 1em;">localVariable1</code>.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Each thread executing <code style="font-family: Courier; font-size: 1em;">methodOne()</code> will also create their own copy of <code style="font-family: Courier; font-size: 1em;">localVariable2</code>. However, the two different copies of <code style="font-family: Courier; font-size: 1em;">localVariable2</code> both end up pointing to the same object on the heap. The code sets <code style="font-family: Courier; font-size: 1em;">localVariable2</code> to point to an object referenced by a static variable. There is only one copy of a static variable and this copy is stored on the heap. Thus, both of the two copies of <code style="font-family: Courier; font-size: 1em;">localVariable2</code> end up pointing to the same instance of<code style="font-family: Courier; font-size: 1em;">MySharedObject</code> which the static variable points to. The <code style="font-family: Courier; font-size: 1em;">MySharedObject</code> instance is also stored on the heap. It corresponds to Object 3 in the diagram above.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Notice how the <code style="font-family: Courier; font-size: 1em;">MySharedObject</code> class contains two member variables too. The member variables themselves are stored on the heap along with the object. The two member variables point to two other <code style="font-family: Courier; font-size: 1em;">Integer</code> objects. These<code style="font-family: Courier; font-size: 1em;">Integer</code> objects correspond to Object 2 and Object 4 in the diagram above.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Notice also how <code style="font-family: Courier; font-size: 1em;">methodTwo()</code> creates a local variable named <code style="font-family: Courier; font-size: 1em;">localVariable1</code>. This local variable is an object reference to an <code style="font-family: Courier; font-size: 1em;">Integer</code> object. The method sets the <code style="font-family: Courier; font-size: 1em;">localVariable1</code> reference to point to a new <code style="font-family: Courier; font-size: 1em;">Integer</code>instance. The <code style="font-family: Courier; font-size: 1em;">localVariable1</code> reference will be stored in one copy per thread executing <code style="font-family: Courier; font-size: 1em;">methodTwo()</code>. The two<code style="font-family: Courier; font-size: 1em;">Integer</code> objects instantiated will be stored on the heap, but since the method creates a new <code style="font-family: Courier; font-size: 1em;">Integer</code> object every time the method is executed, two threads executing this method will create separate <code style="font-family: Courier; font-size: 1em;">Integer</code> instances. The<code style="font-family: Courier; font-size: 1em;">Integer</code> objects created inside <code style="font-family: Courier; font-size: 1em;">methodTwo()</code> correspond to Object 1 and Object 5 in the diagram above.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Notice also the two member variables in the class <code style="font-family: Courier; font-size: 1em;">MySharedObject</code> of type <code style="font-family: Courier; font-size: 1em;">long</code> which is a primitive type. Since these variables are member variables, they are still stored on the heap along with the object. Only local variables are stored on the thread stack.</div>
<h2 style="color: #333333; font-family: Helvetica; margin-top: 30px;">
Hardware Memory Architecture</h2>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Modern hardware memory architecture is somewhat different from the internal Java memory model. It is important to understand the hardware memory architecture too, to understand how the Java memory model works with it. This section describes the common hardware memory architecture, and a later section will describe how the Java memory model works with it.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Here is a simplified diagram of modern computer hardware architecture:</div>
<a href="https://www.blogger.com/null" name="hardware-memory-architecture" style="color: #551a8b; font-family: Helvetica; font-size: 16px; font-weight: 700;"></a><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><img alt="Modern hardware memory architecture." src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-4.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
A modern computer often has 2 or more CPUs in it. Some of these CPUs may have multiple cores too. The point is, that on a modern computer with 2 or more CPUs it is possible to have more than one thread running simultaneously. Each CPU is capable of running one thread at any given time. That means that if your Java application is multithreaded, one thread per CPU may be running simultaneously (concurrently) inside your Java application.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Each CPU contains a set of registers which are essentially in-CPU memory. The CPU can perform operations much faster on these registers than it can perform on variables in main memory. That is because the CPU can access these registers much faster than it can access main memory.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Each CPU may also have a CPU cache memory layer. In fact, most modern CPUs have a cache memory layer of some size. The CPU can access its cache memory much faster than main memory, but typically not as fast as it can access its internal registers. So, the CPU cache memory is somewhere in between the speed of the internal registers and main memory. Some CPUs may have multiple cache layers (Level 1 and Level 2), but this is not so important to know to understand how the Java memory model interacts with memory. What matters is to know that CPUs can have a cache memory layer of some sort.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
A computer also contains a main memory area (RAM). All CPUs can access the main memory. The main memory area is typically much bigger than the cache memories of the CPUs.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Typically, when a CPU needs to access main memory it will read part of main memory into its CPU cache. It may even read part of the cache into its internal registers and then perform operations on it. When the CPU needs to write the result back to main memory it will flush the value from its internal register to the cache memory, and at some point flush the value back to main memory.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The values stored in the cache memory is typically flushed back to main memory when the CPU needs to store something else in the cache memory. The CPU cache can have data written to part of its memory at a time, and flush part of its memory at a time. It does not have to read / write the full cache each time it is updated. Typically the cache is updated in smaller memory blocks called "cache lines". One or more cache lines may be read into the cache memory, and one or mor cache lines may be flushed back to main memory again.</div>
<h2 style="color: #333333; font-family: Helvetica; margin-top: 30px;">
Bridging The Gap Between The Java Memory Model And The Hardware Memory Architecture</h2>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
As already mentioned, the Java memory model and the hardware memory architecture are different. The hardware memory architecture does not distinguish between thread stacks and heap. On the hardware, both the thread stack and the heap are located in main memory. Parts of the thread stacks and heap may sometimes be present in CPU caches and in internal CPU registers. This is illustrated in this diagram:</div>
<a href="https://www.blogger.com/null" name="bridging-the-gap-between-the-java-memory-model-and-the-hardware-memory-architecture" style="color: #551a8b; font-family: Helvetica; font-size: 16px; font-weight: 700;"></a><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><img alt="The division of thread stack and heap among CPU internal registers, CPU cache and main memory." src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-5.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
When objects and variables can be stored in various different memory areas in the computer, certain problems may occur. The two main problems are:</div>
<ul style="color: #333333; font-family: Helvetica; font-size: 16px;">
<li>Visibility of thread updates (writes) to shared variables.</li>
<li>Race conditions when reading, checking and writing shared variables.</li>
</ul>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Both of these problems will be explained in the following sections.</div>
<h3 style="color: #333333; font-family: Helvetica; margin-top: 30px;">
Visibility of Shared Objects</h3>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
If two or more threads are sharing an object, without the proper use of either <code style="font-family: Courier; font-size: 1em;">volatile</code> declarations or synchronization, updates to the shared object made by one thread may not be visible to other threads.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Imagine that the shared object is initially stored in main memory. A thread running on CPU one then reads the shared object into its CPU cache. There it makes a change to the shared object. As long as the CPU cache has not been flushed back to main memory, the changed version of the shared object is not visible to threads running on other CPUs. This way each thread may end up with its own copy of the shared object, each copy sitting in a different CPU cache.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
The following diagram illustrates the sketched situation. One thread running on the left CPU copies the shared object into its CPU cache, and changes its <code style="font-family: Courier; font-size: 1em;">count</code> variable to 2. This change is not visible to other threads running on the right CPU, because the update to <code style="font-family: Courier; font-size: 1em;">count</code> has not been flushed back to main memory yet.</div>
<a href="https://www.blogger.com/null" name="visibility-of-shared-objects" style="color: #551a8b; font-family: Helvetica; font-size: 16px; font-weight: 700;"></a><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><img alt="Visibility Issues in the Java Memory Model." src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-6.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
To solve this problem you can use <a href="http://tutorials.jenkov.com/java-concurrency/volatile.html" style="color: #551a8b; font-weight: 700; text-decoration: none;">Java's volatile keyword</a>. The <code style="font-family: Courier; font-size: 1em;">volatile</code> keyword can make sure that a given variable is read directly from main memory, and always written back to main memory when updated.</div>
<h3 style="color: #333333; font-family: Helvetica; margin-top: 30px;">
Race Conditions</h3>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
If two or more threads share an object, and more than one thread updates variables in that shared object, <a href="http://tutorials.jenkov.com/java-concurrency/race-conditions-and-critical-sections.html" style="color: #551a8b; font-weight: 700; text-decoration: none;">race conditions</a> may occur.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
Imagine if thread A reads the variable <code style="font-family: Courier; font-size: 1em;">count</code> of a shared object into its CPU cache. Imagine too, that thread B does the same, but into a different CPU cache. Now thread A adds one to <code style="font-family: Courier; font-size: 1em;">count</code>, and thread B does the same. Now <code style="font-family: Courier; font-size: 1em;">var1</code>has been incremented two times, once in each CPU cache.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
If these increments had been carried out sequentially, the variable <code style="font-family: Courier; font-size: 1em;">count</code> would be been incremented twice and had the original value + 2 written back to main memory.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
However, the two increments have been carried out concurrently without proper synchronization. Regardless of which of thread A and B that writes its updated version of <code style="font-family: Courier; font-size: 1em;">count</code> back to main memory, the updated value will only be 1 higher than the original value, despite the two increments.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
This diagram illustrates an occurrence of the problem with race conditions as described above:</div>
<a href="https://www.blogger.com/null" name="race-conditions" style="color: #551a8b; font-family: Helvetica; font-size: 16px; font-weight: 700;"></a><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><img alt="Race Condition Issues in the Java Memory Model." src="http://tutorials.jenkov.com/images/java-concurrency/java-memory-model-7.png" style="color: #333333; font-family: Helvetica; font-size: 16px;" /><span style="color: #333333; font-family: Helvetica; font-size: 16px;"></span><br />
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
To solve this problem you can use a <a href="http://tutorials.jenkov.com/java-concurrency/synchronized.html" style="color: #551a8b; font-weight: 700; text-decoration: none;">Java synchronized block</a>. A synchronized block guarantees that only one thread can enter a given critical section of the code at any given time. Synchronized blocks also guarantee that all variables accessed inside the synchronized block will be read in from main memory, and when the thread exits the synchronized block, all updated variables will be flushed back to main memory again, regardless of whether the variable is declared volatile or not.</div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
<br /></div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
<br /></div>
<div style="color: #333333; font-family: Helvetica; font-size: 16px;">
<b>Reference:</b></div>
<div style="color: #333333; font-size: 16px;">
<span style="font-family: Times, Times New Roman, serif;">1. http://tutorials.jenkov.com/java-concurrency/java-memory-model.html</span></div>
<div style="color: #333333; font-size: 16px;">
<span style="font-family: Times, Times New Roman, serif;">2. http://www.artima.com/insidejvm/ed2/jvmP.html</span></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-68034953841975260442015-01-28T23:01:00.000-08:002015-01-28T23:05:15.400-08:00Spring4 MVC cheat sheet on Annotations<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">Spring 2.5 introduced support for annotation based MVC controllers. @RequestMapping, @RequestParam, @ModelAttribute are some of the annotations provided for this implementation.</span><br />
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><br /></span>
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><br /></span>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://javapapers.com/wp-content/uploads/2012/09/SpringControllerAnnotations.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://javapapers.com/wp-content/uploads/2012/09/SpringControllerAnnotations.png" height="320" width="204" /></a></div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
@Controller Annotation</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
In spring-context first we need to declare a bean. This is just a stand spring bean definition in dispatcher’s context.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Latest way of doing this is, enabling autodetection. For the @Controller annotation spring gives a feature of autodetection. We can add “component-scan” in spring-context and provide the base-package.</div>
<pre class="prettyprint lang-xml prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="pln" style="color: #4d4d4c;"> </span><span class="tag" style="color: #c82829;"><context:component-scan</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">base-package</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"com.javapapers.spring.mvc"</span><span class="pln" style="color: #4d4d4c;"> </span><span class="tag" style="color: #c82829;">/></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><mvc:annotation-driven</span><span class="pln" style="color: #4d4d4c;"> </span><span class="tag" style="color: #c82829;">/></span><span class="pln" style="color: #4d4d4c;">
</span></pre>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Then add @Controller annotation to controllers. The dispatcher will start from the base-package and scan for beans that are annotated with @Controller annotation and look for @RequestMapping. @Controller annotation just tells the container that this bean is a designated controller class.</div>
<div class="full-width" style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px; width: 610px;">
</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
@RequestMapping Annotation</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
@RequestMapping annotation is used to map a particular HTTP request method (GET/POST) to a specific class/method in controller which will handle the respective request.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
@RequestMapping annotation can be applied both at class and method level. In class level we can map the URL of the request and in method we can map the url as well as HTTP request method (GET/POST).</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
We can use wildcard characters like * for path pattern matching.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
In the following example, @RequestMapping(“/hi”) annotated at class level maps the request url and at again at lower level method level mapping is used for HTTP request mapping.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="kwd" style="color: #8959a8;">package</span><span class="pln" style="color: #4d4d4c;"> com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">spring</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">mvc</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">stereotype</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Controller</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">ui</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Model</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">web</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">bind</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RequestMapping</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">web</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">bind</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RequestMethod</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">web</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">bind</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RequestParam</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Controller</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@RequestMapping</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"/hi"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">HelloWorldController</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@RequestMapping</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">method </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">RequestMethod</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">GET</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> hello</span><span class="pun" style="color: #4d4d4c;">()</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">return</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"hello"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@RequestMapping</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">method </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">RequestMethod</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">POST</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> hi</span><span class="pun" style="color: #4d4d4c;">(</span><span class="lit" style="color: #f5871f;">@RequestParam</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"name"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> name</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Model</span><span class="pln" style="color: #4d4d4c;"> model</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> message </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"Hi "</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> name </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"!"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
model</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">addAttribute</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"message"</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> message</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">return</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"hi"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<h3 style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Multi-action Controller</h3>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
In a multi-action controller urls are mapped at method level since the controller services multiple urls. In below example two urls are serviced by the controller and they are mapped to separate methods.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="kwd" style="color: #8959a8;">package</span><span class="pln" style="color: #4d4d4c;"> com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">spring</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">mvc</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">stereotype</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Controller</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">ui</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Model</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">web</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">bind</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RequestMapping</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">web</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">bind</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RequestMethod</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> org</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">springframework</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">web</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">bind</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RequestParam</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Controller</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">HelloWorldController</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@RequestMapping</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"/"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> hello</span><span class="pun" style="color: #4d4d4c;">()</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">return</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"hello"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@RequestMapping</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">value </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"/hi"</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> method </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">RequestMethod</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">GET</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> hi</span><span class="pun" style="color: #4d4d4c;">(</span><span class="lit" style="color: #f5871f;">@RequestParam</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"name"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> name</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Model</span><span class="pln" style="color: #4d4d4c;"> model</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> message </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"Hi "</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> name </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"!"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
model</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">addAttribute</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"message"</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> message</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">return</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"hi"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span></pre>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
When using multi-action form controller there is a possibility of creating ambiguity in mapping the urls to methods. In those cases, if we have specified a<br />
org.springframework.web.servlet.mvc.multiaction.MethodNameResolver it will be used to map a method. If a method name resolver is not specified then by default org.springframework.web.servlet.mvc.multiaction.InternalPathMethodNameResolver is used. This default implementation doesnot support wildcard characters.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
When a matching method is not found, action will be mapped to a default method in the controller which does not have any @RequestMapping specified. If there are more such methods in the controller, then method name will be taken into consideration.</div>
<pre class="prettyprint lang-xml prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="tag" style="color: #c82829;"><beans</span><span class="pln" style="color: #4d4d4c;"> ...</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><bean</span><span class="pln" style="color: #4d4d4c;">
</span><span class="atn" style="color: #f5871f;">class</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"</span><span class="pln" style="color: #4d4d4c;"> </span><span class="tag" style="color: #c82829;">/></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><bean</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">class</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"com.javapapers.spring.mvc.controller.HelloWorldController"</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><property</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">name</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"methodNameResolver"</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><bean</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">class</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver"</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><property</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">name</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"mappings"</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><props></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><prop</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">key</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"/"</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">hello</span><span class="tag" style="color: #c82829;"></prop></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"><prop</span><span class="pln" style="color: #4d4d4c;"> </span><span class="atn" style="color: #f5871f;">key</span><span class="pun" style="color: #4d4d4c;">=</span><span class="atv" style="color: #3e999f;">"/hi"</span><span class="tag" style="color: #c82829;">></span><span class="pln" style="color: #4d4d4c;">hi</span><span class="tag" style="color: #c82829;"></prop></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"></props></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"></property></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"></bean></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"></property></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"></bean></span><span class="pln" style="color: #4d4d4c;">
</span><span class="tag" style="color: #c82829;"></beans></span></pre>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
@RequestParam Annotation</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
To bind the request parameter a variable in method scope this @RequestParam annotation is used.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
In below code “name” is bound to the request parameter.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> hi</span><span class="pun" style="color: #4d4d4c;">(</span><span class="lit" style="color: #f5871f;">@RequestParam</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"name"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> name</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Model</span><span class="pln" style="color: #4d4d4c;"> model</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> message </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"Hi "</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> name </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"!"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
model</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">addAttribute</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"message"</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;"> message</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">return</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"hi"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span></pre>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
@ModelAttribute Annotation</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
An annotated method parameter can be mapped to an attribute in a model using @Modelttribute in controller. It can also be used to provide reference data for the model when used at method level.</div>
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">Mostly Used to handle form data and pass all fields to a pojo (<a href="http://viralpatel.net/blogs/spring-3-mvc-handling-forms/">details</a>)</span><br />
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><br /></span>
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><br /></span>
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><b>References:</b></span><br />
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">1. </span><span style="font-family: Segoe UI, Optima, Helvetica, Arial, sans-serif;"><span style="line-height: 28.7999992370605px;">http://javapapers.com/spring/spring-annotation-based-controllers/</span></span><br />
<span style="font-family: Segoe UI, Optima, Helvetica, Arial, sans-serif;"><span style="line-height: 28.7999992370605px;">2. http://viralpatel.net/blogs/spring-3-mvc-handling-forms/</span></span><br />
<span style="font-family: Segoe UI, Optima, Helvetica, Arial, sans-serif;"><span style="line-height: 28.7999992370605px;">3. List of steps to create a spring4-mvc web application </span><span style="line-height: 28.7999992370605px;">http://crunchify.com/simplest-spring-mvc-hello-world-example-tutorial-spring-model-view-controller-tips/</span></span><br />
<span style="font-family: Segoe UI, Optima, Helvetica, Arial, sans-serif;"><span style="line-height: 28.7999992370605px;"><br /></span></span>
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><br /></span></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-54261403803629431702015-01-28T22:40:00.002-08:002015-01-28T22:40:44.507-08:00Java Annotations & Usage<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">Annotations are far more powerful than java comments and javadoc comments. One main difference with annotation is it can be carried over to runtime and the other two stops with compilation level. Annotations are not only comments, it brings in new possibilities in terms of automated processing.</span><br />
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"><br /></span>
<span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">More than passing information, we can generate code using these annotations. Take </span><a href="http://javapapers.com/web-service/soap-web-service-introduction/" style="color: #0000ee; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px; text-decoration: none;" title="Soap Web Service – Introduction">webservices</a><span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;"> where we need to adhere by the service interface contract. The skeleton can be generated using annotations automatically by a annotation parser. This avoids human errors and decreases development time as always with automation.</span><br />
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Frameworks like Hibernate, Spring, Axis make heavy use of annotations. When a language needs to be made popular one of the best thing to do is support development of frameworks based on the language. Annotation is a good step towards that and will help grow Java.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<br /></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<br /></div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Annotation Structure</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
There are two main components in annotations. First is annotation type and the next is the annotation itself which we use in the code to add meaning. Every annotation belongs to a annotation type.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<strong>Annotation Type:</strong></div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><annotation-type-name><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
method declaration</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></annotation-type-name></pre>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Annotation type is very similar to an interface with little difference.</div>
<ul style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<li>We attach ‘@’ just before interface keyword.</li>
<li>Methods will not have parameters.</li>
<li>Methods will not have throws clause.</li>
<li>Method return types are restricted to primitives, String, Class, enums, annotations, and arrays of the preceding types.</li>
<li>We can assign a default value to method.</li>
</ul>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Meta Annotations</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Annotations itself is meta information then what is meta annotations? As you have rightly guessed, it is information about annotation. When we annotate a annotation type then it is called meta annotation. For example, we say that this annotation can be used only for methods.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="lit" style="color: #f5871f;">@Target</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">ElementType</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">METHOD</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">MethodInfo</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Annotation Types</h2>
<ol style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<li><strong>Documented</strong><br />When a annotation type is annotated with @Documented then wherever this annotation is used those elements should be documented using Javadoc tool.</li>
<li><strong>Inherited</strong><br />This meta annotation denotes that the annotation type can be inherited from super class. When a class is annotated with annotation of type that is annotated with Inherited, then its super class will be queried till a matching annotation is found.</li>
<li><strong>Retention</strong><br />This meta annotation denotes the level till which this annotation will be carried. When an annotation type is annotated with meta annotation Retention, RetentionPolicy has three possible values:<br />
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="lit" style="color: #f5871f;">@Retention</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">RetentionPolicy</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">RUNTIME</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Developer</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> value</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<ul>
<li><strong>Class</strong><br />When the annotation value is given as ‘class’ then this annotation will be compiled and included in the class file.</li>
<li><strong>Runtime</strong><br />The value name itself says, when the retention value is ‘Runtime’ this annotation will be available in JVM at runtime. We can write custom code using reflection package and parse the annotation. I have give an example below.</li>
<li><strong>Source</strong>This annotation will be removed at compile time and will not be available at compiled class.</li>
</ul>
</li>
<li><strong>Target</strong><br />This meta annotation says that this annotation type is applicable for only the element (ElementType) listed. Possible values for ElementType are, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE.<br />
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="lit" style="color: #f5871f;">@Target</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">ElementType</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">FIELD</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">FieldInfo</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">}</span></pre>
</li>
</ol>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Built-in Java Annotations</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
@Documented, @Inherited, @Retention and @Target are the four available meta annotations that are built-in with Java.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Apart from these meta annotations we have the following annotations.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<strong>@Override</strong></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
When we want to override a method, we can use this annotation to say to the compiler we are overriding an existing method. If the compiler finds that there is no matching method found in super class then generates a warning. This is not mandatory to use @Override when we override a method. But I have seen Eclipse IDE automatically adding this @Override annotation. Though it is not mandatory, it is considered as a best practice.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<strong>@Deprecated</strong></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
When we want to inform the compiler that a method is deprecated we can use this. So, when a method is annotated with @Deprecated and that method is found used in some place, then the compiler generates a warning.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<strong>@SuppressWarnings</strong></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
This is like saying, “I know what I am doing, so please shut up!” We want the compiler not to raise any warnings and then we use this annotation.</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Custom Annotations</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
We can create our own annotations and use it. We need to declare a annotation type and then use the respective annotation is java classes.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Following is an example of custom annotation, where this annotation can be used on any element by giving values. Note that I have used @Documented meta-annotation here to say that this annotation should be parsed by javadoc.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="com" style="color: #78b937;">/*
* Describes the team which wrote the code
*/</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Documented</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Team</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">int</span><span class="pln" style="color: #4d4d4c;"> teamId</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> teamName</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> teamLead</span><span class="pun" style="color: #4d4d4c;">()</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">default</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"[unassigned]"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> writeDate</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">default</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"[unimplemented]"</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<h3 style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
Annotation for the Above Example Type</h3>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> a java </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Team</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">
teamId </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">73</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;">
teamName </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"Rambo Mambo"</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;">
teamLead </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"Yo Man"</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;">
writeDate </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"3/1/2012"</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">static</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> readCSV</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">File</span><span class="pln" style="color: #4d4d4c;"> inputFile</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> java </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> continues </span><span class="pun" style="color: #4d4d4c;">...</span></pre>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Marker Annotations</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
We know what a marker interface is. Marker annotations are similar to marker interfaces, yes they don’t have methods / elements.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="com" style="color: #78b937;">/**
* Code annotated by this team is supreme and need
* not be unit tested - just for fun!
*/</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">SuperTeam</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> a java </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@SuperTeam</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">static</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> readCSV</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">File</span><span class="pln" style="color: #4d4d4c;"> inputFile</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> java </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> continues </span><span class="pun" style="color: #4d4d4c;">...</span></pre>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
In the above see how this annotation is used. It will look like one of the modifiers for this method and also note that the parenthesis () from annotation type is omitted. As there are no elements for this annotation, the parenthesis can be optionally omitted.</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
Single Value Annotations</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
There is a chance that an annotation can have only one element. In such a case that element should be named value.</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="com" style="color: #78b937;">/**
* Developer
*/</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Developer</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> value</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> a java </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Developer</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"Popeye"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">static</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> readCSV</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">File</span><span class="pln" style="color: #4d4d4c;"> inputFile</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">...</span><span class="pln" style="color: #4d4d4c;"> java </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> continues </span><span class="pun" style="color: #4d4d4c;">...</span></pre>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.7999992370605px;">
How to Parse Annotation</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
We can use reflection package to read annotations. It is useful when we develop tools to automate a certain process based on annotation.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<strong>Example</strong>:</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="kwd" style="color: #8959a8;">package</span><span class="pln" style="color: #4d4d4c;"> com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> java</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">lang</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Retention</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> java</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">lang</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">RetentionPolicy</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Retention</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">RetentionPolicy</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">RUNTIME</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@interface</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Developer</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> value</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="kwd" style="color: #8959a8;">package</span><span class="pln" style="color: #4d4d4c;"> com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">BuildHouse</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Developer</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"Alice"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> aliceMethod</span><span class="pun" style="color: #4d4d4c;">()</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">System</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">out</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">println</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"This method is written by Alice"</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="lit" style="color: #f5871f;">@Developer</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"Popeye"</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> buildHouse</span><span class="pun" style="color: #4d4d4c;">()</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">System</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">out</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">println</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"This method is written by Popeye"</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="kwd" style="color: #8959a8;">package</span><span class="pln" style="color: #4d4d4c;"> com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> java</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">lang</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotation</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Annotation</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">import</span><span class="pln" style="color: #4d4d4c;"> java</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">lang</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">reflect</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Method</span><span class="pun" style="color: #4d4d4c;">;</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">class</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">TestAnnotation</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">static</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> main</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">String</span><span class="pln" style="color: #4d4d4c;"> args</span><span class="pun" style="color: #4d4d4c;">[])</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">throws</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">SecurityException</span><span class="pun" style="color: #4d4d4c;">,</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">ClassNotFoundException</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">for</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">Method</span><span class="pln" style="color: #4d4d4c;"> method </span><span class="pun" style="color: #4d4d4c;">:</span><span class="pln" style="color: #4d4d4c;"> </span><span class="typ" style="color: #4271ae;">Class</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">forName</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">
</span><span class="str" style="color: #718c00;">"com.javapapers.annotations.BuildHouse"</span><span class="pun" style="color: #4d4d4c;">).</span><span class="pln" style="color: #4d4d4c;">getMethods</span><span class="pun" style="color: #4d4d4c;">())</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="com" style="color: #78b937;">// checks if there is annotation present of the given type Developer</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">if</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">method
</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">isAnnotationPresent</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Developer</span><span class="pun" style="color: #4d4d4c;">.</span><span class="kwd" style="color: #8959a8;">class</span><span class="pun" style="color: #4d4d4c;">))</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">try</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="com" style="color: #78b937;">// iterates all the annotations available in the method</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">for</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">Annotation</span><span class="pln" style="color: #4d4d4c;"> anno </span><span class="pun" style="color: #4d4d4c;">:</span><span class="pln" style="color: #4d4d4c;"> method</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">getDeclaredAnnotations</span><span class="pun" style="color: #4d4d4c;">())</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">System</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">out</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">println</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"Annotation in Method '"</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> method
</span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">"' : "</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> anno</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">Developer</span><span class="pln" style="color: #4d4d4c;"> a </span><span class="pun" style="color: #4d4d4c;">=</span><span class="pln" style="color: #4d4d4c;"> method</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">getAnnotation</span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">Developer</span><span class="pun" style="color: #4d4d4c;">.</span><span class="kwd" style="color: #8959a8;">class</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="kwd" style="color: #8959a8;">if</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"Popeye"</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">equals</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">a</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">value</span><span class="pun" style="color: #4d4d4c;">()))</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">System</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">out</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">println</span><span class="pun" style="color: #4d4d4c;">(</span><span class="str" style="color: #718c00;">"Popeye the sailor man! "</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">+</span><span class="pln" style="color: #4d4d4c;"> method</span><span class="pun" style="color: #4d4d4c;">);</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">catch</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">(</span><span class="typ" style="color: #4271ae;">Throwable</span><span class="pln" style="color: #4d4d4c;"> ex</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">{</span><span class="pln" style="color: #4d4d4c;">
ex</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">printStackTrace</span><span class="pun" style="color: #4d4d4c;">();</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span><span class="pln" style="color: #4d4d4c;">
</span><span class="pun" style="color: #4d4d4c;">}</span></pre>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.7999992370605px;">
<strong>Output</strong>:</div>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span class="typ" style="color: #4271ae;">Annotation</span><span class="pln" style="color: #4d4d4c;"> in </span><span class="typ" style="color: #4271ae;">Method</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">'public void com.javapapers.annotations.BuildHouse.aliceMethod()'</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">:</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Developer</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">value</span><span class="pun" style="color: #4d4d4c;">=</span><span class="typ" style="color: #4271ae;">Alice</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">Annotation</span><span class="pln" style="color: #4d4d4c;"> in </span><span class="typ" style="color: #4271ae;">Method</span><span class="pln" style="color: #4d4d4c;"> </span><span class="str" style="color: #718c00;">'public void com.javapapers.annotations.BuildHouse.buildHouse()'</span><span class="pln" style="color: #4d4d4c;"> </span><span class="pun" style="color: #4d4d4c;">:</span><span class="pln" style="color: #4d4d4c;"> </span><span class="lit" style="color: #f5871f;">@com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">Developer</span><span class="pun" style="color: #4d4d4c;">(</span><span class="pln" style="color: #4d4d4c;">value</span><span class="pun" style="color: #4d4d4c;">=</span><span class="typ" style="color: #4271ae;">Popeye</span><span class="pun" style="color: #4d4d4c;">)</span><span class="pln" style="color: #4d4d4c;">
</span><span class="typ" style="color: #4271ae;">Popeye</span><span class="pln" style="color: #4d4d4c;"> the sailor man</span><span class="pun" style="color: #4d4d4c;">!</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">public</span><span class="pln" style="color: #4d4d4c;"> </span><span class="kwd" style="color: #8959a8;">void</span><span class="pln" style="color: #4d4d4c;"> com</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">javapapers</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">annotations</span><span class="pun" style="color: #4d4d4c;">.</span><span class="typ" style="color: #4271ae;">BuildHouse</span><span class="pun" style="color: #4d4d4c;">.</span><span class="pln" style="color: #4d4d4c;">buildHo</span></pre>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;"><span style="color: #4d4d4c;">Reference: <a href="http://javapapers.com/core-java/java-annotations/">javaPapers</a></span></pre>
<pre class="prettyprint lang-java prettyprinted" style="border: 1px dashed rgb(204, 204, 204); font-family: Consolas, Menlo, Monaco, 'Lucida Console', 'Liberation Mono', 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', 'Courier New', monospace, serif; height: auto; line-height: 20.7999992370605px; margin-bottom: 18px; max-height: 600px; max-width: 100%; overflow: auto; padding: 5px; tab-size: 4;">
</pre>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-49372279584232999392015-01-28T21:16:00.000-08:002015-01-28T21:16:12.282-08:00Basics of Hibernate <div dir="ltr" style="text-align: left;" trbidi="on">
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
This is the exact flow of any hibernate application. so you must put little more concentration while you are reading this post, to understand better. [i'm yet to add steps for annotation based mapping for hibernate 4.0]</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Whether the java application will run in the <span style="color: #ff9900;">server</span> or <span style="color: #339966;">without server</span>, and the application may be desktop or stand alone, swing, awt, servlet…what ever, but the steps are common to all.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
In order to work with hibernate we <span style="color: red;">don’t required any server</span> as mandatory but we need hibernate software (.jar(s) files).</div>
<h2 style="border-bottom-color: rgb(0, 0, 0); border-bottom-style: dotted; border-bottom-width: 1px; border-left-color: rgb(236, 27, 76); border-left-style: solid; border-left-width: 3px; color: #555555; font-family: arial; font-size: 16px; line-height: 20.8000011444092px; margin: 10px 0px; padding: 3px 0px 3px 5px; width: 362.386352539063px;">
Follow The Steps:</h2>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
<strong>1.</strong> Import the hibernate API, they are many more, but these 2 are more than enough…</div>
<blockquote style="-webkit-box-shadow: rgb(212, 212, 212) 1px 0px 4px; background: rgb(249, 251, 246); border: 2px solid rgb(213, 221, 198); box-shadow: rgb(212, 212, 212) 1px 0px 4px; font-family: verdana; font-size: 12.7272720336914px; line-height: 20.8000011444092px; padding-bottom: 6px; padding-left: 10px; padding-right: 10px; width: 500px;">
<div style="line-height: 1.7em; margin-top: 4px; text-align: justify;">
import org.hibernate.*;<br />import org.hibernate.cfg.*;</div>
</blockquote>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
<strong>2.</strong> Among <span style="color: #ff9900;">Configuration</span>, <span style="color: green;">Mapping xml</span> files, first we need to load configuration xml, because once we load the configuration file, automatically mapping file will be loaded as we registered this mapping xml in the configuration file.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
So to load configuration xml, we need to create object of <span style="color: #333399;"><strong>Configuration</strong></span> class, which is given in<span style="color: #333399;"><strong>org.hibernate.cfg.*</strong></span>; and we need to call <span style="color: #333399;"><strong>configure()</strong></span> method in that class, by passing xml configuration file name as parameter.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Eg:</div>
<blockquote style="-webkit-box-shadow: rgb(212, 212, 212) 1px 0px 4px; background: rgb(249, 251, 246); border: 2px solid rgb(213, 221, 198); box-shadow: rgb(212, 212, 212) 1px 0px 4px; font-family: verdana; font-size: 12.7272720336914px; line-height: 20.8000011444092px; padding-bottom: 6px; padding-left: 10px; padding-right: 10px; width: 500px;">
<div style="line-height: 1.7em; margin-top: 4px; text-align: justify;">
Configuration <strong>cf</strong> = new Configuration();<br />cf.configure(“hibernate.cfg.xml”);</div>
</blockquote>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Here our <span style="color: blue;">configuration</span> file name is your choice, but by <span style="color: #339966;">default</span> am have been given <span style="color: #ff9900;">hibernate.cfg.xml</span>, so once this configuration file is loaded in our java app, then we can say that hibernate environment is <span style="color: #df1f9e;">started</span> in our program.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
So once we write the line_ <strong>cf.configure(“hibernate.cfg.xml”), </strong>configuration object<strong> cf </strong>will reads this xml file<strong>hibernate.cfg.xml, </strong>actually internally cf will uses DOM parsers to read the file.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Finally…</div>
<ul style="background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Verdana; font-size: 12px; line-height: 20.8000011444092px; list-style: none; margin: 7px 0px 0px; padding: 0px; position: relative;">
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">cf will reads data from <span style="color: #ff9900;">hibernate.cfg.xml</span></li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">Stores the data in different variables</li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">And finally all these variables are grouped and create one <span style="color: #00ccff;">high</span> level hibernate object we can call as SessionFactory object.</li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">So Configuration class only can create this <span style="color: #99cc00;">SessionFactory</span> object</li>
</ul>
<blockquote style="-webkit-box-shadow: rgb(212, 212, 212) 1px 0px 4px; background: rgb(249, 251, 246); border: 2px solid rgb(213, 221, 198); box-shadow: rgb(212, 212, 212) 1px 0px 4px; font-family: verdana; font-size: 12.7272720336914px; line-height: 20.8000011444092px; padding-bottom: 6px; padding-left: 10px; padding-right: 10px; width: 500px;">
<div style="line-height: 1.7em; margin-top: 4px; text-align: justify;">
likeSessionFactory <strong>sf</strong> = cf.buildSessionFactory();</div>
</blockquote>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Actually <span style="color: #df1f9e;">SessionFactory</span> is an interface not a class, and <span style="color: blue;">SessionFactoryImpl</span> is the implimented class for<span style="color: #df1f9e;">SessionFactory</span>, so we are internally creating object of <span style="color: blue;">SessionFactoryImpl</span> class and storing in the interface reference, so this SessionFactory object <strong>sf </strong>contains all the data regarding the <span style="color: #99cc00;">configuation</span> file so we can call<strong> sf </strong>as heavy weight object.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
3. Creating an object of session,</div>
<ul style="background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Verdana; font-size: 12px; line-height: 20.8000011444092px; list-style: none; margin: 7px 0px 0px; padding: 0px; position: relative;">
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;"><span style="color: #df1f9e;">Session</span> is an interface and <span style="color: #3366ff;">SessionImpl</span> is implemented class, both are given in <span style="color: #00ccff;">org.hibernate</span>.*;</li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">When ever session is opened then internally a database connection will be opened, in order to get a session or open a session we need to call <span style="color: #339966;">openSession</span>() method in <span style="color: #99cc00;">SessionFactory</span>, it means SessionFactory produces sessions.</li>
</ul>
<blockquote style="-webkit-box-shadow: rgb(212, 212, 212) 1px 0px 4px; background: rgb(249, 251, 246); border: 2px solid rgb(213, 221, 198); box-shadow: rgb(212, 212, 212) 1px 0px 4px; font-family: verdana; font-size: 12.7272720336914px; line-height: 20.8000011444092px; padding-bottom: 6px; padding-left: 10px; padding-right: 10px; width: 500px;">
<div style="line-height: 1.7em; margin-top: 4px; text-align: justify;">
Session <strong>session</strong> = <strong>sf</strong>.openSession();</div>
<div style="line-height: 1.7em; margin-top: 4px; text-align: justify;">
<strong>sf</strong> = SessfionFactory object</div>
</blockquote>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
4. Create a logical transaction</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
While working with<span style="color: red;"> insert</span>, <span style="color: blue;">update</span>, <span style="color: #99cc00;">delete</span>, operations from an hibernate application onto the database then hibernate needs a logical <span style="color: #df1f9e;">Transaction</span>, if we are selecting an object from the database then we do <span style="color: red;">not</span> require any logical transaction in hibernate. In order to begin a logical transaction in hibernate then we need to call a method<span style="color: #00ccff;">beginTransaction()</span> given by <span style="color: #333399;">Session</span> Interface.</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Transaction tx = <strong>sessio</strong>n.beginTransaction();</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
<strong>session</strong> is the object of Session Interface</div>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
5. Use the methods given by <span style="color: #df1f9e;">Session</span> Interface, to move the objects from application to database and from database to application</div>
<blockquote style="-webkit-box-shadow: rgb(212, 212, 212) 1px 0px 4px; background: rgb(249, 251, 246); border: 2px solid rgb(213, 221, 198); box-shadow: rgb(212, 212, 212) 1px 0px 4px; font-family: verdana; font-size: 12.7272720336914px; line-height: 20.8000011444092px; padding-bottom: 6px; padding-left: 10px; padding-right: 10px; width: 500px;">
<table style="font-family: arial;"><tbody>
<tr><td style="font-size: 13px;">session .save(s)</td><td style="font-size: 13px;" width="50px">-</td><td style="font-size: 13px;">Inserting object ‘s’ into database</td></tr>
<tr><td style="font-size: 13px;">session.update(s)</td><td style="font-size: 13px;">-</td><td style="font-size: 13px;">Updating object ‘s’ in the database</td></tr>
<tr><td style="font-size: 13px;">session.load(s)</td><td style="font-size: 13px;">-</td><td style="font-size: 13px;">Selecting objcet ‘s’ object</td></tr>
<tr><td style="font-size: 13px;">session.delete(s)</td><td style="font-size: 13px;">-</td><td style="font-size: 13px;">Deletig object ‘s’ from database</td></tr>
</tbody></table>
</blockquote>
<ul style="background-attachment: initial; background-clip: initial; background-image: none; background-origin: initial; background-position: initial; background-repeat: initial; background-size: initial; font-family: Verdana; font-size: 12px; line-height: 20.8000011444092px; list-style: none; margin: 7px 0px 0px; padding: 0px; position: relative;">
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">So finally we need to call <strong>commit() </strong>in Transaction, like <strong>tx.commit()</strong>;</li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">As i told earlier, when we open session a connection to the database will be created right, so we must close that <span style="color: #00ccff;">connection</span> as session. close().</li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">And finally close the SessionFactory as <strong>sf.close()</strong></li>
<li style="background: url(http://www.java4s.com/wp-content/themes/strPro4Tut/java4s/arrow.gif) 0% 0% no-repeat rgb(255, 255, 255); color: #313131; font-family: verdana; font-size: 13px; line-height: 1.7em; list-style-type: none; margin-left: 5px; padding-bottom: 4px; padding-left: 18px; text-align: left;">That’s it.., we are done.</li>
</ul>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
Final flow will be______________</div>
<blockquote style="-webkit-box-shadow: rgb(212, 212, 212) 1px 0px 4px; background: rgb(249, 251, 246); border: 2px solid rgb(213, 221, 198); box-shadow: rgb(212, 212, 212) 1px 0px 4px; font-family: verdana; font-size: 12.7272720336914px; line-height: 20.8000011444092px; padding-bottom: 6px; padding-left: 10px; padding-right: 10px; width: 500px;">
<div style="line-height: 1.7em; margin-top: 4px; text-align: center;">
Configuration</div>
<div style="line-height: 1.7em; margin-top: 4px; text-align: center;">
SessionFactory</div>
<div style="line-height: 1.7em; margin-top: 4px; text-align: center;">
Session</div>
<div style="line-height: 1.7em; margin-top: 4px; text-align: center;">
Transaction</div>
<div style="line-height: 1.7em; margin-top: 4px; text-align: center;">
Close Statements</div>
</blockquote>
<div style="font-family: verdana; font-size: 12.7272720336914px; line-height: 1.7em; margin-top: 4px; text-align: justify;">
<br /></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-41083761826441055212014-09-17T06:52:00.000-07:002014-09-28T08:05:21.929-07:00Videos [Good ones] ranging from {anything : anything}<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
1. This one from Ted talks<br />
<br />
<br />
<div>
<iframe allowfullscreen="" frameborder="0" height="360" mozallowfullscreen="" scrolling="no" src="https://embed-ssl.ted.com/talks/kirby_ferguson_embrace_the_remix.html" webkitallowfullscreen="" width="640"></iframe>
<br />
<br />
<br />
<br />
2. The Mind Palace<br />
<br /></div>
<div>
<iframe allowfullscreen="" frameborder="0" height="360" mozallowfullscreen="" scrolling="no" src="https://embed-ssl.ted.com/talks/joshua_foer_feats_of_memory_anyone_can_do.html" webkitallowfullscreen="" width="640"></iframe></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-50509455088690404282014-09-12T06:54:00.000-07:002014-09-12T07:15:43.921-07:00Android Rest Image and Video access application <div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Following are class and layout files:<br />
<br />
<b>1. FoodItem.java</b><br />
<b><br /></b>
<br />
<div>
<pre class="java" name="code">package com.kant.gridviewsmenyudemo;
import android.graphics.Bitmap;
/**
* @author shashi1.k1
*
*/
public class FoodItem {
private Bitmap image;
private String bitmapUrl;
private String videoUrl;
private String title;
private int id;
public FoodItem(Bitmap image, String title , int id ,String bitmapUrl,String videoUrl) {
super();
this.image = image;
this.title = title;
this.id=id;
this.bitmapUrl=bitmapUrl;
this.videoUrl=videoUrl;
}
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBitmapUrl() {
return bitmapUrl;
}
public void setBitmapUrl(String bitmapUrl) {
this.bitmapUrl = bitmapUrl;
}
public String getVideoUrl() {
return videoUrl;
}
public void setVideoUrl(String videoUrl) {
this.videoUrl = videoUrl;
}
}
</pre>
</div>
<div>
<b><br /></b>
<b>2. GridViewAdapter.java
</b><br />
<br /></div>
<div>
<pre class="java" name="code">package com.kant.gridviewsmenyudemo;
import java.net.MalformedURLException;
import java.util.ArrayList;
import com.kant.gridviewsmenyudemo.ImageThreadLoader.ImageLoadedListener;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
*
* @author shashi.k1
*
*/
public class GridViewAdapter extends ArrayAdapter<fooditem> {
private Context context;
private int layoutResourceId;
private ImageThreadLoader imageLoader = new ImageThreadLoader();
private ArrayList<fooditem> data = new ArrayList<fooditem>();
public GridViewAdapter(Context context, int layoutResourceId,
ArrayList<fooditem> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
private ViewHolder holder = null;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.imageTitle = (TextView) row.findViewById(R.id.text);
holder.image = (ImageView) row.findViewById(R.id.image);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
final FoodItem item = data.get(position);
Bitmap cachedImage = null;
try {
cachedImage = imageLoader.loadImage(item.getBitmapUrl(),
new ImageLoadedListener() {
public void imageLoaded(Bitmap imageBitmap) {
holder.image.setImageBitmap(imageBitmap);
notifyDataSetChanged();
}
});
} catch (MalformedURLException e) {
}
holder.imageTitle.setText(item.getTitle());
if (cachedImage != null) {
holder.image.setImageBitmap(cachedImage);
}
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, VideoPLayerActivity.class);
Bundle b = new Bundle();
b.putString("videoUrl", item.getVideoUrl());
intent.putExtras(b); //Put your id to your next Intent
context.startActivity(intent);
}
});
return row;
}
static class ViewHolder {
TextView imageTitle;
ImageView image;
}
}
</fooditem></fooditem></fooditem></fooditem></pre>
</div>
<div>
<br />
<br />
<b>3. ImageThreadLoader.java
</b><br />
<br /></div>
<div>
<pre class="java" name="code">package com.kant.gridviewsmenyudemo;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.State;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.util.Log;
/**
* This is an object that can load images from a URL on a thread.
*
* @author shashi.k1
*/
public class ImageThreadLoader {
private static final String TAG = "ImageThreadLoader";
// Global cache of images.
// Using SoftReference to allow garbage collector to clean cache if needed
private final HashMap<string itmap="" softreference="">> Cache = new HashMap<string itmap="" softreference="">>();
private final class QueueItem {
public URL url;
public ImageLoadedListener listener;
}
private final ArrayList<queueitem> Queue = new ArrayList<queueitem>();
private final Handler handler = new Handler(); // Assumes that this is started from the main (UI) thread
private Thread thread;
private QueueRunner runner = new QueueRunner();;
/** Creates a new instance of the ImageThreadLoader */
public ImageThreadLoader() {
thread = new Thread(runner);
}
/**
* Defines an interface for a callback that will handle
* responses from the thread loader when an image is done
* being loaded.
*/
public interface ImageLoadedListener {
public void imageLoaded(Bitmap imageBitmap );
}
/**
* Provides a Runnable class to handle loading
* the image from the URL and settings the
* ImageView on the UI thread.
*/
private class QueueRunner implements Runnable {
public void run() {
synchronized(this) {
while(Queue.size() > 0) {
final QueueItem item = Queue.remove(0);
// If in the cache, return that copy and be done
if( Cache.containsKey(item.url.toString()) && Cache.get(item.url.toString()) != null) {
// Use a handler to get back onto the UI thread for the update
handler.post(new Runnable() {
public void run() {
if( item.listener != null ) {
// NB: There's a potential race condition here where the cache item could get
// garbage collected between when we post the runnable and it's executed.
// Ideally we would re-run the network load or something.
SoftReference<bitmap> ref = Cache.get(item.url.toString());
if( ref != null ) {
item.listener.imageLoaded(ref.get());
}
}
}
});
} else {
final Bitmap bmp = readBitmapFromNetwork(item.url);
if( bmp != null ) {
Cache.put(item.url.toString(), new SoftReference<bitmap>(bmp));
// Use a handler to get back onto the UI thread for the update
handler.post(new Runnable() {
public void run() {
if( item.listener != null ) {
item.listener.imageLoaded(bmp);
}
}
});
}
}
}
}
}
}
/**
* Queues up a URI to load an image from for a given image view.
*
* @param uri The URI source of the image
* @param callback The listener class to call when the image is loaded
* @throws MalformedURLException If the provided uri cannot be parsed
* @return A Bitmap image if the image is in the cache, else null.
*/
public Bitmap loadImage( final String uri, final ImageLoadedListener listener) throws MalformedURLException {
// If it's in the cache, just get it and quit it
if( Cache.containsKey(uri)) {
SoftReference<bitmap> ref = Cache.get(uri);
if( ref != null ) {
return ref.get();
}
}
QueueItem item = new QueueItem();
item.url = new URL(uri);
item.listener = listener;
Queue.add(item);
// start the thread if needed
if( thread.getState() == State.NEW) {
thread.start();
} else if( thread.getState() == State.TERMINATED) {
thread = new Thread(runner);
thread.start();
}
return null;
}
/**
* Convenience method to retrieve a bitmap image from
* a URL over the network. The built-in methods do
* not seem to work, as they return a FileNotFound
* exception.
*
* Note that this does not perform any threading --
* it blocks the call while retrieving the data.
*
* @param url The URL to read the bitmap from.
* @return A Bitmap image or null if an error occurs.
*/
public static Bitmap readBitmapFromNetwork( URL url ) {
InputStream is = null;
BufferedInputStream bis = null;
Bitmap bmp = null;
try {
URLConnection conn = url.openConnection();
conn.connect();
is = conn.getInputStream();
bis = new BufferedInputStream(is);
bmp = BitmapFactory.decodeStream(bis);
} catch (MalformedURLException e) {
Log.e(TAG, "Bad ad URL", e);
} catch (IOException e) {
Log.e(TAG, "Could not get remote ad image", e);
} finally {
try {
if( is != null )
is.close();
if( bis != null )
bis.close();
} catch (IOException e) {
Log.w(TAG, "Error closing stream.");
}
}
return bmp;
}
}
</bitmap></bitmap></bitmap></queueitem></queueitem></string></string></pre>
</div>
<div>
<br />
<b>4. MainActivity.java</b><br />
<b><br /></b></div>
<div>
<pre class="java" name="code">package com.kant.gridviewsmenyudemo;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
/**
*
* @author shashi.k1
*
*/
public class MainActivity extends Activity {
private GridView gridView;
private GridViewAdapter customGridAdapter;
private static String restURL = "http://107.110.1.125:8080/TestRestApplication/kantServer/webservice/fooditems/";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, position + "#Selected",
Toast.LENGTH_SHORT).show();
}
});
}
/**
*
* @return
*/
private ArrayList<fooditem> getData() {
final ArrayList<fooditem> imageItems = new ArrayList<fooditem>();
// retrieve String drawable array
for (int i = 1; i <= 4; i++) {
//ImageItem item=getImageItemFromServer();
imageItems.add(new FoodItem(null, "Image#" + i, i,(restURL + i + "/image"),(restURL + i + "/video")));
}
return imageItems;
}
}
</fooditem></fooditem></fooditem></pre>
</div>
<div>
<b><br /></b>
<b>5. VideoPLayerActivity.java</b><br />
<b><br /></b></div>
<div>
<pre class="java" name="code">package com.kant.gridviewsmenyudemo;
import java.io.File;
import java.net.MalformedURLException;
import com.kant.gridviewsmenyudemo.VideoThreadLoader.VideoLoadedListener;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.MediaController;
import android.widget.VideoView;
public class VideoPLayerActivity extends Activity {
String videoUrl;
VideoView videoView;
private VideoThreadLoader videoLoader = new VideoThreadLoader();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
videoView = (VideoView) findViewById(R.id.videoView1);
this.videoUrl = getIntent().getExtras().getString("videoUrl");
try {
videoLoader.loadVideo(videoUrl, new VideoLoadedListener() {
@Override
public void videoLoaded(File imageBitmap) {
onDownloadFinished();
}
});
} catch (MalformedURLException e) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.video_player, menu);
return true;
}
public void onDownloadFinished() {
MediaController mc = new MediaController(this);
mc.setAnchorView(videoView);
mc.setMediaPlayer(videoView);
videoView.setMediaController(mc);
// video.setVideoURI(Uri.parse("http://www.youtube.com/watch?v=Eb5uaCHchWw"));
videoView.setVideoPath(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/video.mp4");
videoView.start();
}
}
</pre>
</div>
<div>
<b><br /></b>
<b>6. VideoThreadLoader.java</b><br />
<b><br /></b></div>
<div>
<pre class="java" name="code">package com.kant.gridviewsmenyudemo;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.Thread.State;
import java.lang.ref.SoftReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.util.ByteArrayBuffer;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
public class VideoThreadLoader {
private static final String TAG = "VideoThreadLoader";
// Global cache of images.
// Using SoftReference to allow garbage collector to clean cache if needed
private final HashMap<string ile="" softreference="">> Cache = new HashMap<string ile="" softreference="">>();
private final class QueueItem {
public URL url;
public VideoLoadedListener listener;
}
private final ArrayList<queueitem> Queue = new ArrayList<queueitem>();
private final Handler handler = new Handler(); // Assumes that this is
// started from the main
// (UI) thread
private Thread thread;
private QueueRunner runner = new QueueRunner();;
/** Creates a new instance of the ImageThreadLoader */
public VideoThreadLoader() {
thread = new Thread(runner);
}
/**
* Defines an interface for a callback that will handle responses from the
* thread loader when an image is done being loaded.
*/
public interface VideoLoadedListener {
public void videoLoaded(File imageBitmap);
}
/**
* Provides a Runnable class to handle loading the image from the URL and
* settings the ImageView on the UI thread.
*/
private class QueueRunner implements Runnable {
public void run() {
synchronized (this) {
while (Queue.size() > 0) {
final QueueItem item = Queue.remove(0);
// If in the cache, return that copy and be done
if (Cache.containsKey(item.url.toString())
&& Cache.get(item.url.toString()) != null) {
// Use a handler to get back onto the UI thread for the
// update
handler.post(new Runnable() {
public void run() {
if (item.listener != null) {
// NB: There's a potential race condition
// here where the cache item could get
// garbage collected between when we post
// the runnable and it's executed.
// Ideally we would re-run the network load
// or something.
SoftReference<file> ref = Cache
.get(item.url.toString());
if (ref != null) {
item.listener.videoLoaded(ref.get());
}
}
}
});
} else {
final File bmp = readVideoFromNetwork(item.url);
if (bmp != null) {
Cache.put(item.url.toString(),
new SoftReference<file>(bmp));
// Use a handler to get back onto the UI thread for
// the update
handler.post(new Runnable() {
public void run() {
if (item.listener != null) {
item.listener.videoLoaded(bmp);
}
}
});
}
}
}
}
}
}
/**
* Queues up a URI to load an image from for a given image view.
*
* @param uri
* The URI source of the image
* @param callback
* The listener class to call when the image is loaded
* @throws MalformedURLException
* If the provided uri cannot be parsed
* @return A Bitmap image if the image is in the cache, else null.
*/
public File loadVideo(final String uri, final VideoLoadedListener listener)
throws MalformedURLException {
// If it's in the cache, just get it and quit it
if (Cache.containsKey(uri)) {
SoftReference<file> ref = Cache.get(uri);
if (ref != null) {
return ref.get();
}
}
QueueItem item = new QueueItem();
item.url = new URL(uri);
item.listener = listener;
Queue.add(item);
// start the thread if needed
if (thread.getState() == State.NEW) {
thread.start();
} else if (thread.getState() == State.TERMINATED) {
thread = new Thread(runner);
thread.start();
}
return null;
}
private final static String PATH = Environment
.getExternalStorageDirectory().getAbsolutePath();
/**
* Convenience method to retrieve a bitmap image from a URL over the
* network. The built-in methods do not seem to work, as they return a
* FileNotFound exception.
*
* Note that this does not perform any threading -- it blocks the call while
* retrieving the data.
*
* @param url
* The URL to read the bitmap from.
* @return A Bitmap image or null if an error occurs.
*/
public static File readVideoFromNetwork(URL url) {
InputStream is = null;
BufferedInputStream bis = null;
File file = null;
try {
file = new File(PATH + "/video.mp4");
// long startTime = System.currentTimeMillis();
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
is = ucon.getInputStream();
bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(10000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.close();
} catch (MalformedURLException e) {
Log.e(TAG, "Bad ad URL", e);
} catch (IOException e) {
Log.e(TAG, "Could not get remote video", e);
} finally {
try {
if (is != null)
is.close();
if (bis != null)
bis.close();
} catch (IOException e) {
Log.w(TAG, "Error closing stream.");
}
}
return file;
}
}
</file></file></file></queueitem></queueitem></string></string></pre>
<pre class="java" name="code"><string ile="" softreference=""><string ile="" softreference=""><queueitem><queueitem><file><file><file>
</file></file></file></queueitem></queueitem></string></string></pre>
</div>
<div>
<b><br /></b>
<b>7. activity_main.xml</b><br />
<b><br /></b></div>
<div>
<pre class="xml" name="code"><relativelayout android:background="#f0f0f0" android:layout_height="match_parent" android:layout_width="match_parent" tools:context=".MainActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<linearlayout android:id="@+id/main_frame" android:layout_height="wrap_content" android:layout_width="wrap_content">
<textview android:id="@+id/textView1" android:layout_height="57dp" android:layout_width="wrap_content" android:text="Welcome to someWhere restraunt">
</textview></linearlayout>
<gridview android:columnwidth="100dp" android:drawselectorontop="true" android:gravity="center" android:id="@+id/gridView" android:layout_below="@id/main_frame" android:layout_height="wrap_content" android:layout_margin="5dp" android:layout_width="fill_parent" android:numcolumns="2" android:stretchmode="columnWidth" android:verticalspacing="20dp">
</gridview>
</relativelayout>
</pre>
</div>
<div>
<b><br /></b>
<b>8. activity_video_player.xml</b><br />
<b><br /></b></div>
<div>
<pre class="xml" name="code"><relativelayout android:layout_height="match_parent" android:layout_width="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".VideoPLayerActivity" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<textview android:id="@+id/textView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="@string/hello_world">
<videoview android:id="@+id/videoView1" android:layout_alignleft="@+id/textView1" android:layout_below="@+id/textView1" android:layout_height="wrap_content" android:layout_margintop="87dp" android:layout_width="wrap_content">
</videoview></textview></relativelayout>
</pre>
</div>
<div>
<b><br /></b>
<b>9. row_grid.xml</b><br />
<b><br /></b></div>
<div>
<pre class="xml" name="code"><linearlayout android:background="@drawable/grid_color_selector" android:clickable="true" android:focusable="true" android:layout_height="wrap_content" android:layout_margintop="5dp" android:layout_width="wrap_content" android:orientation="vertical" android:padding="5dp" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<imageview android:id="@+id/image" android:layout_height="200dp" android:layout_width="200dp">
</imageview>
<textview android:gravity="center" android:id="@+id/text" android:layout_height="wrap_content" android:layout_margintop="20dp" android:layout_width="fill_parent" android:textsize="12sp">
</textview>
</linearlayout>
</pre>
</div>
<div>
<b>Note:</b> <i>Next Post on Rest server Code and JAX-RS basics.
</i></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-54680606215803431262014-09-05T02:11:00.000-07:002014-09-05T02:44:40.938-07:00Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="margin-top: 0px; text-align: left;">
<span style="font-family: Times, Times New Roman, serif; font-size: x-small;"><b><br /></b></span></div>
<div style="margin-top: 0px; text-align: left;">
<span style="font-family: Times, Times New Roman, serif;"><b>
Association, Aggregation, Composition, Abstraction, Generalization, Realization, Dependency</b></span><span style="font-family: Century Gothic, CenturyGothic, AppleGothic, sans-serif; font-weight: normal;"> t</span><span style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px;">hese terms signify the relationships between classes. These are the building blocks of object oriented programming and very basic stuff. But still for some, these terms look like Latin and Greek. Just wanted to refresh these terms and explain in simpler terms.</span></div>
<h3 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px; text-align: left;">
<span style="font-size: large;">
Association</span></h3>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Association is a relationship between two objects. In other words<b>, association defines the multiplicity between objects</b>. You may be aware of one-to-one, one-to-many, many-to-one, many-to-many all these words define an association between objects. Aggregation is a special form of association. Composition is a special form of aggregation.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<img alt="" class="alignright size-full wp-image-342" src="http://javapapers.com/wp-content/uploads/2010/06/association.jpg" height="37" style="border: 0px; height: auto; max-width: 100%; vertical-align: middle;" title="Association" width="174" /></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<strong><em>Example:</em> </strong>A Student and a Faculty are having an association.<br />
<b>Reference:</b> http://www.uml-diagrams.org/association.html</div>
<h3 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px; text-align: left;">
<span style="font-size: large;">
Aggregation</span></h3>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Aggregation is a special case of association. A directional association between objects. <b>When an object ‘has-a’ another object, then you have got an aggregation between them</b>. Direction between them specified which object contains the other object. Aggregation is also called a “Has-a” relationship.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<img alt="" class="size-full wp-image-343 alignleft" src="http://javapapers.com/wp-content/uploads/2010/06/aggregation.jpg" height="36" style="border: 0px; height: auto; max-width: 100%; vertical-align: middle;" title="Aggregation" width="176" /><br />
<b>Reference:</b> http://www.uml-diagrams.org/association.html</div>
<h3 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px; text-align: left;">
<span style="font-size: large;">
Composition</span></h3>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Composition is a special case of aggregation. In a more specific manner, a restricted aggregation is called composition. When an object contains the other object, <b>if the contained object cannot exist without the existence of container object</b>, then it is called composition.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<img alt="" class="alignright size-full wp-image-344" src="http://javapapers.com/wp-content/uploads/2010/06/composition.jpg" height="37" style="border: 0px; height: auto; max-width: 100%; vertical-align: middle;" title="Composition" width="178" /></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<strong><em>Example:</em> </strong>A class contains students. A student cannot exist without a class. There exists composition between class and students.<br />
<b>Reference:</b> http://www.uml-diagrams.org/association.html</div>
<h3 style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Difference between aggregation and composition</h3>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Composition is more restrictive. When there is a composition between two objects, the composed object cannot exist without the other object. This restriction is not there in aggregation. Though one object can contain the other object, there is no condition that the composed object must exist. The existence of the composed object is entirely optional. In both aggregation and composition, direction is must. The direction specifies, which object contains the other object.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<em><strong>Example:</strong> </em>A Library contains students and books. Relationship between library and student is aggregation. Relationship between library and book is composition. A student can exist without a library and therefore it is aggregation. A book cannot exist without a library and therefore its a composition. For easy understanding I am picking this example. Don’t go deeper into example and justify relationships!</div>
<div class="ads" style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px; margin: 20px 0px;">
</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px;">
Abstraction</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Abstraction is specifying the framework and hiding the implementation level information. Concreteness will be built on top of the abstraction. It gives you a blueprint to follow to while implementing the details. Abstraction reduces the complexity by hiding low level details.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<em><strong>Example: </strong></em>A wire frame model of a car.</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px;">
<span style="font-size: large;">
Generalization</span></h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Generalization uses a<b> “is-a” relationship from a <i>specialization </i>to the <i>generalization </i>class</b>. Common structure and behaviour are used from the specializtion to the generalized class. At a very broader level you can understand this as inheritance. Why I take the term inheritance is, you can relate this term very well. Generalization is also called a “Is-a” relationship.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<img alt="" class="alignright size-full wp-image-345" src="http://javapapers.com/wp-content/uploads/2010/06/generalization.jpg" height="41" style="border: 0px; height: auto; max-width: 100%; vertical-align: middle;" title="Generalization" width="181" /></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<em><strong>Example:</strong></em> Consider there exists a class named Person. A student is a person. A faculty is a person. Therefore here the relationship between student and person, similarly faculty and person is generalization.<br />
<b>References:</b> http://ourownjava.com/uml/uml-class-diagram-generalization-realization/</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px;">
<span style="font-size: large;">
Realization</span></h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Realization is a relationship between the blueprint class and the object containing its respective implementation level details. This object is said to realize the blueprint class. In other words, you can understand this as the relationship between the interface and the implementing class.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<img alt="" class="alignright size-full wp-image-346" src="http://javapapers.com/wp-content/uploads/2010/06/realization.jpg" height="38" style="border: 0px; height: auto; max-width: 100%; vertical-align: middle;" title="Realization" width="178" /></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<em><strong>Example:</strong></em> A particular model of a car ‘GTB Fiorano’ that implements the blueprint of a car realizes the abstraction.<br />
<b>References: </b>http://ourownjava.com/uml/uml-class-diagram-generalization-realization/</div>
<h2 style="border-bottom-color: rgb(170, 170, 170); border-bottom-style: solid; border-bottom-width: 1px; font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; font-weight: normal; line-height: 28.799999237060547px;">
Dependency</h2>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
Change in structure or behaviour of a class affects the other related class, then there is a dependency between those two classes. It need not be the same vice-versa. When one class contains the other class it this happens.</div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<img alt="" class="alignright size-full wp-image-347" src="http://javapapers.com/wp-content/uploads/2010/06/dependency.jpg" height="37" style="border: 0px; height: auto; max-width: 100%; vertical-align: middle;" title="Dependency" width="176" /></div>
<div style="font-family: 'Segoe UI', Optima, Helvetica, Arial, sans-serif; line-height: 28.799999237060547px;">
<em><strong>Example: </strong></em>Relationship between shape and circle is dependency.<br />
<b>References:</b> http://publib.boulder.ibm.com/infocenter/rsmhelp/v7r0m0/index.jsp?topic=/com.ibm.xtools.modeler.doc/topics/cdepend.html</div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-72122363936430159432014-09-01T02:01:00.000-07:002014-09-01T02:29:03.519-07:00Yahoo Finance Quotes YQL Rest JSON <div dir="ltr" style="text-align: left;" trbidi="on">
<b><br /></b>
<b>References before one goes into stuff:</b><br />
<br />
<ul style="text-align: left;">
<li>https://developer.yahoo.com/java/howto-parseRestJava.html#parsejson</li>
<li>https://developer.yahoo.com/yql/</li>
<li>https://developer.yahoo.com/yql/console/</li>
</ul>
<div>
<br /></div>
<div>
Here is the code:</div>
<div>
<br /></div>
<div>
<pre class="java" name="code">/**
*
*/
package com.kant.yahooFinanceQuoteReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* @author shashi
*
*/
public class YahooFinanceQuoteClient {
private static final String QUOTE = "quote";
private static final String RESULTS = "results";
private static final String QUERY = "query";
private final String[] values = { "symbol", "Ask", "AverageDailyVolume",
"PercentChange", "StockExchange", "DaysRange" };
/**
* @param args
*/
public static void main(String[] args) {
YahooFinanceQuoteClient yahooClient = new YahooFinanceQuoteClient();
List<string> arguments = new ArrayList<>();
arguments.add("YHOO");
arguments.add("AAPL");
arguments.add("GOOG");
arguments.add("MSFT");
yahooClient.checkYahooFinanceForQuotes(arguments);
//Above or this
/*for (String item : arguments) {
yahooClient.checkYahooFinanceForQuote(item);
}*/
}
/**
*
*/
public void checkYahooFinanceForQuote(String symbol) {
try {
String urlString = "http://query.yahooapis.com/v1/public/yql?"/* "http://finance.yahoo.com/rss/headline?" */;
String params = "q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22"
+ symbol
+ "%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
URL url = new URL(urlString + params);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
StringBuffer buffer = getResponseFromWS(conn);
System.out.println("[Response]:");
System.out.println(buffer);
readJsonObject(buffer);
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param args
*/
public void checkYahooFinanceForQuotes(List<string> args) {
try {
String urlString = "http://query.yahooapis.com/v1/public/yql?";
String header = "q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(";
String tail = ")&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=";
Iterator<string> iterator = args.iterator();
String subPart = "%22";
while (iterator.hasNext()) {
if (!subPart.equals("%22"))
subPart = subPart + "%2C%22";
String subStr = iterator.next();
subPart = subPart + subStr + "%22";
}
// System.out.println(header + subPart + tail);
URL url = new URL(urlString + header + subPart + tail);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
StringBuffer buffer = getResponseFromWS(conn);
System.out.println("[Response]:");
System.out.println(buffer);
readJsonObject(buffer);
conn.disconnect();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private StringBuffer getResponseFromWS(HttpURLConnection conn)
throws IOException {
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/text");
if (conn.getResponseCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
String output = null;
StringBuffer buffer = new StringBuffer();
while ((output = br.readLine()) != null) {
buffer.append(output);
}
return buffer;
}
/**
*
* @param buffer
*/
private void readJsonObject(StringBuffer buffer) {
try {
JSONObject jo = new JSONObject(buffer.toString());
jo = jo.getJSONObject(QUERY);
jo = jo.getJSONObject(RESULTS);
if (jo.optJSONObject(QUOTE) == null) {
JSONArray ja = jo.getJSONArray(QUOTE);
for (int i = 0; i < ja.length(); i++) {
JSONObject resultObject = ja.getJSONObject(i);
printJsonObject(resultObject);
}
} else {
JSONObject resultObject = jo.getJSONObject(QUOTE);
printJsonObject(resultObject);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
/**
*
* @param resultObject
* @throws JSONException
*/
private void printJsonObject(JSONObject resultObject) throws JSONException {
System.out.println("--");
for (String value : values)
System.out.println(value + ": " + resultObject.get(value));
System.out.println("--");
}
}
</pre>
</div>
<br />
<br />
<br /></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-40093161858594706682014-07-09T07:45:00.000-07:002017-03-13T03:41:29.103-07:00A brief look at those "Design Patterns & Design Principles" <div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: "verdana" , sans-serif;"><b>M</b>y exploration and experimentation with design patterns has lead to this blog post.</span><br />
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="font-family: "verdana" , sans-serif;">Learning "Design patterns" will give you a <b>shared vocabulary</b> which can be shared and understood across other developers in a precise manner. So start thinking at <b>"Pattern Level".</b></span><br />
<span style="font-family: "verdana" , sans-serif;"><br /></span>
<span style="font-family: "courier new" , "courier" , monospace; font-size: x-small;">Note: I'll be updating the content in every couple of days or so. Happy learning :)</span><br />
<br />
<span style="color: #351c75; font-family: "courier new" , "courier" , monospace;"><b>Design Principles:</b></span><br />
<br />
<span style="font-family: "courier new" , "courier" , monospace;">1. </span><b>Identify the aspects of your application that vary and separate them from what stays the same. </b>[ <i><u>Action:</u></i> Take what varies and "encapsulate" so it won't affect the rest of your code. <i><u>Result:</u></i> Fewer unintended consequences from code changes and more flexibility in your systems.]<br />
<br />
2. <b>Program to interface , not an implementation. </b>[<i><u>Result:</u></i> One will end up creating a set of classes whose entire reason for living is to represent "specification or behaviors", to be implemented later based on requirements. <i><u>Benefit:</u></i> Provides run time polymorphism.]<br />
<span style="font-family: "courier new" , "courier" , monospace; font-size: x-small;">Note: "Program to Interface" really means "Program to Supertype".</span><br />
<br />
3. <b>Favor composition over inheritance. </b>[Creating systems using composition gives you lot more flexibility.It can let you encapsulate a family of algorithms into their own set of classes, but it also lets you change that behavior at runtime as long as the object you're composing with implements the correct behavior interface. There are more keep looking for it's use in other design patterns].<br />
<br />
<span style="color: red; font-family: "georgia" , "times new roman" , serif;"><b>Note: </b>Some basics about the arrows used in the design diagram shown below.</span><br />
<span style="color: red; font-family: "georgia" , "times new roman" , serif;"><br /></span>
<span style="color: red; font-family: "georgia" , "times new roman" , serif;">Straight Line (bold one with -> at end) => represents "has-a" relationship.</span><br />
<span style="color: red; font-family: "georgia" , "times new roman" , serif;">Straight Line (bold one with -|> at end) => represents "is-a" relationship.</span><br />
<span style="color: red; font-family: "georgia" , "times new roman" , serif;">Dotted Lines (dotted ones) => represents "implements".</span><br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://wlloo.files.wordpress.com/2011/07/screen-shot-2011-07-23-at-11-50-51-pm.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://wlloo.files.wordpress.com/2011/07/screen-shot-2011-07-23-at-11-50-51-pm.png" height="377" width="640" /></a></div>
<br />
4. <b>The power of loose Coupling. </b>[<u>Action:</u> Strive for loosely coupled design between objects that interact. <u>Result: </u>Loosely coupled designs allow us to build flexible OO systems that can handle changes because they minimize the interdependency between objects.] <span style="color: #0b5394;">{NEW CONTENT}</span><br />
<br />
<br />
<br />
<br />
<b style="color: #351c75; font-family: 'Courier New', Courier, monospace;">Design Patterns:</b><br />
<span style="color: #0b5394;">{NEW CONTENT}</span><br />
<b style="color: #351c75; font-family: 'Courier New', Courier, monospace;">1. </b> <b>The Strategy Pattern </b>defines a family of algorithms, encapsulates each one , makes them interchangeable. Strategy lets the algorithm vary independently from clients that use it.<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://www.codeproject.com/KB/architecture/_Strategy_Design_Pattern/Strategy.JPG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://www.codeproject.com/KB/architecture/_Strategy_Design_Pattern/Strategy.JPG" /></a></div>
<div class="separator" style="clear: both; text-align: center;">
<br /></div>
<br />
<b style="color: #351c75; font-family: 'Courier New', Courier, monospace;">2.</b> <b>The Observer Pattern. </b>A broadcast class keeps track of all the objects listening to it and anytime a new piece of data comes along it sends a message to each listener. What's cool here is the fact that listeners can register/unregister dynamically anytime to the broadcast. [One-to-Many relationship]<br />
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="http://res.dotnetcoders.com/images/DesignPatterns/ObserverPattern_defined.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="http://res.dotnetcoders.com/images/DesignPatterns/ObserverPattern_defined.png" /></a></div>
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-75349118247051513392014-06-09T04:44:00.001-07:002014-06-09T04:44:28.094-07:00Java "Volatile" keyword<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="background-color: white; border: 0px; color: #222222; font-family: MuseoSans500, Helvetica, Arial, Verdana, Tahoma, sans-serif; font-size: 23px; line-height: 38px; margin: 0px; outline: 0px; padding: 0px; text-rendering: optimizelegibility; vertical-align: baseline;">
Overview:</h2>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<b>V</b>olatile is probably the less known/understood/documented keyword in Java. I have recently read an <a href="http://vanillajava.blogspot.com.au/2012/01/demonstrating-when-volatile-is-required.html" style="border: 0px; color: #20328e; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; text-decoration: none; vertical-align: baseline;" target="_blank">article</a> on one of my favourite blog about the volatile keyword. The author shows a piece of code where the volatile keyword seems to have an influence. This example was not easy to understand and the role of the volatile keyword on the behaviour of the JVM was not really defined. So I have decided to browse the web to find a better code example for the volatile keyword. After one hour, nothing! Only wrong examples, articles comparing volatile with synchronized and other confused examples where the author seems as lost as the reader...</div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<br /></div>
<h2 style="background-color: white; border: 0px; color: #222222; font-family: MuseoSans500, Helvetica, Arial, Verdana, Tahoma, sans-serif; font-size: 23px; line-height: 38px; margin: 0px; outline: 0px; padding: 0px; text-rendering: optimizelegibility; vertical-align: baseline;">
A Basic Example:</h2>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
The following show a basic example where volatile is required</div>
<div class="syntaxhighlighter " id="highlighter_265623" style="background: none rgb(231, 229, 220); border: 0px; bottom: auto; color: #222222; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 15px; height: auto; left: auto; margin: 1em 0px; min-height: 30px !important; outline: 0px; overflow: auto; padding: 1px; position: relative; right: auto; top: auto; vertical-align: baseline; width: auto;">
<div class="bar" style="background: none; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">
<div class="toolbar" style="background: none rgb(248, 248, 248); border: 1px solid rgb(231, 229, 220); bottom: auto; float: none; font-size: 1px; height: auto; left: auto; margin: 0px; outline: 0px; padding: 8px 8px 8px 0px; position: absolute; right: 0px; top: 0px; vertical-align: baseline; width: auto;">
<a class="item viewSource" href="http://java.dzone.com/articles/java-volatile-keyword-0#viewSource" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/page_white_code.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="view source">view source</a><div class="item copyToClipboard" style="background: none; border: 0px; float: left; font-family: inherit; margin: 0px 0px 0px 8px; outline: 0px; overflow: visible; padding: 0px; vertical-align: baseline;">
</div>
<a class="item printSource" href="http://java.dzone.com/articles/java-volatile-keyword-0#printSource" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/printer.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="print">print</a><a class="item about" href="http://java.dzone.com/articles/java-volatile-keyword-0#about" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/help.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="?">?</a></div>
</div>
<div class="lines" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">01.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">public</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">class</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">VolatileTest {</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">02.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">private</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">static</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">final</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Logger LOGGER = MyLoggerFactory.getSimplestLogger();</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">03.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"> </span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">04.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">private</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">static</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">volatile</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">int</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT = </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">0</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">;</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">05.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"> </span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">06.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">public</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">static</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">void</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">main(String[] args) {</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">07.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">new</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">ChangeListener().start();</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">08.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">new</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">ChangeMaker().start();</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">09.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">10.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"> </span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">11.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">static</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">class</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">ChangeListener </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">extends</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Thread {</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">12.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="color1" style="background: none; border: 0px; bottom: auto; color: grey; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">@Override</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">13.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">public</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">void</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">run() {</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">14.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 72px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">int</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">local_value = MY_INT;</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">15.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 72px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">while</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">( local_value < </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">5</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">){</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">16.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 96px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">if</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">( local_value!= MY_INT){</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">17.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 120px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">LOGGER.log(Level.INFO,</code><code class="string" style="background: none; border: 0px; bottom: auto; color: blue; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">"Got Change for MY_INT : {0}"</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">, MY_INT);</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">18.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 126px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">local_value= MY_INT;</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">19.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 96px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">20.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 72px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">21.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">22.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">23.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"> </span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">24.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">static</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">class</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">ChangeMaker </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">extends</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Thread{</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">25.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="color1" style="background: none; border: 0px; bottom: auto; color: grey; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">@Override</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">26.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">public</code> <code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">void</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">run() {</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">27.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"> </span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">28.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 72px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">int</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">local_value = MY_INT;</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">29.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 72px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">while</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">(MY_INT <</code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">5</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">){</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">30.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 96px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">LOGGER.log(Level.INFO, </code><code class="string" style="background: none; border: 0px; bottom: auto; color: blue; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">"Incrementing MY_INT to {0}"</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">, local_value+</code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">1</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">);</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">31.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 96px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT = ++local_value;</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">32.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 96px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">try</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">{</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">33.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 120px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Thread.sleep(</code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">500</code><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">);</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">34.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 96px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">} </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">catch</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">(InterruptedException e) { e.printStackTrace(); }</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">35.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 72px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">36.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 48px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">37.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 24px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">38.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">}</code></span></span></div>
</div>
</div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">With the volatile keyword</strong> the output is :</div>
<div class="syntaxhighlighter " id="highlighter_493112" style="background: none rgb(231, 229, 220); border: 0px; bottom: auto; color: #222222; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 15px; height: auto; left: auto; margin: 1em 0px; min-height: 30px !important; outline: 0px; overflow: auto; padding: 1px; position: relative; right: auto; top: auto; vertical-align: baseline; width: auto;">
<div class="bar" style="background: none; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">
<div class="toolbar" style="background: none rgb(248, 248, 248); border: 1px solid rgb(231, 229, 220); bottom: auto; float: none; font-size: 1px; height: auto; left: auto; margin: 0px; outline: 0px; padding: 8px 8px 8px 0px; position: absolute; right: 0px; top: 0px; vertical-align: baseline; width: auto;">
<a class="item viewSource" href="http://java.dzone.com/articles/java-volatile-keyword-0#viewSource" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/page_white_code.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="view source">view source</a><div class="item copyToClipboard" style="background: none; border: 0px; float: left; font-family: inherit; margin: 0px 0px 0px 8px; outline: 0px; overflow: visible; padding: 0px; vertical-align: baseline;">
</div>
<a class="item printSource" href="http://java.dzone.com/articles/java-volatile-keyword-0#printSource" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/printer.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="print">print</a><a class="item about" href="http://java.dzone.com/articles/java-volatile-keyword-0#about" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/help.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="?">?</a></div>
</div>
<div class="lines" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">01.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">1</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">02.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Got Change </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">for</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT : </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">1</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">03.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">2</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">04.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Got Change </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">for</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT : </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">2</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">05.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">3</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">06.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Got Change </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">for</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT : </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">3</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">07.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">4</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">08.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Got Change </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">for</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT : </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">4</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">09.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">5</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">10.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Got Change </code><code class="keyword" style="background: none; border: 0px; bottom: auto; color: #006699; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">for</code> <code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">MY_INT : </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">5</code> </span></span></div>
</div>
</div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Without the volatile keyword</strong> the output is :</div>
<div class="syntaxhighlighter " id="highlighter_676004" style="background: none rgb(231, 229, 220); border: 0px; bottom: auto; color: #222222; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; font-size: 15px; height: auto; left: auto; margin: 1em 0px; min-height: 30px !important; outline: 0px; overflow: auto; padding: 1px; position: relative; right: auto; top: auto; vertical-align: baseline; width: auto;">
<div class="bar" style="background: none; border: 0px; bottom: auto; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">
<div class="toolbar" style="background: none rgb(248, 248, 248); border: 1px solid rgb(231, 229, 220); bottom: auto; float: none; font-size: 1px; height: auto; left: auto; margin: 0px; outline: 0px; padding: 8px 8px 8px 0px; position: absolute; right: 0px; top: 0px; vertical-align: baseline; width: auto;">
<a class="item viewSource" href="http://java.dzone.com/articles/java-volatile-keyword-0#viewSource" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/page_white_code.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="view source">view source</a><div class="item copyToClipboard" style="background: none; border: 0px; float: left; font-family: inherit; margin: 0px 0px 0px 8px; outline: 0px; overflow: visible; padding: 0px; vertical-align: baseline;">
</div>
<a class="item printSource" href="http://java.dzone.com/articles/java-volatile-keyword-0#printSource" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/printer.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="print">print</a><a class="item about" href="http://java.dzone.com/articles/java-volatile-keyword-0#about" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/help.png) no-repeat; border: 0px; bottom: auto; color: #a0a0a0; display: block; float: left; height: 16px; left: auto; margin: 0px 0px 0px 8px; outline: 0px; overflow: hidden; padding: 0px; position: static; right: auto; text-decoration: none; text-indent: -5000px; top: auto; vertical-align: baseline; width: 16px;" title="?">?</a></div>
</div>
<div class="lines" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">1.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">1</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">2.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">2</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">3.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">3</code></span></span></div>
<div class="line alt2" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">4.</code><span class="content" style="background: none rgb(248, 248, 248); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">4</code></span></span></div>
<div class="line alt1" style="border: 0px; font-family: inherit; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">
<code class="number" style="background: none; border: 0px; bottom: auto; color: #5c5c5c; display: block; float: left; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0.3em 0px 0px; position: static; right: auto; text-align: right; top: auto; vertical-align: baseline; width: 3em;">5.</code><span class="content" style="background: none rgb(255, 255, 255); border-left-color: rgb(108, 226, 108); border-left-style: solid; border-width: 0px 0px 0px 3px; bottom: auto; color: black; display: block; float: none; height: auto; left: auto; margin: 0px 0px 0px 3.3em; outline: 0px; padding: 0px 0px 0px 0.5em; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;"><span class="block" style="background: url(http://cdn.dzone.com/sites/all/themes/dzone2012/scripts/syntaxHighlighter/styles/wrapping.png) 0px 1.1em no-repeat; border: 0px; bottom: auto; display: block; float: none; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px 0px 0px 1.5em; position: static; right: auto; text-indent: -1.5em; top: auto; vertical-align: baseline; width: auto;"><code class="plain" style="background: none; border: 0px; bottom: auto; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">Incrementing MY_INT to </code><code class="value" style="background: none; border: 0px; bottom: auto; color: #009900; float: none; font-family: Consolas, Monaco, 'Bitstream Vera Sans Mono', 'Courier New', Courier, monospace; height: auto; left: auto; margin: 0px; outline: 0px; padding: 0px; position: static; right: auto; top: auto; vertical-align: baseline; width: auto;">5</code></span></span></div>
</div>
</div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
.....And the change listener loop infinitely... </div>
<h2 style="background-color: white; border: 0px; color: #222222; font-family: MuseoSans500, Helvetica, Arial, Verdana, Tahoma, sans-serif; font-size: 23px; line-height: 38px; margin: 0px; outline: 0px; padding: 0px; text-rendering: optimizelegibility; vertical-align: baseline;">
A Simple Explanation:</h2>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
So what happens? Each thread has its own stack, and so its own copy of variables it can access. When the thread is created, it copies the value of all accessible variables in its own memory.<b> The volatile keyword is used to say to the jvm "Warning, this variable may be modified in an other Thread"</b>. Without this keyword the JVM is free to make some optimizations, like never refreshing those local copies in some threads. The volatile force the thread to update the original variable for each variable. The volatile keyword could be used on every kind of variable, either primitive or objects! Maybe the subject of another article, more detailed...</div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<strong style="border: 0px; margin: 0px; outline: 0px; padding: 0px; vertical-align: baseline;">Never used volatile and never met this problem...</strong></div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
Like all threads issues, it happens under specials circumstances. Really special for this one... My example has big chances to show mainly because the ChangeListener thread is busy, thanks to the loop, and the JVM consider that this thread has no time for updating the local variables. Executing some synchronized methods or adding an other variable which is volatile (or even executing some simple lines of code) could modify the JVM behavior and "correct" this problem...</div>
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<b>Why posted this article? </b>.. well i was asked this simple question in an interview. I managed to answer in some way as i have seen it's use in "Single Design Pattern" (when thread safe behavior is required).</div>
<div style="background-color: white; border: 0px; color: #222222; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="font-family: Courier New, Courier, monospace;"><br /></span></div>
<div style="background-color: white; border: 0px; color: #222222; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; vertical-align: baseline;">
<span style="font-family: Courier New, Courier, monospace;"><b>Note: </b></span></div>
<div style="background-color: white; border: 0px; color: #222222; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; text-align: left; vertical-align: baseline;">
</div>
<ul>
<li><span style="color: #333333; font-family: 'Courier New', Courier, monospace; line-height: 1.4em;">The above example and behavior is only true for JDK 1.5 and up.</span></li>
<li><span style="color: #333333; font-family: 'Courier New', Courier, monospace; line-height: 1.4em;">Volatile is noway a replacement of synchronized keyword. It only deals with shared data visibility between threads. </span></li>
<li><span style="color: #333333; font-family: 'Courier New', Courier, monospace; line-height: 1.4em;">The typical use case is simple signaling between threads. One thread sets the signal and the others are only read it. </span></li>
</ul>
<br />
<div style="background-color: white; border: 0px; color: #222222; font-family: Helvetica, arial, verdana; font-size: 15px; line-height: 22.5px; margin-bottom: 1.2em; margin-top: 0.6em; outline: 0px; padding: 0px; text-align: left; vertical-align: baseline;">
<br /></div>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-27352044358744558182014-04-25T04:46:00.004-07:002014-04-25T04:46:50.445-07:00A Programmer , a Hacker and a Developer <div dir="ltr" style="text-align: left;" trbidi="on">
<div style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 1.6em; margin-bottom: 1em; margin-top: 1.1em;">
<img alt="frc" height="240" src="http://1-ps.googleusercontent.com/h/www.danielmiessler.com/images/600x400xprog_hack_dev.png.pagespeed.ic.yu_UIlMqGh.png" width="320" /></div>
<div style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 1.6em; margin-bottom: 1em; margin-top: 1.1em;">
There is often <a href="http://www.hanselman.com/blog/ACoderAProgrammerAHackerADeveloperAndAComputerScientistWalkIntoAVennDiagram.aspx" style="border: 0px; color: #0b5fbd; text-decoration: none;">spirited discussion and debate</a> about the differences between Programmers, Hackers, and Developers. Most descriptions I've seen are flawed in at least one serious way, so I provide below my preferred—and I hope more accurate and elegant—definitions:</div>
<ol style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 30.239999771118164px;">
<li style="line-height: 1.3em; list-style: decimal; margin-bottom: 1.6em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">A <b>Programmer</b> is someone who can solve problems by by manipulating computer code. They can have a wide range of skill levels—from just being "ok" with basic scripting to being an absolute sorcerer with any language.</li>
<li style="line-height: 1.3em; list-style: decimal; margin-bottom: 1.6em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">A <b>Hacker</b> is someone who makes things. In this context, it's someone who makes things by programming computers. This is the original, and purest definition of the term, i.e., that you have an idea and you “hack” something together to make it work. It also applies to people who modify things to significantly change their functionality, but less so.</li>
<li style="line-height: 1.3em; list-style: decimal; margin-bottom: 1.6em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">A <b>Developer</b> is a formally trained programmer. They don't just solve problems or create things, but do so in accordance with a set of design and implementation principles. These include things like performance, maintainability, scale, robustness, and (ideally) security.</li>
</ol>
<div style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 1.6em; margin-bottom: 1em; margin-top: 1.1em;">
In short, all three solve problems using code. Programmer is the umbrella term which means <em>problem solver</em>, a Hacker is the<em>creator/tinkerer</em>, and a Developer is a <em>formally trained programmer</em>who doesn't just solve problems but does so in a structured and disciplined way likely learned a part of a formal education.</div>
<h2 style="background-color: white; color: #003366; font-family: Concourse_t3_regular; font-size: 1.3em; font-weight: 500; line-height: 1.4em; list-style-type: none; margin: 1.1em 0px; padding: 0px;">
Relationships</h2>
<div style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 1.6em; margin-bottom: 1em; margin-top: 1.1em;">
People can be any combination of these. Here's how they relate to each other:</div>
<ul style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 30.239999771118164px;">
<li style="line-height: 1.5em; list-style-type: square; margin-bottom: 1.2em; margin-left: 0px; padding: 0px;">All hackers and developers are programmers.</li>
<li style="line-height: 1.5em; list-style-type: square; margin-bottom: 1.2em; margin-left: 0px; padding: 0px;">Many programmers are not creative or talented enough to be considered hackers.</li>
<li style="line-height: 1.5em; list-style-type: square; margin-bottom: 1.2em; margin-left: 0px; padding: 0px;">Many programmers are not educated or disciplined enough to be developers.</li>
<li style="line-height: 1.5em; list-style-type: square; margin-bottom: 1.2em; margin-left: 0px; padding: 0px;">Many developers are not creative enough to be hackers.</li>
</ul>
<div style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 1.6em; margin-bottom: 1em; margin-top: 1.1em;">
Ideally, one would strive to be all three, i.e., creative enough to be considered a hacker, but with enough formal training and experience to <em>design software</em> rather than simply…well, <em>hacking</em> it together.</div>
<div style="background-color: white; color: #333333; font-family: equity_text_b_regular; font-size: 22px; line-height: 1.6em; margin-bottom: 1em; margin-top: 1.1em;">
But even if you aren't very creative, and/or lack the training to do things the right way, you should still be proud. Solving problems using code is a superpower on its own.</div>
<h3 style="background-color: white; color: #0084ba; font-family: Concourse_t3_regular; font-size: 1.2em; font-weight: 500; line-height: 1.4em; margin: 1.4em 0px 1.3em; padding: 0px;">
Notes: </h3>
<ol class="note" style="background-color: white; color: #333333; font-family: Concourse_t3_regular; font-size: 22px; line-height: 30.239999771118164px; margin-left: 1.1em; padding-left: 0px;">
<li style="font-size: 0.9em; line-height: 1.3em; list-style: decimal; margin-bottom: -0.5em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">A <b>“coder”</b> is basically a synonym for programmer.</li>
<li style="font-size: 0.9em; line-height: 1.3em; list-style: decimal; margin-bottom: -0.5em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">Hacking is often, but not always, associated with poor quality. This is because it usually takes place in a rush of creativity that is best not slowed by the friction of doing things properly. In cases where a hacker is also a developer, or has an eye for design and quality elsewise, they almost inevitably have to come back to their creations afterward and clean them up.</li>
<li style="font-size: 0.9em; line-height: 1.3em; list-style: decimal; margin-bottom: -0.5em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">The determination of who gets called a hacker and who doesn't is a contentious one. The basic rules that most agree on include 1) that you need to have made one or more things that people find useful, and 2) be recognized as a hacker by other hackers.</li>
<li style="font-size: 0.9em; line-height: 1.3em; list-style: decimal; margin-bottom: -0.5em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">In the security world, a hacker also means multiple things. For testers it usually means someone who understands systems and can defeat their security controls. In the top tiers of security, however, the definition returns to its more pure form, i.e. someone who creates security software or tools used by others.</li>
<li style="font-size: 0.9em; line-height: 1.3em; list-style: decimal; margin-bottom: -0.5em; margin-left: 0px; margin-top: 1.6em; padding: 0px;">In popular culture, hacker means computer criminal. Within the security world that type of actor is often referred to as an “attacker”.</li>
<li style="font-size: 0.9em; line-height: 1.3em; list-style: decimal; margin-bottom: -0.5em; margin-left: 0px; margin-top: 1.6em; padding: 0px;"><a href="http://en.wikipedia.org/wiki/Eric_S._Raymond" style="border: 0px; color: #0b5fbd; text-decoration: none;">Eric Raymond</a>, a notable programmer and personality in the original computer community, defines a hacker like so, “An intelligent, creative, and open-minded individual who enjoys problem-solving, learning, and the sharing of knowledge above all else.”</li>
</ol>
</div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-68070451853890297662014-04-13T08:03:00.000-07:002014-04-13T08:03:17.701-07:00Undertanding Java Pass By Value<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">A variable is just a cup. It has a size, and it can hold something.<br />In Java, cups come in two main styles: primitive and reference.</span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">Primitive cups hold primitive values.<br />Reference cups hold remote controls to objects.</span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>Primitives:</b></span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<img src="http://www.javaranch.com/campfire/images/coffeeCupsPrimitives.gif" /></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<br /></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In Java,<b> each of these cups (float, char, long, etc.) is a specific size</b>. Byte is the smallest, double and long are the largest. Rather than measure in milliliters (or ounces as we do in the US) Java variables have a size measured in bits:</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">byte - 8 bits<br />short - 16 bits<br />int - 32 bits<br />long - 64 bits</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">All of these integer types are SIGNED. The leftmost bit represents the sign (positive or negative) and is NOT part of the value. So with a byte, for instance, you don't get the whole 8 bits to represent your value. You get 7. This gives you a range, for bytes, of :<br />(-2 to the 7th) through (2 to the 7th) -1. Why that little -1 on the end? Because zero is in there, and zero counts as negative. Works the same way with the others.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">float - 32 bits<br />double - 64 bits</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>References:</b></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In Java, if you want to stick an object in a variable, remember that the object is created out on the garbage-collectible heap. Always. So it's not IN the variable. There aren't giant, expandable cups which can be made big enough to hold any object. And unlike C/C++, there aren't cups which hold the exact memory location of the object.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In Java, <b>objects are created on the heap</b>, and a REFERENCE to the object is stored in the cup. Think of it as a remote control to a specific type of object. </span><img src="http://www.javaranch.com/campfire/images/sonyRef.gif" style="background-color: transparent;" /> </div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<br /></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In Java you say:</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-family: Courier New, Courier, mono; font-size: small;">Sony32 tv;</span><span style="font-size: small;"> // declare but don't initialize with an actual Sony32 object.</span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<img height="103" src="http://www.javaranch.com/campfire/images/sonyRefNull.gif" style="text-align: left;" width="127" /></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In Java, remote controls are called references. They store a value which the Java Virtual Machine (JVM) uses to get to your object. It sure looks and feels a lot like a pointer, and it might very well be a pointer to a pointer, or...</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>You Can't Know. It's an implementation detail that you, as a programmer, can't access.</b> Don't even think about it. There's no way to use that value other than to access the methods and variables of the actual object the reference refers to. That's part of what makes Java safer than C/C++. You can not go directly to any arbitrary memory location. The JVM allocates memory on your behalf, for your object, and stores an address-like thing in the reference cup (which is most likely a 32-bit cup, but not guaranteed to be).</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">Now into the topic of discussion <b>"Pass By Value"</b></span></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>1) <span style="font-family: Courier New, Courier, mono;">int x = 3;</span></b></span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>2) <span style="font-family: Courier New, Courier, mono;">int y = x;</span></b></span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><img height="86" src="http://www.javaranch.com/campfire/images/copyAPrimitive.gif" width="143" /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<br /></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In line 1, a cup called x, of size int, is created and given the value 3.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">In line 2, a cup called y, of size int, is created and given the value... 3.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>The x variable is not affected!</b></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>Java COPIES the value of x (which is 3) and puts that COPY into y.</b></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>This is PASS-BY-VALUE. </b>Which you can think of as PASS-BY-COPY. The value is copied, and that's what gets shoved into the new cup. You don't stuff one cup into another one.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">Saying int y = x does NOT mean "put the x cup into y". It means "copy the value inside x and put that copy into y".</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>So... what about Reference Variables (remote controls)? How does THAT work?<br /></b>Not so tricky, in fact the rule is the same.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>References do the same thing. You get a copy of the reference.</b></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;">So if I say:</span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-family: Courier New, Courier, mono; font-size: small;"><b>Cat A = new Cat();</b></span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-family: Courier New, Courier, mono; font-size: small;"><b>Cat B = A;</b></span></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<br /></div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><img height="240" src="http://www.javaranch.com/campfire/images/TwoRefsOneObj.gif" width="202" /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><b>The remote control in A is copied. Not the object it refers to.<br />You've still got just one Cat object.</b>But now you have<b> two different references</b> (remote controls) controlling the <b>same</b> Cat object.</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-family: Courier New, Courier, mono; font-size: small;">Cat A = new Cat();<br />doStuff(A);</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-family: Courier New, Courier, mono; font-size: small;">void doStuff(Cat B) {</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-family: Courier New, Courier, mono; font-size: small;"> B = new Cat(); //did NOT affect the A reference<br />}</span></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
Doing this simply "points" B to control a different object. A is still happy.</div>
<div align="center" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<img height="227" src="http://www.javaranch.com/campfire/images/RedirectARef.gif" width="201" /></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
So repeat after me:</div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<b>Java is pass-by-value.</b></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
(OK, once again... with feeling.)</div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<b>Java is pass-by-value.</b></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<b>For primitives, you pass a copy of the actual value.</b></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<b>For references to objects, you pass a copy of the reference (the remote control).</b></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
You never pass the object. All objects are stored on the heap. Always.</div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
Now go have an extra big cup of coffee and write some code.</div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<br /></div>
<div align="left" style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<br /></div>
<div style="background-color: #faf7f1; font-family: Arial, Helvetica, sans-serif;">
<span style="font-size: small;"><br /></span></div>
<div style="background-color: #faf7f1;">
<span style="color: #0c343d; font-family: Courier New, Courier, monospace; font-size: x-small;">References:</span></div>
<span style="font-family: Georgia, Times New Roman, serif;">1. http://www.javaranch.com/campfire/StoryCups.jsp</span><br />
<span style="font-family: Georgia, Times New Roman, serif;">2. http://www.javaranch.com/campfire/StoryPassBy.jsp</span></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-28701110708388959012014-03-10T11:27:00.000-07:002014-03-10T11:31:54.293-07:00LAMP Server on ubuntu 12.04 with SSL <div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<span style="font-family: Georgia, Times New Roman, serif;">1. One can find all that's necesaary install the setup here</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"> [1]. </span><a href="http://fideloper.com/ubuntu-12-04-lamp-server-setup" style="font-family: Georgia, 'Times New Roman', serif;">http://fideloper.com/ubuntu-12-04-lamp-server-setup</a><span style="font-family: Georgia, Times New Roman, serif;"> </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">[best one with ssl set up]</span><br />
<span style="font-family: Courier New, Courier, monospace; font-size: x-small;"> </span><span style="font-family: Georgia, 'Times New Roman', serif;">[2]. </span><a href="https://help.ubuntu.com/10.04/serverguide/httpd.html" style="font-family: Georgia, 'Times New Roman', serif;">https://help.ubuntu.com/10.04/serverguide/httpd.html</a><span style="font-family: Georgia, Times New Roman, serif;"> </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">[ssl set up]</span><br />
<span style="font-family: Georgia, Times New Roman, serif;"> [3]. <a href="http://howtoubuntu.org/how-to-install-lamp-on-ubuntu#.Ux4BsHX_QjA">http://howtoubuntu.org/how-to-install-lamp-on-ubuntu#.Ux4BsHX_QjA</a></span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">2. For free php chat application along with source code [<a href="http://www.phpfreechat.net/quickstart">http://www.phpfreechat.net/quickstart</a>] </span><br />
<span style="font-family: Georgia, Times New Roman, serif;"> You just need to create an alias on your apache server's site-available's configuration and copy the application folder in your root directory </span><span style="font-family: Courier New, Courier, monospace; font-size: x-small;">[for me it was /var/www/chatRoom/]</span><span style="font-family: Georgia, Times New Roman, serif;">. </span><br />
<span style="font-family: Georgia, Times New Roman, serif;"><br /></span>
<span style="font-family: Georgia, Times New Roman, serif;">3. Installing PhpMyadmin </span><br />
<span style="font-family: Georgia, Times New Roman, serif;"> [1]. <a href="https://www.digitalocean.com/community/articles/how-to-install-and-secure-phpmyadmin-on-ubuntu-12-04">https://www.digitalocean.com/community/articles/how-to-install-and-secure-phpmyadmin-on-ubuntu-12-04</a></span></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0tag:blogger.com,1999:blog-8193582855323202335.post-57345937828367666362014-02-19T07:39:00.002-08:002014-02-19T07:39:52.642-08:00Ubuntu[Dell] brightness/Bluetooth/Wireless problem at Start Up fixed<div dir="ltr" style="text-align: left;" trbidi="on">
Edit your <span style="font-family: Courier New, Courier, monospace; font-weight: bold;"><span style="color: #0b5394;">/etc/rc.local</span> </span><span style="font-family: Arial, Helvetica, sans-serif;">file by adding following lines before exit 0.</span><br />
<br />
<br />
<span style="background-color: #f3f3f3; color: #0b5394; font-family: Arial, Helvetica, sans-serif;"># for fixing brightness issue</span><br />
<span style="color: #444444; font-family: Courier New, Courier, monospace; font-size: x-small;"><b style="background-color: #f3f3f3;">sudo echo 7 > /sys/class/backlight/acpi_video0/brightness</b></span><br />
<span style="background-color: #f3f3f3; color: #444444; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="background-color: #f3f3f3; color: #0b5394; font-family: Arial, Helvetica, sans-serif;"># for fixing bluetooth to disabled at startup</span><br />
<span style="color: #444444; font-family: Courier New, Courier, monospace; font-size: x-small;"><b style="background-color: #f3f3f3;">rfkill block bluetooth</b></span><br />
<span style="background-color: #f3f3f3; color: #444444; font-family: Courier New, Courier, monospace;"><br /></span>
<span style="background-color: #f3f3f3; color: #0b5394; font-family: Arial, Helvetica, sans-serif;"># for fixing wireless wlan0 to be disabled at startup</span><br />
<span style="color: #444444; font-family: Courier New, Courier, monospace; font-size: x-small;"><b style="background-color: #f3f3f3;">sudo ifconfig wlan0 down</b></span><br />
<br />
<br />
<span style="color: #134f5c;"><br /></span>
<span style="color: #134f5c;"><br /></span>
<span style="color: #134f5c;">and done restart the system and you will see the change.</span><br />
<span style="color: #134f5c;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="color: #134f5c; font-family: Georgia, Times New Roman, serif;">Note: Fixes above are independent of laptop's build/brand.</span></h4>
<br />
<span style="color: #134f5c;"><br /></span>
<span style="color: #073763;"><br /></span>
<span style="color: #073763;"><b>References:</b></span><br />
<span style="color: #073763;">[1]. http://thanhsiang.org/faqing/node/180</span><br />
<span style="color: #073763;">[2]. http://www.cebuntu.com/how-to/how-set-brightness-on-startup-ubuntu-12-04/</span><br />
<span style="color: #073763;">[3]. http://ubuntuforums.org/showthread.php?t=1977738</span><br />
<span style="color: #073763;">[4]. http://linux.die.net/man/8/ifconfig</span></div>
kantshhttp://www.blogger.com/profile/05242729839282107159noreply@blogger.com0