• Post Reply Bookmark Topic Watch Topic
  • New Topic

Passing and Returning Arrays to Methods  RSS feed

 
Timothy Han
Ranch Hand
Posts: 35
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i'm learning Java from a Youtube tutorial video (https://www.youtube.com/watch?v=_SIptqsH3dI#t=0.988276) and he is showing me how to pass and return an array from a method.  Here is the code:



Now what is display()?  Why is he using it in the video?  I know that he is trying to pass array value name "marks" into display(). 

Also, if he is passing and trying to run a for loop in this array, then why is he naming this array "input" instead of calling it by the actual assign array name "marks"?

In the "System.out.println(input[counter])" line, why is he using "input[counter]"?  Why not say "system.out.println(marks)" ?  Also, why does he have a two square brackets in this line of code (input[counter])?  Let me know.

Thank You!

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Han wrote:
Now what is display()?  Why is he using it in the video?  I know that he is trying to pass array value name "marks" into display(). 


Well, it is video regarding passing an array to a method -- so display() would be the method here.... but in practice, it could be anything, as long that it is consistent (meaning the caller uses the name of the callee).

Timothy Han wrote:
Also, if he is passing and trying to run a for loop in this array, then why is he naming this array "input" instead of calling it by the actual assign array name "marks"?


Again, input is just the name of the parameter... in practice, it could be anything. The input variable is simply a reference variable that refers to the object (array) that is passed in.

Henry
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Timothy Han wrote:
In the "System.out.println(input[counter])" line, why is he using "input[counter]"?  Why not say "system.out.println(marks)" ?  Also, why does he have a two square brackets in this line of code (input[counter])?  Let me know.


First of all, it can't be named marks. The marks variable is not in scope in the display() method.

... but assuming you mean input instead. The input variable refers to the array. The input[counter] variables is a specific element of the array.

Henry
 
Timothy Han
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!  That clears up a lot.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!