FAQ


Where can I get the Java SDK?

Download the latest version from the Sun Java website. Be sure to get J2SE and not J2EE or J2ME. You want the "Core/desktop" edition.

If you have a Mac running OS X, you probably already have Java installed. Open a Terminal (Applications -> Utilities -> Terminal.app) and type java -version at the prompt. It should give you a whole bunch of text, including the version number. If not, run Software Update (from the apple menu)

If you are using Windows, go here to download J2SE 5.0. Choose the one without Netbeans, it should be called "JDK 5.0 Update 6" or whatever the latest update number happens to be.

What is an IDE and which one should I use?

An IDE (Integrated Development Environment) is a programming environment that lets you write code, compile, and test it all in one. Usually recommended IDEs include NetBeans, Eclipse and VisualStudio, but for most Java programming they are overkill.

Some people choose to edit their .java files using notepad.exe or another plain text editor. This is okay since .java files are simply plain text! The problem with it, however, is that plain text editors make it a bit hard to read and format your code.

I recommend using Dr Java. It is a very simple-to-use IDE that will syntax color your code (to make it easy to identitfy keywords and such) as well as help you use proper indentation. On top of that, you can compile and run your code right inside Dr Java without having to go to a command line. Here's how you get it: go to drjava.org and click the button under the "Current Stable Release" that identifies your OS (i.e. Windows or Mac OS X). If you are running Linux or some other operating system, download the Jar file.

why is 1E-14 and not 10E-14 equal to 10 to the negative 14th power?

E is engineering notation. Replace E with "x 10 raised to the". I know it's confusing, but 1E-14 is the same as 10 raised to the -14 power. So 1E-14 is 1 x 10^-14.

How does the remainder (%) operator work?

When you divide floating point numbers (like doubles) there is no remainder -- since they are precise. So when you play with integers, you have two operators: / and %. Together, you can completely divide. Let's take a look at a few examples.
1 / 2 == 0;  //quotient
1 % 2 == 1;  //rest of the first number

2 / 2 == 1;  //quotient
2 % 2 == 0;  //no leftovers

3 / 2 == 1; 
3 % 2 == 1; 
So this last one is more confusing. What this says is: "in the case of three divided by two, two goes into three once with one left over". This means:
2 * 1 + 1 == 3;
(divisor) * (quotient) + (remainder) = (dividend)
In case these terms are not familiar:
(dividend) / (divisor) == (quotient);
(dividend) % (divisor) == (remainder);
One rule of thumb is if the dividend is smaller than the divisor, the quotient will be zero, and the remainder will be equal to the dividend:
3 / 8 == 0;  //3 is too small!
3 % 8 == 3;  //everything left over
Sometimes it helps to look at a long list of examples:
1 % 3 == 1;  1 / 3 == 0;
2 % 3 == 2;  2 / 3 == 0;
3 % 3 == 0;  3 / 3 == 1;
4 % 3 == 1;  4 / 3 == 1;
5 % 3 == 2;  5 / 3 == 1;
6 % 3 == 0;  6 / 3 == 2;
//...
See a pattern? Maybe you remember back to middle school when you did long division. Remember remainders from that? They're calculated the same way you did it then:
  ___0_ r 3
8 )  3
   - 0
   ----
     3 <-- remainder

What does 'formal parameter' mean?

Say you have this method:
void foo(int x, int y) {
  x += y;
}
In this case, the "formal parameter variables" are x and y. Stuff between the () in a method declaration (as above) are formal parameters. Be CAREFUL of the difference between parameters and arguments. Arguments are in the same position but not of the definition, of a call. For example, in foo(12, 13); 12 and 13 are the arguments not parameters; x and y are still the parameters.

What is 'method returns' or 'returning from a method'?

Say you have this method:
void foo(int x, int y) {
  x += y;
}
Okay, now lets consider it step by step. When foo() is done processing, that is "x += y" is done, the method is finished. There is nothing left to do, so it returns to the part of the code where it was called. Sometimes you'll also see a return keyword that will tell the function to return. If the return TYPE of a function has a type (void in foo's case), you can "give back" a value to whomever called the function. foo() doesn't do this (void), but this function might:
int bar(int x, int y) {
  return x + y;
}
bar(1,2) will add x and y then return the result (3). So when you say a method "returns", that means it finishes and possibly passes back a result to where it was called. The origin of the keyword comes from how method calling works. Look at this code:
int a = 1;
a = bar(8, 9);
a++;
When bar() is called, the computer goes to the method, executes it, and then returns back from the method with a value. The program then continues by placing the value in a, and making it bigger with ++.

Why can't I access a method's parameters from outside the method?

Again, say you have this method:
void foo(int x, int y) {
  x += y;
}
And perhaps you want to access x and y after you call the method:
foo(10, 11);
System.out.println(x);

Java will yell at you for using an undefined identifier x. This is because the scope of x is inside the method. When you call the method, you give its parameters values (we call these values arguments). When the method is finished executing, it no longer has need for those values, so they get thrown away and its parameters are once again undefined.

