Object as an Array in Java

In this tutorial, we are going to learn about object as an array in java programming language with explanation and examples. By Deepak Dutt Mishra Last updated : January 14, 2024

Arrays

Arrays are a group of homogeneous variables is a type of primitive data type which can be used in almost every programming language and referenced are by a common name. Though arrays in all programming languages may have initial properties as per the advancement of technologies and languages and their requirements arrays got more powerful and useful due to their less consumption and easy implementations. The arrays in Java functions differently than the ones we are introduced in C / C++:

Java Arrays

  • Java arrays are dynamically allocated as it involves two processes namely, the first one being the declaration of an array variable of desired variable type and the second process involves allocation of memory using the ‘new’ keyword and then assigning it to the array variable.
  • Arrays in Java are nothing but objects as we can find the size of a using the member function length().
  • A Java array variable like other variables can be declared with using '[]' after the data type of the array.
  • Each array in Java is ordered and starts from '0' index.
  • A Java array can be used as a local variable or static field or even as method parameters.
  • There exists a superclass of arrays in Java and are known as Objects.
  • The array cannot be a float, long, or short, it can only and only be an int value.

Java Object Array

As discussed above it is clear that in Java an array can store primitive data values as well as an object of classes. In case of primitive data values, the values are stored in contiguous memory locations whereas when arrays are used to store an object of classes then the values are stored into heap segments.

Creation of an object array

Object array can be created just like normal arrays in Java.

Example

//Where Student is a user-defined class
Student[] Arr = new Student[7];  

In the above example, after the declaration of the array, there is a formation of 7 memory spaces each of sizes equal to the class Student.

Object as an Array in Java

Program to demonstrate object as an array in Java

import java.util.Scanner;

class Employee {
  private int code;
  private String name;
  private long salary;

  Scanner in = new Scanner(System.in);

  void getEmployee() {
    System.out.println("Enter Code: ");
    code = in.nextInt();
    System.out.println("Enter Name: ");
    name = in.next();
    System.out.println("Enter Salary: ");
    salary = in.nextLong();
  }

  void putEmployee() {
    System.out.println(code + "," + name + "," + salary);
  }
}

public class Main {
  public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    System.out.println("No. of Employees: ");
    int n = in.nextInt();
    Employee E[] = new Employee[n];

    for (int i = 0; i < E.length; i++) {
      E[i] = new Employee();
      E[i].getEmployee();
    }

    for (int i = 0; i < E.length; i++) {
      E[i].putEmployee();
    }
  }
}

Output

No. of Employees:
5
Enter Code:
1
Enter Name:
David
Enter Salary:
20000
Enter Code:
2
Enter Name:
James
Enter Salary:
15500
Enter Code:
3
Enter Name:
Peter
Enter Salary:
10000
Enter Code:
4
Enter Name:
Ryan
Enter Salary:
16000
Enter Code:
5
Enter Name:
Jonas
Enter Salary:
25000

1,David,20000
2,James,15500
3,Peter,10000
4,Ryan,16000
5,Jonas,25000


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.