1 2 Previous Next 25 Replies Latest reply: Dec 14, 2006 10:11 AM by 807607 RSS

    float and == operator

    807607
      Hi,

      please:

      I have two instance of Float

      Float f1, f2 = 10f; // for example

      why f1 == f2 return false in this case?

      (f1.floatValue() == f2.floatValue()) is true

      thanks

      Y
        • 1. Re: float and == operator
          807607
          because you're using Float and not float. They have the same value, but are different objects.
          • 2. Re: float and == operator
            807607
            Float is an object. Don't use == on an object, use equals()
            • 3. Re: float and == operator
              800329
              This kind of question really makes me wonder, what they teach in Java courses nowadays ...
              • 4. Re: float and == operator
                807607
                ah sorry - my question was incomplete ...

                the problem is that I'm trying to compare two instance after unboxing - the problem is even they have same hashcodes they seems to be different - for all the other boxed types they're same

                float f1,f2 = 10f;

                =>

                in method they are seen as Float f1,f2 then

                and then f1 != f2

                for char and all the other boxed values they are same
                • 5. Re: float and == operator
                  807607
                  Hi,

                  please:

                  I have two instance of Float

                  Float f1, f2 = 10f; // for example

                  why f1 == f2 return false in this case?

                  (f1.floatValue() == f2.floatValue()) is true

                  thanks

                  Y
                  This won't even compile, since f1 is not initialized, it is null.

                  if you meant
                  Float f1 = 10f, f2 = 10f;
                  then
                            Float f1 = 10f, f2 = 10f;
                            System.out.println("f1 = " + f1);
                            System.out.println("f2 = " + f2);
                            System.out.println("f1 == f2 is " + (f1 == f2));
                            System.out.println("f1.equals(f2) is " + (f1.equals(f2)));
                            System.out.println("f1.floatValue() ==  f2.floatValue() is " + (f1.floatValue() == f2.floatValue()));
                  
                  //OUTPUT
                  
                  f1 = 10.0
                  f2 = 10.0
                  f1 == f2 is false
                  f1.equals(f2) is true
                  f1.floatValue() ==  f2.floatValue() is true
                  ~Tim
                  • 6. Re: float and == operator
                    800329
                    @Autoboxing:
                    If they are the same, that is accidental, because of some values are pre-objectified for autoboxing, especially small ones, which is barely possible for doubles.
                    • 7. Re: float and == operator
                      807607
                      @Autoboxing:
                      If they are the same, that is accidental, because of
                      some values are pre-objectified for autoboxing,
                      especially small ones, which is barely possible for
                      doubles.
                      then you can never assume the identity of objects?
                      how this exactly works - could you give me some refs to docs?
                      i mean - how they are compared internally? they are not compared just according to their hashcodes ..

                      thanks
                      Y
                      • 8. Re: float and == operator
                        807607
                        i mean - how they are compared internally? they are
                        not compared just according to their hashcodes ..
                        They are equal according to '==' if and only if they are the same object. Nothing to do with hash code. Nothing to do with the content. Just whether or not they are the same object.
                        • 9. Re: float and == operator
                          807607
                          i mean - how they are compared internally? they
                          are
                          not compared just according to their hashcodes ..
                          They are equal according to '==' if and only if they
                          are the same object. Nothing to do with hash code.
                          Nothing to do with the content. Just whether or not
                          they are the same object.
                          i understand - but how did I get the signature according the comparsion with == is done? thats my point :) sorry - im not asking clearly ...
                          • 10. Re: float and == operator
                            800329
                            You should not use == in this case, that's all we say, unless you compare primitive types or need to know its the definitely same object and not only one having an equal value.
                            • 11. Re: float and == operator
                              807607
                              i understand - but how did I get the signature
                              according the comparsion with == is done? thats my
                              point :) sorry - im not asking clearly ...
                              In
                              Float f1, f2 = 10f; // for example
                              you created two objects both containing the same value (10f). Not one object. f1 points to one object, f2 points to the other.
                              • 12. Re: float and == operator
                                807607
                                You should not use == in this case, that's all we
                                say, unless you compare primitive types or need to
                                know its the definitely same object and not only one
                                having an equal value.
                                ok - i understand what i 'SHOULD' use - but my point is to find out HOW it works ... ok ... I'll check specifications

                                thanks

                                Y
                                • 13. Re: float and == operator
                                  807607
                                  I'll check
                                  specifications
                                  Sounds good to me.
                                  • 14. Re: float and == operator
                                    807607
                                    i understand - but how did I get the signature
                                    according the comparsion with == is done? thats my
                                    point :) sorry - im not asking clearly ...
                                    In
                                    Float f1, f2 = 10f; // for example
                                    you created two objects both containing the same
                                    value (10f). Not one object. f1 points to one object,
                                    f2 points to the other.
                                    this i understand ... but how (TF) are those objects compared - I'd like to know theirs unique identifiers - if this is possible

                                    Y
                                    1 2 Previous Next