Creation of objects of Array in Java
i know that any kind of array is object. But i am facing a basic problem and i am not understanding it. now i am explaing it:
when we made an object of non-primitive data type, at first we write a class. the name of the class is a new type now.then we write,
type name = new type();
to make an objet of that type. but in case of int type or byte type array, we write,
byte[] name = new byte[5];.....
so, i think there should be a class nemed "byte[]". my question is, is there any class names "byte[]" in java.lang?? if not, in which way the array object is created? which class is called? what is thats type?if you can please explain, it will be very helpful for me. Thank you very much.
Re: Problem with array objects
ok, a byte is a basic data type that is an object. When you create an array of type byte you crate an array of byte objects. An array simply means a group of variable of like type that are adressed by the same name. Its a set of memory blocks that the computer uses the same name to address, but requires what is essentially an offset.
For example and array of size 5 created like so,
Code :
byte[] myArray = new byte[5];
would look something like this
[ ][ ][ ][ ][ ]
and to address each one we use the number of its position, i've added them into the array boxes.
[0][1][2][3][4]
Note, this is not a separate class, its just lots of byte objects lumped together. the new keyword is usedto dynamically assign enough memory at run time. So for arrays, we have to tell the computer exactly what size we would like the array to be, so it is no longer a standard size. So it doesn't know from the word go like it does with say a byte or an int. But it knows we want 5*the size of a byte.
It's hard to explain and understand if you are not too familiar with what is going on. If I was you, if you don't have a great understanding of Java then just accept it does it, and when you know a little more about it, then read into it. Google a few things on Java Arrays, read the java sun documentation, and read up on the new keyword.
Chris
Re: Problem with array objects
Thank you very much for your nice explanation. i know about the "new()" and i have read the documentations which you have told. My conception is about to clear now. But one quesion....
when i write a code like this:
System.out.println(byte[].class);
it shows the output as "class [B" ...................can you tell me please, where is this class loacated??:confused: and what it contains??:confused: Thank you again for you help...
Re: Problem with array objects
Well lets see, there a few primitives, 8 to be specific I believe. Byte happens to be one.
When you create an array of any type, primitive or not, you always create an object. An array is always an object and will inherit the same methods as any other object. When you do a sysout on byte[].class you are effectively printing the name of the class and internally in the JVM that happens to be just what you saw.
byte[].class does not actually contain anything, in fact it is most likely null.
Lets say you create a new type called MyType.
Code :
public class MyType
{
private int number;
public MyType(final int number)
{
this.number = number;
}
public int getNumber()
{
return this.number;
}
}
Like so and then you wanted an array of that you would do this.
Code :
final MyType[] myTypeArray = new MyType[5];
This would create a new object called myTypeArray. This is the actual array. You can then actually create new objects in the array of MyType as such.
Code :
myTypeArray[0] = new MyType(0);
myTypeArray[1] = new MyType(1);
myTypeArray[2] = new MyType(2);
myTypeArray[3] = new MyType(3);
myTypeArray[4] = new MyType(4);
And later on call the getNumber method on your type in the array.
Code :
System.out.println(myTypeArray[2].getNumber());
Which should print out a number 2.
Hope this makes some sense, for more information have a look at a tutorial about array such as Java arrays - Java tutorial
// Json
Re: Problem with array objects
ThaNK you very much Json. Now it seems very clear to me....:cool: