Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between 0.9F and 0.9 in float declaration

 
Hardik Trivedi
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all genius Javans out there..

I am a beginner in java...Please give me the difference between

Float a=new Float(0.5F);
and
Float a=new Float(0.5);
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The numeric literal 0.5 is actually a double which is downcasted, whereas 0.5F is explicitly a float.
For example you cannot say float f = 0.5; as 0.5 is double and cannot be assigned to a float.
 
Rajeev Trikha
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Incidentally I should have mentioned that the Float wrapper class has three constructors. It can accept float, double or string in the argument.

You can also say

Float f = new Float("4.5");
System.out.println(f+5);

and see 9.5 coming out in the output.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic