Java Console output print and println detailed

Source: Internet
Author: User
Tags float double

In the process of using Java programming, console output is used a lot, today I want to comb, print, println, printf three differences.

First, print

With the System.out.print method call, the Print method has a number of parameters: Boolean, char, char[], String, int, float, double, long, and so on.

Print does not wrap at the end of the output.

1. Print (Boolean B)

When you print a Boolean type, the output is only true and false two. We look at the source code of print (Boolean B) and we can see:

Output true when B is true, false to output false. It's intuitive, there's nothing to say.

2. Print (char c)

Prints a single character and, if it is an escape character with a special meaning, displays the effect of the escape character. Take a look at the examples to find out:

The output is: One line is empty, this is the effect of wrapping, and then a tab is separated between B and D.

3. Print (char[] buf)

This method prints out the characters in the BUF array.

The printed result is the same as above:

4. Print (String s)

To print a string, note that if S is null, the output is null instead of nothing.

The output is:, notice that at the beginning, the line end of the print output is not wrapped.

5, print int float double long

All four represent numeric types, and when Java is output, it turns the number into a 10-digit output. Let's look at the source code for Java:

As you can see, Java calls the String.valueof (number) method, turns the number into a string, and then outputs it. Let's go check out the source code for this valueof method:

The valueof method turns the incoming number into a 10-digit number, and then turns it into a string. The other three types (long, float, double) are also converted to 10 binary numbers.

The combined effect is to convert the number to a 10 binary output.

Actually test it:

The output is all converted to 10 binary:

Second, println

PRINTLN is almost the same as print, with two points in different places.

1, println line end will change line

Let's look at the source code for Java:

println (Boolean x) calls print (Boolean x), and then calls the newline () method, which is followed by a line after the Print method output finishes.

Several other println methods are the same, call the corresponding print method, and then wrap the line.

Println is easier to use than print and does not need to add another print (' \ n ') to wrap.

2, println allowed parameter is empty

In this case, the line will be wrapped, and we'll look at the Java source code:

The Print method does not allow parameters to be empty, or compile an error.

I will share the knowledge about print and println, and I would like to welcome criticism.

Reference: Java source code

Java Console output print and println detailed

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.