References in Java and pointers in C

Tan.] der 2022-01-26 19:57:37 阅读数:349

references java pointers


A pointer is a value , And this value is the address of a block of memory , By this value , You can find this memory
C The pointer of language has the following characteristics :
C A pointer to a language can point anywhere in memory
C Language pointers can participate in numerical operations , Add 、 Subtraction
C Language pointers can also point to pointers , And a pointer to a pointer ( Because a pointer is a value , It should also be stored in memory , So it also has a memory address )
Pointer this thing , Only care about the address of memory , Don't care what's in memory , If the object in the address is moved , The pointer does not automatically change to


Java The reference to is also a value , But this value is not the address of a block of memory , It's the address of the memory where a value is located
stay JAVA Data in can be simply divided into two types , Basic types and reference types
If we continue to subdivide, the basic types can be divided into
Four integer types byte、short、int、long
There are two floating point types float、double
A character type char
A boolean type boolean
There are countless reference types , Include String、 Array 、Object wait
stay java Only reference types have references
Reference types are used in memory allocation , Will allocate two pieces of memory , A storage reference , A piece of stored value
When you define variables , Reference has no entity , Does not occupy a space , Only when the variable is assigned , References have actual values
His focus is on value , After the shift , The reference will be changed accordingly ── Memory defragmentation
Java Only value passing , The so-called reference passing , What is passed is the value of the reference

Java The quotation of and c The difference between pointers

C The pointer to is the address of a block of memory
C The value of the pointer of can be considered as int type , The length is usually the length of a computer word
Java The reference of is the address of the memory where a value is located
Java References to , When variables are declared ( No assignment ), Does not occupy a space .( See reference type memory allocation for the reason )
Java The initial value of the reference is null.C The pointer of is int, If the pointer is not initialized , Then its value is not fixed
Java The reference of cannot be calculated .C The pointer of is int, He can calculate , Such as ++ or –, So we often use pointers instead of array subscripts

copyright:author[Tan.] der],Please bring the original link to reprint, thank you.