Viewers: in countries Watching now:
Join author David Gassner as he explores Java SE (Standard Edition), the language used to build mobile apps for Android devices, enterprise server applications, and more. This course demonstrates how to install both Java and the Eclipse IDE and dives into the particulars of programming. The course also explains the fundamentals of Java, from creating simple variables, assigning values, and declaring methods to working with strings, arrays, and subclasses; reading and writing to text files; and implementing object oriented programming concepts.
The Java programming language allows you to create methods that share the same name, but have different signatures. A method signature is the unique combination of keywords and arguments that the method receives. For example, the standard main method receives an argument which is an array of strings. You can create your own custom methods in your class and where necessary, you can create multiple methods that share the same name as long as they have different signatures. The Java Virtual Machine determines at runtime which version of a method it should run, depending on what arguments are passed to it.
Let's create a few very simple methods. I'm working in a beginning main class in the project MethodOverloading. I'll place the cursor after the static void main method and I'll create a new method using private static, I'll set the methods return value to int and I'll name the method addValues. For this first version of the method I'll receive two arguments, and they'll both be data typed as integers. I'll name the first one int1, and the second one int2.
The body of the method will add up these two values and return the result. I'll do it on a single statement. return int1 + int2. In the main method I'll declare two starting variables; I'll declare them both as integers. I'll name the first one value1 and I'll set it to a value of 5. And I'll create the second one as value2 and set it to a value of 10. I'll declare an integer result variable and get its value from addValues and I'll pass in value1 and value2 and I'll output the result using System.out.println, (The result is" and then + result) I'll Save and Run the application and everything is fine. The result is 15.
The data type again will be int and the name will be int3, and I'll change the return statement to add that variable as well. As soon as I make the change, all the errors go away. I'm still left with a warning that I have a method that's not currently being called. But I'll fix that. I'll go back to my main method and create a third value and I'll set it to 15 and then I'll change the way I'm calling the method and I'll pass in value3. I'll Save the change and Run the application and now I get the result as 30. Clearly I'm now running the second version of the method, so you can have more than one method of the same name as long as they have different numbers of arguments.
But you can also distinguish methods from each other by the data types of the arguments. I'll create one more version of the add values method. My code is getting a little longer now, so I'll maximize my editor and move the cursor under all the other methods and I'll create another private, static and returns an int, and once again it will have the name addValues. This version of the method will have two arguments but they'll both be strings, and I'll call them val1 and val2. Now in order to add these values together I need to convert them to integers, so within the method I'll declare two integer variables.
The first one will be called value1 and I'll set its value using integer.parseInt, and I'll pass in val1, and then I'll make a copy of that line, I'll change the second version to create a variable called value2, and it will get its value from val2, and then I'll do a return statement and I'll add together value1 and value2. So now I have three versions of the method, addValues with 2 integers, addValues with 3 integers and addValues with two strings.
I'll go back up to the code and I'll create a couple of new variables, the first one will be a string and it will be named string1, and I'll give it a value of 10, but because it's a string it has to wrapped in double quotes. I'll set string2 to a value of 25, then I'll create a variable called result2, and I'll get its values from the addValues method. And notice when I press Ctrl+Space Eclipse shows me all the different versions of the method that are available. The version with the two integers, the version with two strings and the version with the three ints, I'll choose the version with the two strings.
and I'll pass in string1 and string2. I'll make a copy of my println command and paste it down here, and I'll output the value of result2. I'll run the application and the second version is clearly calling the version that receives the strings. So this sort of coding is called method overloading. When you overload a method, you are reusing the same method name, but you're creating alternative signatures. This is a good coding strategy in Java to handle situations where in one circumstance you have a certain set of variables, in another circumstance you got another set and where the data types can differ as well.
Find answers to the most frequently asked questions about Java Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.