Java Coffee Break Newsletter Volume 2, Issue 4 http://www.davidreilly.com/jcb/ ISSN 1442-3790 ============================================================================= In this issue * Java in the news * Q&A : Why can't my applet read or write to files? * Q&A : How do I convert from a char to an int? * Q&A : How do I compare two strings? ============================================================================= In the News Here are a selection of recent news items that may be of interest to Java developers. /*/ Cygnus Solutions releases GNU native compiler for Java Cygnus has released a compiler that produces native code for Solaris and Linux. Unlike byte-code interpreters, or just-in-time compilers which work with a Java runtime, the new compiler produces completely native code, and is claimed to offer performance increases of up to twenty (20) times Java interpreters. For more information, visit http://www.cygnus.com/news/javalib.html /*/ Sun releases JDK 1.2.1 A small number of changes, including one relating to security, have been made. For more information, visit http://java.sun.com/ /*/ Microsoft pulls the plug on keynote speaker Microsoft has cancelled an address by a keynote speaker at this year's Software Development '99 Conference, where he was to have announced the ultimate Java killer - a virtual machine capable of supporting C++, Java and Visual Basic. The thought of a portable Visual Basic, while impressive, would represent a significant threat to Java, and would challenge its supremecy over its most important feature - portability. For more information, visit the commentary at the Java Lobby site. http://www.javalobby.org/ /*/ Internet Explorer 5 lacks Java Virtual Machine for minimal install The decision to remove the Java Virtual Machine (JVM) from the minimal install of Microsoft's Internet Explorer 5 has raised some concern for developers. However, users that choose a custom install, or use a typical install, will still have access to Java. Users are free to download the virtual machine from Microsoft. However, developers also have the flexibility to avoid Microsoft's JVM entirely, by choosing to use Sun's Java Plug-in. For more information on the Java plug-in, visit http://java.sun.com/products/plugin/index.html ============================================================================= Q&A : Why can't my applet read or write to files? Applets execute under the control of a web browser. Netscape and Internet Explorer impose a security restriction, that prohibits access to the local filesystem by applets. While this may cause frustration for developers, this is an important security feature for the end-user. Without it, applets would be free to modify the contents of a user's hard-drive, or to read its contents and send this information back over a network. Digitally signed applets can request permission to access the local filesystem, but the easiest way around the problem is to read and write to remote files located on a network drive. For example, in conjunction with a CGI script or servlet, you could send HTTP requests to store and retrieve data. ============================================================================= Q&A : How do I convert from a char to an int? If you want to get the ASCII value of a character, or just convert it into an int (say for writing to an OutputStream), you need to cast from a char to an int. What's casting? Casting is when we explicitly convert from one primitve data type, or a class, to another. Here's a brief example. public class char_to_int { public static void main(String args[]) { char myChar = 'a'; int i = (int) myChar; // cast from a char to an int System.out.println ("ASCII value - " + i); } } In this example, we have a character ('a'), and we cast it to an integer. Printing this integer out will give us the ASCII value of 'a'. We can covert from a char to an int by casting - but we can also take it back the other way. An int can be cast back to a char as well. ============================================================================= Q&A : How do I compare two strings? A common error that we all make from time to time is incorrect String comparison. Even once you learn how to compare strings correctly, it's extremely easy to make a mistake and use the == opearator. When we compare primitive data types, such as two ints, two chars, two doubles, etc. we can use the == opeartor. We can also use the == operator to compare two objects. However, when used with an object, the == operator will only check to see if they are the same objects, not if they hold the same contents. This means that code like the following will not correctly compare to strings : if ( string1 == string2 ) { System.out.println ("Match found"); } This code will only evaluate to true if string1 and string2 are the same object, not if they hold the same contents. This is an important distinction to make. Checking, for example, to see if aString == "somevalue", will not evaluate to true even if aString holds the same contents. To correctly compare two strings, we must use the .equals method(). This method is inherited from java.lang.Object, and can be used to compare any two strings. Here's an example of how to correctly check a String's contents : if ( string1.equals("abcdef") ) { System.out.println ("Match found"); } This is a simple, and easy to remember tip that will safe you considerable time debugging applications. Remember - never use the == operator if you only want to compare the contents of strings. ============================================================================= 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/ 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