I've come accross some new syntax in one my books (new for me that is) that I can't seem to find any explanation for. I understand the code and what it is acheiving, I just can't find any reference to why its writen in this way.
The following are three seperate peices of code from seperate classes however they all have 'Item' in parenthases BEFORE their respective classes, objects or methods. (ie. Item show = (Item)store.getItem(i);, return (Item)catalog.get(i);, Item temp = (Item)obj; ). Item is a class that defines a single item in a shop with a handfull of arguments (eg. price, qty etc.).
for (int i = 0; i < store.getSize(); i++) { Item show = (Item)store.getItem(i); System.out.println("\nItem ID: " + show.getId() + "\nName: " + show.getName() + "\nRetail Price: $" + show.getRetail() + "\nPrice: " + show.getPrice() + "\nQuantity: " + show.getQuantity()); }
public Item getItem(int i) { return (Item)catalog.get(i); }
public int compareTo(Object obj) { Item temp = (Item)obj; if (this.price < temp.price) { return 1; } else if (this.price > temp.price) { return -1; } else { return 0; } }
I have searched the API to try and find why this is but can't find it (yet...). If anybody can shed some light on this or point me in the right direction I would be very grateful.