Java methods that pass arguments/methods that have parameters

Watch
Carrying a torch
Badges: 9
Rep:
?
#1
Report Thread starter 1 month ago
#1
I can't wrap my head around methods being used that have parameters and that pass arguments. I've been struggling to understand wht anything needs to be in a parameter but it's a requirement for a task I'm doing.
0
reply
Strange5050
Badges: 16
Rep:
?
#2
Report 1 month ago
#2
Okay, look at it this way. We have a method that needs to check each value in an array. Look at the following:

Code:
bool HasHelloWorld(String[] wordArray) {
    foreach(word : wordArray) {
        if (word.equals("Hello World") {
            return true;
        }
    }
    return false;
}
In the above example, we have a method that takes an array as an argument/parameter. Meaning, that in our program we can pass any String array into it and get a result:

Code:
String[] myArray =  { "Bob", "John", "Ellie", "Billy" };
bool result = HasHelloWorld(myArray); // Will return false.

String[] mySecondArray = {"Apple", "Orange", "Hello World" };
result = HasHelloWorld(mySecondArray); // Will return true
The reason we need parameters is because oftentimes you'll need to re-use that piece of code. If we take the example above. Imagine I had 100 arrays I need to check for the phrase "Hello World". If I didn't have a parameterised method, I would be repeating the same code one hundred times. But, with a method and parameter I can do this:

Code:
foreach (array : myOneHunderedArrays) {
    bool result = HasHelloWorld(array);
    // do something with result
}
This way I can iterate through all 100 arrays and check for "Hello World" without repeating the same code hundreds of times, in only a couple of lines. Plus, methods are often used within classes in Java. So you don't have immediate access to the data you need to do something with anyway, meaning you need to write the logic accounting for some parameter that will be passed into it.

[Note to anyone reading this, I am aware my last example is not how you iterate through what would be a multi-dimensional array, but for the purpose of explanation ]
Last edited by Strange5050; 1 month ago
0
reply
Carrying a torch
Badges: 9
Rep:
?
#3
Report Thread starter 1 month ago
#3
(Original post by Strange5050)
Okay, look at it this way. We have a method that needs to check each value in an array. Look at the following:

Code:
bool HasHelloWorld(String[] wordArray) {
    foreach(word : wordArray) {
        if (word.equals("Hello World") {
            return true;
        }
    }
    return false;
}
In the above example, we have a method that takes an array as an argument/parameter. Meaning, that in our program we can pass any String array into it and get a result:

Code:
String[] myArray =  { "Bob", "John", "Ellie", "Billy" };
bool result = HasHelloWorld(myArray); // Will return false.

String[] mySecondArray = {"Apple", "Orange", "Hello World" };
result = HasHelloWorld(mySecondArray); // Will return true
The reason we need parameters is because oftentimes you'll need to re-use that piece of code. If we take the example above. Imagine I had 100 arrays I need to check for the phrase "Hello World". If I didn't have a parameterised method, I would be repeating the same code one hundred times. But, with a method and parameter I can do this:

Code:
foreach (array : myOneHunderedArrays) {
    bool result = HasHelloWorld(array);
    // do something with result
}
This way I can iterate through all 100 arrays and check for "Hello World" without repeating the same code hundreds of times, in only a couple of times. Plus, methods are often used within classes in Java. So you don't have immediate access to the data you need to do something with anyway, meaning you need to write the logic accounting for some parameter that will be passed into it.

[Note to anyone reading this, I am aware my last example is not how you iterate through what would be a multi-dimensional array, but for the purpose of explanation ]
That was actually quite impressive. You put String [] as your parameter which meant allowing this specific method to be used for several arrays as long as they were String ones. This is like the example of having a method that takes in a string Name and prints the word hello before e.g. "Hello" ___. And when the method is called e.g. MethodtosayHello().. in the brackets the string name can be said: MethodtosayHello("Bob").

I've also noticed that with for loops, it's nice if the loop count entered is simply an int that has been passed on as an argument, especially if we want this from the user. I think I just have to attempt larger projects to see the full use of parameter-filled methods
0
reply
Strange5050
Badges: 16
Rep:
?
#4
Report 1 month ago
#4
(Original post by Carrying a torch)
That was actually quite impressive. You put String [] as your parameter which meant allowing this specific method to be used for several arrays as long as they were String ones. This is like the example of having a method that takes in a string Name and prints the word hello before e.g. "Hello" ___. And when the method is called e.g. MethodtosayHello().. in the brackets the string name can be said: MethodtosayHello("Bob").

I've also noticed that with for loops, it's nice if the loop count entered is simply an int that has been passed on as an argument, especially if we want this from the user. I think I just have to attempt larger projects to see the full use of parameter-filled methods
Definitely. Your example of inserting a name into a string is a perfect use of parameters. In smaller projects, it certainly feels easier to do these things as a one-off. But, as you size up and work with more data then these kinds of things become invaluable. Glad to have helped.
Last edited by Strange5050; 1 month ago
0
reply
X

Quick Reply

Attached files
Write a reply...
Reply
new posts
Back
to top
Latest
My Feed

See more of what you like on
The Student Room

You can personalise what you see on TSR. Tell us a little about yourself to get started.

Personalise

Are you travelling in the Uni student travel window (3-9 Dec) to go home for Christmas?

Yes (107)
28.08%
No - I have already returned home (50)
13.12%
No - I plan on travelling outside these dates (73)
19.16%
No - I'm staying at my term time address over Christmas (39)
10.24%
No - I live at home during term anyway (112)
29.4%

Watched Threads

View All