-3

case 1

float a=033.0   //shows compilation problem

case 2

double a=033.0 //works fine

Why case 1 is showing error but not case 2 or vice-versa?

phuclv
  • 37,963
  • 15
  • 156
  • 475
Stones
  • 13
  • 7
  • The issue you are describing is already answered in http://stackoverflow.com/questions/5076710/what-is-float-in-java – hotzst Feb 04 '16 at 06:53

3 Answers3

0

By default java uses double type to store floating values. since you are storing double in float (down casting) java will throw an error. it can be resolved by two ways

  1. float a=033.0f

  2. float a= (float)033.0

0

case 1----float a=033.0 //shows compilation problem

case 2----double a=033.0 //works fine

In Java, decimal number is interpreted as a double ,so converting from double to float cannot be performed automatically ,so you need to give like this : float a= 033.0f;

0

Its simply the understanding of Java Syntax. You can read the Primitive data types of java. You'll get it anywhere...

Link : https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Direct to this link, do ctrl+F, & paste this "Floating-Point Literals". You wont't waste time wandering...

For your own convenience now, you can prefer this.

float fractionNumber = 25.24F; fractionNumber = 25.24f;

double biggerFractionNum = 56.65555D;

biggerFractionNum = 56.65555;

but generally its like this all over... later you'll get used to it.

float foo = 34.4F;

double doo = 34.4; IMPLEMENT it right away ! ...experience it, understand it...& you'll never forget it. :)

Aniket
  • 303
  • 5
  • 11