You don't know this yet - Advanced Java concepts Part 1
Tags: java, arrays
Published: 2023-01-29 19:25:28

Hello everyone,
I'm Dhiren Serai, currently working for AWS S3 and I use Java extensively on a daily basis. I'm starting to write some blog posts series about Java as I have realized some new things about Java which is worth documenting and didn't come obviously to me until I read them. Hence this is the motivation for me to start writing this blog post series called **"You don't know this yet".**


Java is divided into two parts i.e. Core Java (J2SE) and Advanced Java (JEE). The core Java part covers the fundamentals (data types, functions, operators, loops, thread, exception handling, etc.) of the Java programming language. 
 
Today we will start discussing about Arrays. Even though you will be surprised that Array isn't really an advanced concept and is taught in one of the first few lessons of any programming language course. This is true, but I'm not going to tell about how arrays work here but some interesting things I have noticed with Arrays in Java.

**Arrays - Definition**

An array is a fixed size, homogeneous data structure. The limitation of arrays is that they're fixed in size. It means that we must specify the number of elements while declaring the array.

Most of the people including me, think what if we don't know how many elements will be inserted into the array and we exceed the number that was declared in the program?

Hence, was born dynamic arrays in Java. The dynamic array is a variable size list data structure. It grows automatically when we try to insert an element if there is no more space left for the new element.

Then I tried this program, thinking that when I would declare an array of 100 elements, only to get reminded that index in Computer Science starts from 0 to 99 and not 1 to 100.

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

int[] a = new int[100]; // Allocate array in the heap, a points to the array
a[0] = 13;
a[1] = 26;
System.out.println(a.length); // read-only access .length -- 100
a[100] = 13;
 
}
 
}
```

**Output** : 

```
100
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100 out of bounds for length 100
	at MyArray.main(MyArray.java:10)
```

**Deep Copy vs Shallow Copy in Java**

The shallow copy of an object will have exact copy of all the fields of original object. If the original object from which shallow copy is made is changed, that will  lead to change to shallow copy object. This property is what makes it **"shallow"**, because the copy is still not independent of the original object.
Shallow copy is not 100% disjoint from original object. Shallow copy is not 100% independent of original object.

Deep copy of an object will have exact copy of all the fields of original object just like shallow copy.
Any changes made to cloned object will not be reflected in original object or vice versa.


**How to check for Shallow vs Deep Copy in Java?**

A simple program to check which functions can be used to check for shallow vs deep copy in Java:

```
import java.util.Objects ;
import java.util.Arrays;

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

String[] firstArray  = {"a", "b", "c"};
String[] secondArray = {"a", "b", "c"};

System.out.println("Are they equal 1    ? " + firstArray.equals(secondArray) );
System.out.println("Are they equal 2    ? " + Objects.equals(firstArray, secondArray) );

System.out.println("Are they deepEqual 1? " + Arrays.deepEquals(firstArray, secondArray) );
System.out.println("Are they deepEqual 2? " + Objects.deepEquals(firstArray, secondArray) );

}
 
}
```

Output :
```
dhirensr@dhirensr-XPS-9315:~/open_source/java_scripts$ java MyArray 
Are they equal 1    ? false
Are they equal 2    ? false
Are they deepEqual 1? true
Are they deepEqual 2? true
```


This brings us to end of the Java Array blog. I hope you have enjoyed this post on Java Array. With this, we come to an end to this blog. I will continue this series of Java on different topics and concepts which I have found interesting while programming in Java.
I also share content on [Twitter](https://twitter.com/DhirenSerai).
Also do let me know if you think I should document anything else that would be interesting to you.