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.
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.
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;
2 * 1 + 1 == 3;
(divisor) * (quotient) + (remainder) = (dividend)
(dividend) / (divisor) == (quotient); (dividend) % (divisor) == (remainder);
3 / 8 == 0; //3 is too small! 3 % 8 == 3; //everything left over
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; //...
___0_ r 3 8 ) 3 - 0 ---- 3 <-- remainder
void foo(int x, int y) { x += y; }
void foo(int x, int y) { x += y; }
int bar(int x, int y) { return x + y; }
int a = 1; a = bar(8, 9); a++;
void foo(int x, int y) { x += y; }
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.
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.
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
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.
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.
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
This can occur for one of many reasons.
Cause | Solution |
---|---|
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. |