MasterGberry
MasterGberry

Reputation: 2860

Basic Java Syntax Questions

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.

Upvotes: 1

Views: 946

Answers (6)

paxdiablo
paxdiablo

Reputation: 882646

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;

Upvotes: 2

jack_carver
jack_carver

Reputation: 1526

  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.

Upvotes: 0

joey rohan
joey rohan

Reputation: 3566

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.

Upvotes: 0

varma
varma

Reputation: 11

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

Upvotes: 0

Amadan
Amadan

Reputation: 198526

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.

Upvotes: 0

PurkkaKoodari
PurkkaKoodari

Reputation: 6809

  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.

Upvotes: 1

Related Questions