Contents

keyboard_tab

The course is part of this learning path

Start course
Overview
Difficulty
Intermediate
Duration
1h 19m
Students
83
Ratings
5/5
starstarstarstarstar
Description

This course looks at methods, which are named, self-contained blocks of code that you can call upon to help solve problems for you. We'll then take a look at some projects which will put methods into practice.

Learning Objectives

  • Understand the fundamentals of writing your own methods
  • Learn how to classify methods by their return types as well as their parameters
  • Learn about parameter passing schemes that are used by programming languages to determine how methods treat their parameters
  • Explore method overloading and two-dimensional arrays

Intended Audience

  • Beginner coders or anyone new to Java
  • Experienced Java programmers who want to maintain their Java knowledge
  • Developers looking to upskill for a project or career change
  • College students and anyone else studying Java

Prerequisites

This is a beginner-level course and can be taken by anyone with an interest in learning about Java.

Transcript

In the previous lecture, we began to solidify our knowledge of methods by learning how they can help us break the problems down into more manageable pieces, and then we can find solutions to those pieces. And also that they provide us with the code reuse, calling, and invoking our methods when we need them. We also discussed how to classify methods by the return types as well as their parameters. In this lecture, we'll continue our discussion about methods. When discussing parameter passing, several programming languages such as C++, distinguish between pass-by-value and pass-by-reference. As a reminder, arguments are the data including literals or variables that you pass in when you call or invoke the method, that's when you use it. 

Whereas parameters are the placeholders in the method definition, the header of the definition that receive the value when the method is called. Pass-by-value can be thought of as pass-by-copy because a copy of whatever you pass in as an argument is copied into that parameter. Pass-by-reference passes in the address of the argument that is passed in so that the parameter will act as an alias of the original argument passed in. So, with pass-by-reference, if you make a change to the parameter in the method or function, as we'd say in C++ it affects the argument that we passed in. The natural question would be does Java support pass-by-value as well as pass-by-reference?

Some people say, yes but technically they'd be wrong. Java only supports pass-by-value. This might seem surprising because if you pass an object reference of a class, a mutable object that is such a StringBuilder, to a method and modify it in the method, then the object does get modified. But here's what's interesting. Even though Java passes everything by value, that is, it makes a copy of whatever the argument is and stores it in the parameter. Think about the two different categories of data types that we have. Primitive and reference. The variables of primitive types hold the value were interested in, but the variables of reference types hold what? That's right, a memory address. 

So, when you pass a reference type variable as an argument to a method, the value that is copied into the parameter is a memory address. So, any changes you make inside the method's body will affect the actual object that the argument was pointing to. Pretty cool. If you're not really understanding the pass-by-reference versus pass-by-value fully, that's okay. As you gain experience in programming, it will make more practical sense. You'll hopefully understand it better later in the course when we discuss how to create your own classes. Another major topic that you should be familiar with before we move on is method overloading. Let's write some code and I'll describe it. So, first, create a file named method overloading. Right-click, 'new' Java Class, MethodOverloading. There we go. Write our main method. Good. So, let's see what happens when we have these different methods that are the same name, but different arguments. 

So, static_int getResult. We'll call it num and let's say just return num times 2. So again, pretty trivial what they actually do right now, but it's just to demonstrate what they can do. I know the same exact name, int num1, int num2. Here we go. And of course, let's do another one int num1. And then, let's say String value just to show we can do some really interesting stuff here. So, this is another getResult. So, we're going to return the num1 times Integer.parseInt of the value that's passed in as a string. And of course, we're going to go up to main here and we get result equals getResult of five. And then we're going to print out the result. And then, we're going to reuse it. System.out.println(), getResult five, and then we're going to pass in a string literal. In this case, it could be a variable. Any of these could be variables. They don't have to be literals; they could be variables; they could be method

calls. There's all kinds of options. Well, let's run this and see what happens and we'll talk about it a little. So, right-click, Run, and of course, we get what we expect. We have the five which is multiplied by the two in the first overload. And it's able to distinguish between these overloads even though they all have exactly the same name; they have different parameter lists. So, if you use the name combined with the parameter list, that would include the types, the number of parameters, and the order. So, if you swap order, for example, if I had a version of this, an overload and I had string argument or string parameter first and then an integer even though the same number and the same types would be identical. The order would be different. 

So, I could actually write a different overload based on that. So, all of these are different. Now, that's called the signature when you combine the name with the parameter list. That's called the method's signature. So, it includes the type, and the order and the number of the parameters. All right, pretty cool. The second one is five times four. That's pretty easy. And then this one takes the five and multiplies it by 12, but it has to convert it into an integer first. So, that's pretty cool. So, we're able to work with methods having the same name but different parameter lists which together again constitute the method signature. Notice the return type isn't part of the method signature. I never mentioned the return types. 

So, in all these cases the return type is int, but it could be different, right? It could be string, could be double, could be whatever I want, and then I just make it return whatever I want. However, if I wanted to write another method here that took an int parameter and that alone named getResult. I couldn't just have the return type varied and say make it return a double or a string or something like that because that's ambiguous when you call it. The system has to know which one to call and it figures that out by your parameter list. So, if the parameter list is the same, it has no way of knowing. So, how does it know if you had this right here and it returned an int? What happened if I could put a parameter here with an int and then return a string? How would it know the difference?

Would it know that this was returning an integer and this variable right here, since it's an int or would it think I was returning a string, but I just got it wrong and then this one should be a string. So, how is that supposed to know? So, the compiler needs a way of distinguishing between the method calls so the return type, although it can be different for your overloads, is not considered part of the method signature. Because calling it doesn't help distinguish one call from another, giving it a different return type, that is. The parameters, on the other hand, do. So, when the method names are the same but the parameter lists including types, number, and order of the parameters are different. Then you can create method overloads. Before moving on, it's time for another challenge. 

In this same file MethodOverloading.java, I'd like for you to make another overload of the getResult method that returns a string, and takes two string as parameters, and returns their concatenation with a space in between them. Make sure to test it in the main method. So, pause the video and give this one your best shot. Come back when you're done or if you need some help. How'd that go for you? Were you able to code the method overload that I challenged you with? Hopefully, that one wasn't too challenging for you, but even if it was, don't worry. You'll get better, the more you code, and more practice you have. So, let's work on this together. So, we're going to write this extra overload down here public rather public or public static String_getResult. And we probably should spell string right. There we go. And String str1, I'll call it and then String str2 and it just returns the String str1 with a space in between and the String str2. 

There we go. And we're going to call it up here just to test it. I can't make the result that. And as a matter of fact, you can actually just put it directly in the print line, you don't need a separate variable. But I could make what if I wanted to. So, printIn and I'm just going to put it directly here, getResult and we're going to put "John", "Baugh". Here we go. So, the print line just expects some sort of parameter that it knows how to print out and the argument we pass in just happens to be a string because this is a value returning method that returns a string. Pretty cool. Let's see what it does. Right click, run the method overload example and we get John Baugh.  Nice work everyone. In the next lecture, we'll cover the topic of method calling as well as the topic of recursion, that's where a method calls itself. So, how does that work? Meet me in the next lecture, and find out. I'll see you there.

 

About the Author
Students
759
Courses
20
Learning Paths
4

John has a Ph.D. in Computer Science and is a professional software engineer and consultant, as well as a computer science university professor and department chair.

Covered Topics