The difference between a primitive type and a reference type variable in Java

Source: Internet
Author: User

Basic type:

The basic type naturally needless to say, its value is a number, a character or a Boolean value.

Reference type:

is an object type, what is the value? Its value is a reference to the memory space, which is the address, and the memory that is pointed to holds a value or set of values represented by the variable.

To illustrate:

int A;
a=250;//Declare variable A at the same time, the system assigns a space to a.

The reference type is not, only the variable is assigned a reference space, the data space is not allocated, because no one knows what the data is, integers, characters? Let's look at an example of the error:

MyDate today;

Today.day = 4;//error occurred because the data space of the Today object is not assigned.

So how do we assign it to a value? A reference type variable must be instantiated to open a data space after it is declared in order to access the object to which the variable is directed. As an example:

MyDate today;//Assign a variable a space to save the reference

Today = new MyDate ();//This sentence is 2 steps, first execute new mydate (), open the data space for today variable, and then perform the assignment operation

Reference variable Assignment

MyDate A, B;//open up two reference spaces in memory

A = new mydate ();//open up the data space of the MyDate object and assign the first address of the space to a

b = A;//write the address in a storage space to the storage space of B

As shown

Reference passing and value passing

reference type: In addition to the value of the function is a "reference pass", in any use "=" To assign value to the object variable is "reference passing."

value passing: the transfer of the basic type is a value pass, like the C language, when the basic Java data type (such as int,char,double, etc.) as the entry parameters to the function body, the incoming parameters in the function body into a local variable, This local variable is a copy of the input parameter, and all functions within the function body are for the operation of the copy, and after the execution of the function, the local variable completes its mission, which does not affect the variables as input parameters. Parameter passing in this way is called "value passing".

/*define a class*/class mydate{String year;    String Month;    String Day;    String Date;        Public mydate () {year = "1900";        Month = "01";        Day = "01"; Date = year + "." + Month + "."    + Day;    } public String toString () {return Date;   }}public class objref{mydate mydate1 = new MyDate (); MyDate Mydate2 = mydate1;//= Mydate1 and Mydate2 will point to the same memory space//mydate Mydate2 = new MyDate ();//From New New object, Mydate1 and Mydate2 refer to To different memory spaces://reference NULL/spatial and data space are not the same public void Changeobj (MyDate inobj) {inobj.date = "2    007.09.26 ";        } public static void Main (string[] args) {ObjRef oref = new ObjRef ();        System.out.println ("Before Call Changeobj () method:" + oref.mydate1);        Oref.changeobj (OREF.MYDATE1);        System.out.println ("After call Changeobj () method:" + oref.mydate1);  System.out.println ("After call Changeobj () method:" + oref.mydate2);//Verify that "= Mydate1 and Mydate2 will point to the same memory space" conclusion        }} 

Differences between basic types and reference type variables in Java

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.