Tiemen
-
Posts
6 -
Joined
-
Last visited
Content Type
Profiles
Forums
Events
Posts posted by Tiemen
-
-
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 } }
-
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.559999999999999ThirdWhy 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.21Example in tutorial.int myMethod(int x) float myMethod(float x) double myMethod(double x, double y)
Thank you -
Haha, ok. Thank you. I was afraid it was something simple.
-
I tried it in Eclipse, that seems to work.
-
Tutorial Java Method Overloading
in Java/JSP/J2EE
Posted
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.