All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Create An Array In Java

Last Updated : Jul 1, 2023

How To Create An Array In Java

In this article we will show you the solution of how to create an array in java, here you have to import packages, in case you using eclipse then each program have itself.

After public class definition you can define array variable with or without values under ‘public static void main()’. You have to define through predefined rules otherwise, you will get error. To ensure your array definition below given example will help you.

Step By Step Guide On How To Create An Array In Java :-

Package is must, or else they cannot understand the code what you defined. The class and void main definition are common and necessary thing actually for each program.

Generally array helps you to hold multiple values with same type on single variable.

First you have to declare array values type either int or String. Then you write variable name with pair empty square brackets. Below given example have array declared with int type values.

//Create Array
package sam_prgms;
public class Example{
    public static void main(String[] args)
     int[] numArr={10,25,9,2};
  1. The ‘Package sam_prgms’ may differ as per developer definition and its container name where your overall data stored it. Actually it’s a namespace that organizes a set of related classes and interfaces.
  2. The ‘public static void main(String[] args)’ is starting point from where compiler starts program execution.
  3. Without this you can compile but not possible to run successfully, because it throws error ‘main method not found’.
  4. Firstly we declare array variable numArr before that 'int'- denotes array datatype, with that square bracket must. Because it says particular variable is array and array name specified.
  5. To initialize multiple int datatype values, we have to give '{10,25,9,2}' like this line. Around curly brace you can define as much as count you need.
  6. Whenever trying to print array values you have to specify index of number inside the array variable square bracket.
  7. That specific value in the array only will display on the terminal as output. For print overall values you need support of iteration.
  8. Usually array index will start from zero th position. Thats why here shown example will prints 3rd position value in array 'numArr'.
  9. The ‘system.out.println()’ is used to print an argument that is passed to it. And which is separate into three parts such as System- final class in java, out-instance of PrintStream type that is a public and static member field of System class, println()-instance of PrintStream class hence we can invoke same on out.
  10. Ensure each brackets has closed with its pair perfectly at the end. Then every statement must end by ‘;’.

Conclusion :-

In conclusion now we are able to know how to create array in java. To compile java program you need to tell the java compiler which program to compile through ‘javac’.

After successful compilation you need to execute ‘java Example‘ line for gets the output. Where you can see integer output '9'. On eclipse just press of run button then you will get output inside terminal.

I hope this article on how to create an array in java helps you and the steps and method mentioned above are easy to follow and implement.