3

I just read 《Effective Java》and I saw a sentence which said that

As a consequence, arrays provide runtime type safety but not compile-time type safety and vice versa for generics

I don't quite clear about it and I'm confused even after I've read all the examples given.Can anyone explain this to me,thanks a million.

3 Answers 3

4

You can't change the type of an array (reference) at runtime. But you can compile code which tries to just fine.

String[] strings = new String[1];
Object[] objects = strings;
objects[0] = new Integer(1); // RUN-TIME FAILURE

When you compile your application, no error will be thrown by the compiler.

On the other hand, if you use generics, this WILL give you an error when you compile (build) your application.

ArrayList<String> a = new ArrayList<String>();
a.add(5); //Adding an integer to a String ArrayList - compile-time failure

In other words, you don't need to actually run your application and execute that section of code to find the problem.

Note, compile time failures are preferable to run time failures, since you find out about the problem before you release it to users (after which it's too late)!

Sign up to request clarification or add additional context in comments.

Comments

0

With generic collections, this code, which tries to put an Integer into a String list, gives a compile time error on the second line: Cannot cast from List<String> to List<Object>:

List<String> listOfStrings = new ArrayList<>();
List<Object> listAgain = (List<Object>)listOfStrings;
listAgain.add(123);

The equivalent code with arrays compiles perfectly because it is legal to use a String array as an Object array. (Technically speaking, arrays are covariant.)

String[] arrayOfStrings = new String[10];
Object[] arrayAgain = arrayOfStrings;
arrayAgain[0] = 123;

However, it wouldn't be a valid String array if it actually contained integers, so every operation to store something in it is checked at run time. At run time does it blow up with an ArrayStoreException.

1 Comment

Thank you very much,also a excellent example which helped me a lot.
0

Java Array provides covariant return type while generic doesn't provide covariant return type.

Lets understand with simple example

public class GenericArrayDiff {
    public static void main(String[] args) {
        List<Vehicle> vehicleList = null;
        List<Car> carList =  null;
        Vehicle[] vehicleArrays;
        Car[] carArrays;

        // illegal
        carList = vehicleList;
        // illegal
        vehicleList = carList;
        // illegal
        carArrays = vehicleArrays;
        // legal because array provides covariant return type
        vehicleArrays = carArrays;
    }

}
class Vehicle {

}
class Car extends Vehicle {

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.