Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

How this is the answer.

 
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok ,here is my doubt,may be its a very silly question but i want to know the explanation.

Which one of the following is a correct way to call methodA? Assume data is a non-empty reference to an array object, and lbl is a non-empty reference to a JLabel object.
There are 4 options.
1)methodA(data[], lbl);
2)methodA( data[10], lbl);
3)methodA( data, lbl);
4)methodA( int [] data, lbl);


According to me the answer may be 2,but the answer is 3 can anyone explain me why the answer is 3 .
Thanks in advance.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i did not show the method its shown below.

public void methodA (int [] vals, JLabel display){
// do something
....
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
We don't know the content of the data array, do we ?
 
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
Hi dhwani,


3)methodA( data, lbl);



the argument "data" itself would suffice when the actual argument is of array. It actually refers the base address (ie data[0]) of the array thereby in the called method you would get the reference.

I hope i was clear. Aint I?
 
Raghavan Muthu
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
forgot to add..

in case of your Answer 2, its nothing but the 10th element of the array what you pass to invoke - which will NOT match the syntax of the method declaration.

Is that clear?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

its nothing but the 10th element of the array what you pass to invoke


To which I ask : what if the 10th element is an array of int ?


[ May 29, 2007: Message edited by: Satou kurinosuke ]
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok what raghavan said is it refers to base address data[0] hence it is passed to which i agree,but the code shown by "satou"

here data is object of array.
int[][] data = new int[20][20];

Here data is given two index ,is it that data is a array collection?

but when you pass it in method it is
p.methodA(data[10], lab);//here a single index [10]

so at this point i am getting confused please can you explain.
Thanks in advance..
 
Raghavan Muthu
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
Hi Satou kurinosuke,

That was a good point. But i did tell in a general way and context. In that case, the answer 3 seems to be right but not answer 2. Because the question was just having the plain options and not in detail.

In your case, answer 3 is wrong as it would be of sending the address of 2D array data[][]. But since the answer 3 was right, i think it might have been asked in the same assumption as that of mine.

Aint I?
 
Raghavan Muthu
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
Yes dhwani. You are right on your confusion.

Looking at the source code of the method definition what you gave,



The method definitely accepts only a *1D* array as an actual argument. In that case the answers may vary depends on how you invoke. There are two possibilities..

Case 1: If you have defined a 1D array



Case 2: If you have defined a 2D array,



But the question was very generically asking which one is right, and by default you can go for the 1D array and not the 2D array i guess. If then, you can even go for data[0][1][2][3] also right?

Hope this clears and helps to certain extent.
 
dhwani mathur
Ranch Hand
Posts: 621
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once again Thanks Raghavan for clearing my doubt.
 
Raghavan Muthu
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
My Pleasure dhwani...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic