• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why this code giving an error  RSS feed

 
Parag J Patankar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Try to understand why this code gives an error


Where as my understanding every type of objects is part of Object, then why StringBuilder is not part of object

Pl explain.

Kindly also help me to correct this code.

Regards
Parag
 
Liutauras Vilda
Marshal
Posts: 4117
227
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

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.
 
Syed Naved Ali
Greenhorn
Posts: 8
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for asking such a good question.
  objects[0] = new StringBuilder();  <--- error

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.


However,
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[].

To know more visit, covariance-and-contravariance
and Wiki
 
Parag J Patankar
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

Thanks to replying my basic queries.

You said 

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.

I have not understood clearly. We defined array of Objects and storing string arrays

Object[] objects = strings;

then it is giving runtime error for following

objects[0] = new StringBuilder()

Does it mean StringBuilder is not String and it can not be consider as Object ?

Pl explain

 
Henry Wong
author
Sheriff
Posts: 23188
124
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Parag J Patankar wrote:
objects[0] = 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.

Henry
 
fred rosenberger
lowercase baba
Bartender
Posts: 12478
45
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A StringBuilder is not a String - that much is true.  But a StringBuilder IS an Object.

But that's not the problem.

you have this:



so here, the actual thing that your objects variable points to is a String array. So when you say

objects[0] = new StringBuilder();

you're now trying to put a StringBuilder into the array that is really defined to hold Strings. That is what's not allowed.

at least, that's how I see it.
 
Campbell Ritchie
Sheriff
Posts: 54495
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fred Kleinschmidt
Bartender
Posts: 529
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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[0] is now the string "a new string".
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!