background image

Avoiding java.util.Enumeration

Avoid using 

java.util.Enumeration

 objects unless you want to hide data (in other words, to return an enumeration

of the data instead of the data itself). Asking a vector or hash table for an 

Enumeration

 object is slow and creates unnecessary

garbage. If another thread might modify the vector, synchronize the iteration. The Java® SE uses an 

Iterator

 object for

similar operations, but 

Iterator

 objects are not available in the Java® ME.

Code sample

for( int i = v.size() - 1; i >=0; --i ) {

o = v.elementAt( i );

...

}

synchronized( v ) {

for( int i = v.size() - 1; i >=0; --i ) {

o = v.elementAt( i );

...

}

}

Performing casts using instanceof

Use 

instanceof

 to evaluate whether a cast succeeds.

Code sample

if( x instanceof String ) {

(String)x.whatever();

} else {

...

}

x

Evaluating conditions using instanceof

To produce smaller and faster code, if you evaluate a condition using 

instanceof

, do not evaluate explicitly whether the

variable is null.

Code sample

if( e instanceof ExampleClass ) { ... }

if( ! ( e instanceof ExampleClass ) ) { ... }

Fundamentals Guide

Best practices for writing an efficient BlackBerry Java Application

16

Summary of Contents for JAVA DEVELOPMENT ENVIRONMENT - - FUNDAMENTALS GUIDE

Page 1: ...BlackBerry Java Development Environment Version 4 7 0 Fundamentals Guide...

Page 2: ...SWDT499912 499912 1212115921 001...

Page 3: ...pplications with desktop synchronization 10 Applications with wireless access wireless synchronization or wireless alerting 10 MIDlet applications 10 CLDC applications 11 API control and code signing...

Page 4: ...y device 27 RAM garbage collection on a BlackBerry device 27 Full garbage collection on a BlackBerry device 27 Idle garbage collection on a BlackBerry device 27 5 Data management 28 Support for APIs t...

Page 5: ...cation authentication 36 Server side authentication 36 Controlled APIs and code signing 36 BlackBerry APIs with controlled access 37 IT policy support 38 Application control 38 File encryption on micr...

Page 6: ...n 43 Distribute an application from a computer 43 Distribute an application from a web page 43 Distribute an application for testing 43 Application distribution over the wireless network 43 Wireless p...

Page 7: ...have a longer latency period than standard LANs have less available memory have shorter battery life display one screen at a time Mobile device users use applications on their mobile device different...

Page 8: ...Berry JVM version 4 0 Applications that you create using BlackBerry JDE Version 4 0 only work on BlackBerry devices running BlackBerry Device Software version 4 0 or later You can use the following cr...

Page 9: ...standard addresses the API and BlackBerry JVM needs of a constrained wireless device with a user interface The BlackBerry device supports the Java ME MIDP standard as defined in JSR 118 The Java MEMI...

Page 10: ...tion Networking and I O APIs You can use these APIs to establish network connections and read or write data to a server side application Event Listeners You can use the Event Listeners to respond to B...

Page 11: ...es wireless access to email attachments and access to Internet content The BlackBerry Internet Service includes support for direct HTTP and TCP IP connectivity to the Internet from a third party Black...

Page 12: ...evice user must initiate the data synchronization process manually After the applicationisinstalledontheBlackBerrydevice theBlackBerrydeviceusermustsynchronizeinformationmanuallybyconnecting their Bla...

Page 13: ...applications that the BlackBerry Java Development Environmentincludes use the CLDC application model All of the core BlackBerry applications including message list contacts list calendar and the brows...

Page 14: ...eive your set of code signing keys in about 10 days Object modeling Whether you use the MIDlet or the CLDC application model you must use an object oriented approach when you design your applicationfo...

Page 15: ...numeration Perform casts using instanceof Evaluate conditions using instanceof Avoid using StringBuffer append StringBuffer Avoid returning null Avoid passing null into methods Use caution when passin...

Page 16: ...by using static variables not final instead of constants final The opposite is true for primitive data types such as int For example you might create a String object as follows private static final St...

Page 17: ...0 i size i for int i vector size 1 i 0 i Optimizing subexpressions If you use the same expression twice use a local variable Code sample int tmp i 1 one tmp two tmp Optimizing division operations Div...

Page 18: ...vailable in the Java ME Code sample for int i v size 1 i 0 i o v elementAt i synchronized v for int i v size 1 i 0 i o v elementAt i Performing casts using instanceof Use instanceof to evaluate whethe...

Page 19: ...the method should throw an appropriate exception which forces the caller of the method to deal explicitly with the problem The caller of the method might not need to check for a null return value unl...

Page 20: ...e Code sample catch Throwable t t printStackTrace Best practice Using objects judiciously To allow a BlackBerry Java Application to efficiently use memory resources consider the following questions Gi...

Page 21: ...id creating interfaces unless you foresee multiple implementations of the API Interfaces produce larger slower code Using static inner classes When you use an inner class to hide one class inside anot...

Page 22: ...nguages depending on the locale of the BlackBerry device user without requiring new resource bundles YoucanusetheBlackBerryIntegratedDevelopmentEnvironmenttocompileeachresourcebundleintoaseparatelycom...

Page 23: ...BlackBerry device stores the picture in the file system on the BlackBerry device A BlackBerry Java Application can access the pictures by using the File Connection API for Java ME that is available i...

