• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Java Swing Appication BeatBox

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey! i wrote this code for making a beat-box as written in head first java, instead of using using ArrayList for check-boxes, i applied simple array.
Will there be any difference in the final app.





 
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rahul Guliani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Marshal
Posts: 80281
432
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
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().
 
Rahul Guliani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rahul Guliani wrote:



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)". :/
 
Campbell Ritchie
Marshal
Posts: 80281
432
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Rahul Guliani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:
If you need to initialise it to something, the only thing which won't produce an Exception is null.



Why we've to initialize local variable 'event' to null? Why not just declare 'event' without initialization?
 
Campbell Ritchie
Marshal
Posts: 80281
432
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try it and see what happens.
 
Rahul Guliani
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?

Edit:
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 ;)
 
Campbell Ritchie
Marshal
Posts: 80281
432
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
You don't know me, but I've been looking all over the world for. Thanks to the help from this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic