Java Syntax

The basics look familiar to C/C++ programmers.

Environment variables:

(ksh, csh : export, setenv)

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


Syntax

Some differences from C/C++

Valid Identifiers (or variable names): (Types, Values and Variables) slide 49, 50 from Ch. 1

Some examples that are OK: Some examples that follow convention (Usually, do not use underscore, instead style is to use capitals for beginning of the second word): Examples in Java: Keywords: (Appendix of Core Java)
Some which are reserved but not used (goto):
newest list

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)

Integer: if you type 4, you get an int with value 4

Putting 0's in front means something completely different (base 8 and Hexadecimal (Base 16)
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

You can use e or E (for powers of 10), f or F (for float), d or D (for double) but the d or D is not required since it is the default and hence usually omitted.
The f or F must be given for float types.

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
If you omit the f Java assumes double. So, what would be wrong with
float myValue = 12.3;
What would happen?

A variable must be declared before it can be used. Whatever type it is.

Allowed conversions (from Core Java Ch. 3)

Boolean
true false

Character (char):
if you type 'a', you get a char with value a

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)

These are often used within 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

Reference types (Classes, Interfaces, Arrays)

Arrays

You can declare arrays of any type

optional 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:
however An array is an object (sorta).
It is useful to consider arrays to be a separate kind of reference type from objects.
However, arrays may be assigned to variables of type Object and the methods of the Object class may be invoked for arrays.

Arrays have an attribute length

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.

You cannot create static arrays at compile time:

Nor can you attempt to fill an array without declaring the size of the array with the new operator: won't work.

You can also declare and initialize non-rectangular arrays with nested initializations

Operators and Precedence
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

Block Statements
{}



Flow Control

Branching

if/else (must be Boolean: not 0 or 1)

switch

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:

Alternatively, sometimes one sees "constant" variables set:

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

Do loops Example: For loops

Enhanced for

Example a code fragment (would be a part of a method) that draws several lines and cycles the lines colors between red, blue and green:

note: no code outside of class methods as in Smalltalk, if inside a class, could say simply setColor(Color.green);

General

On the other hand, we have continue

Produces
3
4
5

Inside scoop:
java file - javap - byte code disassembler- located at 0xCAFEBABE
Next, Instance Variables and Methods, Constructors, Finalizers