This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes Interface and array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface and array" Watch "Interface and array" New topic
Author

Interface and array

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Hi, I have few questions on interface and array.

1. What is the difference between these two methods: int compareTo(Object other) and boolean equals(Object other)? I know the former is in the Comparable interface, not sure about the latter one?

2. The following is a code that my teacher did in class as an example but I have some trouble understanding it. My questions are in the code.


Thanks so much for the help!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

The int compareTo(Object other) method is indeed from the Comparable interface. The equals() method comes from class Object, the superclass of all classes in Java. Note that because equals() is in the Object class, all objects in Java always have an equals() method (if you don't declare one yourself in your class, it will inherit it from class Object). But not all objects have a compareTo() method; they will only have that if the class (or a superclass) implements the Comparable interface.

Type casting is a way to tell the compiler "look, I have an object of type X here, but I want you to treat it as type Y. Don't check it, just trust me!". What it essentially does is deferring type checking from compile time to runtime - when, while you run the program, it turns out that the object that you cast cannot be treated as type Y, you will get a ClassCastException.

Line 4: Yes, you're declaring a variable named other, of type CD, and then you cast otherObject to the type CD so that you can assign it to other.

Line 22: You're creating a new array of which the length is the length of collection + 100.

Line 25: I don't know what you mean by "moving the array indexes". What that line does is copy the content of the array collection to the array temp.

Line 27: You simply make the variable collection refer to the array that temp refers to (so, you're replacing the old array that collection referred to with the new, larger array).

Remember that in Java, variables of non-primitive types are references. So temp and collection are not the arrays themselves, they are variables that refer to arrays. Assigning a value to such a variable makes it refer to another array.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38441
    
  23
The code you have been given is, I am afraid, out of date. You should be using genericsThe <CD> bit tells the compiler you only expect CDs there, so you can dispense with the cast.
The @Override annotation will cause the compiler to throw an error if you make any spelling, but it is not necessary in this instance. More like "icing on the cake". A metaphor only understood in UK.
Please don't use those long comments in code tags. If you have to scroll left and right the code is very difficult to read.
Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Jesper de Jong wrote:The int compareTo(Object other) method is indeed from the Comparable interface. The equals() method comes from class Object, the superclass of all classes in Java. Note that because equals() is in the Object class, all objects in Java always have an equals() method (if you don't declare one yourself in your class, it will inherit it from class Object). But not all objects have a compareTo() method; they will only have that if the class (or a superclass) implements the Comparable interface.

Type casting is a way to tell the compiler "look, I have an object of type X here, but I want you to treat it as type Y. Don't check it, just trust me!". What it essentially does is deferring type checking from compile time to runtime - when, while you run the program, it turns out that the object that you cast cannot be treated as type Y, you will get a ClassCastException.



Hi Jesper, thanks for your clarification! It makes a lot of sense now. One thing though, if casting simply defers type checking, so when I run the program, it won't work, then what's the point of casting?
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Sometimes you just need to cast.

In the time before Java version 5, there were no generics. You could not make, for example, a List<String>, there was only a plain List which would hold Objects. If you would get an element out of the list, you were forced to cast it to a String, because List's get() method would only give you an Object. For example:

With generics, casts are not necessary anymore as often as before, but sometimes you can't avoid doing a cast. It's better though to try to avoid casts, because it's better that the compiler checks the types for you than let this happen at runtime, with the risk that the program might crash with a ClassCastException.
 
 
subject: Interface and array