Hello there,
I have a short question about this task. I did solve it but I'm curious - are they both not have the same name (min)?
Isn't that a little confusing for the compiler? What is the difference between them so the compiler knows how to handle it?
Sorry if that question might be dumb :)
Just a question about the functions
Resolved
Comments (5)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
15 January 2019, 16:15solution
They have the same name but different signatures. One is min(int, int) and one is min(int,int,int,int). The compiler knows what one to use based on the values being passed to it. If you typed min(5,6), it would not try to use the min(int,int,int,int) method as that doesn't make sense. It is important to realize that the type of values is what is being evaluated here, so code such as this is invalid:
In this example there are 2 constructors that have the same exact signature: String, int.. Even though one is (name, age) and the other is (address, wealth) the compiler will not be able to determine which one to use and the IDE will not let the code even compile. You could change it like so to make it valid:
Now one has (String, int) while the other has (int, String) which is valid code and it will compile +2
Guadalupe Gagnon
15 January 2019, 16:30
Some more info on overloading constructors:
https://www.geeksforgeeks.org/constructor-overloading-java/
+1
MP
15 January 2019, 19:25
So it doesn't matter what name they have the compiler will look for what fits to the given variables. Okay, thank you now I understand it ! :)
0
Guadalupe Gagnon
15 January 2019, 19:37
Well name and variable signature is determined. In the below example you have two methods that have the same input variables (int, int). The compiler will not get confused because they have different names. It's only when the names are the same that you need to make sure that the inputs are different.
and also it is my pleasure to help. I remember how confusing this was to me when I first started learning. Push through and do every task, and use google to figure out what you can. If you tinker enough and not move too fast, this will all start making sense. I also encourage you to look at the help sections when you reach level 13ish and above. Going back and helping people with questions like this really cemented the ideas for me and I have learned more by reading other people's code than by reading the lessons.
0
MP
15 January 2019, 19:56
Thank you, I really appreciate it!
Yeah some things are really confusing, I haven't get the around the whole method, class and object thing that is a little confusing. But I make the exercises and google it, hopefully I will understand it in near future :)
0