• Post Reply Bookmark Topic Watch Topic
  • New Topic

Array Issue  RSS feed

 
edirg559
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers:

Object obj = new float[20];

I could not understand the statement above. I assumed that obj is a float array that holds 20 elements (maybe I was wrong, please correct me).

But when I tried to System.out.println (obj[0]) it out, I got an error reading: array required, but java.lang.Object found.

How can the JVM found Object instead of array of float? I thought that Arrays are object, and every objects in Java is derived from Object. Arrays of float would be part of object as well?

In the case of this:
**Dog class extends Animal class**
Animal dog = new Dog ();

Even though "dog" has Animal data type. But still the "dog" would be an instance of Dog Class (am I right?). From this analogy, I always assume that "obj" in this case: Object obj = new float [], will always be instance of float array[]?

Thanks,
Erik
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are declaring the variable "obj" to be of type Object, and then assigning a float array to it. As you know, this is perfectly fine, because an array is an Object (even if it contains primitive values).

However, when you call obj[0], there is a problem. As the error message says, "array required" in order to use the index syntax of [0], "but java.lang.Object found" because obj's type is Object.

Therefore, before using an array's index syntax, you need to downcast obj from type Object back to type float[]...
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes an array is an Object but an Object is not necessarily an array. It doesn't matter whether or not the Object obj is actually an array of floats, if you declare it an Object then you cannot use it as if it were an array without casting it back to an array.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Declare the obj to be an Object array like this
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Unnar Bj�rnsson:
Declare the obj to be an Object array like this

Don't you mean this?

Object[] obj = new Float[20]; //java.lang.Float
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
Object[] obj = new float[20];

Don't you mean this?

Object[] obj = new Float[20]; //java.lang.Float

Yeah, that's the difference. An array of primitives is itself an Object, but it is not an array that contains Objects.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeff Albertson:
<hr></blockquote>
Don't you mean this?

Object[] obj = new Float[20]; //java.lang.Float[/QB]


Oh yeah, sorry about that.
But personally, if I wanted an array of float numbers I would just do it like this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!