Two big data types for Java
- Basic data types
- Reference type
In Java programs, new objects are stored in the heap ( reference types ), but when using new to create a small, simple object is often not very effective, so for these types, Java does not use new to create a variable, but instead creates a non-referenced variable ( Base data type ).
Basic data types
- The base data type is stored in the stack and therefore has a faster access speed.
- Basic data types if you want to create a reference type in the heap, you need to use the corresponding wrapper class.
The Java language provides eight basic data 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
- In addition to the eight basic data types, the remaining types are referred to as reference types (arrays, classes, interfaces, strings)
- In Java, a variable of a reference type is very similar to a C + + pointer. A reference type points to an object, and a variable that points to an object is a reference variable. These variables are specified as a specific type at the time of declaration. Once a variable is declared, the type cannot be changed.
- The default value for all reference types is null.
- a data with a reference type does not reside in the stack, but is stored in the heap. That is, allocating memory space in the heap, not directly storing the contained value, but pointing to the value to be stored, whose value represents the address pointed to. When accessing a data with a reference type, you need to check the contents of the variable into the stack, which refers to an actual data in the heap. Data of a reference type data ratio type has greater storage size and lower access speed.
Value passing and reference passing value passing
The basic data type assignment is a value pass, the value passed is a real variable value, is a copy of the original parameter is passed, the value is passed, the argument is passed to the value of the parameter, the parameter changes without affecting the argument.
Reference delivery
Assignment between reference types belongs to reference passing. Reference passing is the object's reference address, which is itself (its own most popular understanding).
The reference pass is the address, which is to pass the address of the argument to the parameter, the parameters change, and the arguments are changed, because they point to the same address.
Reference:
The idea of Java programming
What is the difference between a value type and a reference type in Java?
Java Learning notes-data types