Java Basic data Types
A variable is a request for memory to store a value. In other words, when creating a variable, you need to request space in memory.
The memory management system allocates storage space for variables based on the type of the variable, and the allocated space can only be used to store that type of data.
Therefore, you can store integers, decimals, or characters in memory by defining variables of different types.
Two big data types for Java:
- Built-in data types
- Reference data type
built-in data types
The Java language provides eight basic types. Six types of numbers (four integers, two floating-point types), one character type, and one Boolean type.
Byte
- The byte data type is a 8-bit, signed, integer in binary complement notation;
- the minimum value is -128 ( -2^7);
- The maximum value is 127 (2^7-1);
- The default value is 0;
- The byte type is used to save space in large arrays, mainly in place of integers, because the byte variable occupies only one-fourth of the int type;
- Example:byte a = 100,byte B =-50.
Short
- The short data type is a 16-bit, signed integer with a binary complement representation
- the minimum value is -32768 ( -2^15);
- The maximum value is 32767 (2^15-1);
- The short data type can also save space as Byte. A short variable is one-second of the space occupied by the int variable;
- The default value is 0;
- Example:short s = 1000,short r =-20000.
Int:
- The int data type is a 32-bit, signed integer with a binary complement representation;
- the minimum value is -2,147,483,648 ( -2^31);
- The maximum value is 2,147,483,647 (2^31-1);
- the generic integer variable defaults to the int type;
- The default value is 0;
- Example:int a = 100000, int b =-200000.
Long
- The Long data type is a 64-bit, signed integer with a binary complement representation;
- the minimum value is -9,223,372,036,854,775,808 ( -2^63);
- The maximum value is 9,223,372,036,854,775,807 (2^63-1);
- This type is mainly used on systems that need to compare large integers;
- The default value is 0L;
- Example: long a = 100000l,long B = -200000l.
Float
- The float data type is a single-precision, 32-bit, IEEE 754 compliant floating-point number;
- Float saves memory space when storing large floating-point groups;
- The default value is 0.0f;
- Floating-point numbers cannot be used to denote precise values, such as currency;
- Example:float f1 = 234.5f.
Double
- The double data type is a dual-precision, 64-bit, IEEE 754-compliant floating-point number;
- the default type of floating-point number is double type;
- A double type cannot also represent an exact value, such as a currency;
- The default value is 0.0d;
- Example:double d1 = 123.4.
Boolean
- The Boolean data type represents one bit of information;
- only two values:true and false;
- This type is only used as a sign to record the true/false situation;
- The default value is false;
- Example:boolean one = True.
Char
- The char type is a single 16-bit Unicode character;
- the minimum value is ' \u0000 ' (that is, 0);
- The maximum value is ' \uffff ' (that is, 65,535);
- Char data type can store any character;
- Example:char letter = ' A '.
- reference type variables are created by the constructors of the classes and can be used to access the referenced objects. These variables are specified at the time of declaration as a specific type, such as Employee, Pubby, and so on. Once a variable is declared, the type cannot be changed.
- objects, arrays are reference data types.
- the default value for all reference types is null.
- A reference variable can be used to refer to any type that is compatible with it.
- Example: Animal Animal = new Animal ("Giraffe").
when we remember their order from small to large, and then from small to large to remember their digits, it is easy to remember their respective maximum and minimum value.
In fact,There is another primitive type Void in Java, which also has a corresponding wrapper class java.lang.Void, but we cannot manipulate them directly.
Reference type
byte, int, long, and short can all be represented in decimal, 16-binary, and 8-binary ways.
when a constant is used, the prefix 0 indicates that it is 8 binary, and the prefix 0x represents 16 binary.
int decimal = 100;
int octal = 0144;
int hexa = 0x64;
String in Java is a reference type
The difference between an underlying type and a reference type is that the underlying type represents only simple characters or numbers, and the reference type can be any complex data structure primitive type that simply represents a simple datatype. Reference types can represent complex data types, and they can also manipulate the behavior of this data type
The Java Virtual machine handles the underlying type differently from the reference type, and for the base type, the Java virtual Opportunity allocates the memory space that the data type actually occupies, and for the reference type variable, he is simply a pointer to an instance in the heap area.
Java Basic data types