wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array upcasting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array upcasting" Watch "Array upcasting" New topic

Array upcasting

uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
int[] arr = new int[5];

long[] lrr = new long [5];

lrr = arr;

compilation error please explain. Any help is appreciated.

Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
here u defined long type array reference. It can be reassigned to any long array but cannot be reassigned to anything that is not long array.

byte arrary reference can be reassigned to another byte array
char ..............................................char array, same for all primitive types.
uzma Akbar
Ranch Hand

Joined: Sep 21, 2005
Posts: 40
Thanks Naresh

My idea was as if they are both references so smaller type may be assigned to bigger type as both are Arrays(Object) and array reference somewhat behave as an object reference. but I think that is not the case. I was just comparing it with object references as the smaller type may be assigned to bigger types.

Anyways, thanks a bunch!

vipul patel
Ranch Hand

Joined: Oct 16, 2005
Posts: 146
automatic widening or narrowing does not apply to array types as if it works for primitive types. the reason is that array types are objects.
I agree. Here's the link: http://aspose.com/file-tools
subject: Array upcasting
It's not a secret anymore!