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

I

iherage

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?
 
T

TechBookReport

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?
Do a Google on Java and generics...

HTH

==================================================================
TechBookReport Java http://www.techbookreport.com/JavaIndex.html
 
J

Jesper Nordenberg

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>

This should be template<typename T>.

Java doesn't have as powerful generic programming constructs as C++
templates. The closest you get in Java is generic types introduced in
Java 5 (1.5). The biggest differences are that Java generics only
works for classes and not primitive types, and that Java generics
supports wildcards (very useful feature). If you really need the full
power of generic programming, I would suggest that you use C++. In
most cases, Java works well though.

/Jesper Nordenberg
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top