4

First of all,since java has a strict type system ,programs are checked for type correctness at the compilation time and bytecode of programs is checked when the classes are loaded to bytecode verifier before the execution.

Though the introduction of generics has broaden the potencials of Java type system, but due to java has subtypes many problems occurs such in the above example:

 String [] a ={"Hello"};
        Object [] b= a;
        b[0]=  false;
        String s=a[0];

        System.out.println(s);

In the above example we use that String class is a subclass of Object . My question is what is the reason that the compiler does not produces any warning about the above program .When I try to run it (of course) throws an exception. Also what problems/consequences the above program could have in java implementation (I mean are there any obvious problems?? ).

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
coder
  • 12,832
  • 5
  • 39
  • 53
  • You can start by reading Generics tutorial by Bracha. – Victor Sorokin Sep 07 '16 at 20:13
  • 1
    Regarding question itself, it's a dup: http://stackoverflow.com/questions/18666710/why-are-arrays-covariant-but-generics-are-invariant – Victor Sorokin Sep 07 '16 at 20:14
  • if you want some kind of high-level arch discussion, programmers.se seems to be more suited for such kind of questions. – Victor Sorokin Sep 07 '16 at 20:20
  • If you want to know some _very_ concrete details of JVM impl regarding java type system, you should ask _very_ specific question with `jvm` tag. – Victor Sorokin Sep 07 '16 at 20:21
  • 1
    This question is not realted to generics since you are talking about arrays which are natural part of the java language. Generics are a feature that was add in java 1.5 and allowed you to create data structures such as ArrayList and Trees with a specific types: ArrayList, Tree – omer727 Sep 07 '16 at 20:23
  • @Victor Sorokin ,I don't require some kind of high-level arch discussion or the concrete details of JVM , I would like a simple and reasonable answer about why is happening and some obvious problems in java... – coder Sep 07 '16 at 20:24
  • @omer727 ,you are right at some point but from what i read generics broaden the subtypes properties...so i think it is related – coder Sep 07 '16 at 20:27
  • You should read up on covariance and contravariance, e.g. on wikipedia https://en.wikipedia.org/wiki/Covariance_and_contravariance_(computer_science) – k5_ Sep 07 '16 at 20:38

1 Answers1

2

Arrays in Java are covariant ie. a String[] can be passed to something expecting an Object[]. And this is why the compiler does not stop you sticking your boolean into the String array.

In order to provide covariance but also offer runtime protection, the Java engineers designed arrays such that a tag gets added at compile time which stamps the array with its type. Even though we can add the boolean at compile time, the type on the String[] stops us from adding the boolean at runtime - this is what triggers the ArrayStoreException you probably encountered when you ran your code.

This is a trade off in Java between having covariant arrays and catching the step of adding the wrong thing to an array. This very trade off is why some languages, like Scala, chose not have covariant arrays.

It is possible to create generic arrays but not straightforward and has some type dangers so the approaches are for specific use cases - Josh Bloch's Effective Java discusses these.

It is arguably better to use generic Lists over arrays since you can then catch issues at compile time.

jacks
  • 4,614
  • 24
  • 34
  • @ Nio ,thanks this help a lot it is clear now why it is compiling .Because I don't have that much of experience in java ,does this leads to further problems in java implementation?? (does it have a further impact -Not asking for a broad answer ). – coder Sep 07 '16 at 20:53
  • 1
    @coder, I'm not 100% sure I get your question, but if I do understand you correctly then I would say there are no further impacts you need to worry about. I would say that if you are not that experienced with Java then avoid arrays and use generic Lists since the compiler will stop you adding the wrong thing to the List. If you really want to use Arrays then you just need to be very careful that you protect those arrays ie. don't return them from methods otherwise you run the risk of someone adding the wrong thing which might not manifest until runtime. – jacks Sep 07 '16 at 21:05
  • Nio,thanks your answer helped a lot!! – coder Sep 07 '16 at 21:07