Skip to content

Instantly share code, notes, and snippets.

@codinko
Last active October 8, 2015 19:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codinko/6b7ff91e759bad8989c3 to your computer and use it in GitHub Desktop.
Save codinko/6b7ff91e759bad8989c3 to your computer and use it in GitHub Desktop.
Generics-part4
package com.codinko;
public class GenericClassT<T> {
private T t;
public T get() {
return this.t;
}
public void set(T t) {
this.t = t;
}
public static void main(String args[]) {
GenericClassT<String> type = new GenericClassT<String>();
type.set("Harley");
String str = type.get(); //valid
System.out.println(str);
}
}
Before Generics we declare an ArrayList like this:
List list = new ArrayList();
Using generics , it is
List<String> list = new ArrayList(); // for a list of String
We know that this provides
-Stronger type checks at compile time [w.r.to this example, you can add only String objects]
-Elimination of casts [w.r.to this example, you get always String, no need to cast]
-Enabling programmers to implement generic algorithms [w.r.to this example this cant be explained,
it makes sense when you write Generic class eith type parameter]
Now we are familiar with Java Collections - here ArrayList class,
let's visualize the same concept in any other class
---
We can define our own classes and interfaces with generics type.
A generic type is a class or interface that is parameterized over types.
Use angle brackets (<>) to specify the type parameter.
---
If we don’t provide the type at the time of creation, compiler will produce a warning that
“GenericClassT is a raw type. References to generic type GenericClassT<T>
should be parameterized”.
When we don’t provide type, the type becomes Object and we should avoid this
because we will have to use type casting while working on raw type that can produce runtime errors.
---
See NonGenericClass.java and GenericClassT.java
package com.codinko;
public class NonGenericClass {
private Object t;
public Object get() {
return this.t;
}
public void set(Object t) {
this.t = t;
}
public static void main(String args[]) {
NonGenericClass type = new NonGenericClass();
type.set("Harley"); // valid
//String str = type.get(); // compile-error "Type mismatch: cannot convert from Object to String"
String str = (String)type.get(); // dangerous type-casting which is error-prone!
System.out.println(str);
}
}
--------------
package com.codinko;
public class GenericClassT<T> {
private T t;
public T get() {
return this.t;
}
public void set(T t) {
this.t = t;
}
public static void main(String args[]) {
GenericClassT<String> type = new GenericClassT<String>();
type.set("Harley");
String str = type.get(); //valid
System.out.println(str);
}
}
package com.codinko;
public class NonGenericClass {
private Object t;
public Object get() {
return this.t;
}
public void set(Object t) {
this.t = t;
}
public static void main(String args[]) {
NonGenericClass type = new NonGenericClass();
type.set("Harley"); // valid
//String str = type.get(); // compile-error "Type mismatch: cannot convert from Object to String"
String str = (String)type.get(); // dangerous type-casting which is error-prone!
System.out.println(str);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment