Loading

Java Arrays

  • An array is a group of homogeneous(same type) elements,
  • It will not support heterogeneous(another type) elements.

Key points:

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

Creating an array of int 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
---------------------

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 points:

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

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 class in Java

Key points:

  • Java provides special classes known as Wrapper classes, these classes are used to eliminate primitive values.
  • Instead of using primitive types to store the data we use Wrapper class objects.
  • The Wrapper classes are immutable types.
Primitive typeWrapper class type
                           byte                                     Byte

short

                                      Short

int

Integer

long

Long

float

Float

double

Double

char

Character

boolean

Boolean


Boxing Operation

Key points:

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