Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Question on arrays

 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
What is the difference between
Object o = new int[10];
and
Object[] o = new int[10];

How is first statement correct, Whenever we declare arrays the reference variable must have [] operator, for example int[] a = new int[10]; Here a has [] in the declaration.

Second statement Object[] o = new int[10]; will result in a compiler error "Type mismatch: cannot convert from int[] to Object[]". So i did a typecast
Object[] o = (Object[])new int[10]; but now i get a compiler error "Cannot cast from int[] to Object[]"

Any explainations?
Thanks
Deepak
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak

When you say
Object o = new int[10];
you are actually creating an object which will be the reference for the integer array .
and when you say
Object[] o = new int[10];
you are trying to create an integer array of type o which is not possible cause int is a primitive data type and does not qualify as an Object .
if you do Object[] o = new Integer[10]
it will work

I hope i was able to clarify your doubt
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,
I think Object o = new int[10]; is possible because arrays are object implicity and since in Java every class extends Object class, the implicit class that holds arrays also extends Object class. So here infact we are creating a subClass object and assigning it to the parent class reference.

Is this explanation correct?
Thanks
Deepak
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct. what chandrandeep singh said as well.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Charandeep Singh

You mean if we can do
Object o = 1;

Object []o = new int[10];
will work. right?
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean if we can do
Object o = 1; will be converted to Object o = new Integer(1);
And hence we can do this because 1 will be autoboxed to Integer and since Integer extends Object we can assign Integer class instance to Object reference.

Object []o = new int[10];
will work. right?
This will not work. ["Type mismatch: cannot convert from int[] to Object[]"]
o is a reference to an array that has Objects
and new int[10] returns a reference to an array that has integers.

Thanks
Deepak
 
Charandeep Singh
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankith suresh

you can always do
Object o = 1;

this will compile fine but if you try and print the value of o you will find an exception at run time
"Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Type mismatch: cannot convert from int to Object
"

when you try and create an array of a variable of type Object it has to be of either same class or it's subclass which is not the case in int
but when you say
Object o = new int[10];
it creates it's reference
cause
if you try this piece of code


This code will print true.
so that means when an array is created it is by default of type object only
but when you declare a variable of type object you have to initialize it with either it's sub class or object itself
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ankith suresh posted Today 15:17

You mean if we can do
Object o = 1;

Object []o = new int[10];
will work. right?



Nope.

The first is about autoboxing and the second about the fact that also arrays are objects. Even arrays of primitive arrays are objects.

You can try this:
Object anArray = new int[10];
System.out.println(anArray.toString());
System.out.println(anArray.getClass());

where you invoke methods on that object.


Yours,
Bu.
 
Is this the real life? Is this just fantasy? Is this a tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic