• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question on array conversion

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Prints 112.How is it possible?Actually only arrays of references can be casted right?According to compile time rules of casting
When both old type & new type are arrays,both arrays must contain reference types,and it must be legal to cast from old type to new type
Then how the above code can cast array a[] which is array of primitive types to obj[] array?
Thanks
Veena
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since a is a 2d array, you are casting an array of a[] - each item in obj contains an array of int, not an int.
Bill
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got it Thanks .But Now i don't understand how 2 dimensional array is converted to 1 dimensional.Even 3 dimensional can be converted to 2 dimensional?Any dimensional array can be converted to any dimensional?I am sorry,I think this is a very basic question learnt in school.But I forgot :roll: .Can anybody please remind me?
Thanks
Veena
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This may help:
https://coderanch.com/t/241325/java-programmer-SCJP/certification/Self-Test-Arrays-Kathy-Book
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i don't understand how 2 dimensional array is converted to 1 dimensional


Because a two dimensional array is implemented as an array of arrays. In other words, the elements of a multidimensional array are new arrays. The last elements in the structure are not arrays but the primitives or objects really hold by the multidimensional array.
The array "a" has tree elements:
a[0] points to the array {1, 2}
a[1] points to the array {0, 1, 2}
a[2] points to the array {-1, 0, 2}
a[0][0] is 1 a primitive, the elements that the multidimensional array holds. (Or the ones we are normally interested on)
Look at Muldimensional Arrays from Thinking in Java for more.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by William Brogden:
Since a is a 2d array, you are casting an array of a[] - each item in obj contains an array of int, not an int.
Bill


To check the above statement I wrote the following code.

It compiles fine.The code above throws runtime exception though .
Exception in thread "main" java.lang.ClassCastException: [IBut according to compile time casting rule it should not compile.Coz array i[] is array of primitives ,not references.
Thanks
Veena
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
i.clone() returns an object of runtime type int[], whose compile type is Object. A compile type Object can be casted to Object[]. But as you said it fails at runtime.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


i don't understand how 2 dimensional array is converted to 1 dimensional


How can we fit squares in circles, right ?
What we have here is a mapping of elements of a 2 dimensional array to a 1 dimensional array.
As the dimentsion of the target array (LHS) is 1 less than the source array (RHS) the compiler maps each element of LHS to a 1 dimensional array.
If RHS has n dimensions and LHS has 1 dimension, than element of LHS has dimension n -1
Below is a small program that shows what happens for 3 dimensions.
As you can see each element of RHS is a 2 dimensional array.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veena,
It may be worth knowing what primitive array references can be converted to. A primitive array has no hierarchy, except that it inherits from Object and that it implements the Cloneable and Serializable interfaces. So, conversion is possible to these types as well as the same primitive types. It also explains why, as Jose says, Object[] fails at runtime.
So, given:
int[] i = new int[5];
int[] j = new int[2];
You can do this:
Object o = i;
Cloneable c = i;
java.io.Serializable s = i;
j = i;
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
Veena,
i.clone() returns an object of runtime type int[], whose compile type is Object. A compile type Object can be casted to Object[]. But as you said it fails at runtime.



Thank you Jose.But why is it throwing exception?

And also in an effort to understand multi dimensional array I tried the following code

The above code is giving compiler error
arrconv.java:6: not a statement
arr[][]={{1,2,3},{1,2,3}};
^
arrconv.java:6: ';' expected
arr[][]={{1,2,3},{1,2,3}};
Can't I declar,construct initialize 2 dimensional array by specifieng size?i don't wanna use for loop to initialize...

Thanks
Veena
[ April 28, 2003: Message edited by: Veena Point ]
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Roger Chung-Wee:

It may be worth knowing what primitive array references can be converted to. A primitive array has no hierarchy, except that it inherits from Object and that it implements the Cloneable and Serializable interfaces. So, conversion is possible to these types as well as the same primitive types. It also explains why, as Jose says, Object[] fails at runtime.
So, given:
int[] i = new int[5];
int[] j = new int[2];
You can do this:
Object o = i;
Cloneable c = i;
java.io.Serializable s = i;
j = i;


Roger,
I understand the above statements.But I am not knowing on what basis values of 2 dimensional array are assigned to 1 dimensional array in the example I have asked in this question .What is the logic behind it?
[ April 28, 2003: Message edited by: Veena Point ]
[ April 28, 2003: Message edited by: Veena Point ]
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You cannot initialize after the array is constructed. Either you do this:
int[][] arr = {{1,2,3}, {1,2,3}};
Or this:
int[][] arr = new int[2][3];
arr[0][0] = 1;
arr[0][1] = 2;
arr[0][2] = 3;
arr[1][0] = 1;
arr[1][1] = 2;
arr[1][2] = 3;
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Thank you Jose.But why is it throwing exception?


This is throwing a ClassCastException

Suppose

This yields a compile error because an int cannot be casted to Object. If it were allowed, calling "oa[0].equals()" would try to invoke a method on a primitive. But this is not possible, thus the compiler prevents us from this type of errors at runtime.
Now

Hey you thought you were going to fool the language? because this compiles, but still the JVM must check that the object casted really is the same type or a subtype as the target. It does that to avoid the kind of errors explained above.
This is why Java is called a safe type language. An object cannot receive a method for which is not prepared to response. An object will always be contained in a variable whose type is the same or a supertype as the objects's one. I this way the compiler can check that the method invocations the variable receive are adjusted to the real type of the object pointed to by the variable.
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:


Hey you thought you were going to fool the language?


Originally posted by Jose Botella:
[CODE]
Hey you thought you were going to fool the language?


No I was not gonna fool the language.I thought it is casting from array object to object of Object class.coz in the above code o is object refering to array object.
Thanks
Veena
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic