Java println ASCII Output Formats

Last updated 1997 June 5 by Roedy Green © 1996-1997 Canadian Mind Products.

This essay covers both JDK 1.0.2 PrintStream.print and JDK 1.1.1 PrintWriter.print output formats.

print and println are methods in PrintStream. They are useful for creating 8-bit ASCII output directly readable by humans. Use a DataOutputStream instead if you want compact binary format. JDK 1.1 has classes InputStreamReader and OutputStreamWriter for reading and creating streams of 16-bit Unicode characters.

Java out-the-box contains only the most primitive tools for displaying character output. There are no formatting tools. There are no data input tools other than a stream tokenizer. You have to read characters from the input stream and convert them to binary yourself. In JDK 1.0 you can't even print out the full precision of a double.

PrintStream.print and PrintStream.println have variants for the various possible primitive types: bytes, char, int etc. They just use an appropriate toString method.

PrintStream versus PrintWriter -- UNIX-style Single-Char Newline

Warning! PrintStream.println inserts a single \n, 0x0a, ^J, LineFeed character -- even when running on Windows-95. Windows-95 normally marks line ends in text files with \r \n, 0x0d 0x0a, Cr Lf, ^M ^J. If you want the ^M, you must add it yourself!!. The CMP TabIn or TabOut utility will insert the needed \r's.

In JDK 1.1, PrintStream is deprecated. Use PrintWriter instead. PrintWriter.println is smart enough to emit CrLf under Windows-95 and plain Lf under Unix, however embedded characters are NOT expanded!

Output Formats

print inserts no padding space characters before or after. It just squirts out exactly what toString provides. There are no quotes, commas, trailing nulls or other delimiters inserted.

The output is 8-bit ASCII, not 16-bit Unicode.

Concatentation Gotcha

What do you think this little code snippet produces?

System.out.println('A');
System.out.println ('A'+'B');

You might naively expect: A AB, or perhaps 65 131, however, the answer is: A 131.

The problem is Java's design blunder of using + to mean both addition and concatentation. Addition also promotes to int, which println displays differently from char.

JDK 1.0 Sample println Output

There are quite a few differences in the output of println between JDK 1.0 and 1.1. Make sure you are looking at the appropriate table.

Type Typical Code Output Output
Length
Notes
boolean println(false) false 5 Latin-1 lower case 8-bit chars "false" without surrounding quotes.
println(true) true 4 Latin-1 lower case 8-bit chars "true" without surrounding quotes.
byte println((byte)90) Z 1 Latin-1 8-bit char
e.g. 'Z' == 0x5a == 90
It does not produce "90"!!
char println('Z') Z 1 Latin-1 8-bit char
e.g. 'Z' == \u005a == 90
It does not produce "90"!!
println((char)90) Z 1 It does not produce "90"!!
println('\u0044b') K 1 Unicode chars get high bits stripped.
e.g. Cyrillic small yeri
\u044b == 1099
displays as 'K' = \u004b = 75
It does not produce "1099"!!
It does not produce "75"!!
It does not produce Russian!!
double println(3.0d) 3 1 double displays exactly like float
3.0d shows up just like an int would, without any trailing d.
println(-3.0d) -3 2
println(3.456784d) 3.45678 7 Note only 6 measly significant digits
println(-3.456784d) -3.45678 8
println(0.00123d) 0.00123 7
println(-0.00123d) -0.00123 8
println(1.23457e9d) 1.23457e+009 12
println(-1.23457e-9d) -1.23457e-009 13
float println(3.0f) 3 1 float displays exactly like double.
3.0f shows up just like an int would without any trailing f.
println(-3.0f) -3 2
println(3.456784f) 3.45678 7
println(-3.456784f) -3.45678 8
println(0.00123f) 0.00123 7
println(-0.00123f) -0.00123 8
println(1.23457e9f) 1.23457e+009 12
println(-1.23457e-9f) -1.23457e-009 13
int println(90) 90 2 ASCII decimal display
90 == \u005a == 'Z'
Does not produce a Z!!
println(-90) -90 3
long println(90L) 90 2 ASCII decimal display
90 == \u005a == 'Z'
Does not produce a Z!!
println(-90L) -90 3
short println((short)90) 90 2 ASCII decimal display
90 == \u005a == 'Z'
Does not produce a Z!!
println((short)-90) -90 3
String println("Hello") Hello 5 The string without surrounding quotes.
No leading/trailing spaces are added or removed.
utf Not applicable

JDK 1.1 Sample println Output

JDK 1.1 has gives extra precision for float and double, 8 or 9 digits for float and 16 or 17 for double. I suspect the cases where you get an extra significant digit is a bug because the final digit appears to be random. Note also the change in output format for bytes and Unicode chars.

Type Typical Code Output Output
Length
Notes
boolean println(false) false 5 Latin-1 lower case 8-bit chars "false" without surrounding quotes.
println(true) true 4 Latin-1 lower case 8-bit chars "true" without surrounding quotes.
byte println((byte)90) 90 2 Note difference from JDK 1.0 that produces a character.
e.g. 'Z' == 0x5a == 90
It does not produce "Z"!!
char println('Z') Z 1 Latin-1 8-bit char
e.g. 'Z' == \u 005a == 90
It does not produce "90"!!
println((char)90) Z 1 It does not produce "90"!!
println('\u0044b') ? 1 e.g. Cyrillic small yeri
\u044b == 1099
displays as '?'
It does not produce "K" as in JDK 1.0!!
It does not produce "1099"!!
It does not produce "75"!!
It does not produce Russian!!
double println(3.0d) 3 1 3.0d shows up just like an int would, with out any trailing d.
println(-3.0d) -3 2
println(1.2345678912345671d) 1.23456781234567 17 last digit 1 gives 16 significant digits, rounded down.
println(-1.2345678912345671d) -1.234567891234567 18 last digit 1 gives 16 significant digits, rounded toward zero.
println(1.2345678912345675d) 1.2345678912345674 18 last digit 5 gives 17 significant digits.
println(-1.2345678912345675d) -1.2345678912345674 19 last digit 5 gives 17 significant digits.
println(1.2345678912345679d) 1.23456781234568 17 last digit 9 gives 16 significant digits, rounded up.
println(-1.2345678912345679d) -1.234567891234568 18 last digit 9 gives 16 significant digits, rounded away from zero.
println(0.00123d) 0.00123 7
println(-0.00123d) -0.00123 9
println(1.2345789e9d) 1.2345789e9 11 Note how the exponent is abbreviated compared with JDK 1.0
println(-1.2345789e-9d) -1.2345789e-9 13 Note how the exponent is abbreviated compared with JDK 1.0
float println(3.0f) 3 1 3.0f shows up just like an int would without any trailing f.
println(-3.0f) -3 2
println(1.23456781f) 1.2345678 9 last digit 1 gives 8 significant digits, rounded down.
println(-1.23456781f) -1.2345678 10 last digit 1 gives 8 significant digits, rounded toward zero.
println(1.23456785f) 1.2345679 8 last digit 5 gives 8 significant digits rounded up.
println(-1.234567895f) -1.2345678 10 last digit 5 gives 8 significant digits, rounded away from 0.
println(1.23456789f) 1.2345679 9 last digit 9 gives 8 significant digits, rounded up.
println(-1.23456789f) -1.2345679 10 last digit 9 gives 8 significant digits, rounded away from zero.
println(0.00123f) 0.00123 7
println(-0.00123f) -0.00123 8
println(1.2345789e9f) 1.23456794e9 12 Oddly we get 9 significant digits instead of the usual 8.
Note how the exponent is abbreviated compared with JDK 1.0
println(-1.2345789e-9f) -1.2345679e-9 13 8 significant digits.
Note how the exponent is abbreviated compared with JDK 1.0
int println(90) 90 2 ASCII decimal display
90 == \u005a == 'Z'
Does not produce a Z!!
println(-90) -90 3
long println(90L) 90 2 ASCII decimal display
90 == \u005a == 'Z'
Does not produce a Z!!
println(-90L) -90 3
short println((short)90) 90 2 ASCII decimal display
90 == \u005a == 'Z'
Does not produce a Z!!
println((short)-90) -90 3
String println("Hello") Hello 5 The string without surrounding quotes.
No leading/trailing spaces are added or removed.
utf Not applicable


HTML Checked!
Canadian Mind Products You can get an updated copy of this page from http://mindprod.com/println.html