data types in Java
I. BASIC data Types
1. Basic data types
That is, int,char,float,double,byte, Short , Long , Boolean There are 8 types of such numeric values .
Data type |
Number of digits |
Range of values |
Description |
Byte |
8 |
-128~127 |
The most primitive form of data stored in memory |
Short |
16 |
-32768~32767 |
|
Int |
32 |
-2^31~2^31-1 |
|
Long |
64 |
-2^63~2^63-1 |
|
Float |
32 |
Slightly |
|
Double |
64 |
Slightly |
|
Char |
16 |
'\u0000'~'\uffff' |
|
Boolean |
1 |
True/false |
|
|
|
|
|
2. Conversion of basic data types
(1) automatic type conversion is also called implicit type conversion;
When you assign a variable of one type to a variable of another type, an automatic type conversion occurs whenever the following conditions are true:
Mutual compatibility between the two types;
The target type is greater than the source type (that is, type promotion).
Such as:
char ch = ' A ';
int num = ch;
float f = num;
Double d = f;
type conversions occur automatically, without explicit instructions
(2) cast type
Coercion type conversions are also known as explicit type conversions;
If the data type of the converted value is greater than its target type, some information will be lost;
Such as:
int x = 65;
char ch = x;
Such statements will cause an error because the char type is less accurate than the int type, and the compiler cannot convert it automatically, only cast:
int x = 65;
char ch = (char) x;
Ii. type of reference
1. reference type data definition
Reference type except for the base data type
include classes in the API, such as String,File
Also includes custom classes, such as PersonalAccount,creditaccount
include arrays , such as int[],string[].
2. Scenarios used by reference types
Used as a type of attribute;
Used as the return value type of the method;
Used as the form parameter type of the method;
3. assignment of reference data types
Reference types are objects, so assignments are assigned using the new call construction method.
Exception: The String class can use the = assignment directly, without using new.
Three, the difference between the basic type and the reference type
|
Basic data types |
Reference data type |
Concept |
Variable (simple value) |
Object (Reference) |
Storage location |
To store values in the stack |
Save references in stacks, store property values for specific objects in the heap |
How to assign a value |
Direct Assignment |
New ,String class and wrapper class can be directly assigned values |
Data types in Java