Data type
There are 2 types of data in Java, let's take a look at the following:
- Basic data type;
- reference data type;
You can use a single table to record:
Basic data type integral type
- Byte:1 bytes 8 bits, the value range is: [-128, 127], the direct write value: (byte) 1;
- Short:2 bytes 16 bits, the value range is: [-32768, 32768], the direct write value: (short) 1;
- Int:4 bytes 32 bits, the value range is: [-2147483648, 2147483648], the direct write value: 1;
- Long:8 bytes 64 bits, the value range is: [-9233372036854477808, 9233372036854477808], direct write value: 1L;
- Char:2 bytes 16 bits with a range of values: [0, 65535], direct write value: (char) 1 or ' a ';
(Note that there is no unsigned type in Java, you need to extend it by using an unsigned type, you can refer to my Extension class library )
Floating point Type
- Float:4 bytes 32 bits, direct write value: 1.0F;
- Double:8 bytes 64 bits, direct write value: 1.0D;
Boolean type
- Boolean:1 bytes 8 bits with a value range of: TRUE or false;
Reference data type
- String: Type, no length limit, direct write value: "ABC";
- Java.math.BigInteger: The integer type in the base data can be used to represent arbitrary-sized integer data if it does not meet the requirements;
- Java.math.BigDecimal: Floating-point data in basic data can be used to represent arbitrary precision floating point type when it cannot meet the requirements;
- Other types: classes, interfaces, arrays, and other types;
Stack and Stack memory
Some basic types of variables and object reference variables defined in the function are allocated in the stack memory of the function. When a variable is defined in a block of code, Java allocates a memory space for the variable in the stack, and when the scope of the variable is exceeded, Java automatically frees the memory space allocated for that variable, which can be used immediately by another.
Heap Memory
Heap memory is used to hold objects and arrays created by new. The memory allocated in the heap is managed by the Java Virtual Machine automatic garbage collector.
From the heap and stack function and the role of the popular comparison, the heap is mainly used to store objects, the stack is mainly used to execute the program.
In addition, the contents of the stack memory are automatically removed at the end of the scope, and the heap memory release is done with the Java garbage collection mechanism.
Packing class
Unlike C #, everything is Object, Java, the basic data type is only data, not objects, so in order to be able to treat the base type as an object, and to connect the related methods, Java provides a wrapper class for each basic type. To handle the base data type as an object.
Everything in C # is object, so converting a number to 16 can be written like this:
254.ToString ("X"); // Output FE
But in Java, integer data is not an object, so it needs to be written like this:
New Integer (254). ToString (); // output string "254"
Packaging class functions
- Provide the maximum and minimum values for the data;
- Provides the ability to convert to other data types;
int i = 5; // New Integr (5); //
When it is necessary to put things into the arraylist,hashmap, such basic types as int,double are not put in, because the containers are loaded with object, which is required for these basic types of wrapper class.
- Original type wrapper class
- Boolean Boolean
- Char Character
- BYTE byte
- Short Short
- int Integer
- Long Long
- float float
- Double Double
Variable
Define variables in Java in the following notation:
int a = 0; int b = 1, c = 2;
Scope
The variables defined in the stack memory are scoped:
- Use curly braces to define the scope;
- Scopes can be nested, child scopes can access the parent's variables, and the parent scope cannot access the variables of the child;
- Variable names in the same scope cannot be duplicated;
- Leaving a scope variable is automatically reclaimed by the JVM;
Let's look at a few examples:
1 Public Static voidMain (string[] args)2 {3 intA = 0;4 {5 intb = 1;6A = 10;7 }8A = b;//This linecompilation error, variable B left its own scope is recycled, can not access9}
1 Public Static voidMain (string[] args)2 {3 intA = 0;4 {5 intb = 1;6A = 10;7 }8 intb//This b is a new variable created by the current scope, with a value of 0 instead of 19A = b;//no error, B can access toTen}
1 Public Static void Main (string[] args) 2 {3 int a = 0; 4 {5 int a = +; Error, child scope cannot use variable name 6 }7 } that already exists in parent
Type conversions
Data types often need to be converted to each other, let's look at the following;
Small data conversion to big data
is done automatically by the JVM and does not require additional processing, called implicit type conversions, such as:
1 Public classMain2 {3 Public Static voidMain (string[] args)4 {5 byteA = 16;6 Shortb =A;7 intc =b;8 LongD =C;9 Ten floatE = 16.66f; One Doublef =e; A - test (a); - Test (e); the } - - Private Static voidTestinti) - { + } - + Private Static voidTestDoublei) A { at } -}
The loss of precision occurs when an integer is converted to a floating-point number.
Big data to small data conversion
A cast is required, called an explicit type conversion, such as:
1 Public classMain2 {3 Public Static voidMain (string[] args)4 {5 LongA = 1L;6 intB = (int) A;7 Shortc = ( Short) b;8 byteD = (byte) C;9 Ten DoubleE = 16.66f; One floatF = (float) e; A -Test (byte) a); -Test2 ((float) e); the } - - Private Static voidTestbytei) - { + } - + Private Static voidTest2 (floati) A { at } -}
Pay attention to the problem of data out of bounds when converting.
Java Learning Note (i): Data types and variables