• 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

Question on Return type

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the following code from K&B book :

import java.util.*;
class R{
public static void main (String args[]){
R rvar=new R();
Object o=rvar.test();
Object test(){
//****
}
}

}


Which of the following code inserted at //**** will not compile:

1)char[][] c=new char[2][2];
return c[0][1];

2)char[][] c=new char[2][2];
return c[1];

Ans: 2

Can someone explain why not 1.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because a char is not an object.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,

What I feel is c[1] can be considered as Object since its an array. But c[0][1] is purely char. & Can we apply c[1] in Co-variant scenario.
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot !

But even the 1st option is also char array.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kasak (nice name btw), 1st option is not an array but only a char.
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kasak Tahilramani:
But even the 1st option is also char array.


in this code:

you have CREATED an array. that is an object. your array happens to hold arrays. those are also objects. but those "sub" arrays contain characters - more specifically, chars. a char is NOT an object.

now you have the statement

you are saying "return the second element of the first element of what c refers to". the first element of c is an array that holds chars. the second element of THAT array is a char. So, you are actually returning a single char, which is not an object.
[ December 28, 2006: Message edited by: fred rosenberger ]
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot fred for detail explanation.



Ans: f

can any1 explain?
 
Ranch Hand
Posts: 381
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you think, which line should not compile?
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int[][] a = {{1,2},{0,1,2},{-1,0,2}};
// 1 creates an array of int[3][] :
a[0] ={1,2} (creates an array of int[2])
a[1]={0,1,2}, a[2]={-1,0,2}(creates an array of int[3])
Object[] obj = (Object[])a.clone();
// 2 creates an array of int[3][]
for(int i = 0; i < obj.length; i++) { // 3 obj.length =3

int[] ia = (int[])obj[i];
// 4 first iteration i=0 so ia={1,2}
second iteration i=1 so ia={0,1,2}
third iteration i=2 so ia={-1,0,2}
System.out.print(ia[i]);
// 5 first iteration i=0 so prints 1
second iteration i=1 so prints 1
third iteration i=2 so prints 2
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Object Test(){.........}
The return type of this Method is an object of Object.
char c[][] = new char[2][2];
return (c[0][1]);
Here return statement will return a constant value of type char that is not compitable with its defined return type.
Now,
return (c[1]);
Here return statement will return an object. So, this statement is correct option.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
as c is a two dimentional array,
Here c[1] refers to an array of one dimention
and c[2][2] is the value on that array index

hence you code is expecting an object to be returned , c[1] is correct because all arrays are objects in java.
and c[2][2] will return a character leteral which is not an object and cannot be one, hence this option is incorrect.
 
Kasak Tahilramani
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rajesh and all.
 
Time flies like an arrow. Fruit flies like a banana. Steve flies like a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic