The "Java" array cannot be converted to a string by the ToString method

Source: Internet
Author: User

In Java, all classes, whether they are classes in the Java library, or classes that you create yourself, are all inherited from the class object. One of the methods in object is ToString (), so all classes are created with a ToString method.

What does this method do?

First we need to understand that the Java Output function print (), is not to accept the direct output of the object, only accept strings or numbers, such as output. So what do you want to do with a well-created object? For example:

 Package com.spring.h3;  Public class Test2 {    publicstaticvoid  main (string[] args) {        System.out.println ("New Test2 () = = =" +new  Test2 ()        ); // the output is: New Test2 () [email protected]     }}

According to the type accepted by print, S1 can not be directly output, then does it mean that this cannot be compiled and run? Of course not. Because when print detects that the output is an object, not a character or a number, it calls the ToString method inside the object class, and the output is [type @ hash value]. The source code for the ToString () method in the object class is as follows:

/*** Returns A string representation of the object. In general, the * <code>toString</code> method returns a string, which * "textually represents" this object. The result should * be a concise but informative representation, that's easy for a * person to read. * It's recommended that all subclasses override the This method. * <p> * The <code>toString</code> method for class <code>Object</code> * Returns a string C Onsisting of the name of the class of which the * object is an instance, the At-sign character ' <code>@</code> ; ', and * The unsigned hexadecimal representation of the "* object" of the hash code. In other words, this method returns a string equal to the * value of: * <blockquote> * <pre> * getclass (). Get Name () + ' @ ' + integer.tohexstring (hashcode ()) * </pre></blockquote> * *@returna string representation of the object.*/ PublicString toString () {returnGetClass (). GetName () + "@" +integer.tohexstring (Hashcode ());}

In the array class, there is no overriding (override) for this method, just overloading (overload) as a static method of the class (see Java.util.Arrays). Therefore, the result of using the array directly with ToString () is also the [type @ hash value].

So the array to string should be written as:

Arrays.tostring (a)

This method of ToString () is formatted, that is, the output is [a, B, c], if only want to output ABC, the following two methods are required:

  Method 1: convert directly when constructing a string.

Char [] data = {' A ', ' B ', ' C 'new String (data);

  Method 2: Call the method transformation of the string class.

String.valueof (char[] ch)

Resources:

[1] http://www.hqhome.net/java/303.html

[2] Http://blog.163.com/[email protected]/blog/static/50282494201111597090/

The "Java" array cannot be converted to a string by the ToString method

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.