Keeping an IBT (Idioms Bugs and Tricks) Journal

Typical Format: IBT Journal = {Entries} -- read that as 'a set of entries'.
An ENTRY = HEADING {[SubHeading] {<example> // <comment>}} -- read this as "An Entry is a HEADING in all caps following by a set of <example> // <comment> lines with optional, denoted by square brackets, SubHeadings.
HEADING -- gives the Category of the Entry
SubHeading -- allows elements under a HEADING to be further subdivided (this can be continued with other optional kinds of sub-headings.
<example> // <comment> -- is a LINE within an Entry composed of an example followed by some explanatory comments, ex.

SYNTAX
GENERAL RULES
1) All lines of code generally end with semicolons.  Exceptions are variousl kinds of headings and sometimes blocks of statements terminated by curly braces.
2) Access Modifiers -- generally we make DATA PRIVATE and FUNCTIONS PUBLIC but there are exceptions. public private protected are the access modifiers.


DATA TYPES

Primitive Data Types

Integer Numbers
int    // a four byte integer (32 bits)  ex. 5409  -2006
byte   // byte-sized integer (8 bits) ex. 24  -2
short  // short integer (16 bits) ex. 137 -119
long   // long integer (64 bits)

Real Numbers
float   // single precision floating point 43.889F
double 
// double precision floating point

Other Data Types
char    // a singe character (16 bits) ex. 'm' '?' '\u00F6'
boolean // logical value (true or false)  ex. true false.

Built In Classes note the convention that class names begin with a capital letter.
String // a double quote delimited set of characters, a Class in Java, ex. String name = "Ray Schneider";



FUNCTIONS
Function Headers
<access modifier> <return type> <function name> ( <parameter list> )
<access modifier> = public | private | protected
<return type> = any legitimate type including user defined types and classes, NOTE: void is used with the function is a    procedure, i.e. it returns no value.
<function name> = name of the function following naming conventions and not treading on any predefined names.
<parameter list> = comma delimited list of tuples of the form <type> <variable name>, explicit values can be used when functions are called.

Example Function Headers
String getName() //note that no access modifier is used String is the return type and getName() is the function name and the call requires no parameters.
void changeName(String newName) //a procedure (returns no value) takes a String as an argument and presumably changes the value of a particular String in a class.

CONSTRUCTORS

public ClassName( [optional parameter list] )
{ //Body of Constructor -- constructors initialize an Object of the Class
}
ex. public TicketMachine(int ticketcost)
    {
       price = ticketCost;
       balance = 0;
       total = 0;
    }
Kinds of Constructors

DEFAULT Constructors -- a Constructor without parameters and the Constructor BODY initializes the Fields to DEFAULT values

EXPLICIT VALUE CONSTRUCTOR -- a Constructor with parameters which the caller uses to initialize the Fields

COPY CONSTRUCTOR -- a Constructor that is used to create an identical copy of another Object, it is required when the Class contains pointers (Java does not have pointers in the usual sense so this generally applies to C++)

SELECTION STATEMENTS --

if statement used to make two-way selection.

if(<condition>){
  //statements for true condition
}
else {
  //statements for false condition
}

STATIC METHODS
static methods are methods of a class, not an object and as such can be used independent of classes.  It still resides in a class.  Often these methods are used when an object method would be inappropriate as with mathematical operations that operate on numbers (See the Math class.

main(String[]args) is a special method which Java uses to start applications.  main() must be static because it starts running before the program can create objects.  The argument of main() -- String[] args -- is a String array that holds any command line arguments the program needs for it to run.

IMPORT STATEMENT AND PACKAGES -- A package is a set of related classes.

package
packageName; //is the syntax for declaring that all classes in a file belong to a certain package.  This line of code should be the first line of code in a source file containing the package classes.

import packageName.className; //is a fully qualified path which tells the compiler you want a particular class from a particular package.

import packageName.*; //imports all the classes from a package.  Based on the code, the compiler will select from the package those classes that it needs.

SOME IMPORTANT PACKAGES
java.lang
provides language support, ex. Math, default class.
java.util provides standard utilities, ex. Random.
java.io   provides Input and Output, ex. PrintStream
java.awt  provides Abstract Windowing Toolkit, ex. Color
java.applet provides Applet support, ex. Applet
java.net  provides Networking support, ex. Socket.
java.sql  provides Database access via Structured Query Language
java.swing provides the Swing User Interface, ex. JButton