File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How this is the answer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "How this is the answer." Watch "How this is the answer." New topic
Author

How this is the answer.

dhwani mathur
Ranch Hand

Joined: May 08, 2007
Posts: 621
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

Joined: May 08, 2007
Posts: 621
Sorry i did not show the method its shown below.

public void methodA (int [] vals, JLabel display){
// do something
....
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

We don't know the content of the data array, do we ?


[My Blog]
All roads lead to JavaRanch
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Nov 24, 2005
Posts: 14687
    
  16

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

Joined: May 08, 2007
Posts: 621
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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: Apr 20, 2006
Posts: 3344

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

Joined: May 08, 2007
Posts: 621
Once again Thanks Raghavan for clearing my doubt.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

My Pleasure dhwani...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How this is the answer.