Array creation with new StringBuffer:
We hinted before that our method for creating arrays, using array initializers, was limited. We used syntax like the following:
int []myArray = {1,2,1,2};
The above example creates an array of four integers, initialized to the values 1, 2, 1 and 2; the reference variable myArray is initialized to reference this array.
You would not want to create a large array this way, because you would have to list values for all the elements in the array individually.
All array types in Java are reference types, not primitives. As such, array objects also can be created using the new keyword. We could create an array with room for four int values as follows:
new int [4]
This expression is similar to new StringBuffer("text") in that it uses the word new, the type of the thing we want to create (int[]) and an argument (4). It is different in that it uses square brackets instead of parentheses around the argument.
To make an array reference refer to an array object we now have an alternative syntax. For example, we can write:
int []myArray = new int [4];
Unlike when we used an array initializer, we have not stated what the elements in this array are. We have only stated that there should be room for four integers to be stored. We can initialize these elements (they will default to the value 0) using assignment to individual locations:
myArray [0]= 1; myArray [1]= 2; myArray [2]= 1; myArray [3]= 2;
This is equivalent to the following:
int []myArray = {1,2,1,2};
The significance is that we now have syntax to create arrays of larger dimensions simply:
int [] myArray = new int [400];
Using an array initializer in this case would be hard to read and write, at the very least. We can still initialize elements of such an array, but we would be unlikely to do it by writing one assignment for each location. Instead, we would use iteration.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your Array creation with new StringBuffer homework and assignments? Live Array creation with new StringBuffer experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer Array creation with new StringBuffer homework help, java assignment help and Array creation with new StringBuffer projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours