• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
  • paul wheaton
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Rob Spoor
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:

float contrast

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



1->OK
2->OK o/p =10.000000
3->java.util.IllegalFormatConversionException: f != java.lang.Integer
4->OK o/p =10.000000
5->Not OK (Type mismatch: cannot convert from double to float-No issue)

The question is: while 1 compiles, 5 doesn't. Ok. But while 3 causes exception, 4 using 10.0 does fine. Why this contrast between normal float usage and in format()/printf() ? I mean 10.0 acceptable in one case, while the same is not acceptable in other.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sweta Pillai:



1->OK
2->OK o/p =10.000000
3->java.util.IllegalFormatConversionException: f != java.lang.Integer
4->OK o/p =10.000000
5->Not OK (Type mismatch: cannot convert from double to float-No issue)

The question is: while 1 compiles, 5 doesn't. Ok. But while 3 causes exception, 4 using 10.0 does fine. Why this contrast between normal float usage and in format()/printf() ? I mean 10.0 acceptable in one case, while the same is not acceptable in other.



1 compiles because in Java, an int, which is the default type of 10, can be automatically promoted to a float.

However, 5 does not compile, because a double, which is the default type of 10.0, cannot be assigned to a float without an explicit cast.

In the format string, %f means a floating point number. So in 3, System.out.printf is expecting a float or a double. Since the type of the parameters of System.format are Object, primitives will be boxed to their corresponding wrapper classes. There is no relationship between Integer and Float or Integer and Double.

In 4, 10.0 is a floating point number so System.out.printf with a %f in the format string will accept 10.0.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats correct as Keith Lynn said.

When you simply assign an integer value to a floating point variable as



the integer value automatically gets promoted to float. Thats why you don't get any error on line 1 in your program.

Whereas, when you assign a value with a precision (decimal point), it means by default its of type double. So it CANNOT be assigned to a floating point variable which is of less size, without an explicit cast. Thats why you get an error on line 5 in your program.

You need to assign the value as



Otherwise, the compiler would throw an error saying that "possible loss of precision".

HtH.
 
Sweta Pillai
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks! It's pretty clear now.
 
Don't sweat petty things, or pet sweaty things. But cuddle this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic