Skip to content
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

Fixed codebox formatting of src/boxed_primitives #40

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 8 additions & 8 deletions src/boxed_primitives/boolean.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# Boolean


The type to use for a `boolean` that might be null is `Boolean`.

```java
~void main() {
Boolean b = null;
System.out.println(b);
b = true;
System.out.println(true);
~}
```java, no_run
void main() {
Boolean b = null;
System.out.println(b);
b = true;
System.out.println(true);
}
```
14 changes: 7 additions & 7 deletions src/boxed_primitives/boxing_conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ If you try to assign to a boxed type like `Integer` from some
code that gives you the unboxed version like `int`, then Java will
automatically do that conversion.[^obvious]

```java
~void main() {
int x = 5;
Integer y = x;
```java, no_run
void main() {
int x = 5;
Integer y = x;

System.out.println(x);
System.out.println(y);
~}
System.out.println(x);
System.out.println(y);
}
```

[^obvious]: This might feel obvious, but this is one of the few places in Java where the type of something "magically" changes. `int` and `Integer`, `char` and `Character`, etc. *are* different types.
15 changes: 8 additions & 7 deletions src/boxed_primitives/character.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

The type to use for a `char` that might be null is `Character`.

```java
~void main() {
Character c = null;
System.out.println(c);
c = '%';
System.out.println(c);
~}
```java, no_run
void main() {
Character c = null;
System.out.println(c);
c = '%';
System.out.println(c);
}
```
15 changes: 8 additions & 7 deletions src/boxed_primitives/double.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

The type to use for a `double` that might be null is `Double`.

```java
~void main() {
Double d = null;
System.out.println(d);
d = 3.14;
System.out.println(d);
~}
```java, no_run
void main() {
Double d = null;
System.out.println(d);
d = 3.14;
System.out.println(d);
}
```
12 changes: 6 additions & 6 deletions src/boxed_primitives/integer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

The type to use for an `int` that might be null is `Integer`.

```java
~void main() {
Integer i = null;
System.out.println(i);
i = 5;
System.out.println(i);
```java, no_run
void main() {
Integer i = null;
System.out.println(i);
i = 5;
System.out.println(i);
~}
```
40 changes: 20 additions & 20 deletions src/boxed_primitives/unboxing_conversion.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@ the normal type is expected, it will be implicitly "unboxed."

This means you can use `Integer`s directly in math expressions.

```java
~void main() {
Integer x = 5;
int y = 3;
int z = x * y;

System.out.println(z);
~}
```java, no_run
void main() {
Integer x = 5;
int y = 3;
int z = x * y;

System.out.println(z);
}
```

As well as `Boolean`s in logical expressions.

```java
~void main() {
Boolean hasHat = true;
if (hasHat) {
System.out.println("You have a hat!");
```java, no_run
void main() {
Boolean hasHat = true;
if (hasHat) {
System.out.println("You have a hat!");
}
}
~}
```

And so on for `Double`, `Character`, etc.

But if you use one of these types like this and they happen to be `null` you will
get a `NullPointerException`.

```java,panics
~void main() {
Integer x = null;
// Bool
int y = x;
~}
```java, panics
void main() {
Integer x = null;
// Bool
int y = x;
}
```