Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

1l for long, 1f for float, 1d for double, what about byte?

1l for long, 1f for float, 1d for double, what about byte?

long l = 1l;
float f = 1f;
double d = 1d;
// byte b = 1?;

What's the equivalent for byte? Does it exist?

like image 248
sp00m Avatar asked Mar 29 '13 10:03

sp00m


People also ask

What is 1d in double Java?

It's a double literal.

What does 0d mean in Java?

According to the language specification, 0.0 is the same as 0.0d. JLS Section 3.10.2 describes this as follows: A floating-point literal is of type float if it is suffixed with an ASCII letter F or f; otherwise its type is double and it can optionally be suffixed with an ASCII letter D or d.

What does the D mean after a number Java?

D stands for double. F for float. you can read up on the basic primitive types of java here.

What is D number Java?

It can represent a number as small as 4.9 x 10-324 and as big as 1.7 x 10308 in magnitude. It could be positive or negative. All real numbers are called double literals. A double literal may optionally end with d or D, for example, 1.27d.


2 Answers

No, there is no suffix you can append to a numeric literal to make it a byte.

See 3.10 Literals in the Java Language Specification.

like image 147
Jesper Avatar answered Nov 12 '22 03:11

Jesper


You need to cast to byte like this:

byte b = 1;

b = (byte) 5;

Since by default these numeric constant are treated as int in Java.

like image 43
anubhava Avatar answered Nov 12 '22 05:11

anubhava