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

need help in arrays

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
after going through chapter 3(k&b book) i was trying out a few things and i got stuck in the following code.

Line 8 compiles but line 9 does not.please help.

[ August 23, 2008: Message edited by: ankana mukherjee ]
[ August 23, 2008: Message edited by: ankana mukherjee ]
 
Ranch Hand
Posts: 349
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 8 also will not compile. o is a duplicate local variable.

Ananth Chellathurai
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try to compile it yourself?
What error messages does the compiler give?
What do you think the error message means?
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankana.

I would like to comment two things.

First:
As Ananth said: Line 8 will not compile.
Reason: Redeclaration of variable 'o';

Second (and more important one): Why does line 9 gives an error?
Reason:
You have declared variable 'a' of type A and not A[]! That is the array of class B cannot be referred by a non-array variable.
It will compile fine if you try this: A[] a = new B[1];

Now, the million dollar question....
Then how come line 8 executes fine (ofcourse if you change 'o' to say 'ob')?
You see, Object is superclass of all objects created in Java and it holds true for array variables too... Thus, when you create array of class B, class Object being the superclass of all arrays, will be able to refer it.
Thats it!
 
ankana mukherjee
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ananth..i have rectified that.
the error given is "incompatible types".
i thought that both Object and A are super to B,so,acording to me both should compile.
please tell me where am i wrong.
[ August 23, 2008: Message edited by: ankana mukherjee ]
 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object o=new B[1];
In the above line you are assigning "array of class B obects" into an Object class. Which will easily be done withour errors as array itself is an object and you are asigning abject into an Object class. Basically, new B[1] is an array abject not an abject of type B.

A a=new B[1];
In this line you are assigning an object into a reference of type class A, which obviously is not possible. So, it generates an error.
 
reply
    Bookmark Topic Watch Topic
  • New Topic