My understanding of basic Java types

Source: Internet
Author: User

Q: Why can't a primitive type array be passed to an object array?
A: This is because the primitive type array is considered to be a reference type, but each element in the primitive type array is a primitive type, but Java does not provide a way to manipulate the basic type
Therefore, you cannot convert to an object array type, so you can use the object array in the method to pass the parameter by converting the corresponding primitive type array to the corresponding wrapper class array.

Q: Why are basic types passed to the object parameter?
A: Java has a mechanism called automatic packaging, which, when passed to an object parameter, makes the basic type a new class through the wrapper mechanism.
Each basic type in Java corresponds to the corresponding wrapper class, example: Char:character int:integer ....

Q: With the above two questions, there are also questions, then why not the basic type of the array automatically wrapped in the corresponding wrapper class in the passed to the object array?
A: Because char[] is already a reference type, how do you package the reference type? Only basic types in Java can be wrapped as corresponding wrapper types (reference types)

Q: So why do primitive type array instances pass the application of object objects?
A: I think that's because (in c) He passed the address of the first element in the array to the object parameter, because a reference to a non-primitive type (primitive type) in Java is usually passed as an address;

Q: Since the address of the first primitive type element is passed to the object reference, why not output the first value of the base type?
A: If you think of "char[].class = = class [C" as a derived class of object, then you can understand why you can receive a class line with an object parameter that is the true type of class [C (char[]).
That is, object can receive this type of information, but only the information known by object, that is, class [C, Object-based information. Of course the output of an O (char[] Transformation to object) will output the address code of char[] (assuming that is).

I was wondering why the primitive type array could not be converted to object[], I am aware of the question. Click here to enter the link

Reference reference

My understanding of basic Java types

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.