-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New Java Concept & Terms: Output, .print,.println(),.printf()
#796
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
035d028
Create output.md
SSwiniarski aea1207
Create print.md
SSwiniarski 66b59c0
Create println.md
SSwiniarski fdfd53a
Create printf.md
SSwiniarski 7b256fc
Update printf.md
SSwiniarski 5800e89
Update printf.md
SSwiniarski 0e41aac
Update printf.md
SSwiniarski d3ebde8
Update printf.md
SSwiniarski f119673
Merge branch 'main' into java-printf
SSwiniarski 5cb1522
yarn format
SSwiniarski daea0d7
Update printf.md
SSwiniarski ffacb53
Update printf.md
SSwiniarski eb3e45c
Update content/java/concepts/output/output.md
SSwiniarski fad6eed
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski feab8ec
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski dd32e68
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski 27f3443
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski c723d8f
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski 82d8c22
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski e852ba7
Update content/java/concepts/output/terms/print/print.md
SSwiniarski 3d7c1a9
Update content/java/concepts/output/terms/println/println.md
SSwiniarski cb42a40
Merge branch 'main' into java-printf
SSwiniarski e0f1499
Merge branch 'main' into java-printf
SSwiniarski 5bd85bc
Update content/java/concepts/output/terms/print/print.md
SSwiniarski a0a8ba3
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski 31d7042
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski b09f6a4
Update content/java/concepts/output/terms/print/print.md
SSwiniarski 92efd1c
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski 80b1b30
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski 2370d4a
Update content/java/concepts/output/terms/print/print.md
SSwiniarski 8d51932
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski a2f020f
Update content/java/concepts/output/terms/println/println.md
SSwiniarski e4a5dae
Update content/java/concepts/output/terms/println/println.md
SSwiniarski 1eb3d7c
Update content/java/concepts/output/terms/print/print.md
SSwiniarski 9c296ee
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski 12055a2
Update content/java/concepts/output/terms/println/println.md
SSwiniarski 17f4fd1
Update print.md
SSwiniarski a948e5a
Update println.md
SSwiniarski d906475
Update printf.md
SSwiniarski 546a2dd
Merge branch 'main' into java-printf
SSwiniarski bd55b04
Update content/java/concepts/output/terms/print/print.md
SSwiniarski ed24add
Merge branch 'main' into java-printf
Dusch4593 c9eea88
add 'Characters' tag and bold terms
Dusch4593 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| ``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.