Probably would have been useful if you were mentioned that it gives you a run-time exception. Not an error.
At run-time, Java doesn't let you store a StringBuilder instance to an array of Strings.
What confuses you, is line 6. At line 6 you just referring to an array of Strings with a reference variable, which is of type Object, and that is legal only because every class in Java is derived from Object class. Important is to understand, that underlining array to which object type reference variable is referring to is able to store only Strings and nothing else.
The mentioned problem is a problem of covariance and contravariance.
Here the application will compile just fine but will generate issues at runtime time, the hidden thing is that String is considered a subtype of Object because the subtyping rules of Java also state that an array S is a subtype of the array T if S is a subtype of T.
As per above definition, given below is possible both at compile time and run time.
Will give compile time exception, because we’re trying to put a StringBuffer into a String array. The fact that we are accessing the array through an Object reference is irrelevant here, what matters is that the array is an array of String.
This means that we can pass the compiler, but we cannot pass the run-time type system. And this is so because arrays are what we call a reifiable type. This means that at run-time Java knows that this array was actually instantiated as an array of String which simply happens to be accessed through a reference of type Object.
Parag J Patankar wrote:
objects = new StringBuilder()
Does it mean StringBuilder is not String and it can not be consider as Object ?
First, an instance of StringBuilder is definitely not an instance of String, so the first part of the statement is correct. The conclusion is incorrect, an instance of StringBuilder IS-A instance of Object.
To answer your question. The Object array reference is pointing to a String array instance. This is okay, as an String array IS-A Object array. However, a String array instance isn't allowed to hold StringBuilder objects, so hence, the exception.
Liutauras Vilda wrote:. . . it gives you a run-time exception. Not an error. . . .
Liutauras has a good point there; I know it seems strange to be fussy about jargon, but the jargon has a good purpose. It enables us to understand what you mean very quickly, but only if you use it correctly.
As for the explanation of the exception, I think it has already been explained. But if you need to read more, the details will be in the Java® Language Specification, but that can be difficult to read.
Parag J Patankar wrote:
I have not understood clearly. We defined array of Objects and storing string arrays
Object objects = strings;
No, you are not storing string arrays in the array named "objects".
You are creating an array named "objects", and setting it to be a reference to the array named "strings". You do not have two arrays here, you just have one array with two references to that array.
If you then set you will find that objects is now the string "a new string".