Path: csiph.com!eeepc.pasdenom.info!news.pasdenom.info!news.dougwise.org!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!194.25.134.126.MISMATCH!newsfeed01.sul.t-online.de!t-online.de!news.belwue.de!not-for-mail From: Thomas Richter Newsgroups: comp.lang.java.programmer Subject: Re: Java vs C++ Date: Mon, 07 Feb 2011 01:04:45 +0100 Organization: InterNetNews at News.BelWue.DE (Stuttgart, Germany) Lines: 37 Message-ID: References: <4d4d585c$0$81476$e4fe514c@news.xs4all.nl> <4d4d60dc$0$23763$14726298@news.sunsite.dk> <4d4d8322$0$41117$e4fe514c@news.xs4all.nl> <4d4f042b$0$23753$14726298@news.sunsite.dk> NNTP-Posting-Host: vpn-m-8d3a2f62.campus.uni-stuttgart.de Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: news.belwue.de 1297037087 25513 141.58.47.98 (7 Feb 2011 00:04:47 GMT) X-Complaints-To: news@news.belwue.de NNTP-Posting-Date: Mon, 7 Feb 2011 00:04:47 +0000 (UTC) User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100329) In-Reply-To: <4d4f042b$0$23753$14726298@news.sunsite.dk> Xref: csiph.com comp.lang.java.programmer:25654 Arne Vajhøj wrote: >>> Templates in C++ are basically advanced macros--each invocation of a >>> template type regenerates the class, so a Foo and a >>> Foo are >>> two completely different things. >> >> They are different things in Java as well. > > No. Same code. I don't know in how far "same code" makes sense in Java in first place. The code that is created depends entirely on the JIT, and it may or may not generate the same code depending on how often it is called. For example, if it the JIT "sees" that the code is called with one specific type most of the time, it could create separate code just for this purpose and hence omit the type checks. And in C++, a compiler can also merge template instances because they are identical, i.e. function alike, i.e. for example they all operate on pointers. Thus, I don't really see how that type of argument applies. >>> Generics in Java are basically compile-time syntactic sugar to ensure >>> type-safety of container types at compile time instead of waiting until >>> you hit the code at runtime to get that dreaded ClassCastException. >> >> Same sort of thing in C++. Not really. Generics are run-time checked, templates are compile-time checked. There is no "ClassCastException" in C++. If the class doesn't fit, the compiler will tell at compile time. Otherwise, the code will crash at run time - there is no additional check inserted as it is in java which tests whether the type-erased instance is of the proper type. Greetings, Thomas