1

I am doing a homework assignment (I am not one to lie) and I am honestly stumped at the questions. I have 3 years of programming experience but am stumped on some of the simplest problems in the class, lol. New to Java, not new to programming rather. I would like to discuss why the questions result in an answer and why it is one way or another.

1) Write 3.4, is this a double or a float?

I want to say its a float by default, as it takes up less space (32 bits) versus a double (64) bits. Since 3.4 is a small number, I would assume it is a float?

2) Declare x as a double and assign it the value of 3.4 (as a double).

I think it is double x = 3.4d; not 100% sure on this

3) Declare y as a float and assign it the value 3.4 (as a float).

Similar to above, i think it is float y = 3.4f;

Not usually one to come on here and ask for HW help in this manner...but I have no textbook for another week as I just ordered it. I would rather like to discuss why it is one way or another so I can get a better understanding of how Java works. Thanks.

1

6 Answers 6

2

On the first question, 3.4 is a double. If you wanted a float literal, you would use 3.4f.


Second question:

double x = 3.4; // trailing D/d is optional and rarely used.

Third question, you're correct, declare it as float and use the f suffix on the literal.


See here for more details, specifically the section entitled Floating-Point Literals, copied here, with minor modifications for emphasis:

A floating-point literal is of type float if it ends with the letter F or f; otherwise its type is double and it can optionally end with the letter D or d.

The floating point types (float and double) can also be expressed using E or e (for scientific notation), F or f (32-bit float literal) and D or d (64-bit double literal; this is the default and by convention is omitted).

double d1 = 123.4;
double d2 = 1.234e2; // same value as d1, but in scientific notation
float f1  = 123.4f;
Sign up to request clarification or add additional context in comments.

2 Comments

Why does Java choose to use double by default instead of float?
Because that's what the specs say. C and C++ do a similar thing. To be brutally honest, floats were useful back in the olden days but, unless you have a real pressing need for smaller range/storage, you should be using doubles.
1
  1. 3.4 is a double, put a f after it to get a float.
  2. Correct, but remember to put a ; in the end of the line.
  3. Same as above.

2 Comments

Derp on the ;. I'm curious, why are they doubles by default?
Floats are not as accurate, they want you to specify that you don't need the higher accuracy.
0

Java float literals are double by default, just as if you specified d modifier. If you want 32-bit floats, you have to explicitly state the f modifier.

Comments

0

if it is float means you have to declare like this float y = 3.4*f* .f indicates to jvm it is a float.if u forget that f (3.4f) it will treat as double by default and it takes 64 bit

Comments

0

From your comment:

I'm curious, why are they doubles by default

(IMHO) I think high precision is better than low precision. Java (same thing in C as well) can downconvert to float when you try to, so this thing doesn't cause problems.

Comments

0
  1. By Default java considers float literals as double. Hence 3.4 is double.
  2. double x = 3.4;
  3. What you have written is correct.

2 Comments

Just one nitpick, jack, decimal refers to the base (10), so 3 is as much decimal as 3.4. I suspect you meant to say floating point rather than decimal. Not that it detracts from your answer, it's just that sometimes I feel rather anal retentive :-)
Yeah makes sense, fixed :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.