This essay covers both JDK (Java Development Kit) 1.0.2 PrintStream.print and JDK 1.1.1 PrintWriter.print
print and println are methods in PrintStream. They are useful for creating 8-bit ASCII (American Standard Code for Information Interchange) output directly readable by humans. Use a DataOutputStreaminstead if you want compact binary format. Java version 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.
In JDK 1.1, PrintStream is deprecated. Use PrintWriter instead. PrintWriter. println is smart enough to emit CrLf (under Windows-95, Windows-98 and NT) and plain Lf (under Unix), however embedded \n characters are never expanded! In JDK r1.2 PrintStream was undeprecated again. I have not yet experimented to find out how it handles line separators.
The expansion is controlled by the lineSeparator system property. See File for how this is defined on various platforms.
static String lineSeparator = System.getProperty ("line.separator" );
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.
What
out.println( 'A' ); out.println( 'A' + 'B' );
You might naïvely 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 concatenation. Addition also promotes to int, which println displays differently from char.
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 |
Java version 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 |
This page is posted |
http://mindprod.com/jgloss/printlnformat.html | |
Optional Replicator mirror
|
J:\mindprod\jgloss\printlnformat.html | |
Please read the feedback from other visitors,
or send your own feedback about the site. Contact Roedy. Please feel free to link to this page without explicit permission. | ||
Canadian
Mind
Products
IP:[65.110.21.43] Your face IP:[52.14.7.103] |
| |
Feedback |
You are visitor number | |