Search This Blog

Showing posts with label Array in java. Show all posts
Showing posts with label Array in java. Show all posts

17 June, 2011

Array in java part 2

Part:- 2


In case of array in c/c++ a pointer is created implicitly and values can get assigned to locations out bounds, hence java doen't support array like c/c++.


But if sun Microsystems doesn't provide support for array then it would be a problem for a java programmer because array is a tool which provides the feature of managing a large collection with same id and different index.
Because of this reason java supports array but in a different fashion. In Java special treatment is given to the code which uses array.
The code is entirely changed by the compiler .


Look at the following code.


In the above example my assumptions are three words i.e. IntCollection, setValue and getValue.
Wherever the compiler found [] in the source code, the compiler changed it to either IntCollection , setValue or getValue.


The technical explanation is as follows
int x[]  a class pointer gets created 
x=new  a class pointer gets created
x=new int[5] an object of a class is created and 5 is passed to its parameterized constructor, and because of the constructor the object that gets created contains 5 locations for storing int type values. The object also contains a property is public as well as final. Its value can be accessed but cannot be changed.
x[0]=33 a setter method gets invoked on the object and the method is being passed  0 and 33 as arguments. The setter method assigns 33 to the first block out of the five blocks created to store int type value.


System.out.println(x[0]) a getter method is invoked on the object and the method is being passed 0 as argument. The method returns the value of the first block out of the five blocks.
In short its said that in java array is treated as an object. 
If a value is being assigned to a location out of bounds then code will get compiled be it won't get executed.


<<<< Array in java Part :-1


 Resizing an array>>> >

Array in java part 1

Part:- 1


The Following are examples of c++.
eg.1:-


void main()
{
int *p;
p=(int *) 6352;
*p=545;
}


The aove code gets compiled.
In the above example a memory location is being assigned to pointer p. Then through the pointer 545 is assigned to that particular location to which p points.
This is dangerous because the location might be within the scope of another application in a multi tasking environment.


Now examine the following code:-


class Bulb
{
// Some members
};
void main()
{
Bulb *p;
p=(Bulb*)6352;
*p=545;    // this line doesn't get compiled. 
}


The above code doesn't get compiled because through a class pointer a value can't be assigned to a location.


As per the above examples a pointer of simple data type can cause security problems because a value can be put up at any location through a simple data type pointer whereas this can't happen through a complex data type (class) pointer.


Because of this reason Java doesn't provide support for simple data type (long, int , short, byte, double, float, char and boolean) pointers.
Java provides support for complex data type (class) pointers.
                                                         Continue with part 2

Meetme@