View on GitHub

reading-notes

Reading notes taken while attending Code Fellows classes.

Class 03

Index

Home
Primitives vs. Objects
Exceptions vs. Objects
Using Scanner

Primitives vs. Objects

Integer j = 1;          // autoboxing
int i = new Integer(1); // unboxing

prim-mem

To summarize, objects in Java are slower and have a bigger memory impact than their primitive analogs. Depending on the projects memory needs, use what makes the most sense.

prim-mem2

Exceptions in Java

What is an Exception?

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

Creating an exception object and handing it to the runtime system is called throwing an exception.

The runtime system searches the call stack for a method that contains a block of code that can handle the exception. This block of code is called an exception handler. The exception handler chosen is said to catch the exception.

Catching and Handling Exceptions

Here are three exception handler components:

try
catch
finally

Here is an example of handling exceptions:

import java.io.*;
import java.util.List;
import java.util.ArrayList;

public class ListOfNumbers {

    private List<Integer> list;
    private static final int SIZE = 10;

    public ListOfNumbers () {
        list = new ArrayList<Integer>(SIZE);
        for (int i = 0; i < SIZE; i++) {
            list.add(new Integer(i));
        }
    }

    public void writeList() {
    // The FileWriter constructor throws IOException, which must be caught.
        PrintWriter out = new PrintWriter(new FileWriter("OutFile.txt"));

        for (int i = 0; i < SIZE; i++) {
            // The get(int) method throws IndexOutOfBoundsException, which must be caught.
            out.println("Value at: " + i + " = " + list.get(i));
        }
        out.close();
    }
}

The first line in boldface is a call to a constructor. The constructor initializes an output stream on a file. If the file cannot be opened, the constructor throws an IOException. The second boldface line is a call to the ArrayList class’s get method, which throws an IndexOutOfBoundsException if the value of its argument is too small (less than 0) or too large (more than the number of elements currently contained by the ArrayList).

If you try to compile the ListOfNumbers class, the compiler prints an error message about the exception thrown by the FileWriter constructor. However, it does not display an error message about the exception thrown by get. The reason is that the exception thrown by the constructor, IOException, is a checked exception, and the one thrown by the get method, IndexOutOfBoundsException, is an unchecked exception.

Using Scanner to read in a file in Java

Objects of type Scanner are useful for breaking down formatted input into tokens and translating individual tokens according to their data type.

Here is a quick and dirty example:

import java.io.*;
import java.util.Scanner;

public class ScanXan {
    public static void main(String[] args) throws IOException {

        Scanner s = null;

        try {
            s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));

            while (s.hasNext()) {
                System.out.println(s.next());
            }
        } finally {
            if (s != null) {
                s.close();
            }
        }
    }
}

Back To Top