`
<?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>Syfran.com</title>
	<atom:link href="http://syfran.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://syfran.com</link>
	<description>Coding is an Addiction</description>
	<lastBuildDate>Thu, 04 Mar 2010 20:17:05 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>When performance matters</title>
		<link>http://syfran.com/2010/03/when-performance-matters/</link>
		<comments>http://syfran.com/2010/03/when-performance-matters/#comments</comments>
		<pubDate>Thu, 04 Mar 2010 17:56:36 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[Euler]]></category>
		<category><![CDATA[Full Program]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=301</guid>
		<description><![CDATA[Linked Lists, Double Linked Lists, Arrays, Queues, Stacks... the data structures go on and on, but don't they all do pretty much the same thing? If you're like me you probably don't worry about which you use most the time. Just use whatever is convenient at the time, in C-like languages this will usually be an array, in Functional Languages a List (I believe they use linked lists by default).

Usually the defaults are fine, I mean how much difference could it really make? I didn't think it would be too much, maybe a couple seconds until my recent experience with Project Euler 78.]]></description>
			<content:encoded><![CDATA[<p>Linked Lists, Double Linked Lists, Arrays, Queues, Stacks&#8230; the data structures go on and on, but don&#8217;t they all do pretty much the same thing? If you&#8217;re like me you probably don&#8217;t worry about which you use most the time. Just use whatever is convenient at the time, in C-like languages this will usually be an array, in Functional Languages a List (I believe they use linked lists by default).</p>
<p>Usually the defaults are fine, I mean how much difference could it really make? I didn&#8217;t think it would be too much, maybe a couple seconds until my recent experience with Project Euler 78.<br />
<span id="more-301"></span></p>
<blockquote><p>
Let p(n) represent the number of different ways in which n coins can be separated into piles. For example, five coins can separated into piles in exactly seven different ways, so p(5)=7.<br />
OOOOO<br />
OOOO   O<br />
OOO   OO<br />
OOO   O   O<br />
OO   OO   O<br />
OO   O   O   O<br />
O   O   O   O   O</p>
<p>Find the least value of n for which p(n) is divisible by one million.
</p></blockquote>
<p>After a lot of research I came across the pentagonal number theorem. (I&#8217;m not one of those people who think you need to derive everything yourself, the only thing I care about is that I understand exactly what I&#8217;m doing)</p>
<blockquote><p>
The formulation of Euler&#8217;s generating function is a special case of a q-Pochhammer symbol and is similar to the product formulation of many modular forms, and specifically the Dedekind eta function. It can also be used in conjunction with the pentagonal number theorem to derive a recurrence for the partition function stating that:</p>
<p>    p(k) = p(k − 1) + p(k − 2) − p(k − 5) − p(k − 7) + p(k − 12) + p(k − 15) − p(k − 22) − &#8230;</p>
<p>where the sum is taken over all generalized pentagonal numbers of the form ½n(3n − 1), for n running over positive and negative integers: successively taking n = 1, -1, 2, -2, 3, -3, 4, -4, &#8230;, generates the values 1, 2, 5, 7, 12, 15, 22, 26, 35, 40, 51, &#8230;. The signs in the summation continue to alternate +, +, −, −, +, +, &#8230;
</p></blockquote>
<p>source:<a href="http://en.wikipedia.org/wiki/Partition_%28number_theory%29">wikipedia</a></p>
<p>I&#8217;m far from a mathematician so I had to dig around to understand exactly what was happening here (including looking at other code) and came up with the following program. </p>
<pre class="brush: haskell;">
module Euler78
where

pent = let p n = (n * ((3*n) - 1)) `div` 2
       in map (p) (concat [[i , (-i)] | i &lt; - [1..]])

p = let part 0 = 1
        part 1 = 1
        part n = sum $ zipWith (*) [p (n-k) | k &lt;- takeWhile (&lt;=n) pent] (cycle
    in ((map (part) [0..]) !!)

main78 :: IO ()
main78 = do print $ head $ filter (\x -&gt; (p x) `mod` 10^6 == 0) [1..]
</pre>
<pre>
> ghc --make Main.hs -o out/e78
> time out/e78
      real    277m16.556s
      user    276m26.416s
      sys     0m41.162s
</pre>
<p>(Main.hs contains code that executes main78, and yes I know it&#8217;s not the best configuration)</p>
<p>277 minutes!!!</p>
<p>I know this was the most efficient algorithm for finding the partitions so why was it taking 3 and 1/2 hours?? It got the right answer but it should be under 1min. Curious I downloaded the source code for another solution in Haskell (should look similar, It is what I used to wrap my head around what Wikipedia was saying). <a href="http://github.com/mstahl/project-euler/blob/master/problem_078.hs">Here is the<br />
source</a>.</p>
<pre>
> ghc --make Main.hs
> time ./Main
     real    0m49.656s
     user    0m49.432s
     sys     0m0.134s
</pre>
<p>Looks like I really screwed that up. Comparing the source the only large difference is the memoization of the partitions, their code imports Array just to store the results whereas mine uses a list. By default Haskell uses a linked list. To figure out where the time difference is coming from lets look at the Wikipedia articles for Linked List (yes I use Wikipedia a lot):</p>
<blockquote><p>The principal benefit of a linked list over a conventional array is that the order of the linked items may be different from the order that the data items are stored in memory or on disk. For that reason, linked lists allow insertion and removal of nodes at any point in the list, with a constant number of operations.</p>
<p>On the other hand, linked lists by themselves do not allow random access to the data, or any form of efficient indexing. Thus, many basic operations — such as obtaining the last node of the list, or finding a node that contains a given datum, or locating the place where a new node should be inserted — may require scanning most of the list elements.</p></blockquote>
<p>In summary-<br />
Linked List: Easy insertion and removal, slow access to random elements<br />
Array: Slow Insertion and removal, fast access to random elements</p>
<p>When we try to access
<pre>p (n-k)</pre>
<p> we are access a random element and with a linked list it will have to traverse from the very beginning to find the element. When replaced by an array the function can access the element directly without traversal. With so many calls to p that could definitely add up to some time, by it can be hard to believe it could increase the speed of the program by almost 300x. Regardless let&#8217;s try it just to see what happens.</p>
<pre class="brush: haskell;">
module Euler78
where

import Data.Array

pent = let p n = (n * ((3*n) - 1)) `div` 2
       in map (p) (concat [[i , (-i)] | i &lt; - [1..]])

p = let part 0 = 1
        part 1 = 1
        part n = sum $ zipWith (*) [p (n-k) | k &lt;- takeWhile (&lt;=n) pent] (cycle
    in ((listArray (0,10^6) (map (part) [0..])) !)

main78 :: IO ()
main78 = do print $ head $ filter (\x -&gt; (p x) `mod` 10^6 == 0) [1..]
</pre>
<pre>
> ghc --make Main.hs -o out/e78
> time out/e78

   real    0m59.389s
   user    0m57.495s
   sys     0m1.211s
</pre>
<p>Nice that is exactly what I was thinking for. The 300x change with an import and 1 line changed.</p>
<p>Right now the more experienced programmers are thinking &#8220;Duh, you should have had an array from the very start&#8221;. Unfortunately, we&#8217;re not all expert programmers and don&#8217;t realize how much it really matters.</p>
<p>This program really opened my eyes to performance of data structures, you can read about the efficiency of data structures all you want but the only way to really get it is to see it used in an actual program. Hopefully this will help a couple others realize the same.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d301').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d301" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=When+performance+matters&amp;url=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;h=When+performance+matters" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F&amp;title=When+performance+matters" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+When+performance+matters+@+http%3A%2F%2Fsyfran.com%2F2010%2F03%2Fwhen-performance-matters%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d301').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d301').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2010/03/when-performance-matters/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The death of the GPL</title>
		<link>http://syfran.com/2010/01/the-death-of-the-gpl-2/</link>
		<comments>http://syfran.com/2010/01/the-death-of-the-gpl-2/#comments</comments>
		<pubDate>Tue, 12 Jan 2010 15:56:02 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[opensource]]></category>
		<category><![CDATA[gpl]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=287</guid>
		<description><![CDATA[No guarantees but my prediction is that we are going to see the gradual downfall of the GPL license over the next couple years. For awhile now there has been a trend of developers switching from GPL to less restrictive licenses, often citing the viral nature of the GPL as the reason.
Is it possible to [...]]]></description>
			<content:encoded><![CDATA[<p>No guarantees but my prediction is that we are going to see the gradual downfall of the GPL license over the next couple years. For awhile now there has been a trend of developers switching from GPL to less restrictive licenses, often citing the viral nature of the GPL as the reason.</p>
<p>Is it possible to save the GPL? It might be possible to save but the best solution would be to just let it go. The licenses that are better suited will come into it&#8217;s place. </p>
<p>The GPL stage of open source is ending and viral licenses are no longer needed to spread open source around. What we need now are licenses to make open source more usable. Some of the ones hat have been floating around have been the &#8220;unlicense&#8221; which effectively puts the work into public domain and ms-pl.</p>
<p>Is this trouble for the open source community? Absolutely not. It simply shows that it is going into a stable phase where it does not need developers to force adoption through licenses, and open source will be used regardless.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d287').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d287" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=The+death+of+the+GPL&amp;url=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;h=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F&amp;title=The+death+of+the+GPL" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+The+death+of+the+GPL+@+http%3A%2F%2Fsyfran.com%2F2010%2F01%2Fthe-death-of-the-gpl-2%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d287').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d287').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2010/01/the-death-of-the-gpl-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Gotchas</title>
		<link>http://syfran.com/2009/10/java-gotchas/</link>
		<comments>http://syfran.com/2009/10/java-gotchas/#comments</comments>
		<pubDate>Thu, 08 Oct 2009 22:35:24 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[gotcha]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=133</guid>
		<description><![CDATA[Gotchas are bits of code that do something completely different from what you expected. These bits of code are often hard to catch and identify leaving you debugging for hours on end with no luck. The best way to keep yourself safe is to know them ahead of time. Here are some of the ones I have encountered before.]]></description>
			<content:encoded><![CDATA[<p>Gotchas are bits of code that do something completely different from what you expected. These bits of code are often hard to catch and identify leaving you debugging for hours on end with no luck. The best way to keep yourself safe is to know them ahead of time. Here are some of the ones I have encountered before.<span id="more-133"></span><br />
<br />
<hr />
<ul>
<li><strong>Collections: remove(int) vs remove(Object)</strong></p>
<p>With two different versions of the remove method on a Collection you can unintentional call the wrong one. For example remove(char c); will cast the char to an int and try to remove the object at 99 (the ascii code for &#8216;c&#8217;).  This leads to unpredictable results or an arrayIndexoutofbounds error depending on the size of the array.<br />
<br />
The solution is to use non-primitive types for the object. So instead of remove(char) do remove(Character).
</li>
<p>
<hr />
<li><strong>Boolean Comparison: &amp;&amp; and ||</strong> <br /> This problem can lead to some of the more confusing errors. The root cause is that when java the value for the left side that will set the result regardless of the second side, it won&#8217;t even evaluate the second side. This becomes a problem when the second side does something other than return the value (like many remove functions that return true or false depending on if it actually removes anything).<br />
<br />
The solution here is to make sure the second is a variable. If both sides are functions then assign one of them to a boolean variable before comparing. Of course completely avoiding this situation in the first case would also help : )
</li>
<p>
<hr />
<li><strong>Integer Division </strong> <br />When you type 9/4 into the program you expect 2.25 but instead you only get 2. If both numbers in the operation are integers, the answer is cast to an integer. This gotcha can be found fairly easily if it is a simple equation. The trouble appears when you get really complex equations, imagine trying to pick that out of a quartic equation :P.<br />
<br />
This solution to the problem is to have one of the operands be a double. If the value is assigned somewhere else and you are using variables (int1/int2) you can add &#8216;(double)&#8217; in front of one of the numbers. If the values are defined in the equation (2/5), converting to a double is a simple matter of adding a decimal point  &#8216;2./5&#8242;.
</li>
<p>
<hr />
<li><strong>equality:    == vs .equals()</strong> <br />As beginners we are taught that == means equality. In most programs beginners wrote, this worked fine. 2==2 would come out true and 2 == 1 would come out false, that was all we needed. Then you start learning about object oriented programs and create your own classes.<br />
<br />
Suddenly == doesn&#8217;t work like expected anymore, even though the objects are the same in all ways it comes up false. The reason is that == doesn&#8217;t test for equality, it tests to see if the object takes up the same space in memory ( other languages use &#8216;is&#8217; which I think is more obvious). Because primitives are so simple the jvm can safely store two references to equal primitives in the same memory creating the illusion that == checks for equality. == feels so natural that even after you realize exactly what it does it can slip up and catch you when you get careless.<br />
<br />
The .equals() method is java&#8217;s solution. The .equals() <b>should</b> be designed to test for that ( though the reliability depends on the developer who made the class). When you create your own class you have the option of overriding the .equals() method and designing it to get whether or not the object is equal based on what you decide.
</li>
</ul>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d133').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d133" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Java+Gotchas&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;h=Java+Gotchas" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F&amp;title=Java+Gotchas" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Java+Gotchas+@+http%3A%2F%2Fsyfran.com%2F2009%2F10%2Fjava-gotchas%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d133').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d133').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/10/java-gotchas/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Python base converter</title>
		<link>http://syfran.com/2009/09/python-base-converter/</link>
		<comments>http://syfran.com/2009/09/python-base-converter/#comments</comments>
		<pubDate>Mon, 07 Sep 2009 02:55:36 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[base]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[converter]]></category>
		<category><![CDATA[hexadecimal]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[syfran]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=240</guid>
		<description><![CDATA[Converts from any base less than or equal to 36 to any other base less than or equal to 36. This is my first python program so it is definitely not amazing, but I don&#8217;t think I did too bad.

My verdict on python, the interpreter works great for the quick scripts, but dynamic typing drives [...]]]></description>
			<content:encoded><![CDATA[<p>Converts from any base less than or equal to 36 to any other base less than or equal to 36. This is my first python program so it is definitely not amazing, but I don&#8217;t think I did too bad.<br />
<span id="more-240"></span><br />
My verdict on python, the interpreter works great for the quick scripts, but dynamic typing drives me crazy. For ex. trying to iterate through all the letters in the input it tries to treat it as an int and complains. Just seems too unpredictable and hard to catch errors. I get a feeling like there are a ton of errors waiting to happen : (</p>
<p>Anyway, here is the code, enjoy : )</p>
<pre class="brush: python;">
'''
Created on Sep 6, 2009                                                

@author: syfran
'''            

def convert(oBase, oNum, newBase):

    if(oBase &gt; 36 or newBase &gt; 36):
        print &quot;Please use a base lower than or equal to 36&quot;
        quit()                                             

    numToLett = {10:&quot;A&quot;, 11:&quot;B&quot;, 12:&quot;C&quot;, 13:&quot;D&quot;, 14:&quot;E&quot;, 15:&quot;F&quot;, 16:&quot;G&quot;, 17:&quot;H&quot;, 18:&quot;I&quot;, 19:&quot;J&quot;, 20:&quot;K&quot;, 21:&quot;L&quot;, 22:&quot;M&quot;, 23:&quot;N&quot;, 24:&quot;O&quot;, 25:&quot;P&quot;, 26:&quot;Q&quot;, 27:&quot;R&quot;, 28:&quot;S&quot;, 29:&quot;T&quot;, 30:&quot;U&quot;, 31:&quot;V&quot;, 32:&quot;W&quot;, 33:&quot;X&quot;, 34:&quot;Y&quot;, 35:&quot;Z&quot;}
    # returns a function that is used to quickly calculate a power
    def getToPower(base):
        def toPower(exponent):
            return pow(base, exponent)
        return toPower
    # converts the list into a solid string
    def listToString(list):
        returnString = &quot;&quot;
        for letter in list:
            returnString += str(letter)
        return returnString                                                                                                    

    def replaceAlpha(num):
        for value in numToLett:
            if num.upper() == numToLett[value]:
                return value                   

    def addAlpha(num):
        return numToLett[num]                  

    newNum = []
    oBasePower = getToPower(oBase)
    newBasePower = getToPower(newBase)         

    # first convert to decimal
    deci = 0
    exp = len(oNum) - 1
    for digit in oNum:
        if digit.isalpha():
            digit = replaceAlpha(digit)
        deci += oBasePower(exp) * int(digit)
        exp -= 1                               

    # find the highest possible starting number for the new Base
    exp = 0
    while deci &gt;= newBasePower(exp + 1):
        exp += 1                                                

    # break it down into the divisors
    while exp &gt;= 0:
        coeff = deci / newBasePower(exp)
        deci -= newBasePower(exp) * coeff
        if coeff &gt; 9:
            coeff = addAlpha(coeff)
        newNum.append(coeff);
        exp -= 1

    # return
    return listToString(newNum)

def run():
    num = raw_input(&quot;Please enter number&quot;)
    originalBase = raw_input(&quot;Please enter starting base&quot;)
    newBase = raw_input(&quot;Please enter new Base&quot;)
    print convert(int(originalBase),str(num),int(newBase))

if __name__ == '__main__':
    run()
</pre>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d240').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d240" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Python+base+converter&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;h=Python+base+converter" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F&amp;title=Python+base+converter" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Python+base+converter+@+http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fpython-base-converter%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d240').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d240').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/09/python-base-converter/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Java passes variables</title>
		<link>http://syfran.com/2009/09/how-java-passes-variables/</link>
		<comments>http://syfran.com/2009/09/how-java-passes-variables/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 20:24:53 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[pass]]></category>
		<category><![CDATA[variables]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=218</guid>
		<description><![CDATA[An in depth look at how java passes variables complete with examples.]]></description>
			<content:encoded><![CDATA[<p>In my opinion one of the most confusing topics that Java has to offer is how it passes its variables. Two people can argue for over a page trying to say the same thing but not realize it until after.<br />
<span id="more-218"></span><br />
The subject gets brought up mostly by C++ programmers transferring over to java. This is because in C++ you can choose how you want your variables to be passed ( by passed I mean used as an argument in a method/constructor) and you begin to realize how much it matters. Likewise it is good ( I&#8217;d argue necessary) to know how it is done in Java.</p>
<p>In C++ there are two main ways to pass a variable, pass by reference and pass by value.</p>
<ul>
<li>pass by Value &#8211; Passing by value means you that when you give an object to a method, the method is never going to touch the object you passed it. Instead the method will copy the object an manipulate that.
<p>Passing by value is characterized by the fact that whatever you do inside of the method will have no effect on the variables on the outside. For example</p>
<pre class="brush: cpp;">
void f( int x, int y) {
     int temp = y;
     y = x;
     x = temp;
}

int main() {
    int x = 2;
    int y =3;
    f(x,y);
    cout &lt; &lt; x; // will print out 2
    cout &lt;&lt; y; // will print out 3
}
</pre>
<p>So even though the program swapped the two variables in the method, it had no effect on the outside.</li>
</li>
<li> pass by reference &#8211; Passing by reference is the near opposite of passing by value. Instead of copying a new variable, the old ones are used. Now any changes made to the variables inside will have the same effect on the ones outside.
<p>Here is an example of passing by reference</p>
<pre class="brush: cpp;">
void f( int&amp; x, int&amp; y) {
     int temp = y;
     y = x;
     x = temp;
}

int main() {
    int x = 2;
    int y =3;
    f(x,y);
    cout &lt; &lt; x; // will print out 3
    cout &lt;&lt; y; // will print out 2
}
</pre>
<p>This example will switch the numbers because it is altering the variables like they were the original ones.</li>
</li>
</ul>
<p>That seems easy enough right? Well, unfortunately Java doesn&#8217;t do either. Yet depending on how you do it, the switch test could go either way. </p>
<p>What Java does is called passing the reference by value. I&#8217;m sure that doesn&#8217;t help much, possibly only makes it more confusing, so let me explain.</p>
<p>When you create an object Java assigns it a spot on the memory. Every time you use that object Java uses the variable to locate the Object in the memory.</p>
<pre class="brush: java;">
Double pi = new Double(3.14);
</pre>
<p>In this example the variable pi is pointing to the a Double object in memory.</p>
<p>The key here is to realize that there is a difference between the variable and the object. Just because we said that pi = new Double(); does not mean that pi is exactly the same as the double, it simply points to the Double in memory.</p>
<pre class="brush: java;">
pi = new Double(3.1415);
</pre>
<p>Now we create a new Double object in memory and tell pi to point to it. It does not take the previous Double&#8217;s place in memory. The previous Double will most likely just sit there until the Garbage Collector picks it up.</p>
<pre class="brush: java;">
function(pi);
...
static void function(Double passedPi) {
    passedPi = new Double(3.14);
}
</pre>
<p>Now we are going to pass the variable to the function. Notice: we are not passing the object itself, just the variable that is pointing at it. This variable that is pointing at it is passed by reference.</p>
<p>So once we pass the variable we have pi that is pointing to the Double(3.1415) and when we enter the method passedPi is pointing towards Double(3.1415).</p>
<p>They are both pointing to the same object, but they are completely separated from each-other now. To prove this we will assign passedPi to a Double(3.14).</p>
<p>If you print out the values you will see that pi is still pointing at Double(3.1415) but passedPi is pointing at Double(3.14). There were no outside effects from the change.</p>
<p>So if you can&#8217;t assign new values to variables what can you do? The secret is to modify the memory itself not the pointer to the memory ie. You may modify the attributes of an object and it will apply all around.</p>
<pre class="brush: java;">
Point myPoint = new Point();
myPoint.x = 2;
myPoint.y = 3;
swap(myPoint);

...

void swap(Point p) {
    int temp = p.x;
    p.x = p.y;
    p.y = temp;
}
</pre>
<p>That code will actually swap the x and the y values. This is because you are modifying the object that is in memory.</p>
<p>They key to remembering this is to separate the variables from the actual objects themselves, where the variable is simply a pointer to the Object. </p>
<p>For those that know C++/C it is effectively a pointer that you can&#8217;t selectively dereference.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d218').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d218" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=How+Java+passes+variables&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;h=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F&amp;title=How+Java+passes+variables" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+How+Java+passes+variables+@+http%3A%2F%2Fsyfran.com%2F2009%2F09%2Fhow-java-passes-variables%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d218').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d218').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/09/how-java-passes-variables/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Quick Intro to the Ternary Operator</title>
		<link>http://syfran.com/2009/08/ternary-operator/</link>
		<comments>http://syfran.com/2009/08/ternary-operator/#comments</comments>
		<pubDate>Mon, 31 Aug 2009 08:34:30 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[ternary operator]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=157</guid>
		<description><![CDATA[A short introduction to the ternary operator of java.]]></description>
			<content:encoded><![CDATA[<h4>The Ternary Operator</h4>
<p>The ternary operator is a source of confusion for many beginning programmers. In case you don&#8217;t know what the ternary operator is, it looks something like</p>
<pre>String message = (isvalid?"Valid":"Invalid");</pre>
<p>The ? represents the thought that goes through their head when they see it and they probably won&#8217;t get to the : .<br />
<span id="more-157"></span><br />
One of the reasons why this operator is so confusing is that the symbols have no logical connection to the actual function. Everyone knows that + means addition same with &#8211; , /, and *. It&#8217;s all pretty basic math/computer stuff, even ++ isn&#8217;t too hard to figure out, but what in god&#8217;s name does ? : do? Adding to the confusion the operator is not widely used or taught in books or tutorials, so beginners are never exposed to it.</p>
<p>Lucky you, I&#8217;m here to teach you all about them (or maybe unlucky you? I don&#8217;t know you decide). Let&#8217;s start by pulling an example out of the big black hat of code&#8230;</p>
<p>Say you have created a virtual world and you need to write a code that determines whether your character buys a car or a bicycle based on how much money he has. If he has less than $20,000 he will buy a bike, else he will by a used car.</p>
<p>The first way to write this would be an if else statement:</p>
<pre class="brush: java;">
Item itemToBuy;
if(amountOfMoney &gt; 20000) {
    itemToBuy = new UsedCar();
} else {
    itemToBuy = new Bicycle();
}
itemToBuy.purchase();
</pre>
<p>That looks okay, but that it can be cut down considerably with our operator.</p>
<pre class="brush: java;">
Item itemToBuy = (money &gt; 20000 ? new UsedCar() : new Bicycle() );
itemToBuy.purchase();
</pre>
<p>or even</p>
<pre class="brush: java;">
(money &gt; 20000 ? new UsedCar() : new Bicycle()).purchase();
</pre>
<p>Using the ternary operator allowed it to be much more compact than a usual if/else statement (also much more confusing).</p>
<p>Now that we have something to work with, let&#8217;s break it into pieces.</p>
<ul>
<li>money &gt; 20000</li>
<p>This is the if statement of the operator. The result of the boolean comparison will determine what gets returned.</p>
<li>?</li>
<p>This  is what signals that it is a ternary operator. It separates the &#8216;if statement&#8217; from the return clauses.</p>
<li>new UsedCar()</li>
<p>This is the first return clause. If the expression evaluated to true then the value here, will be returned.</p>
<li>:</li>
<p>This is the second separator. It separates the &#8216;then&#8217; clause from the &#8216;else&#8217; clause.</p>
<li>new Bicycle()</li>
<p>And finally there is the &#8216;else&#8217; clause. The value here is returned if the expression evaluated to false.</ul>
<p>Notice how it is worded that it &#8216;returns&#8217; the value. This is the main difference between the ternary operator and an if/else statement. So a better representation of a ternary operator is an if/else statement within a method.</p>
<pre class="brush: java;">
public Item makeDecision(boolean canAfford) {
    if(canAfford) {
        return new UsedCar();
    } else {
        return new Bicycle();
    }
}
public void someMethod(...) {
    ...
    makeDecision(money &gt; 20000).purchase();
    ...
}</pre>
<p>All right so hopefully you know how the ternary operator works now. Let&#8217;s go over some ways that you can use it.</p>
<p>Almost forgot, for extra confusing boolean logic you can use nested ternary operators.</p>
<pre class="brush: java;">
(money &gt; 20000 ? (money &gt; 40000 ? (money &gt; 100000  ? new SuperFancyCar() : new NewCar() : new UsedCar()) : new Bicycle()).purchase();</pre>
<h4>Using the ternary operator</h4>
<p>Unless you actually believe it will make your code cleaner, don&#8217;t. While it is nifty feature and good for showing off your coding skills, it will just confuse anybody reading your code (including you).</p>
<p>The few times I have seen it put to good use is with strings. Situations where similar to the first one I posted where you need to print a response based on a variable and an if/else would be cumbersome.</p>
<p>Whatever you do, don&#8217;t use it to return a boolean value. I didn&#8217;t believe my eyes when I saw this</p>
<pre class="brush: java;">
if (speed &gt; speedLimit ? true : false;) {
  pullEmOver();
}
</pre>
<p>on the first result when Googling ternary operator. It is adding useless code that is especially confusing. What would have been the problem with the following?
<pre class="brush: java;">
if (speed &gt; speedLimit) {
  pullEmOver();
}
</pre>
<p>Ranting aside, I hope you learned something from this tutorial/short intro (600 words is short right?), now go out there and show off/confuse people with your new awesome coding skillz!!!!</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d157').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d157" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Quick+Intro+to+the+Ternary+Operator&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;h=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F&amp;title=Quick+Intro+to+the+Ternary+Operator" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Quick+Intro+to+the+Ternary+Operator+@+http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fternary-operator%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d157').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d157').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/08/ternary-operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreaded String Permutations</title>
		<link>http://syfran.com/2009/08/threadedpermutations/</link>
		<comments>http://syfran.com/2009/08/threadedpermutations/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 14:11:13 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[multithreaded]]></category>
		<category><![CDATA[permutations]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=137</guid>
		<description><![CDATA[Multithreaded apllication to find every permutation of a String.]]></description>
			<content:encoded><![CDATA[<p>This code snippet is designed to take in a string an print out every possible permutation of that string. The only problem I&#8217;ve encountered so far is that it uses a lot of memory with longer strings.</p>
<p><span id="more-137"></span></p>
<pre class="brush: java;">package com.syfran.Permut;

import java.util.ArrayList;
import java.util.EmptyStackException;
import java.util.List;
import java.util.Scanner;
import java.util.Stack;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Permutations {

	public static void main(String[] pie) {

		String input = &quot;&quot;;

		// if there are no arguments, prompt the user
		if (pie.length &gt; 0) {

			// Get all the arguments, gets rid of the need for pesky quotes
			for (String temp : pie)
				input += &quot; &quot; + temp;
		} else {
			System.out.println(&quot;Please enter String: &quot;);
			input = (new Scanner(System.in).nextLine());
		}

		Permutations p = new Permutations();

		p.begin(input);

	}

	public void begin(final String myString) {

		// Number of threads, probably best to set it just over the number of
		// cores you have
		final int THREADS = 4;

		// Create a thread safe queue, add initialize it with a blank string.
		Stack&lt;String&gt; myStack = new Stack&lt;String&gt;();
		myStack.push(&quot;&quot;);

		// Used to execute the threads
		ExecutorService ex = Executors.newFixedThreadPool(THREADS);

		Runnable myRunnable = new PermutWorker(myStack, myString);

		// make sure there is enough in the queue so that it doesn't end early
		final int SEED = 2;
		for (int x = 0; x &lt; SEED; x++)
			seedQueue(myRunnable, ex);

		// continue to execute the worker
		try {
			while (true) {

				ex.execute(myRunnable);
			}
		} catch (EmptyStackException e) {

		}

		// shutdown the executor
		ex.shutdown();

	}

	private void seedQueue(Runnable run, ExecutorService ex) {
		ex.execute(run);

		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			// give up / sad panda
			return;
		}

	}

	// Main content of the thread
	private class PermutWorker implements Runnable {
		private Stack&lt;String&gt; stack;
		private final String original;

		PermutWorker(Stack&lt;String&gt; myStack, final String original) {
			this.stack = myStack;
			this.original = original;
		}

		@Override
		public void run() {
			String current = null;
			try {
				// get the current state of permutations from the queue
				current = stack.pop();
			}
			// if we get null then wait for awhile then try again
			catch (EmptyStackException ex) {
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					return;
				}
				try {
					current = stack.pop();
				} catch (EmptyStackException e) {
					return;
				}
			}

			// create List of current characters
			List&lt;Character&gt; currentList = new ArrayList&lt;Character&gt;();
			for (Character c : current.toCharArray())
				currentList.add(c);

			// create list of characters in original
			List&lt;Character&gt; remaining = new ArrayList&lt;Character&gt;();
			for (Character c : original.toCharArray())
				remaining.add(c);

			// remove current from the original to get the remaining
			for (Character c : currentList)
				remaining.remove(c);

			// for every remaining character, add it to current
			// if it is the same length as the original then print it
			// else add it to the queue
			for (Character c : remaining) {
				String result = current + c;
				if (result.length() == original.length()) {
					// change this as needed
					System.out.println(result);
				} else {
					stack.push(result);
				}

			}

		}
	}
}
</pre>
<p>UPDATE: Replaced the queue with a stack to try to help with the memory problem. It will still get outofMemory errors on longer strings but it can handle a bit more. The stack should focus on the longest strings first and get them (hopefully if the GC does its job) out of the memory quickly.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d137').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d137" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Multithreaded+String+Permutations&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;h=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F&amp;title=Multithreaded+String+Permutations" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Multithreaded+String+Permutations+@+http%3A%2F%2Fsyfran.com%2F2009%2F08%2Fthreadedpermutations%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d137').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d137').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/08/threadedpermutations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Increase Your Productivity!!</title>
		<link>http://syfran.com/2009/07/productivtim/</link>
		<comments>http://syfran.com/2009/07/productivtim/#comments</comments>
		<pubDate>Sun, 26 Jul 2009 20:07:30 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Full Program]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[program]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=114</guid>
		<description><![CDATA[Just uploaded a project I have been hiding away for awhile. My amazing name for it is Productivity Timer.

I read somewhere that you could increase productivity by writing down what you do every 15 minutes. I saw this and immediately decided to make a program for it. 
This program stays in a small box with [...]]]></description>
			<content:encoded><![CDATA[<p>Just uploaded a project I have been hiding away for awhile. My amazing name for it is Productivity Timer.<br />
<span id="more-114"></span><br />
I read somewhere that you could increase productivity by writing down what you do every 15 minutes. I saw this and immediately decided to make a program for it. </p>
<p>This program stays in a small box with a timer, well out of the way until the timer runs up. At that point it pops up with a small box. You then enter a short summary of your accomplishments and continue. The program writes what you say into the directory you designate labeled with the date. Each line inside is labeled with the time and the comment.</p>
<p>Whether you run it from the files or from the jar it has two arguments. The first one is required, it is the folder you want to keep the logs under. The second is optional. It is the amount of time between polls in minutes, the default is 15.</p>
<p>I cleaned it up and put it up so that it is ready for other people to use, though it is still fairly messy. The method of initializing all the parts that have circular dependencies on eachother is pretty messy as well. I might come back and revise this sooner or later.</p>
<p>All the source code can be found <a href="http://code.syfran.com/productivTim/">http://code.syfran.com/productivTim/</a> including an up to date jar file that can be run with <i>java -jar ProductivityTimer.jar MyDir</i>. It can also be checked out with <i>svn co http://code.syfran.com/productivTim</i>.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d114').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d114" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Increase+Your+Productivity%21%21&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;h=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F&amp;title=Increase+Your+Productivity%21%21" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Increase+Your+Productivity%21%21+@+http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fproductivtim%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d114').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d114').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/07/productivtim/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Modeling the Human Memory</title>
		<link>http://syfran.com/2009/07/modeling-the-human-memory/</link>
		<comments>http://syfran.com/2009/07/modeling-the-human-memory/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 04:48:10 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[AI]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=107</guid>
		<description><![CDATA[A quick idea on modeling the human memory based on relationships.]]></description>
			<content:encoded><![CDATA[<p>All right, this is a very rough idea and is going to be <b>far</b> from perfect (and I&#8217;m sure someone else has though of it before). The idea is to use a node type system that defines an object based on it&#8217;s relationships.<br />
<span id="more-107"></span><br />
Relationships can be strengthened or weakened and will slowly degrade over time. These relationships will change and shift over time similar to the human mind. For example, a tree.</p>
<p>Some of the associations may be color, size, similarities, properties etc. Even the name of the object will be associated by a relationship.</p>
<p>With this model it could be possible to represent abstract concepts and ideas. In addition to that it would perfectly model the differences in everyone&#8217;s thought processes and decision making.</p>
<p>The downside would be that it would have to learn exactly like a human would slowly and one step at a time.</p>
<p>Once again this is just a quick half formed idea I had and most likely will not work or is impossible ( or both), but I&#8217;d figured I&#8217;d share it anyway.</p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d107').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d107" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Modeling+the+Human+Memory&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;h=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F&amp;title=Modeling+the+Human+Memory" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Modeling+the+Human+Memory+@+http%3A%2F%2Fsyfran.com%2F2009%2F07%2Fmodeling-the-human-memory%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d107').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d107').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/07/modeling-the-human-memory/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Converting Images from rgb to grayscale with Java</title>
		<link>http://syfran.com/2009/07/rgb_tograyscale_java/</link>
		<comments>http://syfran.com/2009/07/rgb_tograyscale_java/#comments</comments>
		<pubDate>Mon, 13 Jul 2009 10:03:44 +0000</pubDate>
		<dc:creator>syfran</dc:creator>
				<category><![CDATA[Articles]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Snippet]]></category>
		<category><![CDATA[gray]]></category>
		<category><![CDATA[grayscale]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[rgb]]></category>

		<guid isPermaLink="false">http://syfran.com/?p=61</guid>
		<description><![CDATA[Easy to implement grayscale program to sap the color out of a picture.]]></description>
			<content:encoded><![CDATA[<p>Not sure what motivated me here but I took a little time and came up with this. It takes the original image file ( can be a multitude of formats, has worked with both jpg and png so far) and outputs a grayscale image (jpeg or png).<br />
<span id="more-61"></span><br />
<div id="attachment_62" class="wp-caption alignnone" style="width: 267px"><a href="http://syfran.com/wp-content/uploads/2009/07/tux.png"><img class="size-medium wp-image-62" title="color_Tux" src="http://syfran.com/wp-content/uploads/2009/07/tux-257x300.png" alt="Tux the penguin in full color." width="257" height="300" /></a><p class="wp-caption-text">Tux the penguin in full color.</p></div></p>
<div id="attachment_63" class="wp-caption alignnone" style="width: 267px"><a href="http://syfran.com/wp-content/uploads/2009/07/greyTux.jpg"><img class="size-medium wp-image-63" title="Grayscale Tux" src="http://syfran.com/wp-content/uploads/2009/07/greyTux-257x300.jpg" alt="The aftermath." width="257" height="300" /></a><p class="wp-caption-text">The aftermath.</p></div>
<p>Here is the slightly ugly code&#8230;</p>
<pre class="brush: java;">import javax.imageio.*;
import java.io.*;
import java.awt.image.*;                                   

public class GrayScale {
        public static void main(String [] pie) {

                // Argument Checks
                if(!((pie.length == 3) &amp;&amp; (new File(pie[0]).exists()) &amp;&amp;(pie[2].equals(&quot;jpeg&quot;) || pie[2].equals(&quot;png&quot;)))) {
                        System.out.println(&quot;Usage: java GrayScale {original image} {modified image} {Output Format}\nOutput Format can be jpeg or png&quot;);
                        System.exit(0);
                }                                                                                                              

                BufferedImage image = null;
                try {
                image = ImageIO.read(new File(pie[0])); // Read in the original file
                } catch(IOException e) {
                        System.err.println(&quot;Error reading in file, check file name and format&quot;);
                        System.exit(0);
                }                                                                               

                // loop through every pixel in the image
                for(int x = 0; x &lt;image .getWidth(); x ++) {
                        for(int y = 0; y &lt; image.getHeight(); y++) {
                                int RGB = image.getRGB(x,y);            // Get the rgb of the pixel

                                int blue = (RGB &amp; 0x000000FF);
                                int green = (RGB &amp; 0x0000FF00) &gt;&gt; 8;
                                int red = (RGB &amp; 0x00FF0000) &gt;&gt; 16;
                                int average = (blue + green + red)/3;
                                int newRGB = 0xFF000000 + (average &lt; &lt; 16) + (average &lt;&lt; 8 ) + average; // and reassign it
                                image.setRGB(x,y,newRGB);
                        }
                }
                try {
                        ImageIO.write(image, pie[2], new File(pie[1])); // Writes the new grayscale file.
                } catch(IOException e) {
                        System.err.println(&quot;Unable to output results&quot;);
                }

        }
}
</pre>
<p>UPDATE: Edited thanks to the comment from Gasper. Swapped out the Color class for bitwise operations and it seems to be working well. Also figured out how to format the code in a way that looks better.</image></image></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a title="Click me to see the sites." href="#" onclick="$$('div.d61').each( function(e) { e.visualEffect('slide_down',{duration:2.5}) }); return false;"><strong><em>Bookmark It</em></strong></a>
<br />
<div class="d61" style="overflow:hidden">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Converting+Images+from+rgb+to+grayscale+with+Java&amp;url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.google.com/bookmarks/mark?op=edit&amp;output=popup&amp;bkmk=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;Google Bookmarks"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/google.png" title="Add to&nbsp;Google Bookmarks" alt="Add to&nbsp;Google Bookmarks" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.newsvine.com/_wine/save?u=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;h=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;Newsvine"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/newsvine.png" title="Add to&nbsp;Newsvine" alt="Add to&nbsp;Newsvine" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://slashdot.org/bookmark.pl?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;Slashdot"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/slashdot.png" title="Add to&nbsp;Slashdot" alt="Add to&nbsp;Slashdot" /></a>
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F&amp;title=Converting+Images+from+rgb+to+grayscale+with+Java" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Converting+Images+from+rgb+to+grayscale+with+Java+@+http%3A%2F%2Fsyfran.com%2F2009%2F07%2Frgb_tograyscale_java%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://syfran.com/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
<a style="font-size:90%;text-align: right; " title="Click me to hide the sites." href="#" onclick="$$('div.d61').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); return false;">Hide Sites</a>
</div>
</div>
<!-- Social Bookmarks END -->
<script type="text/javascript">$$('div.d61').each( function(e) { e.visualEffect('slide_up',{duration:0.5}) }); </script>]]></content:encoded>
			<wfw:commentRss>http://syfran.com/2009/07/rgb_tograyscale_java/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
