QA's approach 2 Java - Problems & Solutions to Information storage in Java


4 ways to store information in java:

1. Using Variables

Variable is a container to hold only one value.

When to use?

When we want to store only one value.

Limitations: Can store only one value
if we require to store 120 values, we need 120 variables which will be placed at dis- continuous memory locations, so we have below 2 problems.

Problem 1:

Reading problem:
Data is stored at some random location, hence requires more time for reading it.

Problem2:

Sending problem:
If we take individual primitive variable, we cannot pass multiple values beyond the parameter we defined.
                                    

Cannot even return multiple values as the return type is primitive, to overcome this 2 & 3 have come into picture.

2. Using Class objects

When to use? 
When we store multiple, fixed no of values of different types


3. Using array objects

When to use? 
When we want to store multiple, fixed no of values of same type, in continuous memory locations


Arrays: Stores homogeneous elements of fixed size

Example:

Int[] a = new int[10]; //Primitive type array

Integer a[]=new Integer[20]; //Wrapper type array

Example Ex1=new Example[5]; //User defined Class type array
Object array: Stores dissimilar or heterogeneous elements of fixed size

Object obj[]=new object[20];

can store combination of primitive, reference, user defined elements

There are 4 Limitations:


1.Type problem
1.1. w.r.t primitive types 

            


Primitive array object can store its own primitive type or its sub types which are of lower range only will be accepted 

1.2. w.r.t Class types
           
                            


Employee class object cannot be stored in Student array object.

Student class array can only store student class objects or its sub class objects(i.e., its child class objects)

Class array:

When it comes to classes, its own class objects or its sub class objects can be stored

Class array object contains referenced variables in continuous memory locations.





Solution to Type problem:

Java provides inbuilt solution to type problem using inheritance

Object array is the parent class for all, creating an object class array- any type either primitive or referenced type objects can be stored as they are all sub classes to Object class.


2.Size

Array index out of bound

3.Storing order problem

Array by default stores the objects in insertion order & retrieves the objects in random order.

Wanted to store objects in Asc/Desc order.

Want to retrieve the objects in FIFO/LIFO.

Want to retrieve in Sequential manner.

4.Operators problem

Inbuilt supported methods are not available


Solution:

To solve Size, Sorting, Operators problem- collections came into picture.

4. Using collection objects

When to use? 
When we don't know the type & size of values to be sent by the end user
&
we want to store all the values as a single object

Where to use?

When user wanted to sent all the values from one application to another using a single method parameter.

Common operations performed in collection:

1. Create collection object
2. Add objects to collection
3. Return collection object or pass it as a method argument of some other program
4. Retrieve or remove or replace objects in the collection.

Comments

Popular posts from this blog

QA's approach 2 Java - Understanding Static context

Selenium 4 absolute beginners - How to create Batch execution file

Technologies - Log4J - Create Log4j Configuration File - Where ? How ? What ?