Page 24: ...he number of ticks along an axis surpasses the threshold of the system or a BlackBerry Java Application a navigation event along that axis is dispatched to the software layer and the system resets the...

Page 25: ...ons Screen components provide a standard screen layout a default menu and a standard behavior when the BlackBerry device user presses the Escape key or clicks the trackwheel or trackball Field compone...

Page 26: ...ly 10 MB to 15 MB of flash memory depending on the version Flash memory can store the BlackBerry device user s email messages organizer data and other personal information as well as the data that a B...

Page 27: ...sible when the BlackBerry device is low on memory resources Identifying low memory availability on a BlackBerry device The following conditions can cause the low memory manager to attempt to free memo...

Page 28: ...ds can quickly exhaust the number of persistent object handles A persistent object consumes a persistent object handle and an object handle A transient object consumes only an object handle For exampl...

Page 29: ...any nonpersistent object handles in RAM and flash memory The system might initiate a full garbage collection operation in the following situations The BlackBerry Java Virtual Machine cannot allocate...

Page 30: ...ts on a persistent store however the limit for an individual object within the store is 64 KB The BlackBerry Persistent Store APIs do not provide a relational database model You must create an effecti...

Page 31: ...data to a file on a computer and to restore data to the BlackBerry device When an application uses the Synchronization API the BlackBerry Desktop Manager can back up and restore the application databa...

Page 32: ...ackBerry Desktop Manager add in which notifies the application on the BlackBerry device to send the data to the computer application You can also write data to the computer application using the nativ...

Page 33: ...Java Application to use it as a secure gateway for managing HTTP and TCP IP connections to the intranet If your application connects to the Internet you might be able to use the BlackBerry Enterprise...

Page 34: ...s of an email message Using SMS to transport data You can use the BlackBerry APIs to create applications that use SMS as a transport mechanism for sending and receiving data The BlackBerry APIs allow...

Page 35: ...mation The following examples demonstrate how a BlackBerry device application can invoke a BlackBerry Device Software application The BlackBerry device application invokes the calendar to display a sp...

Page 36: ...kBerry device user performs a local event You can use the email and organizer data listeners to notify a BlackBerry Java Application when new entries arrive or when the BlackBerry device user makes ch...

Page 37: ...T policy to make sure that all BlackBerry device user data stored in the BlackBerry device applications is encrypted locally in flash memory You can create aBlackBerry Java Application that uses APIs...

Page 38: ...erver or to the Internet or an intranet you might want to include additional authentication features when the BlackBerry device users log into the server Most applications that require user authentica...

Page 39: ...search In Motion before you can install these applications on BlackBerry devices You can use the following categories of RIM controlled APIs Runtime APIs BlackBerry Application APIs BlackBerry Cryptog...

Page 40: ...tion on the BlackBerry device cannot access the phone application TheadministratorcanapplyapplicationcontrolpoliciesonlywhentheBlackBerrydeviceisassociatedwithaBlackBerryEnterprise Server If theadmini...

Page 41: ...card password If the BlackBerry device has a password the BlackBerry device user can use the prompt to change the microSD media card password to the BlackBerry device password IT policies and the mic...

Page 42: ...the amount of processing that the BlackBerry device must perform when you install yourapplication TopartiallyverifyyourclassesbeforeyouinstallyourapplicationonaBlackBerrydevice youcanusethePreverify...

Page 43: ...ne and Internet browsing The BlackBerry Smartphone Simulator also serves as a platform on which applications can run This includes the ability for the applicationstomakenetworkconnections storedata an...

Page 44: ...Simulator and either a messaging application such as Microsoft Outlook or POP3 and SMTP servers You do not require a BlackBerry Enterprise Server To get the BlackBerry Email Simulator visit www blackb...

Page 45: ...users with a simple way to install applications from their computers without running the BlackBerry Desktop Manager Distribute an application for testing The BlackBerry Java Development Environment i...

Page 46: ...ackBerry device Wireless push server initiated In the BlackBerry Enterprise Server environment the administrator can push applications to BlackBerry device users over the wireless network for mandator...

Page 47: ...t Transfer Protocol over Secure Sockets Layer I O input output IMAP Internet Message Access Protocol IPC interprocess communication JDWP Java Debug Wire Protocol JSR Java Specification Request JTWI Ja...

Page 48: ...tion Program Compiler RMS Record Management System SHA Secure Hash Algorithm SMS Short Message Service SMTP Simple Mail Transfer Protocol SRAM static random access memory SSL Secure Sockets Layer TCP...

Page 49: ...WAP Wireless Application Protocol XML Extensible Markup Language Fundamentals Guide Glossary 47...

Page 50: ...updates enhancements or other additions to this documentation to you in a timely manner or at all This documentation might contain references to third party sources of information hardware or softwar...

Page 51: ...DING AIRTIME SERVICE PROVIDERS AND THEIR RESPECTIVE DIRECTORS EMPLOYEES AND INDEPENDENT CONTRACTORS IN ADDITION TO THE LIMITATIONS AND EXCLUSIONS SET OUT ABOVE IN NO EVENT SHALL ANY DIRECTOR EMPLOYEE...

Page 52: ...TOSUPERSEDEANYEXPRESSWRITTENAGREEMENTSORWARRANTIES PROVIDED BY RIM FOR PORTIONS OF ANY RIM PRODUCT OR SERVICE OTHER THAN THIS DOCUMENTATION Research In Motion Limited 295 Phillip Street Waterloo ON N2...

Reviews: