There were a number of sessions/workshops I attended at this conference that I found particularly interesting:
Tricky Java Puzzlers & Tricky GUI puzzlers
- Points out tricky aspects of the programming language that aren’t always intuitively obvious
- Examples:
- decimal literals are always positive, but hex literals are signed
- order of operations: + binds tighter than == (equals equals)
- assigning a variable twice in one statement may not give the results you expect, don’t do it
(a+= a++) - & applied to boolean operands works different than & applied to ints:
- ints: bitwise and
- boolean: logical and WITHOUT short-circuiting
- Math.abs() can return a negative-value in one special circumstance Integer.MIN_VALUE
GUI: Text measurement and sizing
- There are different methods you can call to get the size of text depending on whether you want something more exact or less computationally expensive
- Width of a string will not scale linearly with font size (scales parabolic)
Java performance
- Try/catch inside really tight loops is bad, but elsewhere is negligible
- Using “C-Style” ugly code rather than object-oriented code is faster for determining which subclass you are
- an int w/ a switch is faster than using instance of or if/else if/else if/… or virtual calls (abstract method defined in each instance class)
- better to tweak garbage collection flags than try to repool/reuse objects to improve performance… exception = jpanel (cuz it’s a really large overhead class
- nested GUI objects = slow, faster to use a layout manager w/ a grid layout of some sort
- 1.5 features foreach, auto-boxing: no performance hit. Enum’s are slow though b/c of overhead of doing an array clone (only major issue if in a tight loop though)
- tuning the VM params or setting right java flags improves performance
- bigger performance increase to stuff data into data models *before* rendering
- DON’T DO SLOW STUFF (access files, network, complex processing) IN EventDispachThread (swing GUI thread)… use SwingWorker or another thread instead
- Most performance issues are high level design problems not inherent Java issues
Improving GUIs
- Good GUIs you don’t have to add text everywhere to explain how to use it…its intuitive
- Add button mnemonics and keyboard accelerators
- Use LINE_START rather than WEST to ensure better bidirectional support (for languages like Hebrew)
- Filling layouts are better for translatable text than items with set preferred sizes
- User won’t notice overall time is longer for an action if program is responsive…give visual feedback…responsiveness is the more important goal
- Watch out for combinatorial (ctrl-A/select all) number of events being fired…and fire event in super-class if large number of events can be fired simultaneously
- Using XML to define your menu options / gui items = slower, but cleaner & much shorter code, easier to update
JDK 1.5 cool new features
- Concurrency
- Many improvements
- Decouples task submission from thread execution (w/ Callable & Future interfaces)
- Generics (really cool)
- Improved type safety
- Make more compile time checks possible
- Reduces amount of casting needed
- 1.6 fixes the unresponsive grey-rectangle on a gui that’s being updated problem