<?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>Better Software and beyond... &#187; Java 7</title>
	<atom:link href="http://www.thorsten-kamann.de/tag/java-7/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thorsten-kamann.de</link>
	<description>About architecture, development, quality and more</description>
	<lastBuildDate>Tue, 25 Oct 2011 20:16:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Groovy&#039;s Elvis and Safe-Navigation Operator in Java 7</title>
		<link>http://www.thorsten-kamann.de/2009/08/31/elvis-and-safe-navigation-operator-in-java-7/</link>
		<comments>http://www.thorsten-kamann.de/2009/08/31/elvis-and-safe-navigation-operator-in-java-7/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 09:51:57 +0000</pubDate>
		<dc:creator>thorque</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[Java 7]]></category>

		<guid isPermaLink="false">http://www.thorsten-kamann.de/wordpress/?p=236</guid>
		<description><![CDATA[<a href="http://www.thorsten-kamann.de/2009/08/31/elvis-and-safe-navigation-operator-in-java-7/" title="Groovy&#039;s Elvis and Safe-Navigation Operator in Java 7"></a>Both operators are implemented in Groovy to shorten the code. The Elvis Operator shorten your if-conditions. You know the common ternary expression: def gender = user.male ? "male": "female" This you can shorten with the Elvis operator: def gender = &#8230;<p class="read-more"><a href="http://www.thorsten-kamann.de/2009/08/31/elvis-and-safe-navigation-operator-in-java-7/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://www.thorsten-kamann.de/2009/08/31/elvis-and-safe-navigation-operator-in-java-7/" title="Groovy&#039;s Elvis and Safe-Navigation Operator in Java 7"></a><p>Both operators are implemented in Groovy to shorten the code.<br />
The Elvis Operator shorten your if-conditions. You know the common <a href="http://java.sun.com/docs/books/tutorial/java/nutsandbolts/op2.html" target="_blank">ternary expression</a>:</p>
<pre escaped="erte" lang="groovy">def gender = user.male ? "male": "female"</pre>
<p>This you can shorten with the Elvis operator:</p>
<pre escaped="erte" lang="groovy">def gender = user.male ?: "female"</pre>
<p>Only if the expression evaluates to <em>false </em>or <em>null </em>the default value (here: <em>female</em>) will be used.</p>
<p>The other operator is the Safe-Navigation Operator. If you are working with Java Beans and their getter methods you have often a chain of calls:</p>
<pre escaped="erte" lang="java">customer.getAddress().getCity();</pre>
<p>This works fine until one of the getters return null. Then you get a NullPointerException. To avoid this you can surround this call with a try-catch block:</p>
<pre escaped="erte" lang="java">try{
   customer.getAddress().getCity();
}catch (NullPointerException npe){
   //what can I do here???
}</pre>
<p>This are at least 5 lines of code for catching the NullPointerException. But what can you do with this exception? Commonly you do nothing. You can log this or redirect this exception to the next tier. In Groovy there is the Safe-Navigation Operator:</p>
<pre escaped="erte" lang="groovy">customer?.address?.city</pre>
<p>At first in Groovy you can use the properties instead of accessing the getter methods. The ?. operator checks if the expression on the left hand is null. If true the complete expession evaluates to null.</p>
<p>For this both operators there is an <a href="http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000047.html" target="_blank">proposal</a> for a change in the Java Programming language. <a href="http://blogs.sun.com/darcy/entry/project_coin_final_five" target="_blank">For now this proposal is not accepted</a>&#8230;but we hope so <img src='http://www.thorsten-kamann.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.thorsten-kamann.de/2009/08/31/elvis-and-safe-navigation-operator-in-java-7/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

