What are the reference data types in java

  • 2021-12-04 18:42:36
  • OfStack

What are the referenced data types in the directory java? What are the following about the referenced data types in java: 1. Class Class Reference 2. Interface interface Reference 3. Array Reference Array Definition Array Initialization Two ways to initialize arrays: Why are there basic data types and reference data types in Java?

What are the referenced data types in java

There are two data types in Java, among which there are mainly 8 basic data types and reference data types, all of which are reference data types except 8 basic data types, and the basic data types in 8 are byte, short, int, long, char, boolean, float and double respectively, as follows:

1. boolean The data value is only true or false, which is suitable for logical calculation.

2. char char type (character type) data takes up 2 bytes in memory. char type data is used to represent characters in the general sense, each character occupies 2 bytes, Java character adopts Unicode code, its first 128 bytes code and ASCII compatible characters are stored in\ u0000 ~\ uFFFF, attention should be paid to adding '' when defining character type data, for example, '1' means character '1' instead of value 1,

3. byte byte type (byte type) data occupies 1 byte in memory, indicating the storage data range:-128 ~ 127.

4. short short type (short integer) data takes up 2 bytes in memory.

5. int int type (integer type) data takes up 4 bytes in memory.

6. long long type (long integer type) data takes up 8 bytes in memory.

7. float float type (single precision floating point type) data takes up 4 bytes in memory. (float precision is 7-8 bits)

8. double double type (double precision floating point type) data occupies 8 bytes in memory.

Let's talk about the reference data types in java:

There are three types of reference data: class, interface and array.

1. Class Class reference

It can be created by us. I won't talk much about it here. I mainly explain several classes in java library

Object Object is a very important class, Object is the root class of the class hierarchy, each class uses Object as a superclass, and all objects (including arrays) implement the methods of this class. All classes can be defined with Object

Such as:


Object object= new Integer(1);  To define 1 A Interger Class  
Integer i=(Integer) object;      Come and put this Object Cast to Interger Class  
String The String class stands for strings, and all string literals (such as "abc") in an Java program are implemented as instances of this class. Examines individual characters of a sequence, compares strings, searches for strings, extracts substrings, and creates a copy of the string in which all characters are converted to uppercase or lowercase. char0 Date represents a specific moment, accurate to milliseconds. Class 1 of Date is now all replaced by Calendar and GregorianCalendar Void The Void class is a non-instantiable placeholder class that holds a reference to an Class object that represents the Java keyword void.

At the same time, there are corresponding Class such as Integer Long Boolean Byte Character Double Float Short

2. Interface interface reference

It can be created by us. I won't talk much about it here. I mainly explain several interfaces interface in java library

List<E> List, where users of this interface have precise control over where each element is inserted. Users can access elements based on their integer index (position in the list) and search for elements in the list. The List interface provides two ways to search for a specified object. From a performance point of view, these methods should be used with care. In many implementations, they will perform high-overhead linear searches. The List interface provides two efficient ways to insert and remove multiple elements anywhere in a list. add() Inserts the specified element in the list. remove() Removes the element at the specified position in the list. get(int index) Returns the element at the specified position in the list. Map<K,V> :

K-The type of key maintained by this mapping

V-The type of the mapping value maps the key to the object of the value. 1 map cannot contain duplicate keys; Each key can only be mapped to a maximum of 1 value.

put(K key,V value) Associates the specified value with the specified key in this mapping (optional operation). If this mapping previously contains 1 mapping for the key, replace the old value with the specified value (mapping m can only be said to contain the mapping for the key k if and only if true is returned). remove (Object key) If there is a 1 key mapping, remove it from this mapping (optional operation). More precisely, if this mapping contains data from the key = = null? k==null: key. equals (k), the mapping relationship is removed. (This mapping can contain at most one such mapping.) get (Object key): Returns the value mapped by the specified key; If this mapping does not contain the mapping relationship of the key, null is returned.

Here we mainly use String List Map Object is the most commonly used Number ArrayList < E > Arrays etc

3. Array references

Array: A collection of elements of the same data type (reference data type) stored in 1 contiguous block of memory.

Every 1 data in the array is called an array element, and the elements in the array are stored in an index, and the index (subscript) starts from 0.

Definition of Array The first way: type [] array name; Such as int [] nums; The second way: type array name []; Such as int nums [];

Most Java programmers prefer Style 1 because it separates the data type int [] from the variable name num.

Initialization of array

Arrays in Java must be initialized before they can be used.

Initialization is to allocate memory to array elements and assign initial values to each element.

Two ways to initialize arrays:

-Static initialization:

Syntax format: Type [] Array name = new Array type [] {Element 1, Element 2, Element 3,... Element n};

Simplified syntax: Array name of type [] = {Element 1, Element 2, Element 3... Element n};

-Dynamic initialization:

If we don't know what data is stored in the array in advance, we only know the number of data to be stored, so we can use dynamic initialization.

Dynamic initialization: When initializing, we specify the length of the array, and the system automatically assigns initial values to the array elements.

Format: Type [] Array name = new Array type [Array length];

Note: No matter which way the array is initialized, 1 once the initialization is completed, the length of the array is fixed and cannot be changed unless it is reinitialized. That is to say, the array is fixed in length.

Why are there basic data types and reference data types in Java?

The reference type is in the heap and the base type is in the stack.

The stack space is small and continuous, and it is often put in the cache. The reference type cache miss has a high rate and requires one more dereference.

Object needs to store one more object header, which is too high a waste rate for basic data types


Related articles: