Q and A

Here are some questions and answers we have collected so far. If you have something good for posting here, please let us know.

  • Q: What is the recommended way to include applets in Web pages?

    A: At various times in the recent past, Java applets have been included in Web pages with the <object> tag, the <embed> tag, or the <applet> tag. It is still not clear if <object> or <applet> will be the standard.
    It appears that the <object> tag works well for both browsers NN and IE. The <applet> tag is simpler to use but may still not be treated correctly by IE.
    The general form of the <object> tag is in this file.

    The general form of the <applet> tag is:

    
    <applet width = 200 height = 50 code= "Click.class">
    <p>Applet not supported by this browser.</p>
    </applet>
    
  • Q: Java provides many I/O classes. Can you provide a summary to make things easier?

    A: Here are some important points about Java I/O:

    • Java provides low-level byte-oriented I/O, and higher level char I/O, primitive-type I/O, and Object I/O. I/O objects can be connected into pipes.
    • FileInputStream, FileOutputStream --> byte oriented I/O (8-bit)
    • InputStreamReader, OutputStreamWriter --> Character I/O (16-bit) these are built on top of byte oriented I/O through a char-to-byte conversion dependent on the I/O character encoding being used.
    • BufferredReader, BufferedWriter, BufferedInputStream, BufferedOutputStream --> to get buffered I/O and to get readLine() method
    • PrintStream, PrintWriter --> output string representation of primitive and object types
    • RandomAccessFile --> file updating, appending
    • DataInputStream, DataOutputStream --> primitive type binary I/O
    • ObjectInputStream, ObjectOutputStream --> serializable object binary I/O
    • Unicode can use different encodings: UTF-16, UTF-16BE, UTF-16LE, UTF-8, US-ASCII (7-bit)
  • Q: When sending command-line arguments to java or javac the * character is treated as a wildcard. How can I quote it?

    A: On UNIX use "*", '*', or \*. On MS/DOS use "*". The notation "*" will work for both.

  • Q: After downloading and installing Java, how do I make the java commands such as java and javac available?

    A: You can always use the commands in the jdk1.3xxx/bin/ directory by giving the full pathname as the command. But that is too much to type. You simply need to include that bin directory on the command search PATH.

    On UNIX set the environment variable PATH to include the bin directory. For example,

    setenv PATH $PATH":/usr/local/jdk1.3.1_02/bin"
    
    or on MS/DOS
    SET PATH= %PATH%;C:\jdk1.3.1_02\bin
    
  • Q: In Java, how does one perform formatted output where the spacing and precision of numbers can be controlled?

    A: Formatted I/O is not part of the Java I/O stream facility. You use the class NumberFormat to get default formatting for different Locale and the class DecimalFormat to convert numbers to/from strings of designated format. Examples will be shown when we cover I/O.

  • Q: When do I need to add the null constructor to a class?

    A: You should normally include a null constructor in a class unless you are absolutely sure no other code, now or in the future, will need the null constructor for the class. If you don't see a direct instantiation of objects using the null constructor, consider a protected null constructor for class extension purposes. Remember, an extended class constructor that does not explicitly call super(...) will have to invoke the super-class null constructor.

  • Q: Why does E[] arr = new E[cap]; give a compile-time error?

    A: Basically, the operator new is a run-time operation and Java 1.5 removes all generics typing at compile-time. This is the well-known generic array creation an operation not supported by Java 1.5. You need to use
        obj = (E[]) (new Object[cap]);
    
    which will compile with just a warning.

  • Q: Can an inner class be extended?

    A: Yes, if the inner class is not final. But inner classes are usually private or protected, making general extension impossible. See the inner class specifications for more details.

  • Q: In handling a mouse button event, how do I know which mouse button is involved?

    A: You use the getModifiers() method and check against the appropriate Button Mask constant in the MouseEvent class.

  • Q: What are the JNICALL and JNIEXPORT and JNIEnv symbols used in writing the native code under JNI?

    A: JNIEXPORT and JNICALL are preprocessor defined constants specific to each platform. You can find them in the machine-dependent header file jni_md.h under java/include/hp-up or java/inlcude/solaris for example. These are usually defined as null strings.
    The JNIEnv is a typedef defined in jni.h. It provides access from the native code to Java objects.