Java basic data type: Basic Data Type and reference type

Source: Internet
Author: User

Basic data types include byte, Int, Char, long, float, double, Boolean, and short.
The referenced data types include: Class, array, and interface.

The difference between the basic data type and the reference type is that the basic data type is allocated on the stack, and the reference type is allocated on the stack.

Therefore, let's analyze the difference between "=" and equals.

First, I will take two string objects

String A = "ABC ";

String B = "ABC ";

Then

If (A = B) {system. Out. println ("A = B ");}

Else

{System. Out. println ("! = B ");}

ProgramOutput! = B

Cause: the addresses of A and B are different. A = B compares the addresses of two variables.

Define two basic types

Int A = 4;

Int B = 4;

If (A = B) {system. Out. println ("A = B ");}

Else

{System. Out. println ("! = B ");}

Output: A = B

Cause: = compare the content of two variables

Conjecture: whether it is basic data type or reference type, they will first allocate a piece of memory in the stack. for basic types, this area contains the basic type of content; for the object type, this area contains a pointer to the real content, and the real content is manually allocated to the stack.

Related Article

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.