<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title>Gábor's blog</title>
  <link rel="alternate" type="text/html" href="http://www.nekomancer.net/blog/archives/java-cpp-generics-templates"/>
  <link rel="self" type="application/atom+xml" href="http://www.nekomancer.net/node/146/atom/feed"/>
  <id>http://www.nekomancer.net/node/146/atom/feed</id>
  <updated>2008-03-28T19:36:04-05:00</updated>
  <entry>
    <title>Java/C++ generics/templates</title>
    <link rel="alternate" type="text/html" href="http://www.nekomancer.net/blog/archives/java-cpp-generics-templates" />
    <id>http://www.nekomancer.net/blog/archives/java-cpp-generics-templates</id>
    <published>2006-12-06T02:40:13-06:00</published>
    <updated>2008-03-28T19:36:04-05:00</updated>
    <author>
      <name>gabor</name>
    </author>
    <category term="java" />
    <category term="programming" />
    <summary type="html"><![CDATA[<p>I&#8217;ve read today this <a href="http://www.peerbox.com:8668/space/start/2006-12-04/1#Java_vs_C++_Performance">blog entry about java/c++ performance</a> .</p>

<p>i agree with most of his arguments, but the generics one&#8230;</p>
    ]]></summary>
    <content type="html"><![CDATA[<p>I&#8217;ve read today this <a href="http://www.peerbox.com:8668/space/start/2006-12-04/1#Java_vs_C++_Performance">blog entry about java/c++ performance</a> .</p>

<p>i agree with most of his arguments, but the generics one&#8230; well&#8230;</p>

<p>i wanted to post a comment on his blog, but it kept asking me to login when i wanted to post a comment, and i couldn&#8217;t find a way to register, so i am posting my &#8220;reply&#8221; here:</p>

<p>where he argues about java/c++ generics/templates, he says that the java ones are basically better.
while it might be true, that there are certain enhancements in the way java does templates,
we must not forget, that there are certain very useful things one can do with templates, but cannot with generics.</p>

<p>example</p>

<div class="geshifilter"><pre class="geshifilter-cpp"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;string&gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std;
&nbsp;
<span style="color: #0000ff;">class</span> Clazz1
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    string get_hello_message<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>;
<span style="color: #008000;">&#125;</span>;
&nbsp;
string Clazz1<span style="color: #008080;">::</span><span style="color: #00eeff;">get_hello_message</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> string<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Clazz1.hello&quot;</span><span style="color: #008000;">&#41;</span>;
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #0000ff;">class</span> Clazz2
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    string get_hello_message<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>;
<span style="color: #008000;">&#125;</span>;
&nbsp;
string Clazz2<span style="color: #008080;">::</span><span style="color: #00eeff;">get_hello_message</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> string<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Clazz2.hello&quot;</span><span style="color: #008000;">&#41;</span>;
<span style="color: #008000;">&#125;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">class</span> T<span style="color: #000080;">&gt;</span>
T say_hello<span style="color: #008000;">&#40;</span>T obj<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;</span> <span style="color: #000080;">&lt;</span> obj.<span style="color: #00eeff;">get_hello_message</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl;
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
Clazz1 c1;
Clazz2 c2;
say_hello<span style="color: #008000;">&#40;</span>c1<span style="color: #008000;">&#41;</span>;
say_hello<span style="color: #008000;">&#40;</span>c2<span style="color: #008000;">&#41;</span>;
&nbsp;
<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span>;
<span style="color: #008000;">&#125;</span></pre></div>

<p>in this C++ program i define 2 classes, both have a method called get&#95;hello&#95;message, but the two classes are not related to each other.
then i define a function, that takes an object of any type (and calls it&#8217;s get&#95;hello&#95;message method.</p>

<p>try this in java :)</p>
    ]]></content>
  </entry>
</feed>
