Welcome to the Ranch
I had to break the long lines and overlong // comments and you can see the right way to do it. Otherwise your code would be illegible.
What happened when you ran that code? Did you notice any difference? The big difference between a List and an array is that Lists do not have fixed sizes. Your arrays have fixed sizes and you cannot add an additional “instrument”. You would have to create a larger array and copy all the elements from the old array, then you can add to the array. In the case of a List you simply add things.
Thanks @Campbell for your response No there is not any difference. But i confused here why in the book they used ArrayList, whereas they could simply use Array. The code would be much simpler. yeah i know ArrayList can grow dynamically, but there is no need to it.
Presumably Kathy & Bert wanted to show the use of Lists.
There is a big advantage to Lists now that Java8 has come out. You can call their stream() method and get a Stream representing the List's contents. You can't do that with an array, but you can easily convert an array to a List and call stream().
Hey! @Campbell can you clear my another doubt :/ it's not related to array or list. Why we declare "MidiEvent event = null" outside try bock, why not directly declare n initialize inside try-block as "MidiEvent event = new MidiEvent(message, tick)". :/
Don't know, but you will probably find that the MidiEvent constructor (look for its documentation) declares a checked Exception. That means its call must be wrapped in a try or the method must declare that Exception. If you declare the MidiEvent local variable inside the try, it goes out of scope before the return. If you don't assign it outside the try, the compiler will complain there is a local variable possibly not initialised.
If you need to initialise it to something, the only thing which won't produce an Exception is null.
Campbell Ritchie wrote:There is a big advantage to Lists now that Java8 has come out. You can call their stream() method and get a Stream representing the List's contents. You can't do that with an array, but you can easily convert an array to a List and call stream().
Java 8 has added stream(...) to java.util.Arrays to let you stream an array without first converting it to a List.
There are no new questions, but there may be new answers.
Campbell Ritchie wrote:Try it and see what happens.
Compile time error : "the local variable 'event' may not have been initialized"
But, 'event' is initialized inside try-block, then, why this error? And, if the scope of local variable is within the try-block, then, we've declared event outside try-block, can't we just declare outside try-block and initialize inside try-block?
So i tried on other blocks than try, like inside for-loop, or some other functional blocks, we have to initialize the local reference variable to NULL, then only we can use that local variable outside the block. Like in the example below :
But, i'm not getting it why we have to initialize the local variable to NULL, as inside the block the reference of that local variable is ultimately given to some other concrete object. Then, why first we are giving reference to NULL??
P.S.: Sorry for grammatical mistakes, m not a native English speaker ;)
Rahul Guliani wrote: . . .
Compile time error : "the local variable 'event' may not have been initialized" . . .
See, that is what I expected.
But, 'event' is initialized inside try-block, . . .
But what if an Exception occurs in the try? Then event will not have been initialised. Local variables do not have default values like fields. They might have an old value left in the bit of memory occupied by the stack. So the rules require that every local variable be definitely initialised. It can only be definitely initialised if every path through the method initialises it. You can try all sorts of things but the javac tool checks that every path of execution definitely assigns to event. Otherwise ti won't compile. That is why it is first initialised to null, which is the only value certain not to throw an Exception.