Professional Documents
Culture Documents
2.arrays & Strings
2.arrays & Strings
ARRAYS
1. What is an array in Java?
An array is a finite and ordered collection of homogeneous data elements. It is
finite because it contains a limited number of elements. It is ordered because all
the elements are stored one by one in a contiguous location of computer memory
(heap) in a linear fashion. It is homogeneous because all elements of an array are
of the same data type only. We can store either primitive types or object
references into it.
2. Can you declare an Array without Array size?
No, you cannot declare Array without Array size. You will get compile time error.
3. Where does Array stored in JVM memory ?
Array is an object in java. So, Array is stored in heap memory in JVM.
4. Given a primitive Array in java, where does in JVM it is stored?
An Array will always be an object on heap memory, even if the Array is declared
to hold primitive elements.
5. What is the difference between ArrayStoreException and
ArrayOutOfBoundsException ?
ArrayStoreException is thrown if you are trying to add incompatible data type.
For example, if you try to add an integer object to String Array, then
ArrayStoreException is thrown.
ArrayOutOfBoundsException is thrown when an attempt is made to access the
Array with illegal index. For example, illegal index means if the index is either
negative or greater than or equal to the size of the Array.
6. What are the advantages of Array ?
a) We can sort multiple elements of Array at the same time
b) Using index, we can access the element of the Array in O(1) time.
Hence, Java has a special arrangement to store the string objects. String
Constant Pool is one such arrangement. String Constant Pool is the memory
Whenever you create a string object using string literal, JVM first checks the
content of the object to be created. If there exist an object in the string constant
pool with the same content, then it returns the reference of that object. It doesn’t
create a new object. If the content is different from the existing objects then
only it creates new object