In Java why print char array output is value, while others are address value __java base

Source: Internet
Author: User
First, the processing of the array of println methods is first converted to string type, we can enter the println source code look, I was creating a string array to look at the next
    public void println (Object x) {
        String s = string.valueof (x);
        Synchronized (this) {
            print (s);
            NewLine ();
        }
    

First he turns into a string and then goes into the Print method

    public void print (String s) {
        if (s = = null) {
            s = ' null ';
        }
        Write (s);
    }
Next, we'll go into the write method.

    private void Write (String s) {
        try {
            synchronized (this) {
                ensureopen ();//confirm that the output stream is open
                textout.write (s) ;//is a bufferwriter, write to the buffer
                textout.flushbuffer ();//Refresh Buffer
                charout.flushbuffer ();//outputstreamwriter
                if (AutoFlush && (s.indexof (' \ n ') >= 0))
                    Out.flush ()//Here is the one with the char array, and he flushes the string s to the output stream of the console. But remember he didn't have to ToString Method-
            associated
        }
        catch (Interruptedioexception x) {
            thread.currentthread (). Interrupt ( );
        }
        catch (IOException x) {
            trouble = true;
        }
    }
So let's look at the write method for the char array.

    private void Write (char buf[]) {
        try {
            synchronized (this) {
                ensureopen ();
                Textout.write (BUF);
                Textout.flushbuffer ();
                Charout.flushbuffer ();
                if (AutoFlush) {//We can see that he is writing each character to the output stream of the console. for
                    (int i = 0; i < buf.length i++)
                        if (buf[i] = = ' \ n ')
                            Out.flush ()
                ;
        }}} catch (Interruptedioexception x) {
            thread.currentthread (). interrupt ();
        }
        catch (IOException x) {
            trouble = true;
        }
    }


In fact, this is the second time encountered this problem, but not very clear before, this lane clearly recorded, but also for everyone to refer to


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.