• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Casting of Arrays and Object

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Abc
{
static Object o;
public static void main(String[]args)
{
int[]n=(int[])o;
}
}
If someone can explain why the above code compiles and runs
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think its bcos.. Array is a object..so extends the Object class
If you are assigning a superclass reference to a subclass, then you need to cast it to the subclass type and thats what you have done in the code..
Hope it helps..
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Adding on to what Anand had said : "You can assign an array to an object without casting => instance of an Object can refer to array object. Therefore it should be possible to cast an object to array".
Hope it helps.
Cheers
Sankar
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, sonali;
IMHO, your program will compile, but will throw a runtime exception ClassCastException because although o of type Object and array is also subtype of Object( you can get by with explicit casting), at runtime o, of type Object can not represent Array object.
Correct me if I am wrong.
Regards,
 
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
The only reason it doesn't throw a class cast exception is that the value of o is null. If o had been initialized with:
o = new Object();
then a class cast exception would be thrown.
The compiler assumes you know what you are doing with the(int[]) cast, without that you would get a compiler error.
 
Sheriff
Posts: 3341
  • 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:
The only reason it doesn't throw a class cast exception is that the value of o is null. If o had been initialized with:
o = new Object();
then a class cast exception would be thrown.
The compiler assumes you know what you are doing with the(int[]) cast, without that you would get a compiler error.


This compiles and runs! Additionally try the following example and tell me what you get for an output.


 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I get exactly the results William described: if o is initialized with <code>o = new Object();</code> there is a ClassCaseException at runtime. If the class cast <code>(int[])</code> is then dropped, the code doesn't compile.
As for your additional code, it produces the output "a Reference is null.", as expected. I'm not sure what your point is with this code - it verifies that a class variable is automatically initialized to null if not explicitly initialized to something else. Which was part of William's point, right? It also shows that a reference can be cast to a superclass of the original type, which is always true whether the reference is null or not.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess(being very recent to Java),
o is an Object of Object class and which is super class.
int[] - Any array is an object in Java which is a subclass of
object.Here int[] became temporary object ,so we can cast like this (int[])o .
Correct me ,If I am wrong.

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you pl. tell me what is difference hier, when
o declared with:
static Object o;
//or
o=new Object();

i think the both is referenced with a value of null Object.
but we get such different results.

regds
zhewen
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Carl Trusiak:
[B]
[/B]


ur correct but in tha above code u can have only one public class May be u have written all the class as public by mistake.
if we say A a =new B()
then a ia not null
Thanx

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
null can be cast to any reference type.
For eg try this:
System.out.println((Integer)null);
It will print "null".
Thus the point that Bill is trying to make is that since o contains null, it can be cast to int[]. However if we had
Object o = new Object(), then the code would compile (since arrays have Object as superclass), but it would throw a ClassCastException at run-time since o does not actually contain a reference to int[].
 
I am going down to the lab. Do NOT let anyone in. Not even this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic