JAVA primitive type variables and reference type variables

Source: Internet
Author: User

Hugh: Foolish, I was confused again.

The foolish public: Say and see.

Hugh: I know that Java has 8 basic types, and found that there are 8 wrapper classes (Warper), they play the same role, all represent a type of number. So I just made up a sentence:

int I=ineger (8);

The result is a compilation error.

Foolish: Programming cannot be taken for granted. The object-oriented principle of Java is that operations on data and data are bound together, which is the class, the reference data type. Therefore, a class is also a type. Java could have set the base type without setting the base type, only to improve operational efficiency. The main differences between the base type and the reference type are:

The variable name of the base type is the variable itself.

The name of the reference type variable is where complex data is stored.

These two types of corresponding storage methods are shown in 6.


A break: I understand this question, for example, a primitive type represents simple data, and a reference type represents complex data.

Yu Gong: Let me give you an example.

A break: For example, how much money, how long, how high the simple data can be expressed in the basic type. More complex things are represented by reference types, such as automobiles, animals, and so on, which contain many features and behaviors, that is, attributes and actions.

The foolish public: it can be understood. With regard to objects, there are many concepts that need to be understood clearly. For example, the following declaration:

String s= "Hello world!";

Many people have written such statements, but what exactly do we declare? A break, you answer.

A break: Declares a string object with the content "Hello world!".

Foolish: The answer is still rather vague, the exact answer is that this statement creates a string object, and it declares a reference to the object named S. It can point to any object of type string, which currently points to "Hello world!" The object of this string type.

A break: That is, we declare a reference variable s that can only point to a string object and point s to the string object "Hello world!".

Foolish: Yes, if you add a code after the statement just now:

String string=s;

Did you declare a new object?

Hugh: Yes, a new object string is declared.

Fool: No, we're declaring another reference to a string object named String, and no second object is generated, or string is pointing to the original object, that is, and s to the same object. The string class needs to be clear, because this class is clear and many classes are understood.

Hugh: is the string class so important?

Yu Gong: Yes, because this class is used very often and contains many custom properties and methods that we can use directly, and this class is the final class, we cannot inherit it, nor can we inherit and modify its methods with polymorphism.
Hugh: Then I must study it well!

JAVA primitive type variables and reference type variables

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.