forked from Together-Java/ModernJava
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
224 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,39 @@ | ||
# Initialization with new | ||
|
||
Before the initializer for an array, you are allowed to write | ||
`new` followed by a space, the type of thing in the array, | ||
and an empty `[]`. | ||
|
||
```java | ||
~void main() { | ||
char[] mainCharacter = { 'A', 'a', 'n', 'g' }; | ||
System.out.println(mainCharacter); | ||
|
||
char[] sideCharacter = new char[] { 'A', 'a', 'n', 'g' }; | ||
System.out.println(sideCharacter); | ||
~} | ||
``` | ||
|
||
This is required for performing delayed initialization of a variable | ||
holding an array. | ||
|
||
```java | ||
~void main() { | ||
char[] element; | ||
|
||
element = new char[] { 'f', 'i', 'r', 'e' }; | ||
System.out.println(element); | ||
|
||
// This would not work | ||
// element = { 'f', 'i', 'r', 'e' }; | ||
~} | ||
``` | ||
|
||
One ability this gives you is to use an array in an expression. I.E. | ||
the initializer coupled with the `new char[]` is akin to an "array expression." | ||
|
||
```java | ||
~void main() { | ||
System.out.println(new char[]{ 'K', 'a', 't', 'a', 'r', 'a' }[1]); | ||
~} | ||
``` |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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