-2

Ok I got a couple values here:

'F', 0xf, 070, 70L, 77e-1f, 7.7f, 7.77e1, 77.7

I know that 'F' is a char and that 70L has the type long. Also, I know that 0xf is hex(right?) and that 070 is octal. But what are those other numbers? And why the hell is 77.7 double and not float as well?

Andrew Tobilko
  • 48,120
  • 14
  • 91
  • 142
Geddi
  • 33
  • 5
  • 1
    *Why the hell* you have included things that you already know? :) – Saurav Sahu Oct 22 '16 at 11:56
  • 1
    *"And why the hell is 77.7 double and not float as well?"* Research isn't your strength, right? https://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.2. At least you got a "pity upvote" by another bad user :). – Tom Oct 22 '16 at 11:57
  • I don't get it. So 77.7 isn't the same as 77,7? – Geddi Oct 22 '16 at 12:00
  • why the link to the JLS? https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html explains it as well, and there is even a chance that people new to Java will also understand it... – user140547 Oct 22 '16 at 12:01

5 Answers5

1

What you're looking for is this

  • About 77.7:

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.

  • 0xf is not of type hex. There is no type hex. It's simply an int wirtten in hex. Just like 070 is an int written in octal.

  • The literal 77e-1fis clearly a float since it ends with f.

  • The e is exponent i.e. 77e-1f is in fact 77 * 10^(-1) or 7.7. The literal 7.77e1 is a double for the same reason 77.7 is a double, it's just that 7.77e1 is equal to 7.77 * 10 ^ 1 = 77.7.

Malt
  • 28,965
  • 9
  • 65
  • 105
0

The only two which you didn't explain yourself:

7.7f

That f means float - it indicates that this should not be a double literal, but well, a float one! You see, by default any "floating point" literals are automatically of type double, you need the "f" in order to enforce the "smaller" float type.

And

77e-1f 

is using scientific notation, see here for example for further explanations.

Community
  • 1
  • 1
GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

And why the hell is 77.7 double and not float as well?

Float numbers has to be denoted with f on the end but double numbers don't require d on the end.

77e-1f is a float number 77 to the power of 10^-1, so it equals 7.7.

The same with 7.77e1 == 77.7

Defozo
  • 2,946
  • 6
  • 32
  • 51
0

In Java numerical literals that contain a dot are of type double by default. If you want a float, you neef to append the f.

The numbers containing the e are in exponential or scientific notation, but I am not going to explain it here because it has nothing to do with programming.

Frank Puffer
  • 8,135
  • 2
  • 20
  • 45
0

77e-1 means this: 77 is number-multiplier, e (or E) means base 10, and -1 means exponent.

So the result is 77 by (10 powered by -1), or 77 by .1, i. e. 7.7

The name of this notation is scientific or logarithmic and is good for very large or very small numbers, e. g. 1 million = 1.000.000 may be expressed as 1e6

MarianD
  • 13,096
  • 12
  • 42
  • 54