Quick links
Latest articles
Internship
Members
New...
Algorithms
Discrete Mathematics
Big data
Languages
C
C++
C++ STL
Java
Data Structure
C#.Net
Android
Kotlin
SQL
Web
PHP
Python
JavaScript
CSS
Ajax
Node.js
Web prog.
Programs
C
C++
DS
Java
C#
Python
Aptitude
C
C++
Java
DBMS
Interview
C
Embedded C
Java
SEO
HR
CS Subjects
CS Basics
O.S.
Networks
DBMS
Embedded Systems
Cloud Computing
Machine learning
CS Organizations
Linux
DOS
More...
Articles
Puzzles
News/Updates

Home » Java programming language

Object as an Array in Java



In this article, we are going to learn about object as an array in java programming language with explanation and examples.
Submitted by Deepak Dutt Mishra, on June 26, 2018

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 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.

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.

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 Program{
	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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Featured post:
Introduction to Linux (Its modes, Safety, Most popular Applications)
Linux Best Distribution Software (Distros) of 2018

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.

Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.