Tiemen Posted October 29, 2020 Share Posted October 29, 2020 So in the example below they want to give a example that multiple methods can have the same name with different parameters: I have a few questions: I thought variables had unique names, how does it knows we talk about the same x + y. I assume the variables are only defined within the method then, right? And not within the whole public class MyClass Second: Why does the double return the value 10.5599999999999 instead of 10.56 If I edit 6.26 to 6.27 it will return 10.57 public class MyClass { static int plusMethodInt(int x, int y) { return x + y; } static double plusMethodDouble(double x, double y) { return x + y; } public static void main(String[] args) { int myNum1 = plusMethodInt(8, 5); double myNum2 = plusMethodDouble(4.3, 6.26); System.out.println("int: " + myNum1); System.out.println("double: " + myNum2); } } Result: int: 13 double: 10.559999999999999 Third Why call the method the same if your using different types of variables. Will those variables called x be usable if you call on x and there is a value int 2 and float 4.21 Example in tutorial. int myMethod(int x) float myMethod(float x) double myMethod(double x, double y) Thank you Link to comment Share on other sites More sharing options...
Tiemen Posted October 29, 2020 Author Share Posted October 29, 2020 This code below clarifies some part of my confusion in the first part of my question. But the second and 3th question remains. public class MyClass { public static void main(String[] args) { // Code here CANNOT use x { // This is a block // Code here CANNOT use x int x = 100; // Code here CAN use x System.out.println(x); } // The block ends here // Code here CANNOT use x } } Link to comment Share on other sites More sharing options...
Funce Posted October 29, 2020 Share Posted October 29, 2020 To answer your float/double question, decimal numbers don't have infinite accuracy due to their representation in binary. As you can see you're 0.000000000001 out due to that. This is usually why choosing your data type and rounding appropriately are important. Different data types have different accuracies. Your third question seems like there's something else underneath which you aren't asking. I'll answer it as generally as possible. Method Overloading like that is used for when you want to use one function for multiple different data types, or different amounts of them, and you require it to be handled differently per case. Easiest way to see it in action is the '+' operator. String x = "32" + "4"; System.out.println(x); // Outputs 324 int y = 2 + 4; System.out.println(y); // Outputs 6 Two strings concatenate, two numbers add together. With an overload for every combination of data types that "+" can support. Link to comment Share on other sites More sharing options...
Tiemen Posted October 31, 2020 Author Share Posted October 31, 2020 Thank you for taking the time to explain. The Method overload is not completely clear, but I have a feeling I'll get it later when I just do the rest of the tutorial and exercises. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now