• Teaching
    • Montessori Teacher Training 🔥
    • Yoga Teacher Training
  • Coding
    • Full Stack Development Course
    • Data Science Course
    • Python Programming Course
    • Software Testing Course
    • Architecting on AWS
  • Commerce
    • PwC Edge : Strategic Accounting and Finance Programme
    • ACCA Course
    • SAP Fico Course
    • Practical Accounting Course
    • SAP MM Course
    • HR Management Course in Kerala
  • Technical
    • Structural Design Course
    • Embedded Systems Software Engineering in Kerala
    • MEP Course
    • MEP QS Course
    • BIM Training Course
    • Quantity Survey Course
  • Finacademy
    • Forex Trading Course
    • Stock Market Course
    • Mutual Funds Course in Kerala
  • Digital Marketing
    • Digital Marketing Course
    • Performance Marketing Course
  • Language Learning
    • Spoken English Course
    • German Language Course
    • IELTS Exam Coaching
  • Exam Preparation
    • Kerala PSC Coaching
    • Kerala PSC LGS Coaching
    • KAS Coaching
    • Kerala PSC LDC Coaching
    • Kerala PSC LSGS Coaching
    • KPSC Industries Extension Officer Coaching
    • KPSC Technical Mechanical Coaching
    • KPSC Technical Electrical Coaching
    • Kerala PSC Technical Exam Preparation
    • KPSC Technical Civil Coaching
    • SBI PO Coaching
    • IBPS Clerk Coaching
    • IBPS PO Coaching
    • IBPS SO Coaching
    • Bank Exam Coaching
    • Lecturer in DIET Course
    • Kpsc HSA Coaching
    • KPSC LP UP Assistant Coaching
    • EMRS Coaching
    • KTET Coaching
    • SSC Coaching
    • SSC JE Coaching
    • SSC CGL Coaching
    • SSC CHSL Coaching
    • SSC MTS Coaching
    • UPSC Coaching
  • Health Care
    • DHA Course
    • OET Coaching
  • Other Exams
    • GATE Coaching
    • CAT Coaching
    • MAT Online Coaching
    • KMAT Online Coaching
Entri Blog
No Result
View All Result
Thursday, July 17, 2025
  • Kerala PSC
    • Kerala PSC General
      • Kerala PSC Exams
      • KAS Exam
      • Kerala PSC VEO Notification
      • Kerala Police SI
      • Kerala PSC LDC Notification
      • Kerala PSC Village Field Assistant Notification
      • Kerala PSC LD Typist Notification
      • Kerala PSC Food Safety Officer
      • Kerala PSC Excise Inspector Notification
      • Kerala PSC BDO Notification
      • Kerala PSC CPO Notification
      • Kerala PSC LGS Notification
      • Fire and Rescue Officer
      • Women Fire and Rescue Officer
    • Kerala PSC Teaching
      • Kerala PSC HSA
      • Kerala PSC HSST
      • KTET
      • LP/UP Assistant
      • KVS
      • Railway Teacher
    • Kerala PSC Technical
      • LSGD AE
      • Draftsman/Overseer
      • IEO
      • ATP
      • Polytechnic Lecturer
      • AE Electronics
      • Assistant Engineer
  • Banking
    • IBPS PO Notification
    • IBPS Clerk Notification
    • SBI PO Notification
    • SBI Clerk Notification
    • SBI SO Notification
    • SBI Apprentice Notification
    • Canara Bank PO Notification
    • Indian Bank PO Notification
    • RBI Assistant Notification
    • RBI Office Attendant Notification
    • IBPS RRB Notification
    • IBPS RRB Office Assistant Notification
  • Skilling
    • Coding
    • Spoken English
    • Stock Market
      • Share Market Today News
      • Share Market Analysis
    • Digital Marketing
    • Accounting Course
    • SAP
    • OET
    • German
    • IELTS
    • Quantity Survey
    • MEP
    • Structural Design
    • Montessori
    • Yoga Teaching
    • Oil and Gas
  • Courses
    • Data Science Course
      • Data Science Malayalam
      • Data Science Course in Tamil
      • Data Science Course in Telugu
      • Data Science Training in Kochi
      • Data Science Training in Trivandrum
      • Data Science Course in Calicut
      • Data Science Training in Thrissur
    • Data Analytics Course
      • Data Analytics Course in Malayalam
      • Data Analytics Course in Tamil
      • Data Analytics Course in Telugu
    • BIM Course in Kerala
    • Quantity Surveying Course
      • Quantity Surveyor Course in Tamil
      • Quantity Surveying Course in Telugu
      • Quantity Surveyor Course in Hyderabad
      • Quantity Surveying Course in Chennai
      • Quantity Surveying Course in Vijayawada
      • Quantity Surveying Course in Guntur
      • Quantity Surveying Course in Warangal
      • Quantity Surveying Course in Karimnagar
    • Structural Design Course
      • Steel Structure Design Course
      • RCC Design Course
    • MEP Course
      • MEP Quantity Surveying Course
      • MEP Design Course in Kerala
      • HVAC Course
      • MEP Course in Kochi
    • Embedded System Course
    • Full Stack Developer Course
      • Full Stack Development Malayalam
      • Full Stack Development Hindi
      • Full Stack Development Tamil
      • Full Stack Development Telugu
      • Full Stack Development Kannada
    • Stock Market Course
      • Stock Market Course in Malayalam
      • Stock Market Course in Tamil
      • Stock Market Course in Kannada
      • Options Trading Course
    • Mutual Funds Course in Kerala
    • Mutual Funds Course in Tamil
    • Forex Trading Course
    • Spoken English Course
      • Spoken English Course in Malayalam
      • Spoken English Course in Hindi
      • Spoken English Course in Telugu
      • Spoken English Course in Tamil
      • Spoken English Course in Kannada
    • Python Programming Course
    • AI Powered Flutter
    • Ui Ux Designer Course
    • Software Testing Course
      • Software Testing Course in Malayalam
      • Software Testing Course in Tamil
      • Software Testing Course in Telugu
    • AWS Solutions Architect Associate Course
    • PwC Edge : Strategic Accounting and Finance Programme
    • Practical Accounting Course
      • Accounting Course in Malayalam
      • Accounting Course in Tamil
      • Tally Course
      • Taxation Course
      • UAE Accounting
      • GST Course
      • Zoho Books Training
      • GST Practitioner Course in Kerala
      • GCC VAT Course
    • ACCA Training Online
      • ACCA Course in Kerala
    • SAP Training Courses
      • SAP FICO Course
      • SAP MM Course
      • SAP SD Training
      • SAP FICO Course in Tamil
      • SAP MM Course in Tamil
    • HR Management Course
    • Performance Marketing Course
    • Mern Stack Developer Course
      • Full Stack Developer Course in Kochi
      • Full Stack Developer Course in Trivandrum
      • Full Stack Developer Course in Calicut
      • Full Stack Developer Course in Pune
      • Full Stack Developer Course in Bangalore
      • Full Stack Developer Course in Hyderabad
      • Full Stack Developer Course in Chennai
      • Full Stack Developer Course in Indore
      • Full Stack Developer Course in Jaipur
      • Full Stack Developer Course in Coimbatore
    • Digital Marketing Training
      • Digital Marketing Course in Kochi
      • Digital Marketing Course in Trivandrum
      • Digital Marketing Course in Calicut
      • Digital Marketing Course in Kollam
      • Digital Marketing Course in Thrissur
      • Digital Marketing Course in Kottayam
      • Digital Marketing Course in Kannur
    • Montessori Teacher Training
      • Montessori Teacher Training in Calicut
      • Montessori Teacher Training in Kochi, Ernakulam
      • Montessori Teacher Training in Trivandrum
      • Montessori Teacher Training in Kollam
      • Montessori Teacher Training in Malayalam
      • Montessori Teacher Training in Kannada
    • Other Courses
      • German Language Course
      • OET Coaching
        • OET for Pharmacist
        • OET for Nurses
      • DHA Course
      • MOH Exam Coaching
      • Nurses Recruitment Abroad
      • FMGE Coaching
      • ChatGPT Course
      • Forex Trading Course
      • Yoga Teacher Training Course
      • Diploma in Early Childhood Care and Education
      • Business Administration & Hospital Management Course
      • Hospital and Healthcare Administration
      • AI for Teachers Course
  • Others
    • Railway
      • RRB Technician Notification
      • RRB JE Notification
      • RRB ALP Notification
      • RRB NTPC Notification
      • RRB RPF Notification
      • RRB RPF SI Notification
      • RRB RPF Constable Notification
      • RRB Group D Notification
      • RRB RPSF SI Notification
      • RRB SSE Notification
    • SSC
      • SSC CPO Notification
      • SSC MTS Notification
      • SSC CGL Notification
      • SSC CHSL Notification
      • SSC GD Constable Notification
      • SSC Stenographer Notification
      • SSC JE Notification
      • SSC CAPF Notification
      • SSC JHT Notification
      • Delhi Police Constable Notification
      • Delhi Police SI Notification
    • GATE
    • MAT
    • KMAT
      • KMAT Kerala 2024
    • TET
      • APTET
      • CTET
      • DSSSB
      • Karnataka TET
      • Kerala TET
        • KTET Notification
      • KVS
      • MPTET
      • SUPER TET
      • TNTET
      • TSTET
      • UPTET
    • UPSC
      • UPSC CAPF Notification
      • UPSC CDS Notification
      • UPSC NDA Notification
      • UPSC Civil service Notification
  • Free Learn
    • Free Courses
      • Free Coding Courses
        • C Langauge Courses
        • Free Data Science Courses
        • Free Python Programming Courses
        • Free Full Stack Developer Courses
      • Free Accounting Courses
      • Free Digital Marketing Courses
        • Free SEO Courses
        • Web Development for Marketers
      • Engineering Upskilling Courses
      • Free Hospital Administration Courses
      • Free OET Training
      • Free SAP Courses
      • Free German Courses
      • Free IELTS Courses
      • Free Teachers Training Courses
      • Free Trading Courses
        • Free Stock Market Courses in Malayalam
        • Free Options Trading Course Malayalam
        • Free Forex Trading Courses Malayalam
        • Free Personal Finance Courses Malayalam
    • Question Answer
    • Aptitude Questions
    • Webinars
  • Test Prep
    • Kerala PSC Coaching
      • KPSC Degree Level Exam Preparation
      • Kerala PSC 12th Level Exam Preparation
      • Kerala PSC 10th Level Exam Preparation
      • KAS Coaching
      • Kerala PSC LDC Coaching
      • Kerala PSC LD Typist Coaching
      • Kerala PSC Fireman Coaching
      • KSFE Assistant Exam Preparation
      • Secretariat Office Attendant Coaching
      • Secretariat Assistant Coaching
      • Kerala PSC SI Exam Coaching
      • Khadi Board LDC Exam Preparation
      • Devaswom Board LDC Coaching
      • University LGS Exam Preparation
      • University Assistant Exam Coaching
      • KPSC Scientific Officer Exam Coaching
      • KPSC Probation Officer Grade II
      • KPSC Food Safety Officer Coaching
      • KWA Sanitary Chemist Coaching
      • KPSC Diary Farm Instructor Exam Coaching
      • Kerala PSC KWA Lab Assistant Exam Coaching
      • Kerala PSC ICDS Supervisor Exam Coaching
      • KPSC Finger Print Searcher Coaching
      • Kerala PSC Welfare Officer Gr. II Exam Coaching
      • KPSC Assistant Dental Surgeon Coaching
    • KPSC Teaching Exams
      • HSA Exam Preparation
      • KPSC HSA Social Science Coaching
      • KPSC HSA Physical Science Coaching
      • KPSC HSA English Coaching
      • KPSC Junior Language Teacher Hindi
      • KTET Coaching
      • SET Coaching
      • LP/UP Assistant Exam Coaching
      • HSST Exam Preparation
      • HSST Commerce Exam Coaching
      • Kerala PSC Lecturer in Diet Coaching
      • KVS PGT/TGT Coaching
    • Kerala PSC Technical Exams Coaching
      • Assistant Engineer Electrical Exam Coaching
      • Assistant Engineer Electronics Coaching
      • Assistant Engineer Mechanical Coaching
      • Kerala PSC Industries Extension Officer Coaching
      • KPSC LSGD AE Exam Coaching
      • KPSC Civil Engineering Exam Coaching
      • KPSC Mechanical Engineering Exam Coaching
      • KPSC Electrical Engineering Exam Coaching
      • KPSC Electronics Engineering Exam Coaching
      • KPSC Technical Computer Science Coaching
      • KPSC Tradesman Turning Coaching
      • Kerala PSC Overseer Exam Coaching
      • KPSC Assistant Town Planner Coaching
      • Lecturer in Polytechnic Electronics Coaching
    • Teaching Exams Coaching
      • CSIR NET Exam Coaching
      • EMRS Coaching
      • CTET Exam Online Coaching
    • IELTS Training Online
    • Bank Exam Coaching Online
      • SEBI Grade A Coaching
      • IBPS SO Online Coaching
      • IBPSC PO Online Coaching
      • IBPSC Clerk Online Coaching
      • SBI PO Coaching
      • SBI Clerk Online Coaching
      • SBI Apprentice Class
      • SBI SO Online Coaching
      • RBI Grade B Coaching
      • RBI Assistant Coaching
      • CSEB Exam Coaching
      • IBPS RRB Coaching
      • IBPS RRB PO Coaching
      • Canara Bank Po Exam Coaching
      • Kerala Gramin Bank Coaching
      • Kerala Bank Exam Coaching
        • Kerala Bank Clerk Exam Coaching
        • Kerala Bank Office Attendant Exam Coaching
        • Kerala Bank Assistant Manager Exam Coaching
      • JCI Exam Coaching
      • NABARD Grade A Coaching
      • LIC AAO Coaching Online
    • UPSC Online Coaching
      • UPSC CAPF Online Coaching
    • SSC Online Coaching
      • SSC JE Online Coaching
      • SSC CGL Coaching
      • SSC CGL AAO Coaching
      • SSC CHSL Coaching
      • SSC CGL CBI Inspector Coaching
      • SSC MTS Coaching
      • SSC Stenographer Coaching
      • SSC GD Online Coaching
    • RRB Exams Coaching
      • RRB RPF Exam Coaching
      • RRB NTPC Exam Coaching
      • RRB JE Online Coaching
      • RRB JE Electronics Coaching
      • RRB ALP Exam Coaching
      • RRB Technician Exam Coaching
    • CAT Online Coaching
    • CMAT Online Coaching
    • XAT Exam Coaching
    • GATE Online Coaching
      • GATE CE Online Coaching
      • GATE ME Online Coaching
      • GATE EE Online Coaching
    • NMAT Exam Coaching
      • AAI ATC Jr Executive Coaching
    • Cochin Shipyard Executive Trainee Coaching
    • BEL Probationary Engineer Coaching
    • HPCL Junior Executive Coaching
Free Courses
Entri Blog
  • HTML Tutorial
  • DSA Tutorials
  • Tutorials in Hindi
    • HTML Tutorial for Beginners in Hindi
    • Python Tutorial for Beginners in Hindi
    • GIT and GITHUB Tutorial for Beginners in Hindi
  • Tutorials in Tamil
    • JavaScript Tutorial in Tamil
  • Tutorials in Telugu
    • HTML Tutorial For Beginners in Telugu
    • CSS Tutorial for Beginners in Telugu
    • Bootstrap Tutorial for Beginner in Telugu
  • Tutorials in Kannada
    • HTML Tutorial For Beginners in Kannada
entri exclusive offers entri exclusive offers
Home Articles

Arrays in Java: Basic Understanding

by Akhil M G
November 19, 2022
in Articles, Data Science and Machine Learning, Java Programming, React Native, Web and Android Development
Arrays in Java: Basic Understanding
Share on FacebookShare on WhatsAppShare on Telegram

Table of Contents

  • What are Arrays In Java? 
  • Declaring Array Variables
  • Creating Arrays
  • Processing Arrays
  • The foreach Loops
  • Passing Arrays to Methods
  • Returning an Array from a Method
  • The Arrays Class
  • Multidimensional Arrays:

The array data structure in Java maintains a fixed-size sequential collection of items of the same type. A collection of data is stored in an array, although it is generally more convenient to conceive of an array as a collection of variables of the same type. Instead of defining individual variables such as number0, number1,…, and number99, you declare one array variable such as numbers and represent individual variables with numbers[0], numbers[1,…, numbers[99]. This article will teach you how to define array variables, build arrays, and process arrays using indexed variables.

What are Arrays In Java? 

In Java, an array is a collection of variables with similar types that share a name. Arrays in Java behave differently from arrays in C/C++. The following are some key features concerning Java arrays. All arrays in Java are allocated dynamically. (See discussion below.) Contagious memory [consecutive memory regions] is where arrays are stored. Because arrays are objects in Java, we may use the object attribute length to determine their length. This differs from C/C++, where we use sizeof to get the length. A Java array variable may be declared similarly to other variables by adding [] after the data type. The array’s variables are sorted, each with an index starting at 0. A static field, a local variable, or a method parameter can all be utilized with a Java array.

An array’s size must be supplied as an int or short number, not as a long value. The object is the array type’s direct superclass. Every array type implements the Cloneable and java.io interfaces. Serializable. This array storage allows us to access array items at random [Support Random Access]. The array’s size cannot be changed (once initialized). On the other hand, an array reference may be used to point to another array. Depending on the specification of the array, an array can include primitives (int, char, etc.) as well as object (or non-primitive) pointers to a class. The actual values of primitive data types are stored in contiguous memory regions. The real objects of class objects are kept in a heap section.

Learn Coding in your Language! Enroll Here!

Declaring Array Variables

1: What is the default value of a boolean in Java?

2: What is the TreeSet Interface?

3: Which loop construct in Java best suits when the number of iterations is known?

4: What is the size of a byte variable?

5: Which of the following is thread safe?

    Fill out the form to see the results



    Ever wondered how much you really know? It's time to put your brain to the test!

    To utilize an array in a program, you must define a variable to refer to the array and specify the kind of array that the variable may refer to. The syntax for defining an array variable is as follows:

    Syntax

    dataType[] arrayRefVar;   // preferred way.
    or
    dataType arrayRefVar[];  // works but not preferred way.
    

    Note − The style dataType[] arrayRefVar is preferred. The style dataType arrayRefVar[] comes from the C/C++ language and was adopted in Java to accommodate C/C++ programmers.

    Example

    The following code snippets are examples of this syntax −

    double[] myList;   // preferred way.
    or
    double myList[];   // works but not preferred way.
    

    Power up your career with Entri Elevate – Full Stack Development Course!

    Explore Free Coding Courses !

    Take your first step toward mastering in-demand skills, acing interviews, and securing top-tier jobs with Entri's free coding courses.

    👉 Explore Free Courses Now

    Creating Arrays

    You can make an array with the new operator and the following syntax:

    Syntax

    arrayRefVar = new dataType[arraySize];

    The above sentence accomplishes two objectives.

    It constructs an array with new dataType[arraySize].

    It assigns the freshly generated array’s reference to the variable arrayRefVar.

    Declaring an array variable, generating an array, and assigning the array’s reference to the variable can all be done in a single statement, as demonstrated below.

    dataType[] arrayRefVar = new dataType[arraySize];
    

    Alternatively, you can create arrays as follows −

    dataType[] arrayRefVar = {value0, value1, ..., valuek};

    The index is used to access the array of items. Array indices are 0-based, ranging from 0 to arrayRefVar.length-1.

    Example
    The following statement defines an array variable, myList, constructs a double-type array of 10 members, and assigns its reference to myList.

    double[] myList = new double[10];

    The image below depicts the array myList. MyList has 10 double values with indexes ranging from 0 to 9.
    Java Array

    Power up your career with Entri Elevate – Full Stack Development Course!

    Processing Arrays

    Because all of the elements in an array are of the same type and the array’s size is known, we frequently use either a for loop or a foreach loop when processing array elements.

    Example
    Here is an example of how to construct, initialize, and handle arrays.

    public class TestArray {
    
       public static void main(String[] args) {
          double[] myList = {1.9, 2.9, 3.4, 3.5};
    
          // Print all the array elements
          for (int i = 0; i < myList.length; i++) {
             System.out.println(myList[i] + " ");
          }
         
          // Summing all elements
          double total = 0;
          for (int i = 0; i < myList.length; i++) {
             total += myList[i];
          }
          System.out.println("Total is " + total);
          
          // Finding the largest element
          double max = myList[0];
          for (int i = 1; i < myList.length; i++) {
             if (myList[i] > max) max = myList[i];
          }
          System.out.println("Max is " + max);  
       }
    }

    This will produce the following result −

    Output

    1.9
    2.9
    3.4
    3.5
    Total is 11.7
    Max is 3.5

    Learn Coding in your Language! Enroll Here!

    The foreach Loops

    JDK 1.5 added a new for loop called foreach loop or extended for loop that allows you to explore the entire array sequentially without needing an index variable.

    Example
    The code below displays all of the elements in the array myList.

    public class TestArray {
    
       public static void main(String[] args) {
          double[] myList = {1.9, 2.9, 3.4, 3.5};
    
          // Print all the array elements
          for (double element: myList) {
             System.out.println(element);
          }
       }
    }

    This will produce the following result −

    Output

    1.9
    2.9
    3.4
    3.5

    Explore Free Coding Courses !

    Take your first step toward mastering in-demand skills, acing interviews, and securing top-tier jobs with Entri's free coding courses.

    👉 Explore Free Courses Now

    Passing Arrays to Methods

    Arrays can be sent to methods in the same way that primitive-type values can. The following technique, for example, displays the elements of an int array.

    Example

    public static void printArray(int[] array) {
       for (int i = 0; i < array.length; i++) {
          System.out.print(array[i] + " ");
       }
    }

    You can invoke it by passing an array. For example, the following statement invokes the printArray method to display 3, 1, 2, 6, 4, and 2 −

    Example

    printArray(new int[]{3, 1, 2, 6, 4, 2});

    Returning an Array from a Method

    An array can also be returned by a method. The following function, for example, returns an array that is the inverse of another array

    Example

    public static int[] reverse(int[] list) {
       int[] result = new int[list.length];
    
       for (int i = 0, j = result.length - 1; i < list.length; i++, j--) {
          result[j] = list[i];
       }
       return result;
    

    Learn Coding in your Language! Enroll Here!

    The Arrays Class

    The static methods in the java.util.Arrays classes are used for sorting and searching arrays, comparing arrays, and filling array items. All primitive types have these methods overloaded.

    public static int binarySearch(Object[] a, Object key)

    The binary search algorithm is used to search the supplied array of Objects (Byte, Inte, Double, etc.) for the specified value. Prior to performing this call, the array must be sorted. If the search key is found in the list, it returns the index; otherwise, it returns (- (insertion point + 1)).

    public static boolean equals(long[] a, long[] a2)

    If the two supplied long arrays are equal, this function returns true. If two arrays have the same number of items and all related pairs of elements in the two arrays are equal, they are deemed equal. If the two arrays are equal, this function returns true. All other basic data types might utilize the same way (Byte, short, Int, etc.)

    public static void fill(int[] a, int val)

    The supplied int value is assigned to each element of the specified array of ints. All other basic data types might utilize the same way (Byte, short, Int, etc.)

    public static void sort(Object[] a)

    Sorts the supplied array of items in ascending order according to their natural ordering. All other basic data types might utilize the same way ( Byte, short, Int, etc.)

    One-Dimensional Arrays: 

    The general form of a one-dimensional array declaration is

    type var-name[]; OR type[] var-name;
    An array declaration consists of two parts: the type and the name. type specifies the array’s element type. The data type of each array element is determined by the element type. We may generate an array of other primitive data types, such as char, float, double, and so on, in the same way, that we can make an array of integers (objects of a class). As a result, the array’s element type decides what type of data the array will carry.

    Example:

    // both are valid declarations
    int intArray[]; 
    or int[] intArray; 
    
    byte byteArray[];
    short shortsArray[];
    boolean booleanArray[];
    long longArray[];
    float floatArray[];
    double doubleArray[];
    char charArray[];
    
    // an array of references to objects of
    // the class MyClass (a class created by
    // user)
    MyClass myClassArray[]; 
    
    Object[]  ao,        // array of Object
    Collection[] ca;  // array of Collection
                         // of unknown type

    Despite the fact that the initial declaration specifies intArray as an array variable, no real array exists. It just informs the compiler that this variable (intArray) will contain an array of integers. To connect intArray to a physical array of integers, allocate one using new and assign it to intArray.

    Learn Coding in your Language! Enroll Here!

    Instantiating an Array in Java

    Only a reference to an array is created when it is defined. To generate or allocate memory to the array, construct an array in the following manner: The general form of new in the context of one-dimensional arrays is as follows:

    var-name = new type [size]; where type defines the type of data being allocated, size provides the number of array elements, and var-name is the name of the array variable that is connected to the array. You must provide the type and number of items to allocate when using new to allocate an array.

    Example:

    int intArray[];    //declaring array
    intArray = new int[20];  // allocating memory to array

    OR

    int[] intArray = new int[20]; // combining both statements in one
    The array items allocated by new are immediately initialized to zero (for numeric types), false (for boolean types), or null (for reference types). In Java, use the default array values.
    Getting an array requires two steps. First, create a variable of the required array type. Second, you must use new to allocate memory for the array and assign it to the array variable. As a result, all arrays in Java are dynamically allocated.

    Accessing Java Array Elements using for Loop

    Each array entry is accessed by its index. The index ranges from 0 to (total array size)-1. Using Java for Loop, all array items may be retrieved.

    // accessing the elements of the specified array
    for (int i = 0; i < arr.length; i++)
      System.out.println("Element at index " + i + 
                                    " : "+ arr[i]);

    Implementation:

    • Java
    // Java program to illustrate creating an array
    // of integers,  puts some values in the array,
    // and prints each value to standard output.
     
    class GFG {
        public static void main(String[] args)
        {
            // declares an Array of integers.
            int[] arr;
     
            // allocating memory for 5 integers.
            arr = new int[5];
     
            // initialize the first elements of the array
            arr[0] = 10;
     
            // initialize the second elements of the array
            arr[1] = 20;
     
            // so on...
            arr[2] = 30;
            arr[3] = 40;
            arr[4] = 50;
     
            // accessing the elements of the specified array
            for (int i = 0; i < arr.length; i++)
                System.out.println("Element at index " + i
                                   + " : " + arr[i]);
        }
    }

    Output

    Element at index 0 : 10
    Element at index 1 : 20
    Element at index 2 : 30
    Element at index 3 : 40
    Element at index 4 : 50

    Arrays of Objects

    In the same way as an array of primitive type data items is formed, an array of objects is created in the following manner.

    new student[] arr Student[7]; /student is a custom class.
    The studentArray includes seven memory regions, one for each student class size, in which the addresses of seven Student objects can be placed. The Student objects must be constructed using the Student class’s function Object() { [native code] }, and their references must be allocated to the array elements in the following manner.

    [5] Student[] arr = new Student

    Example

    • Java
    // Java program to illustrate creating
    //  an array of objects
     
    class Student {
        public int roll_no;
        public String name;
        Student(int roll_no, String name)
        {
            this.roll_no = roll_no;
            this.name = name;
        }
    }
     
    // Elements of the array are objects of a class Student.
    public class GFG {
        public static void main(String[] args)
        {
            // declares an Array of integers.
            Student[] arr;
     
            // allocating memory for 5 objects of type Student.
            arr = new Student[5];
     
            // initialize the first elements of the array
            arr[0] = new Student(1, "aman");
     
            // initialize the second elements of the array
            arr[1] = new Student(2, "vaibhav");
     
            // so on...
            arr[2] = new Student(3, "shikar");
            arr[3] = new Student(4, "dharmesh");
            arr[4] = new Student(5, "mohit");
     
            // accessing the elements of the specified array
            for (int i = 0; i < arr.length; i++)
                System.out.println("Element at " + i + " : "
                                   + arr[i].roll_no + " "
                                   + arr[i].name);
        }
    }

    Output

    Element at 0 : 1 aman
    Element at 1 : 2 vaibhav
    Element at 2 : 3 shikar
    Element at 3 : 4 dharmesh
    Element at 4 : 5 mohit

    Time Complexity: O(n)
    Auxiliary Space : O(1)

    Power up your career with Entri Elevate – Full Stack Development Course!

    Example

    An array of objects is also created like an array of primitive type data items.

    Student[] studentArray; // No array created
    Student[] studentArray = new Student[5]; // Array of 5 students created but No studen

    The studentArray has five memory slots, one for each student class, in which the addresses of seven Student objects can be kept. The Student objects must be constructed using the Student class’s function Object() { [native code] }, and their references must be allocated to the array elements in the following manner.

    // Java program to illustrate creating
    //  an array of objects
       
    class Student
    {
        
        public String name;
        Student(String name)
        {
            this.name = name;
        }
          @Override
        public String toString(){
            return name;
        }
    }
       
    // Elements of the array are objects of a class Student.
    public class GFG
    {
        public static void main (String[] args)
        {
               // declares an Array and initializing  the elements of the array
            Student[] myStudents = new Student[]{new Student("Dharma"),new Student("sanvi"),new Student("Rupa"),new Student("Ajay")}; // Array of 5 students created but No students are there in the array
       
            // accessing the elements of the specified array
            for(Student m:myStudents){    
                System.out.println(m);
            }
        }
    }

    Output

    Dharma
    sanvi
    Rupa
    Ajay

    Multidimensional Arrays:

    Multidimensional array in java are arrays of arrays, with each array member containing the reference to another array. These are also referred to as Jagged Arrays. Appending one set of square brackets ([]) for each dimension results in a multidimensional array in java.

    int[][] intArray = new int[10][20]; //a 2D array or matrix
    int[][][] intArray = new int[10][20][10]; //a 3D array

    Example

    • Java
    public class multiDimensional {
        public static void main(String args[])
        {
            // declaring and initializing 2D array
            int arr[][]
                = { { 2, 7, 9 }, { 3, 6, 1 }, { 7, 4, 2 } };
     
            // printing 2D array
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++)
                    System.out.print(arr[i][j] + " ");
     
                System.out.println();
            }
        }
    }

    Output

    2 7 9 
    3 6 1 
    7 4 2

    If you are interested to learn new coding skills, the Entri app will help you to acquire them very easily. Entri app is following a structural study plan so that the students can learn very easily. If you don’t have a coding background, it won’t be a problem. You can download the Entri app from the google play store and enroll in your favorite course.

    Power up your career with Entri Elevate – Full Stack Development Course!

    Share61SendShare

    Akhil M G

    Related Posts

    Cover Letter Do's and Dont's
    Articles

    Cover Letter Do’s and Dont’s

    July 17, 2025
    Kerala PSC Staff Nurse Grade 2 Answer Key 2025
    Articles

    Kerala PSC Staff Nurse Grade 2 Answer Key 2025 PDF Released – Raise Objections

    July 17, 2025
    DevOps Engineer Career Path
    Articles

    DevOps Engineer Career Path

    July 17, 2025
    Next Post
    Commonly asked Interview Questions for Front End Developers

    Commonly asked Interview Questions for Front End Developers

    More to Explore

    1. Future Scope of Full Stack Developers
    2. Best Full Stack Developer Course with Placement
    3. Python Full Stack vs Java Full Stack Developers
    4. Top Trends in Full Stack Development Career
    5. Guide to Full Stack Web Development – Skills, Career Opportunities, Salary
    6. Full Stack Developer Salary for Freshers and Experienced
    7. Full Stack Developer Jobs and Career to Follow
    8. Full Stack JavaScript Development

    Free Tutorials For You

    1. SQL Tutorial for Beginners PDF
    2. Python Tutorial for Beginners in Hindi
    3. GIT and GITHUB Tutorial for Beginners in Hindi
    4. HTML Exercises to Practice
    5. JavaScript Tutorial in Tamil
    6. DSA Practice Series
    7. Microsoft Excel Malayalam Tutorial
    8. Learn Bootstrap in Tamil
    9. Introduction to CSS in Malayalam
    10. Introduction to JavaScript in Malayalam
    11. Java Programming Notes PDF
    12. Introduction to HTML in Malayalam

    Full Stack Training in Different Cities

    1. Full Stack Developer Course in Kochi
    2. Full Stack Developer Course in Trivandrum
    3. Full Stack Developer Course in Calicut
    4. Full Stack Developer Course in Pune
    5. Full Stack Developer Course in Bangalore
    6. Full Stack Developer Course in Hyderabad
    7. Full Stack Developer Course in Chennai
    8. Full Stack Developer Course in Coimbatore
    9. Full Stack Developer Course in Indore
    10. Full Stack Developer Course in Jaipur
    11. Full Stack Developer Course in Mumbai

    More to Learn

    1. Top C++ Interview Questions and Answers
    2. Top 100 C# Interview Questions and Answers
    3. Top 100 Angular Interview Questions and Answers
    4. Interview Questions and Answers for Web Development
    5. Career Options after Engineering

    Courses

    • Data Science Course
    • Full Stack Developer Course
    • Software Testing Course
    • Data Science Course in Malayalam
    • Full Stack Developer Course in Malayalam
    • Full Stack Developer Course in Hindi
    • Full Stack Developer Course in Tamil
    • Full Stack Developer Course in Telugu
    • Full Stack Developer Course in Kannada
    • Python Programming Course
    • Practical Accounting Course
    • Structural Design Course
    • Quantity Surveying Course
    • Embedded Systems Course in Kerala
    • BIM Course
    • MEP Course
    • MEP QS Course
    • Stock Market Course
    • Stock Market Course in Malayalam
    • Stock Market Course in Tamil
    • Stock Market Course in Kannada
    • Forex Trading Course
    • Mutual Funds Course in Kerala
    • Mutual Funds Course in Tamil
    • PwC Edge : Strategic Accounting and Finance Programme
    • SAP FICO Training
    • SAP MM Training
    • SAP SD Training
    • ACCA Online Course
    • HR Management Course

    Company

    • Become a teacher
    • Login to Entri Web

    Spoken English Courses

    • Spoken English Course
    • Spoken English Course in Malayalam
    • Spoken English Course in Hindi
    • Spoken English Course in Tamil
    • Spoken English Course in Telugu
    • Spoken English Course in Kannada
    • Spoken English Course for Housewives
    • Spoken English Course for Working Professionals
    • Spoken English Course for School Students
    • Spoken English Course for College Students
    • Spoken English Course for Job Seekers
    • AI Powered Spoken English Course

    Quick Links

    • Explore Free Courses
    • Aptitude Questions
    • Articles
    • Videos
    • Entri Daily Quiz Practice
    • Current Affairs & GK
    • News Capsule – eBook
    • Preparation Tips
    • Kerala PSC Gold
    • Entri Skilling
    • Kerala PSC

    Other Courses

    • OET Coaching Classes
    • DHA Course
    • Nurse Recruitment Abroad
    • Montessori Teachers Training
    • Performance Marketing Course
    • German Language Course
    • Yoga Teacher Training Course
    • Digital Marketing Course
    • Options Trading Course
    • German Language A1 Course
    • German Language A2 Course
    • German Language B1 Course
    • German Language B2 Course
    • Diploma in Early Childhood Care and Education
    • Hospital and Healthcare Administration
    • Business Administration & Hospital Management
    • Hospital and Healthcare Administration

    Popular Exam

    • IBPS Exam
    • SBI Exam
    • Railway RRB Exam
    • Kerala PSC
    • Tamil Nadu PSC
    • Telangana PSC
    • Andhra Pradesh PSC
    • MPPSC
    • UPPSC
    • Karnataka PSC
    • Staff Selection Commission Exam
    • GATE

    © 2023 Entri.app - Privacy Policy | Terms of Service

    No Result
    View All Result
    • Kerala PSC
      • Kerala PSC General
        • Kerala PSC Exams
        • KAS Exam
        • Kerala PSC VEO Notification
        • Kerala Police SI
        • Kerala PSC LDC Notification
        • Kerala PSC Village Field Assistant Notification
        • Kerala PSC LD Typist Notification
        • Kerala PSC Food Safety Officer
        • Kerala PSC Excise Inspector Notification
        • Kerala PSC BDO Notification
        • Kerala PSC CPO Notification
        • Kerala PSC LGS Notification
        • Fire and Rescue Officer
        • Women Fire and Rescue Officer
      • Kerala PSC Teaching
        • Kerala PSC HSA
        • Kerala PSC HSST
        • KTET
        • LP/UP Assistant
        • KVS
        • Railway Teacher
      • Kerala PSC Technical
        • LSGD AE
        • Draftsman/Overseer
        • IEO
        • ATP
        • Polytechnic Lecturer
        • AE Electronics
        • Assistant Engineer
    • Banking
      • IBPS PO Notification
      • IBPS Clerk Notification
      • SBI PO Notification
      • SBI Clerk Notification
      • SBI SO Notification
      • SBI Apprentice Notification
      • Canara Bank PO Notification
      • Indian Bank PO Notification
      • RBI Assistant Notification
      • RBI Office Attendant Notification
      • IBPS RRB Notification
      • IBPS RRB Office Assistant Notification
    • Skilling
      • Coding
      • Spoken English
      • Stock Market
        • Share Market Today News
        • Share Market Analysis
      • Digital Marketing
      • Accounting Course
      • SAP
      • OET
      • German
      • IELTS
      • Quantity Survey
      • MEP
      • Structural Design
      • Montessori
      • Yoga Teaching
      • Oil and Gas
    • Courses
      • Data Science Course
        • Data Science Malayalam
        • Data Science Course in Tamil
        • Data Science Course in Telugu
        • Data Science Training in Kochi
        • Data Science Training in Trivandrum
        • Data Science Course in Calicut
        • Data Science Training in Thrissur
      • Data Analytics Course
        • Data Analytics Course in Malayalam
        • Data Analytics Course in Tamil
        • Data Analytics Course in Telugu
      • BIM Course in Kerala
      • Quantity Surveying Course
        • Quantity Surveyor Course in Tamil
        • Quantity Surveying Course in Telugu
        • Quantity Surveyor Course in Hyderabad
        • Quantity Surveying Course in Chennai
        • Quantity Surveying Course in Vijayawada
        • Quantity Surveying Course in Guntur
        • Quantity Surveying Course in Warangal
        • Quantity Surveying Course in Karimnagar
      • Structural Design Course
        • Steel Structure Design Course
        • RCC Design Course
      • MEP Course
        • MEP Quantity Surveying Course
        • MEP Design Course in Kerala
        • HVAC Course
        • MEP Course in Kochi
      • Embedded System Course
      • Full Stack Developer Course
        • Full Stack Development Malayalam
        • Full Stack Development Hindi
        • Full Stack Development Tamil
        • Full Stack Development Telugu
        • Full Stack Development Kannada
      • Stock Market Course
        • Stock Market Course in Malayalam
        • Stock Market Course in Tamil
        • Stock Market Course in Kannada
        • Options Trading Course
      • Mutual Funds Course in Kerala
      • Mutual Funds Course in Tamil
      • Forex Trading Course
      • Spoken English Course
        • Spoken English Course in Malayalam
        • Spoken English Course in Hindi
        • Spoken English Course in Telugu
        • Spoken English Course in Tamil
        • Spoken English Course in Kannada
      • Python Programming Course
      • AI Powered Flutter
      • Ui Ux Designer Course
      • Software Testing Course
        • Software Testing Course in Malayalam
        • Software Testing Course in Tamil
        • Software Testing Course in Telugu
      • AWS Solutions Architect Associate Course
      • PwC Edge : Strategic Accounting and Finance Programme
      • Practical Accounting Course
        • Accounting Course in Malayalam
        • Accounting Course in Tamil
        • Tally Course
        • Taxation Course
        • UAE Accounting
        • GST Course
        • Zoho Books Training
        • GST Practitioner Course in Kerala
        • GCC VAT Course
      • ACCA Training Online
        • ACCA Course in Kerala
      • SAP Training Courses
        • SAP FICO Course
        • SAP MM Course
        • SAP SD Training
        • SAP FICO Course in Tamil
        • SAP MM Course in Tamil
      • HR Management Course
      • Performance Marketing Course
      • Mern Stack Developer Course
        • Full Stack Developer Course in Kochi
        • Full Stack Developer Course in Trivandrum
        • Full Stack Developer Course in Calicut
        • Full Stack Developer Course in Pune
        • Full Stack Developer Course in Bangalore
        • Full Stack Developer Course in Hyderabad
        • Full Stack Developer Course in Chennai
        • Full Stack Developer Course in Indore
        • Full Stack Developer Course in Jaipur
        • Full Stack Developer Course in Coimbatore
      • Digital Marketing Training
        • Digital Marketing Course in Kochi
        • Digital Marketing Course in Trivandrum
        • Digital Marketing Course in Calicut
        • Digital Marketing Course in Kollam
        • Digital Marketing Course in Thrissur
        • Digital Marketing Course in Kottayam
        • Digital Marketing Course in Kannur
      • Montessori Teacher Training
        • Montessori Teacher Training in Calicut
        • Montessori Teacher Training in Kochi, Ernakulam
        • Montessori Teacher Training in Trivandrum
        • Montessori Teacher Training in Kollam
        • Montessori Teacher Training in Malayalam
        • Montessori Teacher Training in Kannada
      • Other Courses
        • German Language Course
        • OET Coaching
        • DHA Course
        • MOH Exam Coaching
        • Nurses Recruitment Abroad
        • FMGE Coaching
        • ChatGPT Course
        • Forex Trading Course
        • Yoga Teacher Training Course
        • Diploma in Early Childhood Care and Education
        • Business Administration & Hospital Management Course
        • Hospital and Healthcare Administration
        • AI for Teachers Course
    • Others
      • Railway
        • RRB Technician Notification
        • RRB JE Notification
        • RRB ALP Notification
        • RRB NTPC Notification
        • RRB RPF Notification
        • RRB RPF SI Notification
        • RRB RPF Constable Notification
        • RRB Group D Notification
        • RRB RPSF SI Notification
        • RRB SSE Notification
      • SSC
        • SSC CPO Notification
        • SSC MTS Notification
        • SSC CGL Notification
        • SSC CHSL Notification
        • SSC GD Constable Notification
        • SSC Stenographer Notification
        • SSC JE Notification
        • SSC CAPF Notification
        • SSC JHT Notification
        • Delhi Police Constable Notification
        • Delhi Police SI Notification
      • GATE
      • MAT
      • KMAT
        • KMAT Kerala 2024
      • TET
        • APTET
        • CTET
        • DSSSB
        • Karnataka TET
        • Kerala TET
        • KVS
        • MPTET
        • SUPER TET
        • TNTET
        • TSTET
        • UPTET
      • UPSC
        • UPSC CAPF Notification
        • UPSC CDS Notification
        • UPSC NDA Notification
        • UPSC Civil service Notification
    • Free Learn
      • Free Courses
        • Free Coding Courses
        • Free Accounting Courses
        • Free Digital Marketing Courses
        • Engineering Upskilling Courses
        • Free Hospital Administration Courses
        • Free OET Training
        • Free SAP Courses
        • Free German Courses
        • Free IELTS Courses
        • Free Teachers Training Courses
        • Free Trading Courses
      • Question Answer
      • Aptitude Questions
      • Webinars
    • Test Prep
      • Kerala PSC Coaching
        • KPSC Degree Level Exam Preparation
        • Kerala PSC 12th Level Exam Preparation
        • Kerala PSC 10th Level Exam Preparation
        • KAS Coaching
        • Kerala PSC LDC Coaching
        • Kerala PSC LD Typist Coaching
        • Kerala PSC Fireman Coaching
        • KSFE Assistant Exam Preparation
        • Secretariat Office Attendant Coaching
        • Secretariat Assistant Coaching
        • Kerala PSC SI Exam Coaching
        • Khadi Board LDC Exam Preparation
        • Devaswom Board LDC Coaching
        • University LGS Exam Preparation
        • University Assistant Exam Coaching
        • KPSC Scientific Officer Exam Coaching
        • KPSC Probation Officer Grade II
        • KPSC Food Safety Officer Coaching
        • KWA Sanitary Chemist Coaching
        • KPSC Diary Farm Instructor Exam Coaching
        • Kerala PSC KWA Lab Assistant Exam Coaching
        • Kerala PSC ICDS Supervisor Exam Coaching
        • KPSC Finger Print Searcher Coaching
        • Kerala PSC Welfare Officer Gr. II Exam Coaching
        • KPSC Assistant Dental Surgeon Coaching
      • KPSC Teaching Exams
        • HSA Exam Preparation
        • KPSC HSA Social Science Coaching
        • KPSC HSA Physical Science Coaching
        • KPSC HSA English Coaching
        • KPSC Junior Language Teacher Hindi
        • KTET Coaching
        • SET Coaching
        • LP/UP Assistant Exam Coaching
        • HSST Exam Preparation
        • HSST Commerce Exam Coaching
        • Kerala PSC Lecturer in Diet Coaching
        • KVS PGT/TGT Coaching
      • Kerala PSC Technical Exams Coaching
        • Assistant Engineer Electrical Exam Coaching
        • Assistant Engineer Electronics Coaching
        • Assistant Engineer Mechanical Coaching
        • Kerala PSC Industries Extension Officer Coaching
        • KPSC LSGD AE Exam Coaching
        • KPSC Civil Engineering Exam Coaching
        • KPSC Mechanical Engineering Exam Coaching
        • KPSC Electrical Engineering Exam Coaching
        • KPSC Electronics Engineering Exam Coaching
        • KPSC Technical Computer Science Coaching
        • KPSC Tradesman Turning Coaching
        • Kerala PSC Overseer Exam Coaching
        • KPSC Assistant Town Planner Coaching
        • Lecturer in Polytechnic Electronics Coaching
      • Teaching Exams Coaching
        • CSIR NET Exam Coaching
        • EMRS Coaching
        • CTET Exam Online Coaching
      • IELTS Training Online
      • Bank Exam Coaching Online
        • SEBI Grade A Coaching
        • IBPS SO Online Coaching
        • IBPSC PO Online Coaching
        • IBPSC Clerk Online Coaching
        • SBI PO Coaching
        • SBI Clerk Online Coaching
        • SBI Apprentice Class
        • SBI SO Online Coaching
        • RBI Grade B Coaching
        • RBI Assistant Coaching
        • CSEB Exam Coaching
        • IBPS RRB Coaching
        • IBPS RRB PO Coaching
        • Canara Bank Po Exam Coaching
        • Kerala Gramin Bank Coaching
        • Kerala Bank Exam Coaching
        • JCI Exam Coaching
        • NABARD Grade A Coaching
        • LIC AAO Coaching Online
      • UPSC Online Coaching
        • UPSC CAPF Online Coaching
      • SSC Online Coaching
        • SSC JE Online Coaching
        • SSC CGL Coaching
        • SSC CGL AAO Coaching
        • SSC CHSL Coaching
        • SSC CGL CBI Inspector Coaching
        • SSC MTS Coaching
        • SSC Stenographer Coaching
        • SSC GD Online Coaching
      • RRB Exams Coaching
        • RRB RPF Exam Coaching
        • RRB NTPC Exam Coaching
        • RRB JE Online Coaching
        • RRB JE Electronics Coaching
        • RRB ALP Exam Coaching
        • RRB Technician Exam Coaching
      • CAT Online Coaching
      • CMAT Online Coaching
      • XAT Exam Coaching
      • GATE Online Coaching
        • GATE CE Online Coaching
        • GATE ME Online Coaching
        • GATE EE Online Coaching
      • NMAT Exam Coaching
        • AAI ATC Jr Executive Coaching
      • Cochin Shipyard Executive Trainee Coaching
      • BEL Probationary Engineer Coaching
      • HPCL Junior Executive Coaching

    © 2023 Entri.app - Privacy Policy | Terms of Service

    Chat