How to declare and initialize array in Java?

Three ways to declare and initialize array in Java?
16 August 2017   2163

What is Array in Java?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed. You have seen an example of arrays already, in the main method of the "Hello World!" application. This section discusses arrays in greater detail.

How to declare and initialize declare in Java?

We've made a research and found out 3 ways to do it.

For primitive types:

int[] myIntArray = new int[3];
int[] myIntArray = {1,2,3};
int[] myIntArray = new int[]{1,2,3};

For classes, for example String:

String[] myStringArray = new String[3];
String[] myStringArray = {"a","b","c"};
String[] myStringArray = new String[]{"a","b","c"};

The third way of initialising is useful when you declare the array first and then initialise it. Cast is necessary here.

String[] myStringArray;
myStringArray = new String[]{"a","b","c"};

Apache NetBeans 11.1 to be Available

New version supports Java SE, Java EE, PHP, JavaScript and Groovy; C\C++ support is planned to be implemented in one of the next releases
23 July 2019   64

The Apache Software Foundation has introduced the Apache NetBeans 11.1 integrated development environment. This is the third release prepared by the Apache Foundation after the transfer of NetBeans code by Oracle and the first release after transferring the project from the incubator to the category of primary Apache projects. The release contains support for such programming languages as Java SE, Java EE, PHP, JavaScript and Groovy. The transfer of C / C ++ support from the code base transferred by Oracle is expected in one of the next releases.

  • Added support for Java EE 8 with the ability to build web applications using Maven or Gradle. 
  • Added support for new Java features.
  • Added examples for Gluon OpenJFX;
  • Improved support for Maven and Gradle build systems. 
  • Added the ability to use Graal.js, a JavaScript implementation based on GraalVM;
  • Implemented separation of caches with Truffle code between different debugging sessions;
  • Added syntax highlighting support for Kotlin code;
  • Implemented the ability to autocomplete code patterns in the Jade language;
  • Added PHP 7.4 support and updated PHP examples;
  • Improved performance on high pixel density (HiDPI) screens. The splash screen, tab delimiters and icons that are displayed at startup are adapted for HiDPI;
  • A transition to a new development cycle, involving the quarterly formation of new issues, has been completed.

Get more info at the official website.