Hello, in this Java programming video, we are going to take a look at variable arguments. So the only parameters that we can pass in ball we're going to do is create a method, which we've already done before. But this will allow you to pass in as many parameters as you want, without having to specify you know, the amount that we're passing, it's gonna be really cool data restriction if they have to be the same data type. But if that's the pattern in some numbers, and other No, they added them all together, something simple, we can easily do that. So if I call this create a method for public and I'll just do a void because what I'm doing and I'll show you how we would maybe let's just do it with two parameters initially. So into number one Number two, if we were to do no one to like so, and here, if we were to call a dad, I'm just passing through, find this to a variable, so call this int result equals either to free and then we printed that system.
And then if I type in result, run this now we get five sounds pretty cool. If he wants to add another number, this will be the exact process that we just do in free. No, don't pass it. No, I'm just incrementing by one, the ones I'm passing in, but no significance to that. So it's nine now. Okay, so this could be, you know, a bit tedious.
So we'll call this so you know, refer to this, you know the source code that will be provided. And instead, what we're going to do is show you the ultimate method for this. So instead you have a stripe and let's just say the call number. And what you do is after the date stop, you put three dots. So this is specify that, you know, they, we don't know the amount but there could be multiple and less this. If we did the 33.
If we do any numbers, gotten then the name of it equals zero. We'll go into this system. Tell them and we'll print them Another Can you, you know, get rid of this, I won't, we won't do it, we'll just do avoid now, just keep things simple, get rid of this, get rid of this. And we'll do all the printing inside of here. So if the length of it equals zero, so moving past it will say, Please enjoy information. Cuz you know, we have a variable amount of arguments, you might get zero passing.
So let's just do a return statement with no value because it's void, so just basically exits that particular method. And now what we're going to do is just do a loop. So we can do a loop. So what we can do is for, for, for, for, for for the for each loop, so for int num cola Like, so we'll do int total 02. Initially it will say total equals. And let's just system total.
So system total, like so. Now if we run it, we get the value nine, which is pretty cool. We just add another value, we add one, we should get the value 10, we get 10. So we didn't do any extra changes to the method. So I have to be an entity. It could be any day type, because it could be Boolean, maybe you pass in a bunch of unions.
It could be your own custom data type. When you create your own classes, that's fine. Let's just test it to make sure it's working. We put enough in Pat information and if we put a bunch of numbers Excuse me going on. I won't go there. When we should get some relatively low number, I'm gonna assume that that is correct on out the spaces, but at an extra task, feel free to modify it.
So, you know, different data types and I can make sure that this is correct and the addition I'm sure it is, I'm sure it is in good fit for no more advanced way of having a very number of arguments. If you have any questions, feel free to reach out and unusual fans watching and I look forward to seeing you in the next awesome Java video.