Explain how to initialize an array of objects

Explain with example how to initialize an array of objects.

Create an array with new key word as follows:
Film films[] = new Film[4];

Use individual element of array films[] with index for creating individual objects of the class. Select the appropriate constructor for sending parameters. The following example depicts this
films[0] = new Film("Shrek",133);
films[1] = new Film("Road to Perdition",117);
films[2] = new Film("The Truth about Cats and Dogs",93);
films[3] = new Film("Enigma",114);

Explain with example how to initialize an array of objects.

An array can be instantiatd in various ways as follows:
int[] arr = new int[5];

int arr[] = new int[5];

int[] arr = Array.newInstance[int, 5];

int[] arr = {0,1,2,3,4};

int[][] arr2 = new int[length1][length2];       //2D array

list1 = new Object[5];       //An array of objects

How and when can we cast from one class to another?
Java allows us to cast variables of one type to another as long as the casting happens between compatible data types.......
How to send to receive datagram packet?
To send or receive a packet, the sockets send or receive methods can be used: socket.send(packet); socket.receive(packet);....
Difference between field variable and local variable
Field variables: Variable that are declared as a member of a class. OR Variables declared outside any method/constructor but inside the class block......
Post your comment