class Summation[[NumT]] where NumT { NumT add(NumT); }
{
public NumT val;
public Summation(NumT initial_value) { val = initial_value; }
public void AddValue(NumT value) { val = val.add(value); }
}
public class Demo {
public static void main(String[] args) {
Summation[[int]] SI = new Summation[[int]](3);
SI.AddValue(4);
SI.AddValue(3);
System.out.println(SI.val);
}
}
Note that the type int satisfies the constraint on NumT, even
though Java primitive types do not have methods of their own.
As another example, consider the following generic bubble-sort routine.
class Sorter[[ContainerT,ElemT]]
where ContainerT { ElemT fetch(int);
void store(ElemT,int);
int length(); }
// a.compareTo(b) returns -1 if a < b, 0 if a == b, 1 if a > b.
where ElemT { int compareTo(ElemT); }
{
public static void BubbleSort(ContainerT c) {
boolean ordered = false;
while (!ordered) {
ordered = true;
for(int i = 0; i < c.length()-1; ++i) {
if (c.fetch(i).compareTo(c.fetch(i+1)) > 0) {
ordered = false;
ElemT elem = c.fetch(i);;
c.store(c.fetch(i+1), i);
c.store(elem, i+1);
}
}
}
}
}
public class Demo
{
public static void main(String[] args) {
long[] longArray = new long[4];
longArray[0]=5; longArray[1]=4; longArray[2]=1; longArray[3]=4;
Sorter[[long[],long]].BubbleSort(longArray);
for(int i = 0; i < 4; ++i) {
System.out.println(longArray[i]);
}
}
}
java.lang.Integer.toString).
For arrays, this method calls java.lang.Object.toString
java.lang.Integer.hashCode.
For arrays, this method calls java.lang.Object.hashCode.
byte, short, int, long, float, double, and
char are defined to be numeric. In the method signatures
below, "Numeric" should be replaced by the appropriate numeric type.
byte, short, int, and char.
byte, short,
int, long, char, and boolean have the following methods.
(These methods are bitwise operations for all types but boolean, for which
they are logical operations.)
byte, short, int,
long, char) have the following method:
boolean has the following methods:
Element[] have the following methods: