Java:

Description
Site
Support Sites http://www.sun.com
Sun's Java Developer Connection http://java.sun.com/jdc
Java Ranch http://javaranch.com
References For Notes Below
Shorthand
Deitel & Deitel, Java How to Program, 3rd Ed. JHP
Cadenhead, Sams Teach Yourself Java 2in 24 hours SJ2
Bishop & Niegel, JavaGently for Engineers & Scientists, 3ed JG

JAR Files and JAVA Some Java packages don't come precompiled so we must copy the package and compile it using the JDK or Java IDE of choice. In my case, I will be using JDK v1.3, downloaded from Sun. This step will allow the import statement (from above) to work on any new classes

How to download and compile Java packages(using JavaGently reference):
1. Download the compressed or zipped classes from the web (javagently and myutilities) http://javagently.cs.up.ac.za/jg3e/

2. Go to a directory above that which all the programs are and setup *.jar files
The two sets of classes we download (javagently and myutilities) must maintain
their names. So They must stay in their own folders. The original class files
will not be necessary, only the resulting *.jar file in each of these folders.

To create each jar file:
- in the directory containing the javagently classfiles to be jared:
jar cf javagently.jar javagently/*.class or
jar cf laughtrack.jar laughtrack.class laugh.wav

- take the resulting javagently.jar file and stick it into the javagently directory
jar tvf javagently.jar (allows you to view the contents of a jar file)
- repeat for the directory containing the myutilities classfiles
Note: jar command has several options: c = creates archive, f = specifies file names, v = view etc.

3. Add to the Classpath C:\myjava\javagently\javagently.jar;C:\myjava\javagently\myutilities\myutilities.jar
to the classpath so that these packages will be available to all programs that use the
reference them in their import statements
Java Intro:

Allows distributed multi-tier computing over networks (parts of applications operate on seperate computers distributed over a network). A step up from just Client/Server Computing (where computers are just sharing information in a network where certain computers act as file servers, print servers etc..). Java is an Internet based Application capable of Multi-Threading taking advantage of multi-processor hardware vs. sequential programming.. Since Java is Compiled and Interpreted it is portable and platform independant but slow. This is because Interpreters execute slowly compated to fully compiled machine code. Benefit is that an Interpreted program can be executed once its downloaded unlike a source program which must be compiled first.

Steps:
  1. Edit a new file *.java with text editor
  2. Compile java file into byte code to be interpreted during execution.
      javac classname.java (compiles class file)
  3. Load, Verify, Execute (all done with java command
      java classname (executes)
  4. During execution, the Interpreter reads bytecodes and translates into native
      machine code. Security is also checked.

Languages:
  1. Machine(1's & 0's - specific to a computer's hardware).
  2. Assembly (needs translation)
  3. High Level
      - Single statements doing several tasks
      - Compiled to machine language (fast)...hardware dependant
      - Interpreted, directly executed high level languages w/o compiling. This is
        more convenient but slower.
      - Compiled & Interpreted (JAVA)...so not hardware dependant.
This is the reason why C and C++ are not as portable as Java (e.g. primitive datatypes like char or int were not guaranteed to be identical from machine to machine...depending on what machines they were comiled on). C++ allows multiple inheritance which may cause problems along with no automatic garbage collection. In Java you can only inherit from 1 class (if a class doesn't explicitly extend another class it automically extends the Object class).

Object vs. Procedural (JHP 372):
  1. OO (Program the class from which objects are instantiated). Focus is on
      classes that become reuseable components or "std parts"). So as long as
      class interfaces stay the same, no need to change source code. For this
      reason implementations are hidden making code simpler and reusealbe.
      Most other programs are implentation dependant so the implementation
      must always change.
  2. Procedural (Action oriented..program the functions which are grouped into
      programs). Don't focus on reuseable parts.

Java Escape Sequences

\n, new line
\t, horizontal tab
\r, carriage return
\\, back slash
\", quote
Note: these all go inside the " " in a string
(e.g. System.out.println("kk is cool \t beans");

Best Practices

-Class -- capital
-Variable name identifiers -- lowercase
-Keywords -- lowercase
-Use RGB designation to create colors for Background and Text
-Use constant variables to clarify a program

Java Arithmetic, Operators & Data Types %, modulus (e.g. r mod s (remainder of r/s)
= =, is equal to
!=, is not equal to
>=, greater than or equal to
=, assignment operator used to assign values to variables
Note: datatypes must be same when operating on numbers (Java is strongly typed)

Type Size (bits) Value
boolean 8 true / false
char 16 Unsigned
byte 8 -128 to 127
short 16 -32,768 to 32,767
int 32 -2,147,483,648 to 2,147,483,647
long 64 -9.2E18 to 9.2E18
float 32 -3.4E38 to 3.4E38 (decimal values)
double 64 -1.8E308 to 1.8E308
Size is calculated based on powers of 2: so int = 31 bits + 1 bit for the sign. So (-2^31 through 2^31). Examples of binary (base 2) numbers:
-------------------------------------------------------------------------
1(base2) = 1, since 2^0 = 1
1101(base2) = 13, since 2^3 + 2^2 + 0 + 2^0 = 13
1000 0000 = 128, since
-------------------------------------------------------------------------
(JHP 144, DataTypes)
OO Design (JHP 432) -OO Design for implementation inheritance: Subclasses tent to use superclass definitions of mehtods, thereby placing most of the functionality high in the hierarchy.
-OO Design for interface inheritance: functionality low in hierarchy. Subclasses provide custom implementation of methods.
-Implementing an interface: must define all the methods specified by the interface
Applets vs Applications

-Applications have a main statement, Applets do not
-Applets use special tags in the HTML code
  <html>
  <applet code = "rootApplet.class" height =100 width =300>
  </applet>
  </html>
-all applets must subclass JApplet
-all files associated with the applet must be in the same folder as the webpage that loads the program.
-applet windows have an origin at the top left corner by default
-applets use parameters instead of arguments for applications
-applets use init() statement to start, load, values define variables
-applets use run() to start an applet (similar to application's main statement)
-catch & try statements used to handle errors
-paint() handled only when screen is updated, repaint() forces a paint()
-applets can use play() to play *.au, *.aiff, *.wav files

Java Packages

Note: Only the package path's are searched and not subdirectories
package java.lang is always imported in every Java program, contains such classes as System (used for System.out.println();)(JHP 339)
java.lang
-- Object, String, Integer, Math...and more. Automatically imported and avaialble to all java programs
java.util -- contain utility functions like date, time
javax.swing -- contains JApplet class and other GUI classes (used to be com.sun.java.swing in JDK 1.1)
java.awt.* -- (Abstract Windowing Toolkit) contains classes to create fonts based on typeface, style, size

Java Rules -Only 1 class can be public, other classes are helpers. The srouce file name must match the public class name.
-Extend only 1 class, import many classes (idea is to decide on which class you want to subclass).
-For applets, place all referenced classes in the same folder
-When moving graphics or text over a static boundary, use double buffering to create animations (use 3 images: back, front, worksapce)
Java Terminology static methods, called by following their class name with dot notation. Note: make an object method into a class method by using the "static"
parameters -- arguments for an applet (e.g. background, foreground, text, fontname, fontsize)
private variables and public methods-- restricts access to methods in the class itself. May also have private methods and public data in the case of utility classes. Note: Get and Set methods allow users to access and mutuate a private variable from another class (but in a restricted manner as outlined in the set and get implementations!).
static variables-- applies to entire class, use when a single copy of data is good enough. Note: always use class name and not object name when dealing with class variables!
Instance variable (ivar) --applies only to a single
Abstraction -- Abstract classes are created to simply provide inherited interfaces & implementations. Can not be instantiated like "concrete classes".
Polymorphism -- one method (in a super class) effects its subclasses differently (e.g. the drawYourself() method). This promotes extensiblity. Also allows dynamic binding so a new object calling the draw() method doesn't require that the superclass containing the draw() method be recompiled. Another example: Subclass animal with dog object to you can modify the override the sound it makes or the speed it runs etc..
Abstract methods -- e.g. earnings() may be an abstract method since it needs to know the type of employee object to operate on and can't peform on a generic object. This would differentiate between calculating a worker's earnings vs. a manager's earnings. (ref. How to Program pg. 414).
final -- keyword indicating a variable, method or class can't be changed or over-ridden.
primitive variables (attributes)-- variables of the basic data type
reference variables (attributes)-- "remote control" for an object. In general, object variables have unique names and the objects they refer to have unique ID's. So we never use the actual objects created...we only use references to those ojbects. Reference vara
casting -- converts data from one type to another
access control -- if no statement is specified (ie public, private, protected then the variable is is available to any class in the SAME Package
void -- use if a method should not return anything (ie. the main method)
this -- referes to the current object
super -- refers to the immediate superclass of the object
java plug-ins -- add the Java VM and other functionality to web browsers
attibutes -- have a name and value. Parameters are attributes.
implements -- statement used to inherit additional methods beyond those inherited from the super class. (e.g. Runnable & ActionListener interfaces)
importing -- makes classes available so we don't neet to specify the full path name of variables and methods
components -- buttons, text fields created by using a component object and the add() method. Applets are examples of containers because they hold "components"
content pane -- represents main area where components are diplayed. The pane is an object of the component class in java.awt. An objecte's content pane is returned by getConentPane() method.
double buffering -- when you use 3 image objects to smooth an animation(e.g. front, back, and workspace
EJB -- Enterprise Java Beans. Reuseable collection of classes and frameworks for implementing custom logic. Serverside component model and vendor independent programming interface for Java App servers
Enterprise -- "Extremely Robust Computing"
MDOA -- Multitier Distributed Architecture
J2EE -- Java Enterprise Edition Platform (JSP, EJB). Component based, server centric, multitier applications architecture.
Java Environment -- Java Programming Language, Java compiler, Java VM
Components -- predeveloped pieces of application code, not standalone apps..need a container to run in.
Container -- thread in which components can run
CORBA -- Comprehensive Distributed Object Infrastructure based on an Object Request Broker
RMI -- Remote Method Invocation
Persistent Data Store -- e.g. relational database