<?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>Java 7 Archives - Manish Sanger</title>
	<atom:link href="https://www.manishsanger.com/tag/java-7/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.manishsanger.com/tag/java-7/</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 08:23:33 +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>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>
		<item>
		<title>Install Java7 on Linux (Ubuntu 12.04)</title>
		<link>https://www.manishsanger.com/install-java7-on-linuxubuntu-12-04/</link>
					<comments>https://www.manishsanger.com/install-java7-on-linuxubuntu-12-04/#respond</comments>
		
		<dc:creator><![CDATA[manishsanger]]></dc:creator>
		<pubDate>Fri, 12 Apr 2013 10:34:58 +0000</pubDate>
				<category><![CDATA[Installation/SetUp]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[Ubuntu]]></category>
		<category><![CDATA[Ubuntu 12.04 LTS]]></category>
		<category><![CDATA[Oracle]]></category>
		<category><![CDATA[Java 7]]></category>
		<category><![CDATA[install]]></category>
		<guid isPermaLink="false">https://www.manishsanger.com/?p=137</guid>

					<description><![CDATA[<p>If you are running an older version on java on your Ubuntu 12.04 environment, you must remove it before installing Java7. Do the following to remove: If you installed java 7 earlier and having problem with java then you have to do the following to remove it: Installing Oracle Java7 on Ubuntu 12.04 LTS: You [&#8230;]</p>
<p>The post <a href="https://www.manishsanger.com/install-java7-on-linuxubuntu-12-04/">Install Java7 on Linux (Ubuntu 12.04)</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>If you are running an older version on java on your Ubuntu 12.04 environment, you must remove it before installing Java7. Do the following to remove:</p>
<pre><code class="bash"><p>$sudo apt-get purge openjdk*</p>
</code></pre>
<p>If you installed java 7 earlier and having problem with java then you have to do the following to remove it:</p>
<pre><code class="bash">$sudo rm /var/lib/dpkg/info/oracle-java7-installer*<br />
$sudo apt-get purge oracle-java7-installer*<br />
$sudo rm /etc/apt/sources.list.d/*java*<br />
$sudo apt-get update<br />
</code></pre>
<p>Installing Oracle Java7 on Ubuntu 12.04 LTS:</p>
<pre><code class="bash">$sudo add-apt-repository ppa:webupd8team/java<br />
$sudo apt-get update<br />
$sudo apt-get install oracle-java7-installer<br />
</code></pre>
<p>You will be prompted couple of times for permissions and Oracle Policy agreement [You must be agreed to Oracle policies to install Java :-)].</p>
<p>That&#8217;s it! Now you have Oracle Java7 installed on your Ubuntu 12.04 environment, to check the Java version run the following on terminal:</p>
<pre><code class="bash">$java -version<br />
java version "1.7.0_17"<br />
Java(TM) SE Runtime Environment (build 1.7.0_17-b02)<br />
Java HotSpot(TM) 64-Bit Server VM (build 23.7-b01, mixed mode)<br />
</code></pre>
<p>Or You can check the java version by searching java in Ubuntu dashboard.</p>
<p>Oracle Java 7 plugin Control Panel -&gt; General Tab -&gt; About</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-medium wp-image-156" alt="java7 version Ubuntu 12.04" src="https://www.manishsanger.com/wp-content/uploads/2013/04/java7_version_ui.jpg" width="449" height="607" srcset="https://www.manishsanger.com/wp-content/uploads/2013/04/java7_version_ui.jpg 449w, https://www.manishsanger.com/wp-content/uploads/2013/04/java7_version_ui-221x300.jpg 221w" sizes="(max-width: 449px) 100vw, 449px" /></p>
<p>The post <a href="https://www.manishsanger.com/install-java7-on-linuxubuntu-12-04/">Install Java7 on Linux (Ubuntu 12.04)</a> appeared first on <a href="https://www.manishsanger.com">Manish Sanger</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.manishsanger.com/install-java7-on-linuxubuntu-12-04/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<enclosure url="https://www.manishsanger.com/wp-content/uploads/2013/04/java7.jpg" length="31913" type="image/jpeg"/><media:content url="https://www.manishsanger.com/wp-content/uploads/2013/04/java7.jpg" width="377" height="277" medium="image" type="image/jpeg"/>	</item>
	</channel>
</rss>