This means that the parameters (x and y in the case of bar()) are only *available* for use during the method. For example, if I say bar(8,9), then x and y are given the values of 8 and 9 when bar() starts. When bar() returns, x and y no longer have any values -- they are "out of scope". This means you cannot use those parameters *outside* the method.

What is an "empty stub"?

The basic idea of an empty stub, is a method or class that simply does nothing. For example:

class MyMath {
   static int add(int x, int y) {
      return 0;
   }
   static void printHello() {
   }
   static double divide(int x, int y) {
      return 0.0;
   }
}

Someone who develops with stubs, will make the "signature" or "header" of the class, then flesh it all out one method at a time. Sometimes this is called "iterative development". You do a little step, test it, then do another, etc. The point of making stubs is to make your program compile although some features may not work properly.

How do static methods work?

Here's a quick run-down on the word "static": If you want to access something from the class (instead of from an instance) it must have static in front of it. If you want each instance of the class (the objects) to each have their own copy, don't make it static. The special part is that any instance of a class also has access to static stuff. For example:

class Blah {
   public static int x = 3;
}
System.out.println(Blah.x);
// prints 3

Blah.x++;
System.out.println(Blah.x);
// prints 4 

Blah p = new Blah();
System.out.println(Blah.x);
// prints 4

p.x++;
System.out.println(Blah.x);
// prints 5

What is an "invoking instance"?

For starters, an instance is an object, or a programming construct that has been created in the image of a class. An "invoking instance" is the instance that is used when calling a method. In this example, obj is the invoking instance:

obj.doSomethingNeat();

So if the thing to the left of the method is a class and not an object then it is not an invoking instance! It is not an instance at all. Static methods usually don't have invoking instances, though you can still call them through objects.

A program that does not use invoking instances, but still has methods, usually contains all the code in a single class. In this case, the methods can be called "subroutines" and are just split into separate methods to make the code more readable and perhaps efficient.

What does it mean when a function or method "evaluates to" something?

First of all, it's atrocious grammar to say this. In general what "a method evaluates to" and "a method returns" are the same thing. Expressions are combinations of data values and operators. Method-calling can be seen as an operation, and thus an operator. In this way, calling a method evaluates to data just like numeric expressions. For example, these are equivalent expressions, assumming the add() method does what you expect:

add(2,2)
2+2

The special thing about thinking of methods as operators is that they can evaluate to a void data type! There is, after all, a void return type, right? This basically makes the void expression valueless.

What does this error mean: 'java' is not recognized as an internal or external command?

This usually occurs because Windows doesn't know where to find java.exe or javac.exe after the JDK is installed. Here is how you tell the command prompt where to find the programs.
in Windows XP

  1. Locate the folder where javac.exe resides. You can use the "Find File" function in Windows to find "javac.exe". Usually it is installed in "C:\Program Files\Java\jdk1.5.0_06\bin".
  2. Right-click on "My Computer" and choose "Properties"
  3. In the "Advanced" tab, click the "Environment Variables" button near the bottom of the window.
  4. In the "System variables" section (bottom half) click on "Path" and then the "Edit" button.
  5. Add the location of the folder housing "javac.exe" to the front of the "Variable value." You may need to use the home key on the keyboard to get to the beginning of the value. Follow the folder with a semicolon. For example, I would add "C:\Program Files\Java\jdk1.5.0_06\bin;" to the front of my value.
  6. Click "OK" to save the variable
  7. Click "OK" to close the variables window
  8. Click "OK" to close the properties window.
  9. Test the new path by opening a new command prompt and typing "javac -version" and hitting enter. It should find the javac.exe program and print some help information.

What does this error mean: Exception in thread "main" java.lang.NoCLassDefFoundError

This can occur for one of many reasons.

CauseSolution
Your class does not have a main method or the main method is improperly declared Ensure that your class has a main method like this:
  public static void main(String[] args) {
    //your code here
  }
You are including ".class" when running your program. Do not type "java Hello.class", instead just use the class name. E.g. "java Hello".
The ".class" file is not in your current directory. Type "dir" and hit enter to list the items in the directory. If the ".java" file exists but the ".class" file does not, compile it first, then run it. Otherwise, you should change to the directory containing your ".class" file using the cd command.
Your current directory is the one containing the ".class" file, but java.exe cannot find it. Try invoking the java command with a "-cp" parameter. For example, "java -cp . Hello" for the class "Hello." If it works, then you must add "." to your classpath.

How do I add "." (the current working directory) to my classpath so java can find my class files?

in Windows XP
  1. Right-click on "My Computer" and choose "Properties"
  2. In the "Advanced" tab, click the "Environment Variables" button near the bottom of the window.
  3. In the "System variables" section (bottom half) click on "CLASSPATH" and then the "Edit" button.
  4. Add ".;" to the front of the "Variable value." You may need to use the home key on the keyboard to get to the beginning of the value.
  5. Click "OK" to save the variable
  6. Click "OK" to close the variables window
  7. Click "OK" to close the properties window.