<?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/"
	>

<channel>
	<title>Addictive Software &#187; java7</title>
	<atom:link href="http://blog.addictivesoftware.net/tag/java7/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.addictivesoftware.net</link>
	<description>ramblings of a chaotic mind</description>
	<lastBuildDate>Tue, 06 Jul 2010 09:04:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>.Net&#8217;s using() for Java</title>
		<link>http://blog.addictivesoftware.net/2009/11/nets-using-for-java/</link>
		<comments>http://blog.addictivesoftware.net/2009/11/nets-using-for-java/#comments</comments>
		<pubDate>Wed, 11 Nov 2009 19:22:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[java7]]></category>

		<guid isPermaLink="false">http://blog.addictivesoftware.net/?p=39</guid>
		<description><![CDATA[As I'm working as a Java developer in a microsoft oriented company this means I sometime have to do .Net programming.
As there are some things I don't like about .Net (no checked exceptions is one) there is one thing I find very good and useful.
In .Net an object that implements the IDisposable interface can be [...]]]></description>
			<content:encoded><![CDATA[<p>As I'm working as a Java developer in a microsoft oriented company this means I sometime have to do .Net programming.<br />
As there are some things I don't like about .Net (no checked exceptions is one) there is one thing I find very good and useful.</p>
<p>In .Net an object that implements the IDisposable interface can be initiated with the Using command like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>TextWriter w <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">CreateText</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;log.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span> w.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;This is line one&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>After the block has executed, using will take care of closing the writer and freeing the resources.<br />
This gives me solid readable code without memoryleaks.</p>
<p>Now looking at Java, closing resources usually involves putting a close() in a finally block, with another try-catch construction to catch exceptions that the close() can throw.<br />
this is ugly, errorprone and can obfuscate the real exception that happened.</p>
<p>Java 7 comes to the rescue, one of the approved proposals for java 7 is "automatic resource management"<br />
the following code block will demonstrate this</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> copy<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> src, <span style="color: #003399;">String</span> dest<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">IOException</span> <span style="color: #009900;">&#123;</span>
   <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">InputStream</span> in <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileInputStream</span><span style="color: #009900;">&#40;</span>src<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
         <span style="color: #003399;">OutputStream</span> out <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">FileOutputStream</span><span style="color: #009900;">&#40;</span>dest<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> buf <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">byte</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8192</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">int</span> n<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">=</span> in.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span>buf<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
        out.<span style="color: #006633;">write</span><span style="color: #009900;">&#40;</span>buf, <span style="color: #cc66cc;">0</span>, n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">//optional catch block</span>
    <span style="color: #666666; font-style: italic;">//optional finally block</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In Java 6 or lower the above code would contain 2 try-finally blocks to handle closing the input and ouput streams, another 2 try-catch blocks to handle exceptions from the close() commands, and more catches for exceptions you would want to handle specifically</p>
<p>Off all the features in java 7 this is the one I'm looking forward the most, the forkjoin and invokeDynamic changes following close though.<br />
here is a list of all the features in java 7<br />
<a href="http://openjdk.java.net/projects/jdk7/features/">http://openjdk.java.net/projects/jdk7/features/</a></pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.addictivesoftware.net/2009/11/nets-using-for-java/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
