Java character array confusing area-FAQ

Source: Internet
Author: User

Have not been very clear in Java in the declaration of character array of two ways of application scenarios, today suddenly brother Epiphany, and everyone share. The wrong place and the inadequacy of the place also ask you to point out a lot.

The first type is:char[] ta = new char[1024];
The second type is: char[] te = {' A ', ' a ', ' B '};

In-depth analysis of what the JVM looks like when it runs: first when the JVM runs to char[] ta = new char[1024], we all know that the new operator in Java is requesting and allocating memory for that object. There are some children's shoes there may be questions, how is the character array an object? It is also written in this Java book that this is the first application of the Java object type encountered, that is, an array. When the JVM reads such a piece of code, he understands that it needs to allocate a 1024-character space, which is owned by TA (in fact, TA holds an identity in this area of memory, which is called a hash in Java). By default, it is called ToString () to see the ToString () method in object to uncover why we print a Java object, by default it is such a virtue, we can transform the output of virtue, but unfortunately not the focus of this article, Not in detail). Well the JVM reads here, the basic model in memory is as shown in:

As you can see here, it's just allocating memory space, and there's nothing left in this space.

And when executed to char[] te = {' A ', ' B ', ' C '}, when this time is a little bit different from the above, this time the JVM processing a step more, not only to allocate memory, but also to say a B c this three-character memory area, and finally the memory of the identity of the TE in charge. The memory changes after the JVM executes this code are as follows:

Speaking of which, the application of the two scenarios gradually paid off the water. The first case is simply to apply the memory first, then the value, and the second is to initialize the value immediately after the memory has been requested.

So the first situation is more in the case of the need to advance buffer, such as reading files, reading network IO stream and so on. The second scenario is when we know in advance what the value of this character array is, and then we can choose the second option.

Finished

Java character array confusing area-FAQ

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.