Java Coffee Break Newsletter Vol 1, Issue 6 ISSN 1442-3790 This issue includes even more answers to frequently asked questions about Java programming, due to popular demand. - - - - - - - - - - - - - - - - - - - - - - - - - Java Coffee Break News * Free Articles & Tutorials * Programming Tools * Q&A : What causes out of memory exceptions? * Q&A : How do I convert from a string into a number? * Q&A : How do I create a .JAR file? 1. Free Articles & Tutorials We've got free articles and tutorials about Java that teach basic programming concepts, right through to advanced topics like networking, JavaBeans & CORBA. For more information, visit the Java Coffee Break at http://www.davidreilly.com/jcb/ 2. Programming Tools If you're looking for programming tools, check out the great savings available from Beyond.Com, the software superstore. http://www.beyond.com/AF10445/departments/programming/programming.htm - - - - - - - - - - - - - - - - - - - - - - - - - Q&A : What causes out of memory exceptions? The dreaded OutOfMemoryException is something that no Java programmer ever wishes to see. Yet it can happen, particularly if your application involves a large amount of data processing, or is long lived - such as a server that responds to requests and is expected to run indefinitely. There are several reasons that out of memory exceptions can be thrown. Either the virtual machine your Java program is running on may have a limit to the amount of memory it will give access to, the system on which your program is running may have run out of physical and virtual memory, or your application may just be consuming too much memory. In the first case, you may be able to modify the maximum heap size of the virtual machine, in the second or third you'll need to redesign your application. Designing applications for minimal memory consumption isn't an easy task, and isn't specific only to Java programming. There are many techniques available, such as buffering data to disk that isn't needed at the moment, to using more efficient algorithms or subdividing tasks into smaller pieces. This is more of a design problem that a Java one - however there are some steps you can take to avoid your program crashing when it runs out of memory. For a start, in your main method (or those you identify as likely to cause problems), catch OutOfMemoryExceptions. This way your application can terminate gracefully. Also, be sure to clear any data that you know you'll no longer require. The automatic garbage collector looks for objects that contain no references, so be sure to set your objects to null when you're finished. Finally, you can always examine the amount of free memory by calling the freeMemory() method of the system Runtime object. Here's how : Runtime runtime = Runtime.getRuntime(); System.out.println ("Free memory : " - + runtime.freeMemory() ); If you realize that you're going to be performing large amounts of data processing (and consuming large amounts of memory), it might be a good idea to check before beginning. Here's to no more OutOfMemoryExceptions! - - - - - - - - - - - - - - - - - - - - - - - - - Q&A : How do I convert from a string into a number? The first thing you have to be sure of is that your string actually contains a number! If not, then you'll generate a NumberFormatException. We can use the Integer class for converting from a string into an integer (or if we're dealing with decimal numbers, we can use the Double class). The follow example shows how to convert from a string to an integer. String numberOne = "123"; String numberTwo = "456"; // Convert numberOne and numberTwo to an int with Integer class Integer myint1 = Integer.valueOf(numberOne); Integer myint2 = Integer.valueOf(numberTwo); // Add two numbers together int number = myint1.intValue() + myint2.intValue(); You'll notice that when we need to use numbers as a data type (int), we need to convert from an Integer to an int. Confused? An Integer acts as a wrapper for our int data type (making conversion from String to number easy). When we want to use the data type, we call the intValue() method, which returns an int. - - - - - - - - - - - - - - - - - - - - - - - - - Q&A : How do I create a .JAR file? Java Archive (JAR) files allow developers to package many classes into a single file. JAR files also use compression, so this can make applets and applications smaller. Creating a .JAR file is easy. Simple go to the directory your classes are stored in and type :- jar -cf myfile.jar *.class If your application or applet uses packages, then you'll need to do things a little differently. Suppose your classes were in the package mycode.games.CoolGame - you'd change to the directory above mycode and type the following :- (Remember to use / on UNIX systems) jar -cf myfile.jar .\mycode\games\CoolGame\*.class Now, if you have an existing JAR file, and want to extract it, you'd type the following jar -xf myfile.jar Working with JAR files isn't that difficult, especially if you've used the unix 'tar' command before.  If you're planning on packaging an applet for Internet Explorer, or an application for Microsoft's jview, you might also want to consider .CAB files. - - - - - - - - - - - - - - - - - - - - - - - - - The Java Coffee Break Newsletter is only sent out to email subscribers who have requested it, and to readers of the comp.lang.java.programmer and comp.lang.java.help newsgroups. If you'd like to receive our newsletter, and get the latest Java news, tips and articles from our site, then get your FREE subscription from http://www.davidreilly.com/jcb/newsletter/subscribe.html If you are an email subscriber and no longer wish to receive the JCB Newsletter, please unsubscribe using the WWW form located at http://www.davidreilly.com/jcb/newsletter/unsubscribe.html