Loading
Arrays-Overview

An array is a collection of elements of the same data type stored in a contiguous memory location. Index of array in Java starts from 0.




Key Point

  • An array is index-based, and the index number starts from 0.
  • Array generally supports the same type of elements we cannot store different types of elements in the array.
  • If the array has a fixed size length it will not grow dynamically.
  • Array doesn't support any standard data structure it always stores elements linearly.
  • Array doesn't provide any inbuilt algorithms for searching and sorting.
  • If we don't know the number of elements and type of element we want to store non-linearly then we go for Data-Structures (In Java we call them Collections).


1.  Creating an array of integer type with required size.

Syntax:

int[] a1= new int[5]; 


Example:

package com.quipoin;

public class ArrayDemo1 {
	public static void main(String[] args) {
		int[]  a1= new int[5];
		System.out.println("Size of the array:"+a1.length);
		a1[0]=10;
		a1[1]=15;
		a1[2]=20;
		a1[3]=25;
		a1[4]=30;
		System.out.println("---------------------");
		System.out.println("Elements of an array:");
		for(int i=0;i<a1.length;i++) {
			System.out.println(a1[i]);
		}
		System.out.println("---------------------");
	}
}


Output:

Size of the array:5
---------------------
Elements of an array:
10
15
20
25
30
---------------------

2.  Creating an array of String type by adding the elements directly into an array

Syntax:

String a1[] = {"Akash", "Gagan", "Sharath", "Sudheer"};


Example:

package com.quipoin;

public class ArrayDemo2 {
	public static void main(String[] args) {
		String[]  a1= {"Akash","Gagan","Sharath","Sudheer"};
		System.out.println("Size of the array:"+a1.length);
		System.out.println("---------------------");
		System.out.println("Elements of an array:");
		for(int i=0;i<a1.length;i++) {
			System.out.println(a1[i]);
		}
		System.out.println("---------------------");
	}
}


Output:

Size of the array:4
---------------------
Elements of an array:
Akash
Gagan
Sharath
Sudheer
---------------------

Key Point

  • If we create an object class type array we can store any type of object in that array.
  • We can store any class type object, all the objects will be upcasted to Object class type.
  • We can store primitives in an object class type array, the primitive values are converted in Wrapper class type objects(Autoboxing) and it is upcasted to Object class type.


Storing Objects in an Array

Java allows storing objects in arrays. Here, we create an array of Student objects.


Example:

package com.quipoin;

public class Student {
	int rollNo;
	String name;
	double marks;
	public Student(int rollNo,String name,double marks) {
		this.rollNo=rollNo;
		this.name=name;
		this.marks=marks;
	}
	@Override
	public String toString() {
		return rollNo+"\t"+name+"\t"+marks;
	}
}
package com.quipoin;

public class StudentMain {
	public static void main(String[] args) {

		Student[] std=new Student[4];
		// storing student details in array
		std[0]=new Student(101, "Praveen", 69.50);
		std[1]=new Student(102, "Supriya", 83.25);
		std[2]=new Student(103, "Keerthi", 67.00);
		std[3]=new Student(104, "Raju", 71.00);

		System.out.println("Total Students are:"+std.length);
		System.out.println("Student Details:");
		System.out.println("---------------------");
		System.out.println("Roll_no\tName\tMarks");
		System.out.println("---------------------");
		for(int i=0;i<std.length;i++) {
			System.out.println(std[i].rollNo+"\t"+std[i].name+"\t"+std[i].marks);
		}
		System.out.println("---------------------");
	}
}


Output:

Total Students are:4
Student Details:
---------------------
Roll_no	Name	Marks
---------------------
101	    Praveen	69.5
102	    Supriya	83.25
103	    Keerthi	67.0
104	    Raju	71.0
---------------------


In this example, we have created a Student class and stored it in an array.


Wrapper Classes in Java

Java provides special wrapper classes to handle primitive types as objects.

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Boxing and Unboxing in Java

  • Converting primitive type value to respective Wrapper class type object is known as Boxing operation
  • Converting the Wrapper class object back to the primitive type is known as unboxing.
  • Automatic conversion between primitives and wrapper classes is known as autoboxing.
  • From JDK1.5 both boxing and un-boxing are done implicitly hence it is known as auto-boxing and auto un-boxing.

Example:

package com.quipoin;

public class WrapperClassDemo {
	public static void main(String[] args) {
		int x=10;
		System.out.println("x value is:"+x);

		Integer i1=10;                                            // Auto boxing
		System.out.println("i1 value is:"+i1);

		Integer i2=10;
		System.out.println("i2 value is:"+i2);

		if(i1.equals(i2)) {
			System.out.println("Both values are same");
		}
		else {
			System.out.println("Both the values are different");
		}
		System.out.println("------------------------");
		int p=i1;
		int q=i2;
		int res1=p+q;
		System.out.println(res1);

		System.out.println("------------------------");
		int res2=new Integer(100)+ new Integer(200);
		System.out.println(res2); // First un-boxing will happen then addition.
	    System.out.println("------------------------");
	} 
}


Output:

x value is:10
i1 value is:10
i2 value is:10
Both values are same
------------------------
20
------------------------
300
------------------------

Two Minute Drill

  • Array: An array is a fixed-size collection of elements of the same type.
  • Wrapper Classes: Wrapper classes are used to convert primitive data types into objects.
  • Autoboxing: Converting a primitive type to a wrapper object automatically.
  • Unboxing: Extracting the primitive value from a wrapper object automatically.