Tuesday, 29 October 2013

Creating Arrays in java

To create an array value in Java, you use the new keyword, just as you do to create an object. Arrays don't need to be initialized like objects do, however, so you don't pass a list of arguments between parentheses. What you must specify, though, is how big you want the array to be. If you are creating a byte[], for example, you must specify how many byte values you want it to hold. Array values have a fixed size in Java. Once an array is created, it can never grow or shrink. Specify the desired size of your array as a non-negative integer between square brackets: 

byte[] buffer = new byte[1024];
String[] lines = new String[50];
When you create an array with this syntax, each of the values held in the array is automatically initialized to its default value. This isfalse for boolean values, '\u0000' for char values, 0 for integer values, 0.0 for floating-point values, and null for objects or array values. 

No comments:

Post a Comment