Results 1 to 5 of 5
  1. #1
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default A little problem about comparing int with float

    A little problem about following code:
    Java Code:
    public class test {
    	public static void main(String[] args) {
    		int i = 10;
    		float f = 10.0f;
    		if (i == f) {
    			System.out.println("True");
    		}
    	}
    }
    The output is "True".
    My question is how can a int compare to a float?
    If can do this , how is JAVA comparing them?
    Thanks for answering
    Best Regards~

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: A little problem about comparing int with float

    If Java has to compare two primitive, but unequal, types, it casts one type to another implicitly; it always does so in a 'widening' way, i.e. no information will be lost; in this case the int is converted to a float so two floating point values can be compared.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default Re: A little problem about comparing int with float

    Quote Originally Posted by JosAH View Post
    in this case the int is converted to a float so two floating point values can be compared.
    yes, but not with == but with the method compareTo(), which will return 0 if the values are equals.

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: A little problem about comparing int with float

    Quote Originally Posted by j2me64 View Post
    yes, but not with == but with the method compareTo(), which will return 0 if the values are equals.
    Primitives (int, float etc) don't implement any methods and for that special class of wrappers (Integer, Float etc.) auto (un)boxing does the rest, i.e. you don't need any comparison methods.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    blackdiz is offline Member
    Join Date
    Apr 2012
    Posts
    16
    Rep Power
    0

    Default Re: A little problem about comparing int with float

    OK~I've got it.
    Thank your for answering~^^

Similar Threads

  1. About Float and readLine() begginer problem.
    By Kaahrl in forum New To Java
    Replies: 5
    Last Post: 02-19-2012, 12:17 PM
  2. Problem Comparing Strings (its not what you think)
    By hilather in forum New To Java
    Replies: 7
    Last Post: 11-19-2008, 06:43 PM
  3. Problem comparing three numbers
    By gammaman in forum New To Java
    Replies: 1
    Last Post: 02-09-2008, 05:43 PM
  4. Comparing null problem
    By thirdy_veritech in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 08:46 AM
  5. Comparing problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 01-24-2008, 03:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •