instanceof
and typecasting
Examples of instanceof
and typecasting for object types.
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
public class Typecasting {
/**
* The program demonstrates instanceof and typecasting
*/
public static void main(String[] args) {
List<Integer> list1 = new ArrayList<Integer>();
List<Integer> list2 = new Stack<Integer>();
if (list1 instanceof List) { //Note: list1 instanceof List<Integer>
// doesn't compile: cannot use generics
// in instanceof
System.out.println("list1 is an instance of List");
} else {
System.out.println("list1 is not an instance of List");
}
if (list2 instanceof List) {
System.out.println("list2 is an instance of List");
} else {
System.out.println("list2 is not an instance of List");
}
if (list1 instanceof ArrayList) {
System.out.println("list1 is an instance of ArrayList");
} else {
System.out.println("list1 is not an instance of ArrayList");
}
if (list2 instanceof ArrayList) {
System.out.println("list2 is an instance of ArrayList");
} else {
System.out.println("list2 is not an instance of ArrayList");
}
// typecasting:
// list2.push(2); doesn't compile since list2 is of type List and
// doesn't have push() method. Need to typecast in order to call push():
Stack<Integer> theStack = (Stack<Integer>) list2; // note that <Integer> in the cast
// is required but the type doesn't actually get checked
theStack.push(2);
// or you can avoid using a variable:
((Stack<Integer>) list2).push(5);
// checking that the elements are actually in list2:
System.out.println(list2);
Stack<Integer> anotherStack; // declaring a stack outside of try/catch
// to avoid variable scope issues
// if the object is of the wrong type, typecasting throws an exception:
try {
anotherStack = (Stack<Integer>) list1;
} catch (ClassCastException e) {
System.out.println(e);
}
// if you are not sure whether a given object is of the right type,
// use instanceof before typecasting:
if (list1 instanceof Stack) {
anotherStack = (Stack<Integer>) list1;
} else {
System.out.println("Cannot cast list1 to Stack");
}
}
}
Add the equals method to the Card class that tests for value and suit equality.
public class TestCardEquals {
public static void main(String[] args) {
Card aceSpades = new Card("Spades", "A");
Card aceDiamonds = new Card("Diamonds", "A");
Card anotherAceSpades = new Card("Spades", "A"); // someone's cheating
// expect false
System.out.println(aceSpades.equals(aceDiamonds));
// expect true
System.out.println(aceSpades.equals(anotherAceSpades));
// expect false
System.out.println(aceSpades.equals("Ace of Spades"));
}
}
public class Card {
final private String suit;
final private String value;
private int numericValue = 0;
final private String[] arr = {"2","3","4","5","6","7","8","9","10","J","Q","K","A"};
public Card(String suit, String value){
this.suit = suit;
this.value = value;
for (int i = 0; i < 13; i++) {
if (arr[i].equals(value)) {
numericValue = i + 2;
}
}
// this.numericValue = arr.indexOf(value) + 2;
}
public String getSuit(){
return suit;
}
public String getValue(){
return value;
}
public int getNumericValue(){
return numericValue;
}
public String toString(){
return value + " of " + suit;
}
public int compareTo(Card that){
return this.numericValue - that.numericValue;
}
public boolean equals(Object other) {
if (!(other instanceof Card)) {
return false;
}
Card otherCard = (Card) other;
return (this.value.equals(otherCard.value) &&
this.suit.equals(otherCard.suit));
}
}