Floats don't work in this program where Doubles do...why?

Why doesn't this work?

I'm using java version 1.6.0_23

I'm receiving these compiler errors

questionOne.java:9: cannot find symbol
symbol : method add(double)
location: class java.util.ArrayList
fishLengthList.add<10.0>;

questionOne.java:10: cannot find symbol
symbol : method add(double)
location: class java.util.ArrayList
fishLengthList.add(15.5);

etc.

BTW, the following code DOES work when I replace Double everywhere that the code says Float.


import java.util.ArrayList;
import java.lang.Float;
import java.lang.*;

class questionOne {
public static void main(String[] args) {
//Float fishLengthList;
ArrayList fishLengthList = new ArrayList();
fishLengthList.add(10.0);
fishLengthList.add(15.5);
fishLengthList.add(18.0);
fishLengthList.add(29.5);
fishLengthList.add(45.5);
for (Double hook : fishLengthList)
if (hook > 28){
System.out.print(hook + " ");;}

}
}

Thanks,
Terri

Comments

  • public static void main(String[] args) {

    ArrayList fishLengthList = new ArrayList();
    fishLengthList.add(10.0f); <---- mark for compiler with an f (float)
    fishLengthList.add(15.5f); <----
    fishLengthList.add(18.0f); <----
    fishLengthList.add(29.5f); <----
    fishLengthList.add(45.5f); <----
    for (Float hook : fishLengthList)
    if (hook > 28){
    System.out.print(hook + " ");}

    }
    }
    the arraylist understands that you're using floats but the floats are (by default) doubles, so, you can add an f to the number to make it realize you're using floats.
  • This post has been deleted.
  • This post has been deleted.
  • Thank you very much for this explanation, Mike. I really appreciate it.

    So I guess the guideline to take from this is that whenever you pass a number as a parameter into a method, the default value is a double unless specified otherwise. Is this correct?

    Best,
    Terri
  • In an arraylist yes
  • I've looked at the specification of the ArrayList and add method and I am unable to find anything that even suggests the rule we've been discussing. Which makes me think that this rule must apply to more than the arraylist.


    http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html

    http://download.oracle.com/javase/6/docs/api/java/util/List.html#add(int, E)
  • I don't think that's specific to the ArrayList, Mike. I just attempted to compile a simple program using floats and they required that I place an f right after the assignment value. It threw the same error when I didn't be the f after the assignment value.

    float a = 10.3; //Throws error: Possible loss of precision. Found double, required float.
    float a = 10.3f; //Does not throw error.
  • haha well right on. Just use the f's as I suggested when necessary
  • : float a = 10.3; //Throws error: Possible loss of precision. Found
    : double, required float.
    : float a = 10.3f; //Does not throw error.

    the reason for the error is: when you use decimal numbers in java then the jvm handle these decimal numbers as doubles. so when you write float a = 10.3 the jvm try to assign a double to a float which throws an error. the workaroud is: use f or F like in your example or force the jvm to cast the decimal as a float with float a = (float)10.3;

  • Thanks a lot for this response. It's good to know why.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories