|
|
Program: Write a simple generics class example with two type parameters.
Below example shows how to create a simple generics class with two type parameters. Look at the class
definition, we defined two types of parameters called U & V, seperated by ",". You can define multiple type parameters
seperated by ",". Look at sample code for more comments.
package com.java2novice.generics;
public class MySimpleTwoGenerics {
public static void main(String a[]){
SimpleGen<String, Integer> sample
= new SimpleGen<String, Integer>("JAVA2NOVICE", 100);
sample.printTypes();
}
}
/**
* Simple generics class with two type parameters U, V.
*/
class SimpleGen<U, V>{
//type U object reference
private U objUreff;
//type V object reference
private V objVreff;
//constructor to accept object type U and object type V
public SimpleGen(U objU, V objV){
this.objUreff = objU;
this.objVreff = objV;
}
public void printTypes(){
System.out.println("U Type: "+this.objUreff.getClass().getName());
System.out.println("V Type: "+this.objVreff.getClass().getName());
}
}
|
|
Output: |
U Type: java.lang.String
V Type: java.lang.Integer
|
|
|
|
|
Java Generics Sample Code Examples
- Write a simple generics class example.
- Write a simple generics class example with two type parameters.
- How implement bounded types (extend superclass) with generics?
- How implement bounded types (implements an interface) with generics?
- What is generics wildcard arguments? Give an example.
|
|
|
wait Vs sleep methods
sleep():
It is a static method on Thread class. It makes the current thread into the
"Not Runnable" state for specified amount of time. During this time, the thread
keeps the lock (monitors) it has acquired.
wait():
It is a method on Object class. It makes the current thread into the "Not Runnable"
state. Wait is called on a object, not a thread. Before calling wait() method, the
object should be synchronized, means the object should be inside synchronized block.
The call to wait() releases the acquired lock.
Discipline is just choosing between what you want now and what you want most.
-- Unknown Author
|