Review of some JAVA Basics

May 23rd and 25th, 2000

    JAVA    p. 423-426

JAVA Coding Standard

Why do we need a coding standard?

What is good about it and how is it useful?

IDENTIFIERS

p.427-430

Should begin with a letter and may contain additional letters and digits. Identifiers are the same as variables.

Need to print extra characters?

What about the unicode set?

This is the format: \uHHHH

Remember any other escape characters such as \n, \t and \\?

 

The following lists some of these conventions based on the type of identifier:

Class name

The first letter of each word is capitalized, examples:

Mammal,  SeaMammal

 

Function name

The first letter of each, except the first, word is capitalized, examples:

getAge, setHeight

 

Variable name

The first letter of each, except the first, word is capitalized, examples:

age, brainSize

Constant names

Every letter is capitalized and underscores are used between words, examples:

MAX_HEIGHT, MAX_AGE

 

RESERVED WORDS

As you may have noticed, many of Java's keywords are borrowed from C/C++. Also, as in C/C++, keywords are always written in lowercase. Generally speaking, Java's keywords can be categorized according to their function as follows (examples are in parenthesis):

Data declaration keywords (boolean, float, int)

Loop keywords (continue, while, for)

Conditional keywords (if, else, switch)

Exception keywords (try, throw, catch)

Structure keywords (class, extends)

Modifier and access keywords (private, public)

Miscellaneous keywords (true, null)

The following keywords and their use, you SHOULD know.

boolean default for private switch
break double if protected this
case else import public throws
catch extends int return try
char final new static void
class float package super while

VARIABLE SCOPE

Where is it appropriate to declare a variable? Does it make a difference?

PRIMITIVE DATA TYPES

  • int (16-bit)
  • float (32-bit)
  • double (64-bit
  • boolean (true/false)
  • char (16-bit unicode)

 

 

OBJECTS

ARRAYS

int[] array = new int[5]; // one-dimensional array

it may also be declared as:

int[] array = {1, 2, 3, 4, 5}; // it is initialized and declared at the same time.

Arrays can be of any primitive data type or object. Arrays have an instance variable "length" that we can access to find out how big the array is. Remember that an Array can throw an IndexOutOfBoundsException. Make use of it to make your code safe.

VECTORS

Must import java.util.*;

Unlike arrays, vectors can only hold objects, no primitive data types are allowed. Vectors can grow big and unbounded. You may want to specify the capacity of the vector but you don’t need to. The default capacity is 10 and it doubles in size every time the capacity is reached.

Just like arrays have the "length" instance variable, Vectors have a .size() method that returns the current size of the vector.

ENUMERATION

This is an iterator and it is very important concept in data structures for ICS211. An Enumeration lets us visit every element of a Vector one by one.

Vector v = new Vector();

Enumeration e = v.elements();

While(e.hasMoreElements()){

Object o = e.nextElement;

// we do something with each element.

}

 

OPERATORS AND PRECEDENCE IN JAVA

Pages 436, 437 AND 438

You need to know:

FLOW CONTROL / REPETITION STATEMENTS

Must be familiar with:

 

The student must be familiar with: