• Post Reply Bookmark Topic Watch Topic
  • New Topic

Integer[] and Number[]  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone

the above code compiles.
what I know is Integer is Number
but Integer[] isn't Number[]

then how this initialization compiles?

I think as each element of Integer[] is Integer type which is
a Number so Integer[] is Number[] whose each element is Number.
Am I correct??


with regards
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sawan Mishra wrote:Hey everyone

the above code compiles.
what I know is Integer is Number
but Integer[] isn't Number[]

then how this initialization compiles?

I think as each element of Integer[] is Integer type which is
a Number so Integer[] is Number[] whose each element is Number.
Am I correct??


with regards


Yes an Integer[] is-a Number[] and this is why it works.
I remember reading a response that had a very nice tree representation of this inheritance scheme.
I'll post it if I find it.

Chan.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the thread I was talking about. It's the fifth post in the thread.

HIH.
Chan.
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's a fairly controversial feature that allows Integer[] to be a subtype of Number[] - meaning that arrays in Java are covariant - because it can break compile time type safety. The main reason it was allowed, I believe, is that in the absence of generics (early Java didn't have generics) you needed this to be able to write general purpose array methods, like Arrays.sort. You need to be able to pass a String[] to a method with a signature like sort(Object[]), for example.
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matthew Brown wrote:It's a fairly controversial feature that allows Integer[] to be a subtype of Number[] - meaning that arrays in Java are covariant - because it can break compile time type safety. The main reason it was allowed, I believe, is that in the absence of generics (early Java didn't have generics) you needed this to be able to write general purpose array methods, like Arrays.sort. You need to be able to pass a String[] to a method with a signature like sort(Object[]), for example.


Also although making arrays covariant in Java breaks compile time type safety, the Java runtime system has the ArrayStoreException that can actually deal with it. So it's probably not very bad?



gives me

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Integer
at tobedeleted.TestClass.changeFirstElement(TestClass.java:21)
at tobedeleted.TestClass.main(TestClass.java:27)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chan Ag wrote:
Matthew Brown wrote:It's a fairly controversial feature that allows Integer[] to be a subtype of Number[] - meaning that arrays in Java are covariant - because it can break compile time type safety. The main reason it was allowed, I believe, is that in the absence of generics (early Java didn't have generics) you needed this to be able to write general purpose array methods, like Arrays.sort. You need to be able to pass a String[] to a method with a signature like sort(Object[]), for example.


Also although making arrays covariant in Java breaks compile time type safety, the Java runtime system has the ArrayStoreException that can actually deal with it. So it's probably not very bad?



One of the arguments here is ... is an Integer[] IS-A Number[]? Well, the instanceof operator reports true. However, an Integer[] doesn't behave like a Number[], as you can put a Float element in an Number[]. So, is an Integer[] IS-A Number[]?

IMO, you can argue yes or no. Regardless, you have to be careful, in how you confirm the array type with the instanceof operator, in order to use the array correctly.

Henry
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!