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 6 of 6

Thread: Break it down

  1. #1
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Break it down

    Could anyone break down the terms for the Java programming language? Could you tell me what the terms mean. Such as:


    class Car{}; // minimal dummy class
    Car[] cars1; // null reference
    Car[] cars2 = new Car[10]; // null references
    for (int i = 0; i < cars2.length; i++)
    cars2[i] = new Car();
    // Aggregated initialization
    Car[] cars3 = {new Car(), new Car(), new Car(), new Car()};
    cars1 = {new Car(), new Car(), new Car()};


    I have no idea why those symbols are needed. Please explain.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Break it down

    Ok, first of all the first line has a few syntax errors. It should be this:
    class Car{}

    This basically tells Java that you are creating a class named Car. Anything between the curly brackets defines what a Car is, and how it can be used. It's a dummy class because there's nothing in there

    Car[] cars1;
    This is NOT a null reference. Instead, this is a variable declaration. Variable declarations tell the compiler that you may sometime in the future want to use a variable called cars1, which holds an array of Car.

    Car[] cars2 = new Car[10];
    Again, this is not a null reference. Instead, this is another variable declaration, with an instantiation. Instantiation simply means setting the variable to that value. new Car[10] is creating an array of Car with 10 elements. The special thing about instantiating arrays is that they are set to a null default value, rather than being un-initialized.

    for (int i = 0; i < cars2.length; i++)
    {
         cars2[i] = new Car();
    }
    I changed what you originally had to make things a little clear-er. the first line is the declaration of a for loop. A for loop works like this:

    for( intialization_step; condition; increment_step)
    {
         // commands to run while condition is true
    }

    Here's how it gets executed:
    1. Run the intialization_step
    2. Test the condition
    3. If true, run the commands between the curly brackets. Else, end.
    4. run the increment_step
    5. Go back to 2 and continue the process

    Car[] cars3 = new {new Car(), new Car(), new Car(), new Car()};

    This declares the variable cars3 and instantiates it. Arrays are special because you can use the curly brackets to create an initial array in this manner.
    cars1 = {new Car(), new Car(), new Car()};
    Since earlier we declared we wanted to use a variable called cars1, here we are actually using it. Again, like above it instantiates cars1.

  3. The Following User Says Thank You to helloworld922 For This Useful Post:

    JavaPF (January 4th, 2010)

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

    Default Re: Break it down

    Just to clarify some things.

    Car[] cars1;

    Now this will result in a null reference because you are just creating a reference but not an actual array object.


    Car[] cars2 = new Car[10];

    This actually creates the array object but all the 10 entries in it will be null.


    Car[] cars3 = new {new Car(), new Car(), new Car(), new Car()};

    This contains a syntax error because you should not use the new keyword in front of the array initialisation.

    // Json

  5. The Following User Says Thank You to Json For This Useful Post:

    JavaPF (January 4th, 2010)

  6. #4
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Break it down

    lol, i missed that last one. And no, Json. Car[] cars1 does not create a null reference. It is un-initialized, and holds no value at all (not even null!). It's true that in other programming languages (C/C++) this line would auto-initialize to null if no custom initializing is done, but Java would prefer to throw a compile error if you tried to use a variable with no value at all.

  7. #5
    Super Moderator Json's Avatar
    Join Date
    Jul 2009
    Location
    Warrington, United Kingdom
    Posts
    1,282
    My Mood
    Happy
    Thanks
    70
    Thanked 158 Times in 154 Posts

    Default Re: Break it down

    True, good point.

    // Json

  8. #6
    Junior Member
    Join Date
    Sep 2009
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Break it down

    Wow o.O

    I wasn't expecting to get such detailed responses. Thank you guys so much for your time!

Tags for this Thread