This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array upcasting

 
uzma Akbar
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] arr = new int[5];

long[] lrr = new long [5];

lrr = arr;


compilation error please explain. Any help is appreciated.

Uzma
 
Naresh Gunda
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

uzma
 
vipul patel
Ranch Hand
Posts: 146
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic