0

Array

How can declaration , instantiation and initialzation of java array in one line

31st Aug 2020, 2:06 AM
Nasratullah Safai
Nasratullah Safai - avatar
3 Respostas
+ 2
int[] a = new int[] {}; Declaration: int[] a Instantiation: new int[] {} Initialization: a = new int[] {}; // yeah, this overlaps mostly with instantiation since we're creating an instance to initialize variable a. More detail is at: https://www.educative.io/edpresso/how-to-initialize-an-array-in-java
31st Aug 2020, 2:10 AM
Josh Greig
Josh Greig - avatar
0
Building on Josh Greig's answer, you can initialize with values between the curly braces: String chars[] = new String[] { "a", "b", "c" }; int nums[] = { 1, 2, 3 };
31st Aug 2020, 3:19 AM
David Carroll
David Carroll - avatar
0
Tanks for your help
31st Aug 2020, 10:08 AM
Nasratullah Safai
Nasratullah Safai - avatar