• 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

Questions on Arrays

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question 1)

1. class Dims {
2. public static void main(String[] args) {
3. int[][] a = {{1,2,}, {3,4}};
4. int[] b = (int[]) a[1];
5. Object o1 = a;
6. int[][] a2 = (int[][]) o1;
7. int[] b2 = (int[]) o1;
8. System.out.println(b[1]);
9. } }

Question 2)

1. class Zippy {
2. String[] x;
3. int[] a [] = {{1,2}, {1}};
4. Object c = new long[4];
5. Object[] d = x;
6. }



What is the result in these cases?

I could not figure out the assignment operators used in these questions. can any one explain ..
thanks in advance
 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The output for first question is "exception is thrown at runtime"


the out put for second question is "Compilation succeeds."


Can any one explain
 
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object o1 = a;

for the first question you will get an error here
it should not be a runtime exception
you must get an compile time exception since cannot convert from int[][] to Object
[ March 26, 2007: Message edited by: Prasad Shindikar ]
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Prasad Shindikar:
Object o1 = a;

for the first question you will get an error here
it should not be a runtime exception
you must get an compile time exception since cannot convert from int[][] to Object

[ March 26, 2007: Message edited by: Prasad Shindikar ]



All Arrays are Objects first. So that should be possible.Infact

int[][] a;

(o instanceOf Object)

returns true ;
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exception should be thrown here afaik:
int[] b2 = (int[]) o1;
 
Sanjeev Narula
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually answer which i have given are right..Because i have taken these questions and answers from SCJP book by Katherine Sierra,Bert Bates in chapter :- 'Assignments'
 
Amit Wadhwaa
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

int[] b2 = (int[]) o1;



The above results in an error because at runtime o1 holds a 2d array, which cannot be cast to a 1d array
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[] b2 = (int[]) o1;


In that we will get a runtime exception because o1 is a reference of array 'a' which is two dimensional,but we are assigning to 'b2' which is one dimensional array.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic