Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
035d028
Create output.md
SSwiniarski Jun 10, 2022
aea1207
Create print.md
SSwiniarski Jun 10, 2022
66b59c0
Create println.md
SSwiniarski Jun 10, 2022
fdfd53a
Create printf.md
SSwiniarski Jun 10, 2022
7b256fc
Update printf.md
SSwiniarski Jun 10, 2022
5800e89
Update printf.md
SSwiniarski Jun 11, 2022
0e41aac
Update printf.md
SSwiniarski Jun 11, 2022
d3ebde8
Update printf.md
SSwiniarski Jun 11, 2022
f119673
Merge branch 'main' into java-printf
SSwiniarski Jun 11, 2022
5cb1522
yarn format
SSwiniarski Jun 11, 2022
daea0d7
Update printf.md
SSwiniarski Jun 11, 2022
ffacb53
Update printf.md
SSwiniarski Jun 11, 2022
eb3e45c
Update content/java/concepts/output/output.md
SSwiniarski Jun 23, 2022
fad6eed
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 23, 2022
feab8ec
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 23, 2022
dd32e68
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 23, 2022
27f3443
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 23, 2022
c723d8f
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 23, 2022
82d8c22
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 23, 2022
e852ba7
Update content/java/concepts/output/terms/print/print.md
SSwiniarski Jun 23, 2022
3d7c1a9
Update content/java/concepts/output/terms/println/println.md
SSwiniarski Jun 23, 2022
cb42a40
Merge branch 'main' into java-printf
SSwiniarski Jun 23, 2022
e0f1499
Merge branch 'main' into java-printf
SSwiniarski Jun 24, 2022
5bd85bc
Update content/java/concepts/output/terms/print/print.md
SSwiniarski Jun 24, 2022
a0a8ba3
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 24, 2022
31d7042
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 24, 2022
b09f6a4
Update content/java/concepts/output/terms/print/print.md
SSwiniarski Jun 24, 2022
92efd1c
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 24, 2022
80b1b30
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 24, 2022
2370d4a
Update content/java/concepts/output/terms/print/print.md
SSwiniarski Jun 24, 2022
8d51932
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 24, 2022
a2f020f
Update content/java/concepts/output/terms/println/println.md
SSwiniarski Jun 24, 2022
e4a5dae
Update content/java/concepts/output/terms/println/println.md
SSwiniarski Jun 24, 2022
1eb3d7c
Update content/java/concepts/output/terms/print/print.md
SSwiniarski Jun 24, 2022
9c296ee
Update content/java/concepts/output/terms/printf/printf.md
SSwiniarski Jun 24, 2022
12055a2
Update content/java/concepts/output/terms/println/println.md
SSwiniarski Jun 24, 2022
17f4fd1
Update print.md
SSwiniarski Jun 24, 2022
a948e5a
Update println.md
SSwiniarski Jun 24, 2022
d906475
Update printf.md
SSwiniarski Jun 24, 2022
546a2dd
Merge branch 'main' into java-printf
SSwiniarski Jun 24, 2022
bd55b04
Update content/java/concepts/output/terms/print/print.md
SSwiniarski Jun 24, 2022
ed24add
Merge branch 'main' into java-printf
Dusch4593 Jun 25, 2022
c9eea88
add 'Characters' tag and bold terms
Dusch4593 Jun 25, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions content/java/concepts/output/output.md
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.
44 changes: 44 additions & 0 deletions content/java/concepts/output/terms/print/print.md
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
```
104 changes: 104 additions & 0 deletions content/java/concepts/output/terms/printf/printf.md
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
```
46 changes: 46 additions & 0 deletions content/java/concepts/output/terms/println/println.md
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
```