Nice book written by Ivor Horton.
Below are few quotations i found from this book from the next 3 chapters ( 4-5-6).
1) String objects are said to be immutable—which just means that they cannot be changed.
2) We use an array to hold multiple values of the same type, identified through a single variable name.
3) We reference an individual element of an array by using an index value of type int. The index value for an array element is the offset of that element from the first element in the array.
4) An array element can be used in the same way as a single variable of the same type.
5) We can obtain the number of elements in an array by using the length member of the array object.
6) An array element can also contain an array, so you can define arrays of arrays, or arrays of arrays of arrays, and so on.
7)A String object stores a fixed character string that cannot be changed. However, you can assign a given String variable to a different String object.
8) You can obtain the number of characters stored in a String object by using the length() method for the object.
9) The String class provides methods for joining, searching, and modifying strings—the modifications
being achieved by creating a new String object.
10) StringBuffer and StringBuilder objects can store a string of characters that you can modify.
11) StringBuffer and StringBuilder objects support the same set of operations. StringBuffer objects are safe when accessed by multiple threads of execution whereas StringBuilder object are not.
12) You can get the number of characters stored in a StringBuffer object by calling its length() method, and you can find out the current maximum number of characters it can store by using its capacity() method.
13) You can change both the length and the capacity for a StringBuffer object.
14) You can create a String object from a StringBuffer object by using the toString() method of the StringBuffer object.
15) While the pass-by-value mechanism applies to all types of arguments, the effect for objects is different from that for variables of the primitive types. You can change an object, because a variable of a class type contains a reference to an object, not the object itself. Thus, when you use a variable of a class type as an argument to a method, a copy of a reference to the object is passed to the method, not a copy of the object itself. Because a copy of a reference still refers to the same object, the parameter name used in the body of a method will refer to the original object that was passed as the argument.
16) A class definition specifies the variables and methods that are members of the class.
17) Each class must be saved in a file with the same name as the class, and with the extension .java.
18) Class variables are declared using the keyword static, and one instance of each class variable is shared among all objects of a class.
19) Each object of a class will have its own instance variables—these are variables declared without using the keyword static.
20) Methods that are specified as static can be called even if no class objects exist, but a static method cannot refer to instance variables.
21) Methods that are not specified as static can access any of the variables in the class directly.
22) Recursive methods are methods that call themselves.
23) Access to members of a class is determined by the access attributes that are specified for each of them. These can be public, private, protected, package private, or nothing at all.
24) Classes can be grouped into a package. If a class in a package is to be accessible from outside the package, the class must be declared using the keyword public.
25) To designate that a class is a member of a package, you use a package statement at the beginning of the file containing the class definition.
26) To add classes from a package to a file, you use an import statement immediately following any package statement in the file.
27) A nested class is a class that is defined within the definition of another class. Objects of a nested class type can be created only in the context of an object of the outer class type.
28) Objects of a static nested class type can be created independently, but the static nested class name must be qualified by the outer class name.
29) A native method is a method implemented in a language other than Java. Java programs containing native methods cannot be applets and are no longer portable.
30) Below is the same example of LinkedList implementation.
package Ch06.TryLinkedList;
public class LinkedList {
private ListItem start = null; // First ListItem in the list
private ListItem end = null; // Last ListItem in the list
private ListItem current = null; // The current item for iterating
// Default constructor - creates an empty list
public LinkedList() {}
// Constructor to create a list containing one object
public LinkedList(Object item) {
if(item != null) {
current=end=start=new ListItem(item); // item is the start and end
}
}
// Construct a linked list from an array of objects
public LinkedList(Object[] items) {
if(items != null) {
// Add the items to the list
for(int i = 0; i < items.length; i++) {
addItem(items[i]);
}
current = start;
}
}
// Add an item object to the list
public void addItem(Object item) {
ListItem newEnd = new ListItem(item); // Create a new ListItem
if(start == null) { // Is the list empty?
start = end = newEnd; // Yes, so new element is start and end
} else { // No, so append new element
end.next = newEnd; // Set next variable for old end
end = newEnd; // Store new item as end
}
}
// Get the first object in the list
public Object getFirst() {
current = start;
return start == null ? null : start.item;
}
// Get the next object in the list
public Object getNext() {
if(current != null) {
current = current.next; // Get the reference to the next item
}
return current == null ? null : current.item;
}
private class ListItem {
// Constructor
public ListItem(Object item) {
this.item = item; // Store the item
next = null; // Set next as end point
}
// Return class name & object
public String toString() {
return "ListItem " + item ;
}
ListItem next; // Refers to next item in the list
Object item; // The item for this ListItem
}
}
package Ch06.TryLinkedList;
public class Point {
// Create a point from its coordinates
public Point(double xVal, double yVal) {
x = xVal;
y = yVal;
}
// Create a point from another point
public Point(Point point) {
x = point.x;
y = point.y;
}
// Convert a point to a string
public String toString() {
return x+","+y;
}
// Coordinates of the point
protected double x;
protected double y;
}
package Ch06.TryLinkedList;
public class PolyLine {
// Construct a polyline from an array of coordinate pairs
public PolyLine(double[][] coords) {
Point[] points = new Point[coords.length]; // Array to hold points
// Create points from the coordinates
for(int i = 0; i < coords.length ; i++) {
points[i] = new Point(coords[i][0], coords[i][1]);
}
// Create the polyline from the array of points
polyline = new LinkedList(points);
}
// Construct a polyline from an array of points
public PolyLine(Point[] points) {
polyline = new LinkedList(points); // Create the polyline
}
// Add a Point object to the list
public void addPoint(Point point) {
polyline.addItem(point); // Add the point to the list
}
// Add a point from a coordinate pair to the list
public void addPoint(double x, double y) {
polyline.addItem(new Point(x, y)); // Add the point to the list
}
// String representation of a polyline
public String toString() {
StringBuffer str = new StringBuffer("Polyline:");
Point point = (Point) polyline.getFirst();
// Set the 1st point as start
while(point != null) {
str.append(" ("+ point+ ")"); // Append the current point
point = (Point)polyline.getNext(); // Make the next point current
}
return str.toString();
}
private LinkedList polyline; // The linked list of points
}
package Ch06.TryLinkedList;
public class TryPolyLine {
public static void main(String[] args) {
// Create an array of coordinate pairs
double[][] coords = { {1., 1.}, {1., 2.}, { 2., 3.},
{-3., 5.}, {-5., 1.}, {0., 0.} };
// Create a polyline from the coordinates and display it
PolyLine polygon = new PolyLine(coords);
System.out.println(polygon);
// Add a point and display the polyline again
polygon.addPoint(10., 10.);
System.out.println(polygon);
// Create Point objects from the coordinate array
Point[] points = new Point[coords.length];
for(int i = 0; i < points.length; i++) {
points[i] = new Point(coords[i][0],coords[i][1]);
}
// Use the points to create a new polyline and display it
PolyLine newPoly = new PolyLine(points);
System.out.println(newPoly);
}
}
31) An abstract method is a method that has no body defined for it and is declared using the keyword abstract.
32) An abstract class is a class that contains one or more abstract methods. It must be defined with the attribute abstract.
33) You can define one class based on another. This is called class derivation or inheritance. The base class is called a superclass, and the derived class is called a subclass. A superclass can also be a subclass of another superclass.
34) A subclass inherits certain members of its superclass. An inherited member of a class can be referenced and used as though it were declared as a normal member of the class.
35) A subclass does not inherit the superclass constructors.
36) The private members of a superclass are not inherited in a subclass. If the subclass is not in the same package as the superclass, then members of the superclass that do not have an access attribute are not inherited.
37) The first statement in the body of a constructor for a subclass should call a constructor for the superclass. If it does not, the compiler will insert a call for the default constructor for the superclass.
38) A subclass can re-implement, or overload, the methods inherited from its superclass. If two or more subclasses, with a common base class, re-implement a common set of methods, these methods can be selected for execution at run time.
39) A variable of a superclass can point to an object of any of its subclasses. Such a variable can then be used to execute the subclass methods inherited from the superclass.
40) A subclass of an abstract class must also be declared as abstract if it does not provide definitions for all of the abstract methods inherited from its superclass.
41) You can import the static members of a class that is defined in a named package to allow the static members to be referenced by their unqualified names.
42) An enumeration type is a specialized form of class, and the enumeration constants that you define are instances of the enumeration class type.
43) A class defined inside another class is called a nested class or inner class. An inner class may itself contain inner classes.
44) An interface can contain constants, abstract methods, and inner classes.
44) A class can implement one or more interfaces by declaring them in the class definition and including the code to implement each of the interface methods.
45) A class that does not define all the methods for an interface it implements must be declared as abstract.
46) If several classes implement a common interface, the methods declared as members of the interface can be executed polymorphically.
About the Author
Ivor Horton started out as a mathematician, but shortly after graduating, he was lured into messing about with computers by a well-known manufacturer. He has spent many happy years programming occasionally useful applications in a variety of languages as well as teaching mainly scientists and engineers to do likewise. He has extensive experience in applying computers to problems in engineering design and to manufacturing operations in a wide range of industries. He is the author of a number of tutorial books on programming in C, C++, and Java. When not writing programming books or providing advice to others, he leads a life of leisure.
No comments:
Post a Comment