<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:media="http://search.yahoo.com/mrss/">

<channel>
	<title>Development Archives - Manish Sanger</title>
	<atom:link href="https://www.manishsanger.com/tag/development/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.manishsanger.com/tag/development/</link>
	<description>Java, J2EE, Spring Framework, application security,big data,database,design patterns,hibernate,j2ee,java,jdbc,micro-services,mongodb,multithreading,nosql</description>
	<lastBuildDate>Tue, 20 Mar 2018 12:17:44 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.3</generator>
	<item>
		<title>Type Erasure &#8211; Java Generics</title>
		<link>https://www.manishsanger.com/type-erasure-java-generics/</link>
					<comments>https://www.manishsanger.com/type-erasure-java-generics/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Tue, 20 Mar 2018 11:59:31 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[java]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=344</guid>

					<description><![CDATA[<p>What is type Erasure? Generics in Java provide type checking at compile time and it has nothing to do at runtime. Java compiler uses Type Erasure feature to remove all generics type checking code (replace it with bound or Object if the type parameters are unbounded) in bytecode, insert type casting if necessary and generate [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/type-erasure-java-generics/">Type Erasure &#8211; Java Generics</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><b>What is type Erasure?</b></p>
<p>Generics in Java provide type checking at compile time and it has nothing to do at runtime. Java compiler uses <b>Type Erasure</b> feature to remove all generics type checking code (replace it with bound or Object if the type parameters are unbounded) in bytecode, insert type casting if necessary and generate bridge methods to preserve polymorphism in extended generic types.</p>
<p>Here is an example of generic a class:</p>
<pre><code class="java">class TypeErasureExample {<br />
private T t;</p>
<p>public TypeErasureExample(T t) {<br />
this.t = t;<br />
}</p>
<p>public T getT() {<br />
return t;<br />
}<br />
}<br />
</code></pre>
<p><b>After compilation the type will be replaced with String as follows:</b></p>
<pre><code class="java">class TypeErasureExample {<br />
private String t;</p>
<p>public TypeErasureExample(String t) {<br />
this.t = t;<br />
}</p>
<p>public String getT() {<br />
return t;<br />
}<br />
}<br />
</code></pre>
<p><u><b>Type erasure at class or class variable level:</b></u><br />
At the class level, type parameters on the class are removed at compile time and replaced with its first bound or Object if the type parameter is unbound.<br />
Here are few examples:<br />
<b>Type erasure when type parameter is unbound:</b><br />
<pre><code class="java">class TypeErasureUnboundExample {<br />
private T t;</p>
<p>public TypeErasureUnboundExample(T t) {<br />
this.t = t;<br />
}</p>
<p>public T getT() {<br />
return t;<br />
}<br />
}<br />
</code></pre>
<p><b>After compilation the type will be replace with Object (because type parameter is unbound) as follows:</b><br />
<pre><code class="java">class TypeErasureUnboundExample {<br />
private Object t;</p>
<p>public TypeErasureUnboundExample(Obhect t) {<br />
this.t = t;<br />
}</p>
<p>public Object getT() {<br />
return t;<br />
}<br />
}<br />
</code></pre>
<p><b>Type erasure when type parameter is bound:</b><br />
<pre><code class="java">class TypeErasureExample {<br />
private T t;</p>
<p>public TypeErasureExample(T t) {<br />
this.t = t;<br />
}</p>
<p>public T getT() {<br />
return t;<br />
}<br />
}<br />
</code></pre>
<p><b>After compilation the type will be replaced with String (with first bound) as follows:</b></p>
<pre><code class="java">class TypeErasureExample {<br />
private String t;</p>
<p>public TypeErasureExample(String t) {<br />
this.t = t;<br />
}</p>
<p>public String getT() {<br />
return t;<br />
}<br />
}<br />
</code></pre>
<u><b>Type Erasure at method level:</b></u><br />
Method type parameters are converted to its parent type Object if its unbound or its bound class when its bound:</p>
<p>For example:<br />
If type parameter is unbounded:<br />
<pre><code class="java">public static <T> void genericMethod(T t){<br />
        System.out.println(t);<br />
    }<br />
</code></pre>
<p>Will be converted to following at compile time:<br />
<pre><code class="java">public static void genericMethod(Object t){<br />
        System.out.println(t);<br />
    }<br />
</code></pre>
<p>If type parameter is bounded:</p>
<pre><code class="java">public static <T extend String> void genericMethod(T t){<br />
        System.out.println(t);<br />
    }<br />
</code></pre>
<p>Will be converted to following at compile time:<br />
<pre><code class="java">public static void genericMethod(String t){<br />
        System.out.println(t);<br />
    }<br />
</code></pre>
<p>The post <a href="https://www.manishsanger.com/type-erasure-java-generics/">Type Erasure &#8211; Java Generics</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/type-erasure-java-generics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<enclosure url="https://www.manishsanger.com/wp-content/uploads/2018/03/type-erasure-Java-generics.png" length="15679" type="image/png"/><media:content url="https://www.manishsanger.com/wp-content/uploads/2018/03/type-erasure-Java-generics.png" width="703" height="160" medium="image" type="image/png"/>	</item>
		<item>
		<title>Java Generics</title>
		<link>https://www.manishsanger.com/java-generics/</link>
					<comments>https://www.manishsanger.com/java-generics/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Tue, 20 Mar 2018 08:34:44 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[bounded-type-parameter]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=334</guid>

					<description><![CDATA[<p>Generics are a feature of generic programming which was introduced in Java 5. This allows a type or method to work with various object types while providing compile-time type safety. For example, collection framework supports generics. We can use Hashset, ArrayList, HashMap etc. to store various type of objects, and while retrieving the data we [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/java-generics/">Java Generics</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Generics are a feature of generic programming which was introduced in Java 5. This allows a type or method to work with various object types while providing compile-time type safety. For example, collection framework supports generics. We can use Hashset, ArrayList, HashMap etc. to store various type of objects, and while retrieving the data we don&#8217;t need to type cast.<br />
In Java 5, java rewrote the collection framework to incorporate the generics.</p>
<p>Here is an example of ArrayList without using generics:</p>
<pre><code class="java">List names = new ArrayList();<br />
names.add("Manish");<br />
String name = (String) names.get(0); //Type casting when retrieving the element from the array list</p>
<p>Integer number = (Integer) names.get(0); //Run-time error, ClassCastException will be thrown.<br />
</code></pre>
<p>When we use the ArrayList without generics, will have to type cast when we retrieve the element from the ArrayList, as the program doesn&#8217;t know the type of element stored in ArrayList. Also, when we try to type cast the element in the wrong way as I did in the code above, trying to type cast the element to Integer it will throw run-time ClassCastException error. Without generics, we only get the errors at the run-time not at compile time.</p>
<p>We can rewrite the code using generics as follows:</p>
<pre><code class="java">List<String> names = new ArrayList();<br />
names.add("Manish");<br />
String name = names.get(0); //No type casting is required.<br />
</code></pre>
<p><b>Creating custom Java generic Class:</b><br />
We can create our own generic class. A generic type is a class or interface that is parameterized over types, we use angle bracket (<>) to specify the type parameter.<br />
Note: Type argument can&#8217;t be of a primitive type.</p>
<pre><code class="java">public class GenericExample<T> {<br />
    private T t;<br />
    public T getT() {<br />
        return t;<br />
    }<br />
    public void setT(T t) {<br />
        this.t = t;<br />
    }<br />
}</p>
<p>class GenericTest{<br />
    public static void main(String[] args) {<br />
        //Instance with String type<br />
        GenericExample<String> genericExample = new GenericExample<>();<br />
        genericExample.setT("Test");</p>
<p>        //Instance with Integer type<br />
        GenericExample<Integer> genericExample1 = new GenericExample<>();<br />
        genericExample1.setT(100);</p>
<p>        //Type argument can't be primitive type.<br />
    }<br />
}<br />
</code></pre>
<p><b>Class with multiple type parameters:</b></p>
<pre><code class="java">public class GenericExample<T, U> {<br />
    private T t;<br />
    private U u;</p>
<p>    public GenericExample(T t, U u) {<br />
        this.t = t;<br />
        this.u = u;<br />
    }<br />
}</p>
<p>class GenericTest{<br />
    public static void main(String[] args) {<br />
        //Instance with String type<br />
        GenericExample<String, Integer> genericExample = new GenericExample<>("Test", 100);<br />
    }<br />
}<br />
</code></pre>
<p><b>Generic Method</b><br />
We can also create the generic method or constructor, no need to parameterize the whole class.</p>
<pre><code class="java">class GenericMethodTest{<br />
    static <T> void genericMethod(T t){<br />
        System.out.println(t);<br />
    }<br />
    public static void main(String[] args) {<br />
        GenericMethodTest.<String>genericMethod("Test");<br />
        GenericMethodTest.genericMethod("Test 2"); //Using type interface<br />
    }<br />
}<br />
</code></pre>
<p>In the above code, we can specify the type when calling the method or can just call like a normal method without specifying any type, JVM will do it for you. This feature is called type interface.</p>
<p><b>Java generics bounded type parameters:</b><br />
If we want to restrict the type of objects which can be used as parametrized type:<br />
<pre><code class="java">class GenericBound<T extends List> {<br />
    private T t;</p>
<p>    public GenericBound(T t) {<br />
        this.t = t;<br />
    }<br />
}<br />
</code></pre>
<p><b>Java generic type naming convention (Best practices)</b><br />
As per the Java best practices, type parameter should be single and uppercase letters. Following are the commonly used type parameters:</p>
<ul>
<li>T &#8211; Type</li>
<li>E &#8211; Element (Used extensively in collection framework)</li>
<li>N &#8211; Number</li>
<li>K &#8211; Key</li>
<li>V &#8211; Value</li>
<li>S,U,V etc. &#8211; 2nd, 3rd, 4th types</li>
</ul>
<p>The post <a href="https://www.manishsanger.com/java-generics/">Java Generics</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/java-generics/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<enclosure url="https://www.manishsanger.com/wp-content/uploads/2018/03/Java-generics.png" length="15016" type="image/png"/><media:content url="https://www.manishsanger.com/wp-content/uploads/2018/03/Java-generics.png" width="731" height="125" medium="image" type="image/png"/>	</item>
		<item>
		<title>Java Try with Resources</title>
		<link>https://www.manishsanger.com/try-with-resources/</link>
					<comments>https://www.manishsanger.com/try-with-resources/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Mon, 19 Feb 2018 11:50:16 +0000</pubDate>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Java 7]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[try-with-resources]]></category>
		<category><![CDATA[file-handling]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=320</guid>

					<description><![CDATA[<p>A resource is an object that must be closed once the program is finished or done using it, for example, file resource, JDBC database connection resource, socket connection resource etc. Prior to Java 7, there was no auto resource management, we open the resource in a try block and close the resource in finally block. [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/try-with-resources/">Java Try with Resources</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A resource is an object that must be closed once the program is finished or done using it, for example, file resource, JDBC database connection resource, socket connection resource etc.</p>
<p>Prior to Java 7, there was no auto resource management, we open the resource in a try block and close the resource in finally block. For example:</p>
<pre><code class="java">try{<br />
//open resources<br />
}catch(IOException){<br />
//Exception handling<br />
}finally{<br />
//close resource<br />
}<br />
</code></pre>
<p>In the above approach there were few drawbacks:</p>
<ol>
<li>It causes memory leaks and performance issue if the developer forgot to close the resource.</li>
<li>Finally block will always be executed, it doesn&#8217;t matter if the exception is thrown from the try block. It means, it tries to close the resource in finally block, even if it doesn&#8217;t exist which again cause an exception to be thrown from finally block.</li>
<li>Exception thrown from finally block will be propagated up the call stack, though exception from try block is more relevant.</li>
</ol>
<p><b><u>try-with-resource</u></b><br />
In Java 7, a new approach called &#8220;try-with-resources&#8221; is introduced for auto resource management. In this when try block finishes, it automatically closes the resource.</p>
<p>Here is an example for try-with-resource construct:<br />
<pre><code class="java">private static void readFile() throws IOException {<br />
try(FileInputStream input = new FileInputStream("file.txt")) {<br />
int data = input.read();<br />
while(data != -1){<br />
System.out.print((char) data);<br />
data = input.read();<br />
}<br />
}<br />
}<br />
</code></pre>
<p>&nbsp;</p>
<p><b>try-with-resource using multiple resources</b><br />
We can use multiple resources in try-with-resource block, it will automatically close both the resources when program finish executing try block.</p>
<pre><code class="java">private static void readFile() throws IOException {<br />
try(  FileInputStream input = new FileInputStream("file.txt");<br />
BufferedInputStream bufferedInput = new BufferedInputStream(input)<br />
) {<br />
int data = bufferedInput.read();<br />
while(data != -1){<br />
System.out.print((char) data);<br />
data = bufferedInput.read();<br />
}<br />
}<br />
}<br />
</code></pre>
<p>The resources are closed in reverse order of they are declared inside the try. <i>BufferedInputStream</i> will be closed first, then <i>FileInputStream</i>.<br />
&nbsp;<br />
<b><u>AutoCloseable Custom implementation</u></b><br />
Java 7 has introduced an interface <b><i>java.lang.AutoCloseable</i></b>. To use any resource in try-with-resource block, it must implement the AutoCloseable interface. We can also implement <i>java.lang.AutoCloseable</i> interface in our own classes and use them with the try-with-resources construct.<br />
<i>AutoCloseable </i>interface only has one method class <i>close()</i>. Here is the <i>AutoCloseable</i> interface:</p>
<pre><code class="java">public interface AutoCloseable {<br />
public void close() throws Exception;<br />
}<br />
</code></pre>
<p>&nbsp;<br />
<b><u>Exception handling with try-with-resource:</u></b><br />
If one or more exceptions are thrown by try block in try-with-resources, the exceptions thrown are <b><i>suppressed exception</i></b>.<br />
Java added an constructor and two methods in <b>Throwable</b> class to deal with suppressed exceptions. We can get these exceptions by using the <b><i>getSuppress()</i></b> method of <b>Throwable</b> class.</p>
<p><b><u>Advantages of using try-with-resource construct:</u></b></p>
<ol>
<li>Auto resource management.</li>
<li>More readable code.</li>
<li>No need of finally block just to close the resource.</li>
<li>Meaning &amp; relevant exceptions.</li>
</ol>
<p>The post <a href="https://www.manishsanger.com/try-with-resources/">Java Try with Resources</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/try-with-resources/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<enclosure url="https://www.manishsanger.com/wp-content/uploads/2018/02/Java-try-with-resource.png" length="34462" type="image/png"/><media:content url="https://www.manishsanger.com/wp-content/uploads/2018/02/Java-try-with-resource.png" width="757" height="232" medium="image" type="image/png"/>	</item>
	</channel>
</rss>
