Java Numerical Constant Operation of Three Binary Systems
- 2021-08-28 19:58:32
- OfStack
I won't talk too much, let's just look at the code ~
package cn.nxl2018;
class Test{
//10 Binary constant assignment
void decimals(){
byte b=10;
short s=10;
char ch=69;
int i=10;
long l=10l;//l/L Can be added or not
float f=10.1f;//float f=10, You can not add it f Will 10 The default is converted to float Type, output 10.0 . But when it is a decimal, it must be added f .
float f2=10.2e3f;// It can be expressed in exponential form
double d=10.1;// The decimal constant defaults to double Type
double d1=10.01d;//d Can be omitted
}
//8 Binary constant assignment
void octa(){
byte b=03;
short s=03;
char ch=076;
int i=03;
long l=03;
float f=03.1f;// Non 8 Binary number, equivalent to 3.1f
double d=03.1d;// Feifei 8 Binary number, equivalent to 3.1d
// Which means floating-point numbers cannot represent 8 Binary number
}
//106 Binary constant assignment
static void hex(){
byte b=0x12;
short s=0x12;
char ch=0x78;
int i=0x12;
long l=0x12;
float f=0x1.2p2f;//106 Binary use p Indicates an index, not e; In addition 106 Binary floating-point numbers must be expressed in exponential form
double d=0x1.2p2d;
//java Not provided in 2 Representation of binary constant
// If you assign a negative value to a variable, 10 If you add a minus sign directly, 106 The binary is directly set to the highest bit 1
}
}
Supplement: Java Integer Constant Binary Representation
Java provides four representations for integer constants
Binary (0 1), beginning with 0b, for example: 0b10000 for 16
Octal (0 ~ 7), beginning with 0, for example: 020 for 16
The decimal system (0 ~ 9) is used in our life. For example, 16 means 16
Binary 106 (0 ~ 9, a ~ f), beginning with 0x, e.g. 0x10 for 16