IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Array in Java

Go down

Array in Java Empty Array in Java

Post by BIT0112-Rokon Sun Jan 10, 2010 5:00 am

Array in Java

We all more or less know about array. Though we have at least the idea about array, Im going to discuss about array, because it very important term in programming. So read it carefully. If you have more knowledge about array, no problem, read it please, so that you can help me by making concern about my mistakes.

Firstly I will remind you some basic term of java. We all know about data types as well as the primitive data types. There are eight primitive data types in java. They are:- byte, short, int, long, char, float, double and boolean. 

These can be put in four groups: 

Integers: These group includes short, long, int and byte. All of these are signed, positive and negative values. 
Note: integer are not unsigned in java. but other many programming language support unsigned. but java designer felt, unsigned are unnecessary.
the width short is 16 bit(2 Byte), int is 32 bit(4 byte), long is 64 bit(8 byte) and byte is 8 bit(1 byte).

Characters: it includes char, which represents letters and numbers as well as ASCII lists. Its width 16 bit(2 Byte);

Floating point types: it includes float and double which represents fractional numbers. the width of double is 64 bit(8 byte) and float is 32 bit(4 byte).

Booleans: boolean is a special type which represents true/false values. its width is 8 bit(1 byte).

We all know about declaration of variable. So I can skip it. My main goal is to discuss about array in java. so lets go through the topic. 

Array:
Array, what is it? okay lets see-. Array is nothing but a group of contiguous memory location that all have the same name and the same type. To refer a particular location or element in the array, we specify the name of the array and the position number of the particular element in the array. 
Any elements may be refereed by giving the name of the array following by the position number of the particular element in square bracket([]). The first element of the array is the zeroth element. 

Why array is needed? it can be your question. Array is very important terminology in programming language. For say, You might come across a situation where you need to store similar type of values for a large number of data items. what you'll do..? Will you declare thousands variable of similar type?For say, you need to store the marks of all the students of IIT. To store the marks of 30 student, you need to declare 30 variable of same type. But think, is it a good way? We the programmers always should prefer the best possible way, so that our program consume least time. So the alternative way is Array. 
Now lets discuss how can we Declare and allocate array.
We already know that array occupy space in memory. The programmer specifies the type of the element and use operator new to dynamically allocate the number of elements required by each array. 
[Note: Array are allocated with new because array are considered to be objects and all objects must be created with new]  
e.g.
int c[] = new int[12];
another way,
int c[]; // declare the array
c = new int[12]; //allocate the memory

lets see some more declaration.
byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

all shoud be dynamically allocate memory using new.

[Note: Like C or C++ declaration like int c[12] is syntax error in java]

Now lets go through a example.
Code:

public class ArrayExample_01 {
   public static void main(String[] args) {
      final int ARRAY_SIZE = 10;
      int rok[];       // declare an array
      rok = new int[ARRAY_SIZE]; // allocate the array

      for (int i = 0; i < rok.length; i++) { // set the value of array..
         rok[i] = 2 + 2 * i;
      }
      
      //print the values of array
      for (int i = 0; i < rok.length; i++) {
         System.out.println(rok[i]);
      }
   }
}
Call by reference and Call by value:
there is two way to pass arguments to methods. They are call by value and call by reference.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum