Overloaded function throws an error for float arguments - java

This is my Java program code. I overloaded the add function for the data types int and float, but the call add(2.3, 2.4) throws an error, instead of calling add(float, float).
public class Main {
public static void main(String[] args) {
// This calls add(int, int) as expected
System.out.println(add(2,4));
// This call throws an error
System.out.println(add(2.3,3.4));
}
public static int add(int a, int b){
return (a + b);
}
public static float add(float a, float b){
return (a + b);
}
}

You defined the overloaded methods correctly!
What you got wrong is the way you call the methods. You are calling add(2.3,3.4). 2.3 and 3.4 are all doubles. That's why they can't be directly put into a method that accepts floats.
"What? Why are they doubles?" you might ask.
By default, all number literals without a . or e are considered to be ints. And all number literals that has either or both a . or e are considered to be doubles.
To create a float literal, add f to the end of the numbers. i.e. these are all floats:
1f
1000f
1.1f
-9f
1e99f
So you should call your method like this
add(2.3f,3.4f)

There are add methods for int and float, while literal 2.3 has type double. There two ways to fix this:
Use float literals, i.e. 2.3f and 3.4f (notice the f suffix).
Define add method for doubles.

Related

How would I use a wrapper class to call methods within a main?

public static void main(String[] args) {
Double ans1 = add(5, 9);
Number ans2 = add(new Integer(3), new Double(2.4));
double ans3 = add(10, 3.2);
System.out.println(answer1 + " " + answer2 + " " + answer3);
}
Given the main method above, how would I create a static method (as opposed a set of overloaded methods) which in turn would could successfully call the methods within the main?
Thanks in advance!
write a single static method (not a set of overloaded methods) which
can be called successfully by all the methods in main.
It is very basic, you can simply do that by creating a new static add() method (a single method contains the code for all calls to different overloaded add methods) as below:
public static void main(String[] args) {
add();
}
public static void add() {
Double ans1 = add(5, 9);
Number ans2 = add(new Integer(3), new Double(2.4));
double ans3 = add(10, 3.2);
System.out.println(answer1 + " " + answer2 + " " + answer3);
}
OK. I think I understand what your teacher wants, despite the wrong terminology he's using. He wants you to write a unique static add() method. And the code should compile and run after this unique add() method is being added to the code.
Since the method is called with arguments that are of type int, Integer, Double and double, the argument types must be a parent class of all these types (after boxing/unboxing). For example Number, or Object.
Since the returned value is assigned to a variable of type Double, Number and double, the return type must be a common class or subclass subclass of all this (after boxing/unboxing).
A candidate would thus be, for example:
private static double add(Object i, Object i1) {
return 0;
}
Of course, the ansX variables need to be renamed to answerX, too, otherwise the code will never compile.
I guess we can also safely assume that add() should add the arguments. I leave that as an exercise for your.
In order for the add method to be called successfully by the main method, you need to figure out what types to use for the return value and the parameters.
Here's a template:
private static ??? add(??? a, ??? b) {
return a + b;
}
As you can see, main calls add with all kinds of arguments, and it stores the result in different types of variables as well.
If you analyze the main method, you will see that the add method must be able to accept the following types of parameters:
(int, int)
(Integer, Double)
(int, double)
It can be deduced that an int, a double, an Integer and a Double can be implicitly converted to the parameters' type. If you do a little bit of thinking, you will know that double is one of the types that satisfy this requirement. This means that the parameters can be of tyoe double!
And it must be able to return a value that's compatible with the following types:
Double
double
Number
It can be deduced that the return type must be able to be implictly converted to Double, double, or Number. double also satisfy this requirement. This means that the method returns a double as well!
Here's the completed method:
private static double add(double a, double b) {
return a + b;
}

The method is ambiguous for the type Error

I am trying to understand how Overloading in JAVA works and trying to get grasp of various overloading rules that are applied in case of widening, autoboxing and varargs in JAVA. I am not able to understand what is happening in the following scenario:
package package1;
public class JustAClass {
public static void add(int a, long b) {
System.out.println("all primitives");
}
//public static void add(Integer a, long b) {
// System.out.println("Wraper int, primitive long");
//}
public static void add(int a, Long b) {
System.out.println("Primitive int, Wrapper long");
}
public static void add(Integer a, Long b){
System.out.println("All wrapper");
}
public static void main(String[] args) {
int a = 10;
Integer b = 10;
long c = 9;
Long d = 9l;
add(a,c);
add(a,d);
add(b,c);
add(b,d);
}
}
At this point, I get a compilation error at the third invocation of the add method saying The method is ambiguous for the type Error .
Why is this so? What are the rules for determining which invocation of method will work? What is exactly happening in the following case?
I feel that fourth overloaded add method should work. Please help me understand the concept behind this.
There are 3 stages to method overloading resolution. The first stage doesn't do auto-boxing/unboxing, which means methods that require boxing/unboxing of the passed parameters in order to match one of the overloaded versions of add will only be considered if no match was found that doesn't require boxing/unboxing. That's why 3 of your calls, which have a single exact match, work. Regarding add(b,c);, see below why it's ambiguous.
add(a,c); // exact match to add(int a, long b)
add(a,d); // exact match to add(int a, Long b)
add(b,c); // there is no exact match, so at least one of the passed parameters must
// be boxed or unboxed. However, by unboxing b to int or boxing
// c to Long, each of the three add methods can match, and the
// compiler doesn't know which one to prefer
add(b,d); // exact match to add(Integer a, Long b)

