Skip to content

Instantly share code, notes, and snippets.

@oxbowlakes
Last active April 6, 2016 11:57
Show Gist options
  • Save oxbowlakes/26a8d2aae6304dca8f2af4ae2664891e to your computer and use it in GitHub Desktop.
Save oxbowlakes/26a8d2aae6304dca8f2af4ae2664891e to your computer and use it in GitHub Desktop.
import java.util.*;
class Unchecked {
static class Foo<A> {
Optional<A> doIt(Object o) { return Optional.of((A) o);}
}
public static void main(String[] args) {
System.out.println(new Foo<String>().doIt(3));
}
}
/* compile it */
//javac Unchecked.java
//Note: Unchecked.java uses unchecked or unsafe operations.
//Note: Recompile with -Xlint:unchecked for details.
//javac -Xlint:unchecked Unchecked.java
//Unchecked.java:4: warning: [unchecked] unchecked cast
// Optional<A> doIt(Object o) { return Optional.of((A) o);}
// ^
// required: A
// found: Object
// where A is a type-variable:
// A extends Object declared in class Foo
//1 warning
/* Run it */
//java Unchecked
//Optional[3]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment