 Hi, this is Christian. Welcome. In this video, we're going to explore Varax and Java, and how you can use a single Varax method to replace overloaded methods in your program. So what are Varax? Varax is the short name for variable arguments, and it is a parameter that can accept a number of values. The Varax parameter is declared by pre-pending a set of three dots, also known as ellipses, to the parameter as you see here. A Varax parameter is a one-dimensional array of that particular type, and a method that uses Varax is often called a Varax method. So rules for using Varax are as follows. You can only have one Varax parameter in a method, and it can only be used in a method. The parameter must be declared in the last position, and the parameter can accept an array or a sequence of values, but not both. So a question you may ask is what can Varax solve? In my opinion, I wouldn't say it's solving a problem. It's probably more of a convenience. If you use Varax efficiently, you can eliminate any unnecessary overloaded methods in your program, makes your code much leaner. If you use the generic type of Varax, that can also help eliminate multiple parameterized types, as you'll see in an example. Okay, so what that further I do, let's go and see how this is done in Java. Here I'm using IntelliJ here. So here I have a function to print the sum of two numbers a and b here. I just basically add those two and pass it to the function down here to print a result, and we're just going to print that to the console. Okay, so a classic typical example you see here. I'm going to remove this and let's call the function printSum. I'm going to pass here maybe like two and three, right? So I'm going to run this. I'm going to expect to get a result of five, as you will see over here on the right side. Okay, so no problem. Now this becomes a problem when you start to have more options. So I want to print maybe another one here. I want to add four and five and six, right? And so on. So what you do is you say, okay, well, I'm going to copy this and I'm going to make another function down here. And this one is going to take three parameters and c, add that to the function called down here. And then my problem solved, I got my solution, right? And then you would have four or five or 10 more. So you keep building these functions. So these are your overloading methods many, many times. And you can see how that can be really tedious in your problem. Okay, consumes a lot of memory in terms of size. So the solution to this is, you know, to eliminate these by creating what's called a array, right? I'll copy this and I'll put a top and then I'll just turn this off. We don't need these two for now. Okay, so we're going to use the array approach. So here you will put array of numbers. And then of course, now the implementation will be a little bit different, be something like sum is zero. And then you just put four these integers and n of numbers. Oops, let me just type that correctly, numbers. And then we'll just put sum plus equal n. And then we're going to pass the sum to the function. And then that is solved, right? Well, the only thing is that now you pass the parameters to the arguments to this function in array format, not a sequence like this. So now I have to go back here and change this to the array. So you can just do it here. Like that, right? I'll copy this and put it down here. And then here we go, I have a three, four, five, six, seven. It doesn't matter what size I have now, because the array can take any size from two to one function. And there I solve my problem, right? Which is cool, which is okay in this case. Okay, but the caveat is by doing this way, you must always pass an array. What if you don't want to do that, right? What if you read some data from a text file or database or from the user directly, and you want to pass this data to this print function? Well, you have to structure your code in here to build the array first before you pass to this function. Okay, so that's just a couple extra steps. Now here is where the verax parameter come in very handy. The verax parameter, as I mentioned, is indeed an array, just like this. So what you do is you basically replace this score brackets here with the three dots. And that's it. You can see that I changed that. Nothing changes in my code except only the three dots. Everything here is still working as usual. If I run it now, you see, it worked just fine. It behaves exactly the same. The body implementation here is exactly the same. And the input here also is exactly the same. But here's another cool thing about it. I can pass in a sequence of numbers. Okay, I don't have to pull those into an array because this number here takes both types, both formats, either a sequence of numbers or values, whether they are as long as they match the type, or it can take an array of values. And you see that works beautifully. Okay, but it does not allow both. So in other words, I cannot go in here and include this array in here like this. Okay, so it doesn't allow that. This parameter is the verax parameter is prepended with the ellipsis in front of it. Okay, it must go in front or between the type and that one here. It doesn't have to be in space like this. You can attach directly to the variable name, or you can attach it to the type name over here, or you can just attach all together. It doesn't matter, as long as it's in front of the variable name, it does not let you put in over here like this, right? You can't do that. Whereas, you know, regular array, of course, you can put here, or you can put in front here, right in Java, it lets you do that, do both the C sharp and then the Java way. But for this one here, it must go in front of that parameter. Okay, so that is the power of using the verax and your method. Now, as I mentioned, you cannot have two of them. I cannot go in here and put in, you know, and then A, okay, because it's just a rule, it's not allowed. What if I want to pass in here doubles, right? I want to pass in 1.5. And then of course, now you have a problem because it's in type, you're passing a double in there. So it doesn't work. So what do you do? Well, you have to write another method to overload this. So you would do something like this, go back here like we would have before. This will be a double. And then now, this will all be double. And then same thing here. If I do that, my sum is now int. But I'm passing here is, I mean, in down here, passing a double to that. So you have to do a casting, you know, all sorts of stuff, right? Pass, kind of cast an int. And then you print that down there. And then it's fine. So now when I run this, I'm going to call the second method instead of the first one, because it matches the second parameter here. Okay, what about I want to print bytes or along more flow, right? Other types. In that case, then you can use a generic type. So let me remove this. And then go back here and changes to the t type. Okay, so we here put t and then everywhere is integer put t here. Now the only thing is that you cannot do like t sum here and then try to add something to t assigned to t because t in this case is unknown, right? It could be integer, it could be a string, it could be Boolean, it could be object. So you cannot assign a value like that to t. What you need to do is you just have to, you know, decide what data you want. So in this case, if I only want int, then my sum is going to be just integer. And so now the t here takes an object class type. And this way it works because of the unboxing and boxing capability that Java provides. You can take primitive data types here and it will unbox that to the class type and the vice versa. So it's going to work here. However, when you do a summation like this, it doesn't work because this is an integer and I'm trying to get a t type of number. I try to add that here. So you can only do that to accept only numbers because I'm doing mathematical operations here is to, you know, restrict the t here or we call it to bound this t type to only number types. So you would do is go here and say t extends the number class. Okay, that would take all numbers from your list. This is no longer valid because, you know, that's an array of stuff. Let me just turn it off. So you can take, you know, my decimal numbers and bytes and float and things like that. Go to the t here and then here I can then grab the value. I want to digrat this right the int value of n. If I pass in a double, I'll put a double if I pass in if I would grab the flow, I will get a flow and so on. So in this case, I'm choosing to choose the integer values. I get that. And then I pass that to the data here, you go to some down here and then here I'm using and so I'm using a type. So it's better to just go with the type t down here as well. We accept all types. So put your t as well. And then now it works just like before. Okay, so now the data that you pass to can now be of any type, any number type. And if you're using classes of your own design, this will be the parent class and then any child classes can also be accepted here. Okay, now just do one more example and they will be done with this video. Something maybe more meaningful or useful is let's say you want to get a let's say these are like maybe a keys, keys of some numbers. And you want to find out the number of frequencies of these numbers in that key set. Let me write a function here. I will use the type of say I use k, usually k is very common used for k for key. You can also call it key if you want. Maybe I'll call key is fine. Void, let's say print, what should we call it, print the keys. Okay, now pass here the type of key. This has the same type as the type of here. Let me just put some spaces here. The type of key I'm using a barrage of keys. Okay, so here what I do, I'm going to use the map to map the key. And then for each key I'm going to count based on how many keys I have. So I'm using a key value pair, right? So the key will return the number of integer for that key. And then this would be I'll just call it the frequency, I guess, of the new map is an abstract. So I'd use the tree map. I use the tree map here. And then key and also integer. And then, you know, let me import this and also import this class over here. Okay, let me close this for now. Right, so when I get that key, then I'm going to loop through the keys here. So if I pass in, let me change this to say print keys. Okay, I'm going to pass all these keys in here and how many duplicates I have. Let me go ahead and modify this a little bit. Three, three, five, six, seven, and one, two. Okay, so it goes in here. And then I need to verify, right? So I guess for every key, key type of key and the keys, just like this down here, right? I want to check if, you know, this key is already present in the map or not. So you can do something like, maybe I'll do an account first and the count is equal to the frequency of get the key. If that key is found, then because it's going to return the number, the integer, right? A looking for 1.5. If that is present, then return the number here, right? If that is equal to no, that means that it's the first time I encounter that key is not in the list yet. If that's the case, then give me 1, maybe the first occurrence. Otherwise, give me the frequency of that key and then use a plus 1 to that. Okay, so that will determine the number of counts. And then finally just going to push it to the map. So the frequency put the current key and then the count. Okay. And then finally, once I'm done, I can pass that to the print result function, the frequency. As you can see, because I'm using a generic type, it accepts anything, right? Very simple. We're very useful that way. So here we go. I go ahead and run this application now and hopefully it works. There's a problem over here. Let's figure why. Probably it's that casting issue. So let's go ahead and expand this one here. I want to see what it says. Cannot convert integer to double. Yeah, okay. Yeah, the back to the same issue again here. I'm having a key type. I'm passing two different types here. You know, double and integer. So I'm going to move it back to just integer type. Okay. And then let's try again. Okay. So you can see here the result prints up here. Kind of small to see, but let me maximize it a little bit. It tells you that, you know, the result of numbers I have, right? I have one, one, two, twos and three threes and one, fours and two, five and so on. So that's the frequency of these numbers here. So that is one use case. You can also pass in, you know, any type. As long they're all the same type, like Apple, that's what A, B, C here. Okay. So you can see it takes the numbers and any type you want. And it will do that for you because of that operator and also the generic types. So you can see that I don't have to care about what type it is because it solved the problem. Okay. Well, any questions are always welcome. Suggestions are always welcome as well. Thank you for watching. I hope to see you in the next video. Bye now.