Different instances of value types and reference types in Java (i)

Source: Internet
Author: User

Simple classification, in addition to the value type in Java is the object. A value type is a basic type in Java, except that the base types are objects.

Memory "A data with a value type is stored in a variable within the stack. That is, allocate memory space in the stack, store the value directly, and its value represents 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, 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. "(cainiao123hack,2012)

(cainiao123hack,2012)

This topic will be divided into three parts of the code:

The first part of the code is a simple example, the second part is a value and a reference in an array, and the third part refers to passing and referencing passing

Here is the first part:

 Public class topcoder{    publicstaticvoid  main (string[] args)    {         int a=2;         int b=A;        System.out.println (b);        System.out.println (a);                b=3;        System.out.println (b);        System.out.println (a);        }}

Output is:

2
2
3
2

 class   number{ public  int   num;}  public  class   topcoder{ static  void   main (string[] args) {Nu        Mber a  =new   number ();        A.num  =2;        Number b  =A;        System.out.println (B.num);                System.out.println (A.num);        B.num  =3;        System.out.println (B.num);        System.out.println (A.num); }}

Output is:

2
2
3
3

Reference

cainiao123hack,2012, http://blog.csdn.net/cainiao123hack/article/details/7749386

Different instances of value types and reference types in Java (i)

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.