aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes JLS understanding... 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 "JLS understanding..." Watch "JLS understanding..." New topic
Author

JLS understanding...

Santhosh Kumar
Ranch Hand

Joined: Nov 07, 2000
Posts: 242
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

Joined: Nov 28, 2000
Posts: 62
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

Joined: Nov 28, 2000
Posts: 62
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

Joined: Nov 15, 2000
Posts: 59
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


Aniruddha
Amond Adams
Ranch Hand

Joined: Nov 28, 2000
Posts: 62
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

Joined: Oct 21, 2000
Posts: 4347
    
    2

dont dis the compiler.


SCJP
Visit my download page
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: JLS understanding...