• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JLS understanding...

 
Santhosh Kumar
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, JLS says
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC.
is widening conversion. I tried,
int a[] = new int[10];
long l[];
l = a;
it throwning compile error. Can some body tell me the meaning of the above sentence (if possible with an ex)?
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSL states:
From any array type SC[] to any array type TC[], provided that SC and TC are reference types and there is a widening conversion from SC to TC.
int []i where i is a reference to an object that object is an array of primitive int.
However the stated JLS widening conversion law applies without any error to the arrays of Objects (Array of Object TYPE);
Integer []i = {new Integer(1), ........}
Number[] n = null;
n = i;
Compiles fine even when performed a reverse (a narrow conversion) with an explicit cast.
Now the question follows why the same is not true for primitive arrays?
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why the same is not true for primitive arrays?
Well from one prespective its rather simple.
Consider that int[]i = new int[10] will allocate space onto heap with 32 bit fixed volumes for 10 elements. Now if we were to assign i to l i.e. long[] l =i that cannot be simply right. Why because long[] l is suppose to point to a heap with 64-bit fixed size elements, so even in theory if i is assinged to l l will supposedly contain 64-bit volumes but in reality it will contain 32-bit (Will attempt to read two int elements as one long). That pretty much sounds like a array mess-up that Gosling & Co. were supposedly so tired of that they happened to create Oak (Java).
Any additional/contraditional comments..........
 
Aniruddha Mukhopadhyay
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Amond,
The rule may be arbitrary, but array of primitives CANNOT be converted. It has nothing to do with space need for storage, as following example shows. Array of 5 Objects can refer to array of 500 Strings and return correct value:
class Test {
public static void main(String[] args){
Object o[] = new Object[5];
String s[] = new String[500];
s[4] ="4";
s[400] = "400";
o= s;
System.out.println(o[4]);
System.out.println(o[400]);
}
}
Aniruddha
 
Amond Adams
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You just proved my point buddy.
Object arrays do not need fixed size volume(s) allocations. They are allocated on a heap and are accessable/convertable because a general map of object(s) exists.
But with primitive array allocation the a volume has to contain a definite size of a element 8/16/32/62/32/62 according to and in lieu of their repected types so conversion is not viable in an array case. It can't implicitly, an extensive recursive code has to exists in order to make a element to element conversion. Java being a every thing at Runtime thing can't afford the efficiency loss if it tried to implement such.

Any other comments..........
 
Randall Twede
Ranch Hand
Posts: 4482
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dont dis the compiler.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic