Reference type variables, objects, value types, value passing, reference passing differences and definitions

Source: Internet
Author: User

What is called a reference-type variable in Java?

Reference: Query by memory address

For example: string s = new string (); This is actually allocating a chunk of memory space in the stack memory to S, in the heap memory

New has a string type of space that, at run time, is the storage space in the stack memory that s points to the heap memory


The basic data class type holds the value itself, and the reference type variable is a reference to the data, not the data itself.

A reference type variable is an indirect way to get data.

Reference-type variables are object types, such as arrays, classes, strings, and so on, that are reference-type variables. Therefore, the reference type

The variable contains the address of the data.

The basic data type variable is like something that is placed directly in the cupboard, and the reference data type variable is the cabinet corresponding to the code

The key. The key number corresponds to the cabinet.





Two. What is the difference between a variable and an object of reference type in Java?

Object: is established according to the template "class", the location is the memory

For example, a is a class. A X is the creation of a reference variable, x=new a () creates an object in memory (opens up 1 spaces)

, the object's template is a (which can be understood as copying the variables in a class into space in space). You can now use the reference variable X

To reference the variables in the object.



Three. What is the difference between a value type and a reference type?

Custom

A reference type indicates that the data you are manipulating is the same, that is, when you pass a parameter to another method, you change it in another method

The value of this variable, then calling this method is the value of the variable passed in will also change. A value type means copying a current variable to a method,


[Value type]

That is, the basic data type is often referred to as four types of eight

Four categories:

1, Integer 2, float type 3, character 4, logic type

Eight kinds:

1, integral type 3 kinds of Byte,short,int,long

2, Float type 2 kinds of float,double

3, character type 1 char

4, Logic type 1 Boolean


[Reference type]

All types are referred to as reference types (arrays, classes, interfaces, strings), except for four classes of eight basic types.



[Value passing]

The basic data type assignment is a value pass, and the value passed is a real variable value, a copy of the original argument, and after the value is passed, the argument

The value passed to the parameter, and the parameter changes without affecting the argument.


[Citation Pass]

Assignment between reference types belongs to reference passing. Reference passing is the object's reference address, which is itself (its own most popular understanding).

Reference passing: The address is passed, that is, the address of the argument to the formal parameter, the parameters are changed, and the arguments are changed, because they point to the same address.

The reference is similar to our pointer, but it does not require us to go to the specific operation


[Memory allocation]

A data with a value type is stored in a variable within the stack. is to allocate memory space in the stack and store the package directly

Contains values that represent the data itself.

Data of a value type has a faster access speed.

A data with a reference type (reference type) does not reside in the stack, but is stored in the heap. That is, allocating memory space in the heap,

Instead of storing the contained value directly, you point to the value you want to store, and its value represents the address that you are pointing to. When you access a reference type that has a

Data, you need to check the contents of the variable into the stack, which references an actual data in the heap. Data with reference type data ratio type

There is greater storage size and lower access speeds.



There is a garbage collection mechanism in Java, and the variables in the stack memory are destroyed naturally with the end memory of the method, and when using the reference type, when the method knot,

Beam, this object may be applied by another reference type, not destroyed, only if an object


Reference type variables, objects, value types, value passing, reference passing differences and definitions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.