Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Creation of objects of Array in Java

  1. #1
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question 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.


  2. #2
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Location
    Wales, Bangor & England, Warwickshire
    Posts
    820
    My Mood
    Cynical
    Thanks
    7
    Thanked 104 Times in 90 Posts

    Default 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,
    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
    chris[at]javaprogrammingforums[dot]com

    Prifysgol Bangor University, North Wales

  3. #3
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb 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?? and what it contains?? Thank you again for you help...

  4. #4
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,274
    My Mood
    Happy
    Thanks
    70
    Thanked 156 Times in 152 Posts

    Default 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.

    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.

        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.

        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.

        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

  5. #5
    Junior Member
    Join Date
    Jul 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem with array objects

    ThaNK you very much Json. Now it seems very clear to me....

Similar Threads

  1. Java program to encrypt an image using crypto package
    By vikas in forum Java Networking
    Replies: 1
    Last Post: July 7th, 2009, 11:00 AM
  2. [SOLVED] Array loop problem which returns the difference between the value with fixed value
    By uplink600 in forum Loops & Control Statements
    Replies: 5
    Last Post: May 15th, 2009, 04:31 AM
  3. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM
  4. Error of missing return statement while implementing Array
    By MS_Dark in forum Collections and Generics
    Replies: 1
    Last Post: December 10th, 2008, 02:18 PM
  5. Replies: 1
    Last Post: November 22nd, 2008, 12:32 PM