Java Arrays Programming for beginners

Java arrays are objects.

In arrays we can store multiple variables of same type that are referred by a common name.
An array is always an object,even if the array is declared to hold object references or primitives .

In array variables are placed in indexed order.These variables are called elements of an array. So in java arrays we can access the elements or values through the index.Every element in an array is just a variable.

Now we have to know how to declare the array ,creation of array and accessing the elements from array.

Declaration of arrays are in two forms:

form1: type arrayname[]; Type followed by name of the array,followed by Square bracket .

ex: int abc[];

form2: type[] arrayname; Type followed by bracket,followed by name of the array.

ex: float[] abc;

form2 is the recommended way,because name is clearly separated from the type.

At the time of declaration, we should not enter the size of an array.violation leads to compile time exception.

for example int[5] a; it gives compile time exception.

After declaration of an array ,we can't put any other data type except the declared array type.
but implicitly promoted data types we can use, means less than the declared array type.for
example int[] x, the allowed data types are byte,short,char,int.

After creation of java arrays,once you fix the size of an array,we can't change the size of an array.But you can change an individual array element. If it is need to change the array size frequently while the program is running then we should go for another concept, known as Arraylist.

In java arrays are work differently than they do in c,c++.

creation of java arrays:

After declaring an array,we need to put it into memory.arrays must be declared in the memory before they are used.

Java arrays are creating only using new keyword .

array type[] array name=new array type[size];

Ex: int[] x=new int[50];

Array type is int , the allowed data types are byte/short/char/int.
This statement sets up an array that can hold 50 integers means 0 to 49 ,not 1 to 50 .

At the time of creation we must specify the size, otherwise compile time exception occurs.Even
we can give zero number also.but we can't specify the size with negative number we will get Run time Exception(RTE).we have to use integer to specify the size violation leads to compile time exception.

for example:

int[] x = new int[55]; It is OK

int[] x = new int[0]; It is OK

int[] x = new int[]; it gives compile time exception.

int[] x = new int[-5]; RTE, Negative Array Size Exception

int[] x = new int[9.5]; CTE,Possible loss of precission

we can write declaration and creation separately like this,

int[] x;

x=new int[8];

If you want to find the number of elements of an array, use arrayName.length.

For example :

for (int i = 0; i < style="font-weight: bold; color: rgb(51, 51, 255);">

Initialization of Arrays:

Once an array is created,all the elements will assign with default values based on declared data type.

For example : for int default value is zero.
for string default value is null.
for Boolean default value is false.

java arrays are created starting with a subscript of zero and ends with a value one lessthan the size declared.

In java arrays the declaration,creation and initialization in a single line is possible.
for this syntax, specify the declaration and size,later we can give the values at run time.

int a[];

a=new int[5];

a[0]=10;
a[1]=20;
a[2]=30;
a[3]=40;
a[4]=50;

Instead of above declaration,creation and initialization we can write like this,

int[] a={10,20,30,40,50};

But for this syntax, at the time of declaration we must know the array size and initialization values,can't give the values at run time.

It is possible to convert an array object to another array,but the array types must same.

int[] x={10,20,30};

int[] y;

y=x;

Accessing array elements:

int[] a=new int[5];

a[5]=60 it leads RTE, "Array Index Out Of Bound Exception".

a[4.0]=50;

If you are accessing array element with unmatched data type then it gives compile time exception, "Incorrect Syntax".

a[-4]=50 ; it leads RTE,"Array Index Out Of Bound Exception".

a[2.5] =50; it leads CTE, "Possible Loss of precision"

Either Float/Double/Long data type indexes should not use to accessing the array elements,violation leads to compile time exception.

Array element assignment is possible , when the class object is the subtype of element type.That means if an array is declared as object reference array ,we are allowed to assign the objects of either declared type or it's child classes.


ex 1: Number[] a = new Integer[5]; // Length 10, element type Integer

Double f = new Double(3.14); // Type Double, class Double

Integer i = new Integer(115); // Integer, class Integer

Number n = i; // Type Number, class Integer

a[0] = i; // OK, Integer is subtype of Integer

a[1] = n; // OK, Integer is subtype of Integer

a[2] = f; // No, Double not subtype of Integer

The above program successfully compile,but at run time a[2] =f throws "Array Store Exception", because the class of the object bound to f is not a subtype of a's element type (that is, Integer).

ex 2: String[] s = new String[5];

s[0] = "A"; it is OK

s[1]= new Integer[12]; CTE

Integer is not a child class of String,but expected thing is String or child class of String class .

ex 3: Object ob = new Object[8];

ob[0] = "X";

ob[1]= new Thread();

ob[2] = new Exception();

ob[3] = new Number();

ob[4] = new Integer(15);

the above program compile and run successfully, because all are subclasses of declared array type.
How do we Assignment of Array variables :

In general, we are allowed to place int variable in the place of double variable, because implicitly int will be promoted to double.but it is not possible to place int array in the place of double array. Like this , a character element can be assigned in the place of int element.But the char array can't be assigned in the place of int array.It gives compile time exception.


example : int[] a= {1,3,6,8};

char[] c={'a','b','c','d','e'};

int[] b = c; CTE, Incompatible Type


In the case of Object arrays ,we are allowed to place child class arrays in the place of parent class array.

example 1: wheels[] w= new wheels[8];
CLASSES AND OBJECTS IN JAVA

JAVA PROGRAMMING ENVIRONMENT

JAVA EXECUTION

JAVA CONSTRUCTORS

FIELD DECLARATIONS IN CLASS OF JAVA

EXCEPTIONS IN JAVA

EXCEPTIONS IN JAVA PART TWO

VARIABLE PARAMETERS IN JAVA

COMMENTS TYPES AND VARIABLES

JAVA JDBC AND ODBC

INTERFACE IN JAVA

OPERATORS IN JAVA



No comments:

Post a Comment