Contents:
  1. Introduction
  2. Wrapped Class
  3. Array, ArrayList and Vector
  4. Encapsulation
  5. Inheritance


Introduction

Java is developed by a team led by James Gosling at Sun Microsystems. Java was originally called Oak (1991), and was used in embedded chips in appliancers. In 1995, Oak was renamed to Java, and is used to design internet applications.

Java Language Specification and API
Java Language Speficition is a technical definition of the language which includes the syntax and semantics of the Java programming language. The API (Application Program Interface) contains pre-defined class and interfaces to develop Java programs.

Java Edition and JRE
Three Java editions:
  • Java Standard Edition: to develop client-side applications.
  • Java Enterprise Edition: to develop server-side applications.
  • Java Micro Edition: to develop mobile device applications.

Java Runtime Environment (JRE) is a software that executes Java based applications. Java Virtual Machine (JVM) is a collection of programs to execute java bytecode on any computer program.

Java Development Kit
Java Development Kit is a software development environment. It consists of sets of separate programs, each invoked from a command line. It is used to develop Java applications. Examples of JDKs are:
  • JBuilder by Borland
  • NetBeans Open Source Sun
  • Eclipse Open Source by IBM
  • Code Warrior by Metrowerks
  • TextPad Editor
  • JCreator LE
  • JEdit

The Java source code file (.java) is compiled by the Java compiler, that generates Java bytecode executable file that will be executed by JVM.

Wrapper Class

All primitive data types are wrapped into a class in Java. Wrapper Class is contained in the package java.lang. Wrapper Class symbolizes the primitive data types as objects.

There are 8 primitive data types to be wrapped as a class in Java:
Primitive Type Wrapper Class Const. Arguments
byte Byte byte or string
short Short short or String
int Integer int or String
long Long long or String
float Float float, double or String
double Double double or String
char Character char
boolean Boolean boolean or String

All numeric class derived from abstract class Numeric have the method doubleValue(), floatValue(), intValue(), longValue(), shortValue() and byteValue(), which will return the value according to its data type.

Wrapper Class Constants
Each wrapper class has the constants MAX_VALUE and MIN_VALUE. As the name suggests, these constants represent the highest and lowest possible value of its data type. If we want to find the highest possible value of Integer, then we use Integer.MAX_VALUE.

Conversion Function
When initializing a value in a form of string, we can either use the static function valueOf, or use constructor:

If we have to parse value from a string, we can use the function parseInt, parseDouble, parseLong, parseShort, parseByte accordingly. If we want to parse a string to double, then we use Double.parse("12.4").

String Declaration
String is a collection of characters. There are several ways to declare string:

String Comparison
String comparison can be done in 3 ways:
  • Using the operator str1==str2.
  • Using the method str1.equals(str2)
  • Using the method str1.compareTo(str2). The method returns 0 if word1 is the same as word2.

String Methods
There are several methods that can be used:
  • str.length() is used to find the length of the string str.
  • str.charAt(idx) is used to find the character at index idx in the string str.
  • str1.concat(str2) is used to combine str1 and str2 into one string.
  • str.substring(idb,ide) is used to find the substring that begins at index idb and ends at index ide.
  • str.toLowerCase() is used to change all letters in str to lowercase.
  • str.toUpperCase() is used to change all letters in str to uppercase.
  • str.trim() is used to erase all leading and preceding spaces in str.
  • str.replace(sub1,sub2) is used to replace sub1 with sub2 in str.
  • str.replaceFirst(sub1,sub2) has the same functionality as the previous method, but this method only replaces the first occurence of sub1 in the entire str.
  • str.split(format,num) is used to split the string str to num string(s). The format acts like a separator that separates sub-string that will be formed upon splitting str.