The output of the following program is 1,3,3 can someone explain it how?

The output of the following program is 1,3,3 can someone explain it how?
will it consider 10.25 as a object to the method argument?
public class Test {
void methodOfTest(int i) {
System.out.println(1);
}
void methodOfTest(Integer I) {
System.out.println(2);
}
void methodOfTest(Object o) {
System.out.println(3);
}
public static void main(String[] args) {
Test t = new Test();
t.methodOfTest(10);
t.methodOfTest(10.25);
t.methodOfTest(new Double("25.25"));
}
}
t.methodOfTest(10);
10 is interpreted as int literal, so methodOfTest(int i) is called
t.methodOfTest(10.25);
There is no method, that takes a double, so the only method where 10.25 fits in is methodOfTest(Object o)
t.methodOfTest(new Double("25.25"));
Here we have a Double object, but again, no method is found that takes a Double, so the only method that takes this is again methodOfTest(Object o).
Therefore your output is 1,3,3.
It does not consider 10.25 to be an object. It does consider it to be assignment-convertible to an Object reference, by boxing conversion to a Double reference followed by widening reference conversion.
The boxing answers are absolutely correct.
I just wanted to add this: you were perhaps expecting an automatic (implicit) conversion from Double to Integer, but such a conversion must be declared explicitly (casting Double to Integer).
Without an explicit conversion, your Double value is interpreted as an Object (since you provided that method overload), which is the base class of about everything.
Hope this helps ;)
Because 10.25 can be converted to a Double. (boxing into Double)
So the constructor of Double will be called with 10.25 as parameter and will be passed to the method : void methodOfTest(Object o).

Overloading methods

I saw below question posted on this site.
"What happens when we pass int arguments to the overloading method having float as a parameter for one method and another having double param".
I thought I understood the concept and wrote this code:
public class TestClass {
public static void main(String args[])
{
TestClass t=new TestClass();
t.sum(1/4);
}
void sum(double d)
{
System.out.println("Double==="+d);
}
void sum(int i)
{
System.out.println("Integer==="+i);
}
void sum(short s)
{
System.out.println("Short==="+d);
}
}
According to my understanding explained on this site (as mentioned above), I thought it will print Short===0, but to my surprise it prints Integer===0. Can any one explain this to me?
First of all, these are overloaded methods, not overridden methods.
1 and 4 are integers. Therefore 1/4 is an integer division, returning 0.
Therefore, the method being called is sum(int i).
sum(short s) would never be called for an int parameter, since that would require a narrowing primitive conversion (JLS 5.1.3), that may cause data loss, and is not allowed in method invocation conversion (JLS 5.3). Such a conversion can be done with an explicit cast.
If you remove the int version, sum(double d) would be called, and if you remove the double version, the code won't compile.
In order to call the short version, you must cast the parameter to short :
t.sum ((short)(1/4));
If you don't explicitly tell the compiler what are the types of 1 and 4, it assumes they are of type int. Then, / operator will apply integer division and will produce another int (which will be 0.)
After that, the method with the most specific to integer parameter type will be invoked. In your case, this will be sum(int i).
If you want to invoke some of the other overloaded methods, you will have to explicitly:
do a cast. For example, sum((short) (1/4)); will invoke sum(short s) due to the cast.
point the type of the operands. For example, sum(1d/4) will invoke sum(double d), since 1d/4 will result to double
For integer number, the type int is a default choice. So, although 1 and 4 can be defined as both int or short, since you did not defined anything, the compiler identified 1 and 4 as int and therefore it entered into the function for 1/4 division (0), which took the parameter int.

Incorrect Casting

For some reason I am getting a precision error when I try to compile my code. The precision error comes in the return of my second method where I am trying to calculate the circumference. What am I doing incorrectly?
public class Methods2
{
public static final double PI = 3.14;
public static double calcCirc(double x)
{
return PI*x;
}
public static int calcCirc(int x)
{
return (2*(double) x)*PI;
}
public static void main(String[]args)
{
System.out.println(calcCirc(10.2));
System.out.println(calcCirc(4));
}
}
You are attempting to return a double value in a method declared to return an int. Java won't let you implicitly narrow your value like that.
If you're okay with the loss of precision, then explicitly cast the value to int -- Java will let you do that.
return (int) ((2*(double) x)*PI);
However, I would change the method to return double instead, not to lose precision:
public static double calcCirc(int x)
... as you already did with your other calcCirc method.
Both versions of calcCirc() ought to return doubles.
Also, side note--consider using different method names since they accept inputs that differ not only in type but also in semantics.
E.g. calcCircFromRadius(double radius), calcCircFromDiameter(double diameter). There's not really a reason to take an int as an input type here since Java will automatically cast ints to doubles for you.
try
public static int calcCirc(int x){
return (int)((2*x)*PI);
}

Categories