Frequently Asked Questions for 605.201

Introduction to Programming Using Java


Compiling and Running Java programs

  1. Where are javac, java, and the appletviewer executables located on the university UNIX system?

    There are several versions of the Java Development Kit (Java from Sun) on-line. For this class, I recommend using Java 1.1 which can be found in: /usr/local/jdk1.1.6/bin. To change your UNIX environment so that these files are always available add the following line to the end of your .cshrc file: setenv PATH /usr/local/jdk1.1.6/bin:$PATH

  2. How do I compile a program

    1. First create a source with with your Java program. If the class is named Foo, your filename should be Foo.java
    2. Use the javac command to compile your .java file. In the example above, javac Foo.java will compile Foo.java and produce Foo.class

  3. How do I run a Java program?

    There are two cases. If your program is not an applet, just run the file using java. In the above example you could run the program Foo by typing java Foo. If your program is an applet, you must create an HTML (Hypertext Markup Language) file that refers to your applet. Then you can simply view the HTML file using appletviewer, or other web browser software.


  1. When running a Java program I get a NULL pointer exception. What is going on?

  2. When compiling some code, I get a warning from the compiler about some "deprecated" methods. The code appears to run fine otherwise. What is going on?

General Programming Questions

  1. What is an infinate loop?

    Simply put, an infinate loop is a for, while, or do-while loop that never terminates. The test that controls such a loop is always returning true, so the loop never exits and gets on with the rest of the program.

  2. Why is 5/9 zero?

    Java, (like C and C++) truncates integer division unless you use an explicit cast operator. Dividing 5 by 9 is something like 0.55, which gets truncated to 0. Similiarly 9/5 is 1. 9 divided by 5 is 1.8, which as an integer gets truncated to 1. If you wanted a floating point expression instead, you could write ((float) 5 / (float) 9).

  3. When should a method be a static method?

    A static method (like Math.sqrt(double)) is a method that only makes use of input arguments and does not refer to any class instance variables. You should use static methods when the return value of the method is only a function of the input arguments, and not otherwise.

Applets and HTML

  1. Can I run applets in Netscape and Internet Explorer if my code is using Java 1.1 features?

    It depends on the release of your browser. Until about May of 1998, no browsers supported Java 1.1. Netscape 4.05 and greater does and the latest version of IE should as well. Another way to run Applets with Java 1.1 code is by using the appletviewer program that comes with the JDK.

  2. How do I print the output of my applet?

    The easiest thing to do is probably use a screen capture utility to grab an image which may be printed out.

  3. Can my Applet read from or write to a file?

    Yes and No. Normally, applets may not read and write from a file. If you want people to be able to run your applet anywhere on the Internet, the answer is no. However, it is possible to have digitially signed applets, where surfers can elect to override their browser's normal security policy and permit your applet to do certain things (like accessing files).

At Work!

Feel free to send me suggestions of other topics for this file.

Paul McNamee: http://pmcnamee.net/ (paulmac@jhu.edu)

W3C Wilbur Checked!