Search code examples
javacomparable

Is Comparable[] a good way to store all sorts of variables?


    Comparable w[] = new Comparable[5];
    w[0] = 1;
    w[1] = "string";
    w[2] = 2.0;

Is it a good way? And actually why does it work (I tried it just randomly)? Maybe because Comparable extends Integers, Strings, Doubles etc.? So what?


Solution

  • Is it a good way?

    If you can't think of a good reason to do some thing, its probably not a good idea. ;)

    Maybe because Comparable extends Integers, Strings, Doubles etc.?

    Numbers and Strings implement Comparable, but you have the right idea.

    So what?

    There are many odd things you can do in Java, but most of them don't have a use. ;)

    In this case, its not particularly useful because the objects are not Comparable with each other. e.g. Arrays.sort(w); won't work.