Java Array
Normally, array is a collection of similar type of elements that have contiguous memory
location.
Java array is an object the contains elements of similar data type. It is a data structure
where we store similar elements. We can store only fixed set of elements in a java array.
Array in java is index based, first element of the array is stored at 0 index.
Advantage of Java Array
B
Single Dimensional Array in java;
Syntax to Declare an Array in java
1. dataType[] arr; (or) //int[] ar;
2. dataType []arr; (or) //int []ar;
3. dataType arr[]; //int ar[]
Instantiation of an Array in java
1. arrayRefVar=new datatype[size];
Example of single dimensional java array
Let's see the simple example of java array, where we are going to declare, instantiate,
initialize and traverse an arra
1. class Testarray{
2. public static void main(String args[])
3. {
4.
5. int a[]=new int[5];//declaration and instantiation /
6. a[0]=10;//initialization
7. a[1]=20;
8. a[2]=70;
9. a[3]=40;
10. a[4]=50;
11.
12. //printing array
13. for(int i=0;i<[Link];i++)//length is the property of array
14. [Link](a[i]);
15.
16. }}
Test it Now
Output: 10
20
70
40
50
Declaration, Instantiation and Initialization of Java Array
We can declare, instantiate and initialize the java array together by:
1. int a[]={33,3,4,5};//declaration, instantiation and initialization
Let's see the simple example to print this array.
1. class Testarray1{
2. public static void main(String args[]){
3.
4. int a[]={33,3,4,5};//declaration, instantiation and initialization
5.
6. //printing array
7. for(int i=0;i<[Link];i++)//length is the property of array
8. [Link](a[i]);
9.
10. }}
Test it Now
Output:33
3
4
5
Passing Array to method in java
We can pass the java array to method so that we can reuse the same logic on any array.
Let's see the simple example to get minimum number of an array using method.
1. class Testarray2{
2. static void min(int arr[]){
3. int min=arr[0];
4. for(int i=1;i<[Link];i++)
5. if(min>arr[i])
6. {
7. min=arr[i];
8. }
9.
10. [Link](min);
11. }
12.
13. public static void main(String args[]){
14.
15. int a[]={33,3,4,5};
16. min(a);//passing array to method
17.
18. }}
Test it Now
Output:3
Multidimensional array in java
In such case, data is stored in row and column based index (also known as matrix form).
Syntax to Declare Multidimensional Array in java
1. dataType[][] arrayRefVar; (or)
2. dataType [][]arrayRefVar; (or)
3. dataType arrayRefVar[][]; (or)
4. dataType []arrayRefVar[];
Example to instantiate Multidimensional Array in java
1. int[][] arr=new int[3][3];//3 row and 3 column
Example to initialize Multidimensional Array in java
1. arr[0][0]=1;
2. arr[0][1]=2;
3. arr[0][2]=3;
4. arr[1][0]=4;
5. arr[1][1]=5;
6. arr[1][2]=6;
7. arr[2][0]=7;
8. arr[2][1]=8;
9. arr[2][2]=9;
Example of Multidimensional java array
Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional
array.
1. class Testarray3{
2. public static void main(String args[]){
3.
4. //declaring and initializing 2D array
5. int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
6.
7. //printing 2D array
8. for(int i=0;i<3;i++){
9. for(int j=0;j<3;j++){
10. [Link](arr[i][j]+" ");
11. }
12. [Link]();
13. }
14.
15. }}
Test it Now
Output:1 2 3
2 4 5
4 4 5
What is the class name of java array?
In java, array is an object. For array object, an proxy class is created whose name can be
obtained by getClass().getName() method on the object.
1. class Testarray4{
2. public static void main(String args[]){
3.
4. int arr[]={4,4,5};
5.
6. Class c=[Link]();
7. String name=[Link]();
8.
9. [Link](name);
10.
11. }}
Test it Now
Output:
Copying a java array
We can copy an array to another by the arraycopy method of System class.
Syntax of arraycopy method
1. public static void arraycopy(
2. Object src, int srcPos,Object dest, int destPos, int length
3. )
Example of arraycopy method
1. class TestArrayCopyDemo {
2. public static void main(String[] args) {
3. char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
4. 'i', 'n', 'a', 't', 'e', 'd' };
5. char[] copyTo = new char[7];
6.
7. [Link](copyFrom, 2, copyTo, 0, 7);
8. [Link](new String(copyTo));
9. }
10. }
Test it Now
Output:caffein
Addition of 2 matrices in java
Let's see a simple example that adds two matrices.
1. class Testarray5{
2. public static void main(String args[]){
3. //creating two matrices
4. int a[][]={{1,3,4},{3,4,5}};
5. int b[][]={{1,3,4},{3,4,5}};
6.
7. //creating another matrix to store the sum of two matrices
8. int c[][]=new int[2][3];
9.
10. //adding and printing addition of 2 matrices
11. for(int i=0;i<2;i++){
12. for(int j=0;j<3;j++){
13. c[i][j]=a[i][j]+b[i][j];
14. [Link](c[i][j]+" ");
15. }
16. [Link]();//new line
17. }
18.
19. }}
Test it Now
Output:2 6 8
6 8 10
Wrapper class in Java
Wrapper class in java provides the mechanism to convert primitive into object and object
into primitive.
Since J2SE 5.0, autoboxing and unboxing feature converts primitive into object and object
into primitive automatically. The automatic conversion of primitive into object is known and
autoboxing and vice-versa unboxing.
One of the eight classes of [Link] package are known as wrapper class in java. The list of
eight wrapper classes are given
below:
Primitive Type Wrapper class
boolean Boolean
char Character
byte Byte
short Short
int Integer
long Long
float Float
double Double
Wrapper class Example: Primitive to Wrapper
1. public class WrapperExample1{
2. public static void main(String args[]){
3. //Converting int into Integer
4. int a=20;
5. Integer i=[Link](a);//converting int into Integer
6. Integer j=a;//autoboxing, now compiler will write [Link](a) internally
7.
8. [Link](a+" "+i+" "+j);
9. }}
Output:l
20 20 20
Wrapper class Example: Wrapper to Primitive
1. public class WrapperExample2{
2. public static void main(String args[]){
3. //Converting Integer to int
4. Integer a=new Integer(3);
5. int i=[Link]();//converting Integer to int
6. int j=a;//unboxing, now compiler will write [Link]() internally
7.
8. [Link](a+" "+i+" "+j);
9. }}
Output:
3 3 3
Call by Value and Call by Reference in Java
There is only call by value in java, not call by reference. If we call a method passing a
value, it is known as call by value. The changes being done in the called method, is not
affected in the calling method.
Example of call by value in java
In case of call by value original value is not changed. Let's take a simple example:
1. class Operation{
2. int data=50;
3.
4. void change(int data){
5. data=data+100;//changes will be in the local variable only
6. }
7.
8. public static void main(String args[]){
9. Operation op=new Operation();
10.
11. [Link]("before change "+[Link]);
12. [Link](500);
13. [Link]("after change "+[Link]);
14.
15. }
16. }
download this example
Output:before change 50
after change 50
Another Example of call by value in java
In case of call by reference original value is changed if we made changes in the called
method. If we pass object in place of any primitive value, original value will be changed. In
this example we are passing object as a value. Let's take a simple example:
1. class Operation2{
2. int data=50;
3.
4. void change(Operation2 op){
5. [Link]=[Link]+100;//changes will be in the instance variable
6. }
7.
8.
9. public static void main(String args[]){
10. Operation2 op=new Operation2();
11.
12. [Link]("before change "+[Link]);
13. [Link](op);//passing object
14. [Link]("after change "+[Link]);
15.
16. }
17. }
download this example
Output:before change 50
after change 150
java Strictfp Keyword
Java strictfp keyword ensures that you will get the same result on every platform if you
perform operations in the floating-point variable. The precision may differ from platform to
platform that is why java programming language have provided the strictfp keyword, so that
you get same result on every platform. So, now you have better control over the floating-
point arithmetic.
Legal code for strictfp keyword
The strictfp keyword can be applied on methods, classes and interfaces.
1. strictfp class A{}//strictfp applied on class
1. strictfp interface M{}//strictfp applied on interface
1. class A{
2. strictfp void m(){}//strictfp applied on method
3. }
Illegal code for strictfp keyword
The strictfp keyword cannot be applied on abstract methods, variables or constructors.
1. class B{
2. strictfp abstract void m();//Illegal combination of modifiers
3. }
1. class B{
2. strictfp int data=10;//modifier strictfp not allowed here
3. }
1. class B{
2. strictfp B(){}//modifier strictfp not allowed here
3. }