Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

C++ has the key word <template>.Do Java have something alike?
I am learning Java,but I happened to read a book about C++.I find in
C++,you can use <template T>.
e.g.
<template T>
void swap(T a,T b)
{
T temp=a;
a=b;
b=a;
}
T can be char,int ect.
So whatever I want to swap int or char or something else,I only need to
write the codes once.
Can Java do this?


Report this thread to moderator Post Follow-up to this message
Old Post
iherage@gmail.com
06-01-05 09:01 PM


Re: C++ has the key word <template>.Do Java have something alike?
Hi,

Genericity (template classes) was just added in the new
java version J2SE 5.0

http://java.sun.com/j2se/1.5.0/docs...s.html#generics

HTH
Regards,
Giovanni

<iherage@gmail.com> wrote in message
news:1117636888.757651.187950@o13g2000cwo.googlegroups.com...
>I am learning Java,but I happened to read a book about C++.I find in
> C++,you can use <template T>.
> e.g.
> <template T>
> void swap(T a,T b)
> {
>    T temp=a;
>      a=b;
>      b=a;
> }
> T can be char,int ect.
> So whatever I want to swap int or char or something else,I only need to
> write the codes once.
> Can Java do this?
>



Report this thread to moderator Post Follow-up to this message
Old Post
Giovanni Azua
06-01-05 09:01 PM


Re: C++ has the key word <template>.Do Java have something alike?
Giovanni Azua wrote:
> Hi,
>
> Genericity (template classes) was just added in the new
> java version J2SE 5.0
>
> http://java.sun.com/j2se/1.5.0/docs...s.html#generics
>
> HTH
> Regards,
> Giovanni
>
> <iherage@gmail.com> wrote in message
> news:1117636888.757651.187950@o13g2000cwo.googlegroups.com...
> 

This will only do what you wanted if you make those reference parameters
as in swap( T &a, T &b )
 

As Giovanni noted generics were added in JDK 1.5, but generics are not
the same as C++ templates and will not work with the primitive types.
 

The swap method also depends on pass by reference which is something
that Java does not support (thank goodness).

--
Dale King

Report this thread to moderator Post Follow-up to this message
Old Post
Dale King
06-02-05 09:01 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Java Help archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 06:41 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.