The basics look familiar to C/C++ programmers.
JAVA_HOME The base directory for Java (CSUC: already set)
CLASSPATH A ":" separated list of directories containing compiled
.class files for use. Also include :. for the current
working directory (i.e., the one you are running the program from). For Windows, directories are separated with a ";".
The JDK java classes used to be a /lib/classes.zip file but it is no longer
at that same location and the user no longer needs to set the value in
the CLASSPATH...it is done at installation)
When the compiler must refer to your own classes you need to specify their location. The CLASSPATH is a sequence of directories (or zip files or jar files) in which javac searches for classes not already defined in any of the files specified directly as command arguments. The compiler looks in the class path for both a source file and a class file, recompiling the source (and regenerating the class file) if it is newer.
PATH The normal executable search list should include $JAVA_HOME/bin (In Chico at /usr/java/bin )
On a PC, it depends on where you put the files and what version of Java.
In the newest versions of Java, this variable is set up for you automatically upon installing. However, if you try to run a command from the prompt and your system says it does not
know the command, you should set it. Various OS versions are set in different ways for different OSs (Java 1.2 "changed the rules". In the JDK 1.2 readme file,
we see this particularly the last paragraph
)
The PATH is where the
tools to run java are actually located. (From the complete tools and utilities page with its link
to the javadoc page)
NOTE:A very common error (when people get "can't find class" errors), is because either the CLASSPATH is not set correctly, file permissions are not set to be readable, or (more intricate) the files are in a package and you are not in the right place for java to see the package.
API Java API (Application Programming Interface) ***look at it!
The Java Class Libraries -- original packages:
new ones in 1.1:
new ones in 1.2 (Enterprise Edition):
See my paper on some of these current java tools and use in AI.
When you program in Java, you place all of your code inside methods.
Comments:
/* continues until the next */
// until the end of the line
/** used to generate documentation automatically */ See javadoc
Valid Identifiers (or variable names): (Types, Values and Variables) slide 49, 50 from Ch. 1
More Convention:
Class names are started with capitals (Applet, Graphics, MyApplet).
Methods and variable names are started with lower case (paint, myVariable)
Primitive Data Types and Literals:
Java uses four basic (primitive) element types: integer, floating point,
boolean, character.
(Remember Boolean and Character wrappers
since utility classes require the use of objects)
(Realistically: four integer types, two floating-point types, char and boolean)
| decimal | octal | hexadecimal |
| 2.21 | 077 | 0xDC00 |
Integer:
| Length | Name | 8 bits | byte | 16 bits | short | 32 bits | int | 64 bits | long |
Here is the range of values for each
| Data Type | Range |
| byte | [-128 , 127] |
| short | [-32768 , 32767] |
| int | -2,147,483,648 to 2,147,483,647 |
| long | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
The most common integer type is int.
Integer: if you type 4, you get an
int with value 4. Usually programmers use int by default.
Java is strongly typed - every variable and every expression has a type that is known at compile time.
If you
want it to be a long, you need to add an L (or lowercase l) at the end of the number.
long
myNumber = 12345678900L;
Floating Point (Decimal)
Float:
| Length | Name | 32 bits | float | 64 bits | double |
Here are some examples of numbers with decimal values:
3.14
3.1E12 means
3.1 X 1012
2e12 means 2 X 1012 (E or e makes no difference)
12.34f means 12.34
floating point
-32.0f means -32.0 floating point
The ranges for these types are something like these given but I suggest you go to the Java Language Specification if you want a detailed explanation (for all intents and purposes - they allow big numbers):
| Data Type | Range |
| float | +-1.4 * 10-45 to +-3.4 * 1038 |
| double | +-4.9 * 10-324 to +-1.8 * 10308 |
A variable must be declared before it can be used. Whatever type it is.
Allowed conversions (from Core Java Ch. 3)
Boolean
true false
Examples of some other "keystrokes" (special escape sequences) represented in Java:
\t (tab)
\b (backspace)
\n (linefeed)
\r (carriage return)
\f (form
feed)
\" (double quote)
\' (single quote)
\\ (backslash)
Strings (see below)
String (** actually this is a class: java.lang.String )
"This is a string literal"
Strings are immutable - values cannot be changed after creation
(they are not simple arrays of characters as in C++)
Each String constant results in a String object
**Notice: When you use a String literal in a Java program, Java automatically
creates an instance of a the class String.
So,
a = = "" ; // can never be true since "" creates a new empty string
This used to be true but is not anymore. Interesting.
Note: can concatenate, and work with substrings, but cannot change specific chars in a string
Discussion on strings and how differ from C - Core Java : Chapter 3
More on StringBuffer class later
Arrays
You can declare arrays of any typeoptional placement of []
You can even build arrays of arrays:
Array boundaries are checked at run-time to avoid overflowing a stack and corrupting memory.
Note:
Arrays have an attribute length
You cannot create static arrays at compile time:
You can also declare and initialize non-rectangular arrays with
nested initializations
Operators and Precedence
Block Statements
Branching
if/else (must be Boolean: not 0 or 1)
expressions must be constants: int, byte, short or char (convertable via
casting to int (automatic promotion) )
casting aside: ((Circle)this).r ;
Consider for some graphics method specify Class Variables:
They are constant since final says no one can change them
If no breaks, drop-through (once match, executes all below)
Looping (while, do and for same as C, C++)
While loops
Enhanced for
To create an array, you can use one of two methods:
To make an empty array
Or you can fill an array with its initial values:
This is equivalent to:
And, yes, this works for Objects as well. (I.e., you can list the
Objects in {} to initialize the array of some type of object.
Nor can you attempt to fill an array without declaring the size of the array
with the new operator:
won't work.Aside: From Core Java, Of course, while it is true that the ++ operator gives the C++ language
its name, it also led to the first joke about the language.
C++ haters point out that even the name of the language contains a bug:
"After all, it should really be called ++C, since we only want to use a language after it has
been improved."
note: In C++ precedence of new
new T
C++: (new T).msg(); // must put parenthesis
java: new T.msg(); // no parens needed. Creates new T, then sends message
{}
Flow Control
switch
Alternatively, sometimes one sees "constant" variables set:
Do loops
Example:
For loops
note: no code outside of class methods as in Smalltalk, if inside a class, could say simply setColor(Color.green);
General