• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array types compatibility

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 375
1
Python Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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..
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Saurabh Agrahari
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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[])
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

reply
    Bookmark Topic Watch Topic
  • New Topic