0% found this document useful (0 votes)
18 views12 pages

Lec6 Mcas2130

The document discusses one-dimensional arrays in Java. It explains that arrays are dynamically allocated objects that can hold primitive types or object references. The document covers creating and initializing arrays using the new keyword and accessing array elements. It also discusses arrays of objects.

Uploaded by

Tdm Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views12 pages

Lec6 Mcas2130

The document discusses one-dimensional arrays in Java. It explains that arrays are dynamically allocated objects that can hold primitive types or object references. The document covers creating and initializing arrays using the new keyword and accessing array elements. It also discusses arrays of objects.

Uploaded by

Tdm Don
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

School of Computing Science and Engineering

Course Code : MCAS2130 Course Name: Programming in Java

UNIT II
Class and Methods

Name of the Faculty: Dr. Avneesh Kumar Program Name: MCA


Arrays- One-Dimensional
DSDM framework Arrays
• Arrays in Java
• An array is a group of like-typed variables that are referred to by a
common name.Arrays in Java work differently than they do in C/C++.
Following are some important point about Java arrays.
• In Java all arrays are dynamically allocated.(discussed below)
• Since arrays are objects in Java, we can find their length using member
length. This is different from C/C++ where we find length using sizeof.
• A Java array variable can also be declared like other variables with [] after
the data type.
• The variables in the array are ordered and each have an index beginning
from 0.
Program Name: MCA 2
Arrays- One-Dimensional
DSDM framework Arrays

• array can be also be used as a static field, a local variable or a method


parameter.
• The size of an array must be specified by an int value and not long or short.
• The direct superclass of an array type
• Every array type implements the
interfaces Clonis Object.eable and java.io.Serializable.
• Array can contains primitives (int, char, etc) as well as object (or non -
primitives) references of a class depending on the definition of array. In
case of primitives data types, the actual values are stored in contiguous
memory locations. In case of objects of a class, the actual objects are
stored in heap segment.

Program Name: MCA 3


Creating, Initializing, and Accessing an Array
DSDM framework

Program Name: MCA 4


DSDM framework


One-Dimensional Arrays :
The general form of a one-dimensional array declaration is

type var-name[]; OR type[] var-name; An array declaration has two components: the type and the name. type declares
the element type of the array. The element type determines the data type of each element that comprises the array.
Like array of int type, we can also create an array of other primitive data types like char, float, double..etc or user
defined data type(objects of a class).Thus, the element type for the array determines what type of data the array will
hold.
Example:

Program Name: MCA 5


DSDM framework

• 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

Program Name: MCA 6


DSDM framework

• // of unknown type Although the above first declaration establishes the fact
that intArray is an array variable, no array actually exists. It simply tells to the
compiler that this(intArray) variable will hold an array of the integer type. To
link intArray with an actual, physical array of integers, you must allocate one
using new and assign it to intArray.

Program Name: MCA 7


Instantiating an Array in Java
DSDM framework

• When an array is declared, only a reference of array is created. To actually


create or give memory to array, you create an array like this:The general form
of new as it applies to one-dimensional arrays appears as follows:
• var-name
Example:
• int intArray[]; //declaring array
• intArray = new int[20];
• // allocating memory to array = new type [size]; OR
• int[] intArray = new int[20]; // combining both statements in one

Program Name: MCA 8


DSDM framework

Program Name: MCA 9


Arrays
DSDM of Objects
framework

• Arrays of Objects
• An array of objects is created just like an array of primitive type data items in
the following way.
• Student[] arr = new Student[7]; //student is a user-defined class
• Student[] arr = new Student[5];

Program Name: MCA 10


References

References:

R. Naughton and H. Schildt – Java2 (The Complete Reference) –


Fifth Edition – TMH – 2004.

Program Name: MCA 11

You might also like