Eric Hsieh
Eric Hsieh

Reputation: 31

JAVA: change element type

I am brand new in JAVA, and I didn't understand the line talking about "Item temp = (Item)obj;". What does the "(Item)" mean? Does it force to change type of obj? Please help me to figure it out, thanks!

public class Item implements Comparable {
    private String id;
    private String name;
    private double retail;
    private int quantity;
    private double price;

    Item(String idIn, String nameIn, String retailIn, String quanIn) {
        id = idIn;
        name = nameIn;
        retail = Double.parseDouble(retailIn);
        quantity = Integer.parseInt(quanIn);

        if (quantity > 400)
            price = retail * .5D;
        else if (quantity > 200)
            price = retail * .6D;
        else
            price = retail * .7D;
        price = Math.floor( price * 100 + .5 ) / 100;
    }

    public int compareTo(Object obj) {
        Item temp = (Item)obj;
        if (this.price < temp.price)
            return 1;
        else if (this.price > temp.price)
            return -1;
        return 0;
    }
    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public double getRetail() {
        return retail;
    }
    public int getQuantity() {
        return quantity;
    }
    public double getPrice() {
        return price;
    }
}

Upvotes: 2

Views: 1155

Answers (3)

Zia
Zia

Reputation: 1011

This is known as the casting(Genrally its categorised in two type Down & Up Casting). Casting allows the use of generic programming in Java, where code is written to work with all objects of classes descended from some base class (often java.lang.Object, for utility classes). However, the use of casting causes a unique set of problems.If you try to cast two object which doesn't share same type hierarchy, i.e. there is no parent child relationship between them, you will get compile time error.On the other hand if you type cast objects from same type hierarchy butthe object which you are casting are not of the same type on which you are casting then it will throw ClassCastException in Java.

And @Elliot have already explained it by example

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201439

Item temp = (Item)obj; is performing type conversion (or typecasting) Object obj to the type Item. If obj is not an Item, then it will throw a ClassCastException, the Javadoc of which says (in part)

Thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance. For example, the following code generates a ClassCastException:

Object x = new Integer(0);
System.out.println((String)x);

However, because Comparable is generic, it's also possible to make Item like

public class Item implements Comparable<Item>

and then compareTo(Item) like

@Override
public int compareTo(Item temp) {
    return Double.compare(this.price, temp.price);
    // if (this.price < temp.price)
    //     return 1;
    // else if (this.price > temp.price)
    //     return -1;
    // return 0;
}

Upvotes: 2

Mohit Gupta
Mohit Gupta

Reputation: 5

This line: Item temp = (Item)obj; Taking another object of this class for comparing. This is for sorting of group/list of item class objects. So When we will sort list of Item, It will take this(current) object with the passing object of this class.

Upvotes: 0

Related Questions