wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Array types compatibility Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Array types compatibility " Watch "Array types compatibility " New topic
Author

Array types compatibility

Saurabh Agrahari
Greenhorn

Joined: Aug 22, 2012
Posts: 4
Hi All

Object[] ob ;
int[] arr1 = {1,2,3,4};
ob=arr1;

I get the following exception :

incompatible types
found : int[]
required: java.lang.Object[]
ob = arr1;
^
1 error

I understand that ob is the array of Object refrences and arr1 is the array of primitives so they are incompatible .Is this the only reason ?.Is there a way to check the types for compatibility ?.Please advice.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Welcome to the Ranch

You declared ob as an array of Objects. An int[] is not an array of Objects, but it is an Object in its own right. Experience will allow you to work out the types of references for yourself.
R. Jain
Ranch Hand

Joined: Aug 11, 2012
Posts: 375
    
    1

Saurabh Agrahari wrote:Hi All

Object[] ob ;
int[] arr1 = {1,2,3,4};
ob=arr1;

I get the following exception :

incompatible types
found : int[]
required: java.lang.Object[]
ob = arr1;
^
1 error

I understand that ob is the array of Object refrences and arr1 is the array of primitives so they are incompatible .Is this the only reason ?.Is there a way to check the types for compatibility ?.Please advice.



Well, you must be knowing that an Array in Java is itself an Object..
And, you don't assign an Object to an array of Object..
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8016
    
  22

Saurabh Agrahari wrote:I understand that ob is the array of Object refrences and arr1 is the array of primitives so they are incompatible .Is this the only reason ?

Yes.

Is there a way to check the types for compatibility ?.Please advice.

Many, but without more clarification it's difficult to know what to advise.

An Object[] can accept an array of any reference (ie, object) type, but not of a primitive type.

My suggestion: back up and explain what you want to do, not how you want to do it.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Saurabh Agrahari
Greenhorn

Joined: Aug 22, 2012
Posts: 4
Thanks everyone for your suggestions.Actually what i was trying to do is as follows:



The code works fine and prints the output as :1

But when i changed the code to :


I get a hexadecimal number like :[I@3e25a5

I dont understand why (although i understand that int[] is not compatable with Oject[])
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4422
    
    8

Have a closer look at the definition of the asList method:
Now, there are some important things to note here:
- It's a varargs method - done like that to allow you to quickly create Lists from a number of stated arguments. E.g. list = Arrays.asList(1, 2, 3, 4, 5).
- A varargs method like T... gets compiled to a method that takes a T[] argument
- T has to be a reference type - so int doesn't count.
- But arrays are reference types, so int[] is a possible T

Which means that when you try and pass in an int array into asList, it doesn't recognise it as a T array (because int isn't a valid value for T). So instead, it interprets the array as the first argument in a set of var-arg arguments. In other words, it interprets T as int[]. This means that it returns an array containing one object, where that object is the int array!

[I@3e25a5 is what you get when you call toString on an int[].

Does that make sense? You probably need to read up about variable arguments and generic types (that's what the Ts above are referring to) if you haven't come across them yet.

Incidentally, this would probably all be clearer if you were using generic types on all your collections , as the compiler would probably prevent the usage you didn't want. Then these examples would look something like this:


Saurabh Agrahari
Greenhorn

Joined: Aug 22, 2012
Posts: 4
Awesome !! Thanks Matthew for the precise explanation .. From your suggestion its clear, that to have a crystal clear understanding of methods in collections framework one must have clear understanding of Java types and Generics .I hope i will be able to crack the hard nut of Generics soon.Thanks once again..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array types compatibility