Skip to content

Latest commit

 

History

History
44 lines (35 loc) · 925 Bytes

difference_between_initializer_and_literal.md

File metadata and controls

44 lines (35 loc) · 925 Bytes

Difference between Initializer and Literal

The reason { 1, 2, 3 } is called an "array initializer" and not an "array literal" is somewhat subtle.

When you have a literal, like a String literal, you can assign that to a variable and then use that String afterwards.

~void main() {
String name = "Alana";
// l
System.out.println(name.charAt(1));
~}

But you can also perform those operations using the literal itself, without an intermediate variable.

~void main() {
// l
System.out.println("Alana".charAt(1));
~}

Array initializers work in the case where you first assign them to a variable before using the array.

~void main() {
char[] name = { 'A', 'm', 'a', 'n', 'd', 'a' };
// m
System.out.println(name[1]);
~}

But they do not work to perform operations on directly.

~void main() {
// Will not run
System.out.println({ 'A', 'm', 'a', 'n', 'd', 'a' }[1]);
~}