This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Conversion from byte[] -> int[]

 
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are conversions from byte[] to int[] "not" permitted (even with a cast)?
I didn't find any 'rule' in the JLS specifically in this regard. There's a statement:


There is no permitted conversion from array type SC[] to array type TC[] if there is no permitted conversion other than a string conversion from SC to TC.


in the section on "Forbidden Conversions" in the JLS:
http://java.sun.com/docs/books/jls/second_edition/html/conversions.doc.html#175034
but it doesn't clearly explain it because conversions between (primitive) byte and (primitive) int are allowed.
Any ideas?
Thanks
Harwinder
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, I found the answer in the next section in the JLS. But still, it should be explicitly mentioned in the "Forbidden Conversions" section.
Here I'm answering my own question :


Assignment of a value of compile-time reference type S (source) to a variable of compile-time reference type T (target) is checked as follows:
...
...
...
- If S is an array type SC[], that is, an array of components of type SC:
...
...
- If T is an array type TC[], that is, an array of components of
type TC, then a compile-time error occurs unless one of the following
is true:

- TC and SC are the same primitive type.

- TC and SC are both reference types and type SC is assignable to TC,
as determined by a recursive application of these compile-time
rules for assignability.


Cheers
Harwinder
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be convertable, classes must belong to the same branch on the hierarchy tree and pass IS-A test. e.g: Horse IS-A Animal, Oak IS-A Tree. Then, assignments like Animal a=new Horse(); are perfectly fine at both compile- and runtime. You can also do

and even

but the second example will throw a ClassCastException at runtime since a object is really not Horse.
byte[] and int[] object types don't pass IS-A test. This is a special case for arrays containing primitives. If you do this:

you will receive false.
[ November 08, 2003: Message edited by: Vad Fogel ]
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vad
I think you didn't read the question properly. All I'm saying is that the "Forbidden Conversions" section fails to mention this point clearly.
(I already have a reasonably good understanding of everything you explained above).
Thanks
Harwinder
 
Harwinder Bhatia
Ranch Hand
Posts: 150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vad
And just taking forward what you mentioned
int a[];
byte b[] = new byte [3];
a = b ; // Compile time error
a = (int []) b; // Compile time error
It doesn't even compile so I'm not even talking about run-time errors.
Thanks
Harwinder
 
Vad Fogel
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problemo. I'm glad I was able to reiterate that concept to myself once again.
 
Slime does not pay. Always keep your tiny ad dry.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!