diff --git a/content/java/concepts/output/output.md b/content/java/concepts/output/output.md new file mode 100644 index 00000000000..0220d060bef --- /dev/null +++ b/content/java/concepts/output/output.md @@ -0,0 +1,16 @@ +--- +Title: 'Output' +Description: 'The System.out stream allows a Java program to output characters to the console.' +Subjects: + - 'Computer Science' +Tags: + - 'Characters' + - 'Output' + - 'Strings' + - 'Print' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`System.out`** stream allows a Java program to output characters to the console. It has several methods that allow printing output. Some of the significant ones are listed below. diff --git a/content/java/concepts/output/terms/print/print.md b/content/java/concepts/output/terms/print/print.md new file mode 100644 index 00000000000..b0075081184 --- /dev/null +++ b/content/java/concepts/output/terms/print/print.md @@ -0,0 +1,44 @@ +--- +Title: '.print()' +Description: 'Prints its argument to the console.' +Subjects: + - 'Computer Science' +Tags: + - 'Characters' + - 'Output' + - 'Strings' + - 'Print' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`.print()`** method prints its argument to the console. Unlike the similar [`.println()`](https://www.codecademy.com/resources/docs/java/output/println) method, `.print()` does not follow its argument with a new line, and any subsequent characters sent to the console will begin wherever the prior `.print()` command left off. + +## Syntax + +```pseudo +System.out.print(argument); +``` + +The `argument` passed to the `print()` method will be displayed on the console. + +## Example + +The following example prints some content to the console (Note: if there is to be any spacing between uses of `.print()` it must be accounted for in the arguments.): + +```java +public class PrintExample { + public static void main(String[] args) { + System.out.print("Output"); + System.out.print(123.456); + System.out.print(true); + } +} +``` + +This results in the output: + +```shell +Output123.456true +``` diff --git a/content/java/concepts/output/terms/printf/printf.md b/content/java/concepts/output/terms/printf/printf.md new file mode 100644 index 00000000000..67c37e063cb --- /dev/null +++ b/content/java/concepts/output/terms/printf/printf.md @@ -0,0 +1,104 @@ +--- +Title: '.printf()' +Description: 'Prints output to the console using various formatting commands.' +Subjects: + - 'Computer Science' +Tags: + - 'Characters' + - 'Output' + - 'Strings' + - 'Print' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`.printf()`** method prints output to the console with the use of various formatting commands. + +## Syntax + +```pseudo +System.out.printf(formatstring, arg1, arg2, ... argN); +``` + +Where `formatstring` is a string containing various format commands defining how to print an arbitrary number of additional arguments, `arg1, arg2, ... argN`. The format string contains text which is rendered as it appears in the string and format specifiers that output an argument in a specified format. + +### Format Specifiers + +Format specifiers obey the following sequence: + +```pseudo +%[index$][flags][width][.precision]conversion-character +``` + +The elements in square brackets (`[...]`) are optional. + +`index$` is a number followed by `$` which specifies the index of the argument being formatted. The default is to format arguments in the order they appear. + +`flags` can contain one or more of the following characters: + +- `-`: Left-justify. Defaults to right-justify. +- `+`: For numerical values display a leading `+` or `-`. +- `0`: Zero-pad numerical values (blank padding is the default). +- `,`: Use a comma grouping separator for numerical values (i.e., 1,000,000). +- A space will show a leading `-` for negative numbers or a space for positive numbers. + +`width` is a number representing the minimum width used for outputting characters. + +`precision` is a number representing the number of digits used to the right of the decimal for floating point numbers, or the length of a substring to extract from a string. + +`conversion-characters` are one of the following: + +- `b`: Boolean value. +- `B`: Boolean value, uppercase. +- `c`: Unicode character. +- `C`: Unicode character, force uppercase. +- `d`: Decimal integer. +- `f`: Floating-point number. +- `h`: Hashcode. +- `n`: Newline character, platform specific. +- `s`: String. +- `S`: String, force uppercase. +- `t`: Time/date formatting. + +The `t` time/date character is followed by one of the following characters to extract parts of a datetime value: + +- `T`: Time in hh:mm:ss format. +- `H`: Hour. +- `M`: Minute. +- `S`: Second. +- `L`: Millisecond. +- `N`: Nanosecond. +- `p`: A.M./P.M. +- `z`: Time zone offset. +- `A`: Full day of the week. +- `d`: Two digit day of the month. +- `B`: Full month name. +- `m`: Two-digit month. +- `Y`: Four-digit year. +- `y`: Last two digits of year. + +## Example + +The following example uses `.printf()` to format various types of output. + +```java +import java.util.*; + +public class PrintExample { + public static void main(String[] args) { + System.out.printf("This is a string: '%10s'%n", "Output"); + System.out.printf("This is a float: %+.2f%n", 123.456); + Date date = new Date(); + System.out.printf("This is a date: %1$tA, %1$tB %1$td %1$tY %n", date); + } +} +``` + +This will produce the following output (with the current date): + +```shell +This is a string: ' Output' +This is a float: +123.46 +This is a date: Saturday, June 11 2022 +``` diff --git a/content/java/concepts/output/terms/println/println.md b/content/java/concepts/output/terms/println/println.md new file mode 100644 index 00000000000..b5d56d9cb3d --- /dev/null +++ b/content/java/concepts/output/terms/println/println.md @@ -0,0 +1,46 @@ +--- +Title: '.println()' +Description: 'Prints its argument to the console followed by a new line.' +Subjects: + - 'Computer Science' +Tags: + - 'Characters' + - 'Output' + - 'Strings' + - 'Print' +CatalogContent: + - 'learn-java' + - 'paths/computer-science' +--- + +The **`.println()`** method prints its argument to the console follwed by a new line. It is probably the most common method of displaying output to the console. + +## Syntax + +```pseudo +System.out.println(argument); +``` + +The `argument` will be displayed on the console, followed by a new line. + +## Example + +The following example prints some content to the console. + +```java +public class PrintExample { + public static void main(String[] args) { + System.out.println("Output"); + System.out.println(123.456); + System.out.println(true); + } +} +``` + +This results in the output: + +```shell +Output +123.456 +true +```