• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interface and array

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 69
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic