1

I cannot seem to figure out why I can convert a float to an int doing the following:

float a = 3;
int b = (int)a;

but when I try the following:

public class MyTestCode{
    public static int Add(Object a, Object b){
        int c = (int)a;
        int d = (int)b;
        return c + d;
    }
}

...it gives the following error:

*Exception in thread "main" java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.Integer
at myTestCode.MyTestCode.Add(MyTestCode.java:15)
at ch02.ex01.Ch02Ex01.main(Ch02Ex01.java:25)
Java Result: 1*

Why can I convert from float to int in the one example, but not the other?

Alan Pauley
  • 121
  • 1
  • 10
  • what part of `java.lang.Float cannot be cast to java.lang.Integer` is not clear. `java.lang.Float` != float` –  Apr 24 '16 at 01:24
  • That makes sense but why is it converting my float to a Float? I initialized it as float and input that into the method so was not expecting it to convert it to Float. – Alan Pauley Apr 24 '16 at 01:42

1 Answers1

6

This is because in one case you have a primitive float and in the other you have a java.lang.Float object. To convert that object to a float, something like this should work:

public static int add(Object a, Object b){
    float c = (Float)a;
    float d = (Float)b;
    return (int)(c + d);
}

Casting back to the primitive float first should solve the issue.

nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • I tried this and it did not work. Not sure if I'm missing something: public class MyTestCode { public static int Add(Object a, Object b){ float c = (Float)a; float d = (Float)b; return (int)(c + d); } } – Alan Pauley Apr 24 '16 at 01:27
  • 1
    @Alan What error do you receive?. Also please start method names with lowercase. – uncaught_exception Apr 24 '16 at 01:34
  • 1
    @AlanPauley Tried it on my machine. Worked perfectly, assuming I passed it two floats. Just while we're on this topic, why do you need the method signature to take Object if you are always passing a float? – nhouser9 Apr 24 '16 at 01:42
  • Hmm I'll have to try again once I get home. Wasn't working for me but I'll look closer. As for the why it's because it won't always be a float. Sometimes it'll be an int or a double, etc. trying to make a catch all. I'm new to programming so that may be a bad way to handle it but that was my goal anyways. Didn't want to write X number of methods to account for every possibility. – Alan Pauley Apr 24 '16 at 01:53
  • @AlanPauley This code will still give error if passed something other than Float - that's probably your issue. You could do some kind of typechecking to handle different cases, like `if (Float.isInstance(a))` then handle the float case, `if (Integer.isInstance(a))` then handle the int case, etc. – nhouser9 Apr 24 '16 at 02:51
  • @AlanPauley No problem - want to join a chatroom to discuss more? http://chat.stackoverflow.com/rooms/110037/generic-math – nhouser9 Apr 24 '16 at 03:03
  • @nhouser9 you are correct. Is there not a more efficient way to do this than check for every possible type? That's what I was trying to avoid in the first place. :( – Alan Pauley Apr 24 '16 at 03:03
  • @nhouser9 looks like I don't have enough StackOverflow reputation to do a chat. :( doh! One issue after another. I appreciate your offer to help though, friend. – Alan Pauley Apr 24 '16 at 03:05
  • @AlanPauley Yeah unfortunately there is no truly generic way. I have been researching to try to find something for you, but you can see from this question (http://stackoverflow.com/questions/8669838/java-generics-and-adding-numbers-together) that it just doesn't seem to be possible unless you provide custom logic for handling arithmetic operations for different types. – nhouser9 Apr 24 '16 at 03:12
  • @nhouser9 no worries, this was all very helpful and informative. Thank you very much for all of your help. I'll continue studying and learning. Best of luck to you! – Alan Pauley Apr 24 '16 at 03:19