How are arrays implemented in java? - java

Arrays are implemented as objects in java right? If so, where could I look at the source code for the array class. I am wondering if the length variable in arrays is defined as a constant and if so why it isn't in all capital letters LENGTH to make the code more understandable.

Although arrays are Objects in the sense that they inherit java.lang.Object, the classes are created dynamically as a special feature of the language. They are not defined in source code.
Consider this array:
MySpecialCustomObject[] array;
There is no such source code for that. You have created it in code dynamically.
The reason why length is in lower case and a field is really about the fact that the later Java coding standards didn't exist at the time this was developed. If an array was being developed today, it would probably be a method: getLength().
Length is a final field defined at object construction, it isn't a constant, so some coding standards would not want that to be in upper case. However in general in Java today everything is generally either done as a constant in upper case or marked private with a public getter method, even if it is final.

For every Array we declare, corresponding classes are there in Java but it's not available to us.You can see the classes by using getClass().getName()
int[] arr=new int[10];
System.out.println(arr.getClass().getName());
Output : [I
where "[" represents one dimension array and "I" represents Integer.
Similarly, we can have
[F for one-dimensional float arrays
[Z for one-dimensional boolean arrays
[J for one-dimensional long arrays
[[I for two-dimensional int arrays
and so on.

Implementing array in Java requires access to memory location or do pointer arithmetic. As Java does not let you to allocate memory, it does the Arrays implementation for you. Java language provides that implementation.

We can say that An array is a container that holds a fixed length of data of single data type.
eg.
int[] MyArray = new int[101]; // allocates memory for 101 integers, Range from 0 to 100.
and for multidimensional
String[][] names = {{"FirstName", "LastName"},{"Kaji", "Islam"},...};
and for character array
char[] ch={'a','b'....};

Related

Why is an ArrayList of ArrayLists not multidimensional?

I recently appeared for an interview in which the interviewer asked me a question regarding Arrays and ArrayList.
He asked me if an array of arrays can be multidimensional, then why is an ArrayList of ArrayList's not multidimensional?
For example:
// Multidimensional
int[][] array = new int[m][n];
// Not multidimensional
ArrayList<ArrayList<Integer>> seq = new ArrayList<ArrayList<Integer>>();
Can anyone help me to understand this?
Cay S. Horstmann has stated within his book Core Java for the impatient:
There are no two-dimensional array lists in Java, but you can declare a
variable of type ArrayList<ArrayList<Integer>> and build up the rows
yourself.
due to the fact that ArrayLists can expand and shrink and become jagged rather than multi-dimensional, one could say it is not a two-dimensional array, multi-dimensional meaning fixed rows and columns, hence why I have also stated within the comments Java doesn't have true multi-dimensional arrays but this is outside the scope of your question.
if you're curious as to why I said Java doesn't have true multi-dimensional arrays have a read at the differences between a multidimensional array and an array of arrays in C#?
Just to make my answer clearer regarding whether Java has true multi-dimensional arrays or not, I did not say java doesn't have multi-dimensional arrays, I said Java doesn't have true multi-dimensional arrays and as expect the JLS has stated:
A multidimensional array need not have arrays of the same length at
each level.
For the same reason the shopping bag I put all my spare shopping bags into is not a multidimensional shopping bag.
If I put a nut in one bag then put that bag in another bag, I have to perform two operations to get the nut.
If I instead put the nut in a two dimensional component tray, I can perform one operation to access it using two indices:
source
Similarly, there is a fundamental difference between a list of lists ( or array of arrays ) and a true two dimensional array - a single operation taking two indices is used to access the elements in a two dimensional array, two operations each taking one index are used to access the elements in a list of lists.
An ArrayList has a single index, so it has a rank of 1. A two dimensional array has two indices, its rank is 2.
note: by 'two dimensional array' I am not referring to a Java array of (references to) arrays, but a two dimensional array as found in other languages such as FORTRAN. Java does not have multidimensional arrays. If your interviewer was specifically referring to Java 'arrays of arrays' then I would disagree with them, as Java's int[][] defines an array of references to arrays of integers, and that requires two dereferencing operations to access the elements. An array of arrays in C for example supports access with a single dereferencing operation so is closer to the multidimensional case.
I'm going to go out on a limb here and answer this one, however there is no correct answer for this broad question.
We first have to ask, what makes an array multidimensional?
I'm going to assume that your interviewer considers a multidimensional array one with a fixed size (as you've shown in your question), where it cannot be considered "jagged". According to Microsoft, a jagged array in C# is as follows:
The elements of a jagged array can be of different dimensions and sizes.
In Java, a multidimensional array is simply an array, where each element is also an array. These arrays must be defined with a fixed size in order for elements to be indexed within them, but jagged arrays can be of different sizes, as stated above.
An ArrayList is backed by an array; however, the array expands when a certain number of elements are added to it. For this reason, the ArrayList could become jagged, and could be considered not to be multidimensional any longer.
Edit: After rereading everything over a few times, I'm sure that your interviewer was just trying to confuse you. It honestly doesn't make sense for one data type (an array) to be multidimensional, and another data type (an ArrayList that uses an array) to not be multidimensional.
Looking at it from the other side: you can use lists in the very same way as "multi dimensional" arrays. You only have to replace array[row][column] with someList.get(row).get(column)!
And in the end, java arrays are implemented in similar ways: a two dim matrix is also just a one dim array of one dim arrays! In other words: the difference is more on the surface, not rooted in deep conceptual reasons!
And to be really precise: the Java type system allows you to put down Object[][] so in that sense, it knows that type of Object[][]; but as said, in reality, there are no multi-dimensional arrays; as Java sees that "two dim" thing as an array of references to arrays!
On the other hand: there is a certain notion of "multi dimensional arrays", as for example the JVM specification explicitly mentions:
The first operand of the multianewarray instruction is the run-time constant pool index to the array class type to be created. The second is the number of dimensions of that array type to actually create. The multianewarray instruction can be used to create all the dimensions of the type, as the code for create3DArray shows. Note that the multidimensional array is just an object and so is loaded and returned by an aload_1 and areturn instruction, respectively.
The interviewer's claim is nonsensical.
One can argue, as you see on this page, that Java does not have true multidimensional arrays, in which case it does not have multidimensional ArrayLists either. On the other hand, it certainly allows you to represent multidimensional structures via arrays and ArrayLists in the same way.
To define a major distinction between the two is fairly arbitrary and pointless.
Possibly the interviewer was just trying to start a technical debate, to test your ability to explain the details.
An ArrayList is an implementation of List. It's a List that is implemented using arrays. The usage of arrays is an implementation detail. The list interface doesn't support the concept of multi-dimensional lists therefore you wouldn't expect ArrayList to either. Further it isn't addressed as a use case of a traditional list data structure.
Arrays support multi-dimensionality because it's a language feature of Java.
Because it isn't dimensional at all. It is an object with an API. Any appearance of multi-dimensionality is provided by its API, but it is purely in the eye of the beholder. An array on the other hand is dimensional, and can therefore be multidimensional too.

Purpose of new keyword in creating array in Java

I want to know why an array created in Java static even when we use the new keyword to define it.
From what I've read, the new keyword allocates a memory space in the heap whenever it is encountered during run time, so why give the size of the array at all during definition.
e.g. Why can't
int[] array1=new int[20];
simply be:
int[] array1=new int[];
I know that it does not grow automatically and we have ArrayList for that but then what is the use of keyword new in this? It could have been defined as int array1[20]; like we used to do it in C, C++ if it has to be static.
P.S. I know this is an amateurish question but I am an amateur, I tried to Google but couldn't find anything comprehensive.
This may be an amateurish question, but it is one of the best amateurish questions you could make.
In order for java to allow you to declare arrays without new, it would have to support an additional kind of data type, which would behave like a primitive in the sense that it would not require allocation, but it would be very much unlike a primitive in the sense that it would be of variable size. That would have immensely complicated the compiler and the JVM.
The approach taken by java is to provide the bare minimum and sufficient primitives in order to be able to get most things done efficiently, and let everything else be done using objects. That's why arrays are objects.
Also, you might be a bit confused about the meaning of "static" here. In C, "static" means "of file scope", that is, not visible by other object files. In C++ and in Java, "static" means "belongs to the class" rather than "belongs to instances of the class". So, the term "static" is not suitable for describing array allocation. "Fixed size" or "fixed, predefined size" would be more suitable terms.
Well, in Java everything is an object, including arrays (they have length and other data). Thats why you cannot use
int var[20];
In java that would be an int and the compiler would be confused. Instead by using this:
int[] var;
You are declaring that var is of type int[] (int array) so Java understands it.
Also in java the length of the array and other data are saved on the array, for this reason you don't have to declare size of array during declaration, instead when creating an array (using new) the data are saved.
Maybe there is a better reason that oracle may have answered already, but the fact that in Java everything is an object must have something to do with it. Java is quite specific about objects and types, unlike C where you have more freedom but everything is more loose (especially using pointers).
The main idea of the array data structure is that all its elements are located in the sequential row of memory cells. That is why you can not create array with variable size: it should be unbounbed space vector in memory for this purpose, which is impossible.
If you want change size of array, you should recreate it.
Since arrays are fixed-size they need to know how much memory to allocate at the time they are instantiated.
ArrayLists or other resizing data structures that internally use arrays to store data actually re-allocate larger arrays when their inner array data
structure fills up.
My understanding of OP's reasoning is:
new is used for allocating dynamic objects (which can grow like, ArrayList), but arrays are static (can't grow). So one of them is unnecessary: the new or the size of the array.
If that is the question, then the answer is simple:
Well, in Java new is necessary for every Object allocation, because in Java all objects are dynamically allocated.
Turns out that in Java, arrays are objects, different from C/C++ where they are not.
All of Java's variables are at most a single 64bit field. Either primitives like
integer (32bit)
long (64bit)
...
or references to Objects which depending on JVM / config / OS are 64 or 32 bit fields (but unlike 64bit primitives with atomicity guaranteed).
There is no such thing as C's int[20] "type". Neither is there C's static.
What int[] array = new int[20] boils down to is roughly
int* array = malloc(20 * sizeof(java_int))
Each time you see new in Java you can imagine a malloc and a call to the constructor method in case it's a real Object (not just an array). Each Object is more or less just a struct of a few primitives and more pointers.
The result is a giant network of relatively small structs pointing to other things. And the garbage collector's task is to free all the leaves that have fallen off the network.
And this is also the reason why you can say Java is copy by value: both primitives and pointers are always copied.
regarding static in Java: there is conceptually a struct per class that represents the static context of a class. That's the place where static instance variables are anchored. Non-static instance variables are anchored at with their own instance-struct
class Car {
static int[] forAllCars = new int[20];
Object perCar;
}
...
new Car();
translates very loosely (my C is terrible) to
struct Car-Static {
Object* forAllCars;
};
struct Car-Instance {
Object* perCar;
};
// .. class load time. Happens once and this is referenced from some root object so it can't get garbage collected
struct Car-Static *car_class = (struct Car-Static*) malloc(sizeof(Car-Static));
car_class->forAllCars = malloc(20 * 4);
// .. for every new Car();
struct Car-Instance *new_reference = (struct Car-Instance*) malloc(sizeof(Car-Instance));
new_reference.perCar = NULL; // all things get 0'd
new_reference->constructor();
// "new" essentially returns the "new_reference" then

initialize java array vs other initialization?

I know that when I initialize a char array:
I have to
char[] b= new char[5];
or
char[] b= new char[5]({1,2,3,4,5});
why not like
ArrayList<Charset> list = new ArrayList<Charset>();
initialize array :
char[] b = new char[5](); ?
Why they are different? Is it one of java philosophical nature or some reasons behind it ?
If you've ever used C, then the answer is fairly simple. In C, the way you create arrays is by allocating a static length of memory on the stack that is large enough to contain the number of elements, and point to the first element with a pointer - or dynamic length of memory on the heap, and point to the first element with a pointer.
int a[5]; //stack, static allocation
int* a = (int*)malloc(sizeof(int)*5)); //heap, dynamic allocation
And in C++, the second version was changed to this, obviously because it's more obvious what is happening:
int* a = new int[5];
And they took this type of array creation over to Java.
int[] a = new int[5];
Arrays don't really work like typical objects, hence why even creating them and manipulating them with reflection uses a different Array class in order to manipulate the object. (see http://docs.oracle.com/javase/tutorial/reflect/special/arrayInstance.html )
ArrayLists are different, because they're just everyday classes like most things in java, so you initialize them with an actual constructor call:
List<T> = new ArrayList<T>();
Basically, arrays and classes just work in different ways.
That's is simply design of Java. ArrayList and Arrays are two different things. No need to be same declaration.
I guess the guys who created Java wanted to keep a syntax close to the C syntax. In Java, arrays are minimalist low-level objects, so their case is a bit particular.
ArrayList is a container, it's similar as Vector in C++, it can add and remove elements, but array can't change its size
Arrays and ArrayList are used for different purposes. If you need a fixed size collection of objects then go for array but if you need dynamically growing collection of objects then go for arraylist. In some way compiler need to know about what is your need, hence the syntax is different.

Java arrays and objects

If arrays are treated as objects in Java and if we have a multidimensional array like an array of integer arrays then is it correct to say that this multidimensional array is also an array of objects?
I am kinda confused as I have read that arrays can hold only two types i.e. either primitives or references so if an array holds an array will it be correct to regard the same a array of objects?
Is it correct to say that this multidimensional array is also an array of objects?
Yes, that is correct. An array is an object. So, an array of arrays, is an array of objects.
Yes it is correct because arrays are objects
Arrays are not treated as objects, they are objects.
Multidimensional arrays are "arrays of arrays". You can get more information here http://www.ensta-paristech.fr/~diam/java/online/notes-java/data/arrays/arrays-2D-2.html
So it is also possible to create multi-dimensional arrays which are not square and so on
Yes, your reasoning is correct. When we say "array of objects" in Java we generally mean "array of references to objects", so when we have something like
int[][] x = ...;
then x is an array holding references to int[]s (i.e. int arrays).
From the JLS ยง10:
The component type of an array may itself be an array type. The components of such an array may contain references to subarrays. If, starting from any array type, one considers its component type, and then (if that is also an array type) the component type of that type, and so on, eventually one must reach a component type that is not an array type; this is called the element type of the original array, and the components at this level of the data structure are called the elements of the original array.
There are some situations in which an element of an array can be an array: if the element type is Object or Cloneable or java.io.Serializable, then some or all of the elements may be arrays, because any array object can be assigned to any variable of these types.

How to calculate the length of array using a macro function?

In java is it possible and if not how to calculate in c?
if it's really array (not a pointer), you can do sizeof(arr)/sizeof(*arr)
In Java, the length of a primitive array is array.length, while the length of an ArrayList (and most other collections) is arrayList.size()
In C, the length of an array is sizeof(array) / sizeof(array[0]), but this is nearly useless since you can't pass arrays as arguments (they degenerate to pointers). The normal way to find the size of an array in C is to pass it as an extra argument to the function, or sometimes to terminate it with a sentinel value (eg. strings are \0 terminated)
There is no way to calculate that. in C (not C++, which has std::array and std::vector) an array is transmitted as its pointer, which you might increase by some offset. So you really don't know the runtime size of an "array", except by some conventions.
In particular for formal arrays, there is no way to know the size of the actual array passed
e.g. as void f(int arr[]) { /*...*/ } unless you give a static dimension.
Likewise, with an external array declared as extern int xarr[]; you cannot get its dimension with sizeof(xarr)/sizeof(xarr[0]).
In C, there is no way of calculating the size of array if you have only a pointer to it. You must store it in separate variable.
In fact you HAVE TO keep the size of an array in separate variable because you have to allocate memory if you want to use dynamic-size array.
And if you want to use fixed-size array you know it's size by the time you're writing your code so why not use #define, variable or const to store it?
Java is totally different language than C and the philosophy of programming is different-you should always keep that in mind.
In Java, you should use array.length, look here for example: http://www.roseindia.net/help/java/a/java-array-length.shtml
Simply, there is no possibility if you recieve only a pointer. That's why main has an argc argument. It defines the number of entries in argv. If you have an array "datatype" (actually the same as a pointer, but the behaviour depends on the context), you can use
int[] arr = new int[10];
sizeof(arr)/sizeof(int) // or whatever type is contained in ``arr``

Categories