Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SCJP array problem

 
Anand Shrivastava
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear friends,

Kindly find time to guide me through the following example of K&B


My problem is with line:
int [] b= (int []) a[1];
It is clear that though a is a two dimensional array, yet a[1] is a single dimensional array. Therefore why explicit casting was required here. Why the following line would not have been proper :
int [] b = a[1];
Please explain.
While preparing, i got struck down by lot of self test questions in this (Assignments) chapter, though could understand them later on but at first go could make the correct choices. This sort of has depressed me. Kindly help.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Anand Shrivastava:
...
My problem is with line:
int [] b= (int []) a[1];
It is clear that though a is a two dimensional array, yet a[1] is a single dimensional array. Therefore why explicit casting was required here. Why the following line would not have been proper :
int [] b = a[1];
...

Did you try compiling and running this without the explicit cast?
 
Gaurav Bhatia
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried and it worked without the explicit cast also.

I used ->

PS: Using JDK 1.5 and Eclipse as IDE
 
Nabila Mohammad
Ranch Hand
Posts: 664
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Gaurav Bhatia:
[QB]I tried and it worked without the explicit cast also.



If you tried and it Did work, then it's Perfectly fine with out the cast.
What seems the problem?

Having something extra that is not required doesn't make it wrong.
You just need to know when it's right and when it is not so that you can point out when it will have an error and when it won't .
Coz that's what the exam is all about....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic