The basic types in Java include:Byte,Short,Int,Long,Float,Double,CharAndBoolean.
They can be divided into four types: integer, floating point, Char, and Boolean.
Integer:Byte,Short,Int,LongSpace occupied by 1, 2, 4, and 8 bytes respectively;
Floating point type:Long,Float4 and 8 bytes respectively;
Char type:CharOccupies 2 bytes;
Boolean Type:BooleanOccupies 1 digit.
In Java, binary numbers cannot be directly represented, but can be indirectly expressed in octal or hexadecimal notation. How do these numeric types represent in the computer? Through the following code, you can see a rough idea. For more information, see the articles "how to express numbers in computers" on my blog, in fact, this code is mainly used in combination with those articles.
As for the char type, we will introduce it separately later, because the char type in Java is unique and occupies two bytes of space. Boolean does not need to be introduced, but remember that unlike C, the Boolean Type and integer in Java do not have to be converted between them.
Public classPrimitivetype{
Public static void main (string [] ARGs ){
// The integer number calculation in Java uses the complement code. values can be directly assigned to the integer number in hexadecimal or octal format.
// Directly use the binary number
// Byte
Byte maxbyte = 0x7f;
System. Out. println ("Maximum byte: 0x7fTrue Value: "+ maxbyte );
Byte minbyte = (byte) 0x80;
System. Out. println ("minimum byte: 0x80True Value: "+ minbyte );
// Short
Short maxshort = 0x7fff;
System. Out. println ("maximum short: 0x7fffTrue Value: "+ maxshort );
Sh ort minshort = (short) 0x8000;
System. Out. println ("minimum short: 0x8000True Value: "+ minshort );
// Int
Int maxint = 0x7fffffff;
System. Out. println ("Maximum INT: 0x7fffffffTrue Value: "+ maxint );
Int MININT = 0x80000000;
System. Out. println ("minimum INT: 0x80000000True Value: "+ MININT );
// Long
System. Out. println ("Maximum long: 0x7fffffffffffffffTrue Value: "+
Long. Decode ("0x7fffffffffffff "));
System. Out. println ("minimum long: 0x8000000000000000True Value: "+
Long. Decode ("-0x8000000000000000 "));
// It is a bug. It indicates that it is already a signed number.
// In Java, the direct decimal number is of the double type by default. If the float type is defined, the letter F must be added after the decimal number. The case is not limited.
Float F = 0.1f;
Double D = 0.1d; // defines double. You can add d after decimal places. The case is not limited.
// --------------------- Floating point calculation in Java follows the ieee754 standard -------------------------//
// Unlike the integer number, floating point numbers cannot be defined in the hexadecimal or octal format in Java.
// The maximum positive number (0 11111110 11111111111111111111111) = (0x7f7fffff)
Float maxpositivenumber = float. intbitstofloat (0x7f7fffff );
System. Out. println ("maximum positive single precision floating point: 0x7f7fffffTrue Value: "+ maxpositivenumber );
// Minimum positive number (0 00000001 00000000000000000000000) = (0x00800000)
Float minpositivenumber = float. intbitstofloat (0x00800000 );
System. Out. println ("Minimum Positive precision floating point: 0x00800000True Value: "+ minpositivenumber );
// 0, ieee754 requires E = 0 m = 0, then the value is 0, (0 00000000 00000000000000000000000) = (0x00000000)
Float positovezero = float. intbitstofloat (0x00000000 );
System. Out. println ("positive zero:" + positovezero );
Float negativezero = float. intbitstofloat (0x80000000 );
System. Out. println ("negative zero:" + negativezero );
// Maximum negative number (1 00000001 00000000000000000000000) = (0x80800000)
Float maxnegativenumber = float. intbitstofloat (0x80800000 );
System. Out. println ("maximum negative single precision floating point: 0x80800000True Value: "+ maxnegativenumber );
// Minimum negative value (1 11111110 11111111111111111111111) = (0xff7fffff)
Float minnegativenumber = float. intbitstofloat (0xff7fffff );
System. Out. println ("minimum negative single precision floating point: 0xff7fffffTrue Value: "+ minnegativenumber );
// Nan, non-numeric value, jdk api doc also mentioned above
System. Out. println ("Nan: E = 255 M0Example: 0x3f800001Running result: "+
Float. intbitstofloat (0x7f800001 ));
// Positive_infinity, positive infinity
System. Out. println ("positive_infinity: S = 0 e = 255 m = 00x7f800000Running result: "+
Float. intbitstofloat (0x7f800000 ));
// Negative_infinity, negative infinity
System. Out. println ("negative_infinity: S = 0 e = 255 m = 00xff800000Running result: "+
Float. intbitstofloat (0xff800000 ));
// Double-precision floating-point numbers are also available
// Discuss the char type below
Char [] codeunits;
// Basic multi-language level basic multilingual plane
// Asc2 code
For (INT I = 0; I Codeunits = character. tochars (I );
If (codeunits. Length = 1 ){
System. Out. println (I + "" + (char) I );
}
}
// Chinese Character 'strict'
Char Yan = '/u4e25 ';
Codeunits = character. tochars (0x4e25 );
System. Out. printf ("Chinese Character:" + Yan +"Length of code units occupied by code points "+ codeunits. Length +
"(0x % x), which belongs to the basic multilingual level. /N ", (INT) codeunits [0]);
// Auxiliary character supplementary character
// Code Point 0x1d56b
Codeunits = character. tochars (0x1d56b );
// Determine the level and level of the Code Unit
System. Out. printf ("Code Point 0x % x is broken down into two code units in the UTF-16 representation 0x % x 0x % x/N ",
0x105600, (INT) codeunits [0], (INT) codeunits [1]);
System. Out. printf ("0x % x is highsurrogate:" + character. ishighsurrogate (codeunits [0]) +
"/N", (INT) codeunits [0]);
System. Out. printf ("0x % x is highsurrogate:" + character. ishighsurrogate (codeunits [1]) +
"/N", (INT) codeunits [1]);
System. Out. printf ("0x % x is lowsurrogate:" + character. islowsurrogate (codeunits [0]) + "/N ",
(INT) codeunits [0]);
System. Out. printf ("0x % x is lowsurrogate:" + character. islowsurrogate (codeunits [1]) + "/N ",
(INT) codeunits [1]);
}
}