static int square(final int x) {
return x * x;
}
class Pair <E1, E2> {
private final E1 element1;
private final E2 element2;
public Pair(final E1 e1, final E2 e2) {
element1 = e1;
element2 = e2;
}
public E1 getFirst() {
return element1;
}
public E2 getSecond() {
return element2;
}
@Override
public String toString() {
return "(" + element1.toString() + ", " + element2.toString() + ")";
}
}
class Sock {}
class Man {}
class Woman {}
class Test {
public static void main(String args[]) {
Pair <Sock, Sock> pairOfSocks;
Pair <Man, Woman> churchMarriedCouple;
Pair <Man, Man> civilPartners;
}
}
Pair <Sock, Sock>
Pair <Man, Woman>
Vector <String>
Collection <Integer>
Collection <Pair <Sock, Sock>>
extends
ή
super
και το όνομα ενός τύπου.
Παραδείγματα:
Collection <?>
Collection <? extends E>
Collection <? super E>
class LinkedList <E> {
/** Node's value */
private E value;
/** Next node */
private LinkedList <E> next;
/** Construct a list with a single element v */
LinkedList(final E v) {
value = v;
next = null;
}
/** Return a list with element n added to it */
public LinkedList <E> add(final E v) {
var n = new LinkedList <E>(v);
n.next = this;
return n;
}
/** Return a string representation of the list */
@Override
public String toString() {
final String me = value.toString();
/* Recursive implementation */
if (next == null)
return me;
else
return me + " -> " + next;
}
/** Test harness */
static public void main(String args[]) {
var ilst = new LinkedList <Integer>(0);
ilst = ilst.add(1);
ilst = ilst.add(18);
ilst = ilst.add(45);
for (int i = 0; i < 5; i++)
ilst = ilst.add(i * 10);
System.out.println(ilst);
}
}
double
square(final double x)
{
return x * x;
}
Complex
square(final Complex x)
{
return new Complex(
square(x.real) + square(x.imaginary),
2 * x.real + x.imaginary);
}
switch
με ταίριασμα προτύπων).
public void run()
MyThreadClass tc = new MyThreadClass();
Thread t = new Thread(tc);
MyThreadClass tc = new MyThreadClass();
Thread t = new Thread(tc);
t.start();
public class CocktailGuest implements Runnable {
/** What the guest will say */
private final String mumble;
/** How many seconds will he/she pause before speaking */
private final int pause;
/** How long the guest will stay */
private final int stay;
/** How long the guest has stayed */
private int hereFor;
/** Constructor */
public CocktailGuest(String mumble, int pause, int stay) {
this.mumble = mumble;
this.pause = pause;
this.stay = stay;
hereFor = 0;
}
/** Execution method */
@Override
public void run() {
try {
while (hereFor < stay) {
Thread.sleep(pause * 1000);
hereFor += pause;
System.out.println(mumble);
}
} catch (InterruptedException e) {
System.out.println("Something has come up; got to go.");
return;
} finally {
System.out.println("Good bye.");
}
}
public static void main(String args[]) {
final int NGUEST = 5;
var guest = new CocktailGuest[NGUEST];
var thread = new Thread[NGUEST];
int i = 0;
guest[i++] = new CocktailGuest("Can I have another drink?", 8, 30);
guest[i++] = new CocktailGuest("Nice food!", 7, 120);
guest[i++] = new CocktailGuest("Ha ha ha...", 3, 100);
guest[i++] = new CocktailGuest("Hi, I am Maria.", 5, 60);
guest[i++] = new CocktailGuest("Hello, I am Petros.", 15, 60);
// Create the threads
for (i = 0; i < NGUEST; i++)
thread[i] = new Thread(guest[i]);
// Start the threads
for (i = 0; i < NGUEST; i++)
thread[i].start();
}
}