Math Methods
There are several methods that can be used:
  • Trigonometric Methods: Math.sin(x), Math.cos(x), Math.tan(x), Math.asin(x), Math.acos(x), Math.atan(x), Math.toRadians(x), Math.toDegrees(x).
  • Exponent Methods: Math.exp(x), Math.log(x), Math.log10(x), Math.pow(x), Math.sqrt(x)
  • Math.ceil(x) is used to round-up x.
  • Math.floor(x) is used to round-down x.
  • Math.round(x) is used to round-down x+0.5.
  • Math.rint(x) is used to round x to the nearest even integer.
  • Math.max(x,y) is used to determine and returns the largest value between x and y.
  • Math.min(x,y) is used to determine and returns the lowest value between x and y.
  • Math.abs(x) is used to take the absolute value of the number x.
  • Math.random() is used to generate random numbers. This returns a double between 0-1.

Array, ArrayList and Vector

Array
Array is a group consisting of homogenous data type, with fixed dimension. It stores linear collections of elements. The elements of array can be accessed by index. Array is declared as such:


Processing an array can be done through simple looping, since the size of array is known.

Duplicating an array can be done through various ways:
  • Looping one by one.
  • System.arraycopy(array1, sp, array2, dp, n) is used to copy n elements from array1 starting from index sp, to array2 starting from index dp.
  • dataType [] dest = (dataType[]) sorc.clone() is used to copy array sorc to the array dest.

Array, ArrayList and Vector
Unlike array, the size of ArrayList and Vector is dynamic. ArrayList increases its size by half its original size. ArrayList is not synchronized. Vector increases its size by doubling its original size. Vector is synchronized.

ArrayList Methods
  • arlist.add(elm) is used to add elm to the ArrayList arlist.
  • arlist.clear() is used to delete all elements from the ArrayList arlist.
  • ArrayList dest = (ArrayList) sorc.clone(); is used to copy ArrayList sorc to dest.
  • arlist.contains(elm) is used to check the existence of the element elm in the ArrayList arlist.
  • arlist.get(idx) is used to find the element with the index idx in the ArrayList arlist.
  • arlist.isEmpty() is used to check whether the ArrayList arlist is empty or not.
  • arlist.remove(elm) is used to remove elm from the ArrayList arlist.
  • arlist.size() is used to determine the number of elements contained inside the ArrayList arlist.
  • arlist.set(idx,elm) is used to assign elm as the element with the index of idx.

Vector Methods
  • vec.addElement(elm) is used to add elm to the Vector arlist.
  • vec.clear() is used to delete all elements from the Vector arlist.
  • Vector dest = (Vector) sorc.clone(); is used to copy Vector sorc to dest.
  • vec.contains(elm) is used to check the existence of the element elm in the Vector vec.
  • vec.insertElement(elm,idx) is used to add elm at the index idx.
  • vec.elementAt(idx) is used to find the element with the index idx in the Vector vec.
  • vec.isEmpty() is used to check whether the Vector vec is empty or not.
  • vec.remove(elm) is used to remove elm from the Vector vec.
  • vec.size() is used to determine the number of elements contained inside the Vector vec.
  • vec.set(idx,elm) is used to assign elm as the element with the index of idx.

Encapsulation

Encapsulation is a process of concealing data inside of an object. Which means, the data inside the object cannot be directly accessed by other parts of the program. To simulate encapsulation, we can create a new class and define a variable inside that class with the keyword private. In order to access that variable, we can also define a public method in that class which will return the variable itself.

Class Relationships

Association is a general binary relationship that describes an activity between two classes. It is visually illustrated by a line that connects these two classes.
Aggregation implies a relationship where the child classes cannot exist without the existence of the parent class.
Composition implies a relationship where the child classes can exist without the existence of the parent class.


Inheritance

Inheritance is a process where an object acquires the properties of another object through the keyword extends and implements. In this case, the classes that gains the properties is called the subclass, while the class that has the properties since the beginning is called the superclass or parent class. However, a subclass cannot acquire inheritance more than one parent class.

Overriding
Should there exist method with the same name in parent class and subclass, the method in subclass will be used instead.

Overloading
There can be multiple methods with the same name, but different parameters.