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

arrays

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi,
have a look at the foll. code.
int i=2;
long l=i;// compiles and runs since i is converted to long type
But, if int[] i1 = new int[2];
long[] l1=i1;//compiler error as int[] cant be
converted to float[].
FOr arrays,i1 must be compatible and can be converetd to l1 ,then why is it giving error?
Thanks!
[This message has been edited by avn (edited August 04, 2000).]
Ajith Kallambella
Sheriff

Joined: Mar 17, 2000
Posts: 5782
Widening and narrowing conversions are not applicable to primitive arrays. Though int gets widened to long/float in assignment conversions, an int array cannot be assigned to long/float arrays.
The only thing you can do, though trivial, is assign an int array reference to another int array reference!. This is called identity conversion. JLS section 5.1.7 Forbidden Conversions is worth a look since these concepts can be questioned in SCJP.
Ajith


Open Group Certified Distinguished IT Architect. Open Group Certified Master IT Architect. Sun Certified Architect (SCEA).
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Thanks! Ajith. I saw forbidden conversions of JLS.But couldnt
understand few points.Can u provide me examples for the
follwing points since i tried , but where not working as said:
1)Except for the string conversions, there is no permitted conversion from any primitive type to any reference type.
2)There is no permitted conversion from the type boolean other than the identity conversion and string conversion.
3)There is no permitted conversion from interface type J to interface type K if J and K declare methods with the same signature but different return types.
Does this mean it is permitted if they have same return types???
4)There is no permitted conversion from any array type to any class type other than Object or String.
Working for Object, but not for String.
Thanks!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: arrays