ITEC 120
Supplemental Reading 2a

Creating Objects

You should be familiar with the four primitive data types we are using in this class: int, double, char, and boolean. In addition to the primitive data types, Java provides objects. An object is an instance of a class.

We have been using the Scanner class to read input from the keyboard. To use a scanner we must create an instance of the Scanner class. An instance of a class is also called an object.

Consider the two declarations below.

int num;
String name;

The first declaration creates a variable that stores an integer, a primitive data value. The second declaration creates a variable that stores a string – an object. Classes serve as data types when variables are declared. Consider the names of the data types: int and String. Can you identify a difference between these two data types that will help you to distinguish between classes and primitive data types?

All primitive data types start with a lowercase letter and classes always start with a capital letter. Which of the variables declared below hold an object?

Zat front;
Glop goo;
char ch;
Spork utensil;

Zat, Glop, and Spork start with a capital letter so they are classes which means that front, goo, and utensil are variables that hold an object.

After a variable is declared it may be initialized by assigning an initial value. Consider the two lines of code below.

num = 7;
name = new String("Bubba Smith");

The first line of code assigns the integer 7 to the variable num. The second line of code creates a String object with the value "Bubba Smith" and assigns the object to the variable name.

Primitive values do not have to be created because primitives are defined within the programming language. All objects must be created because objects are not part of the language. Objects are created using the new operator followed by a call to a constructor, a special type of method. The name of the constructor is exactly the same as the name of the class – class names are case sensitive.

Objects are useful because they provide methods that perform operations. For example, the next and nextInt methods in the Scanner class return input from the keyboard.

Consider the variable declarations below.

boolean done = false;
String title = new String("Java Software Solutions");
Scanner scanObj = new Scanner(System.in);
String courseName = "Principles of Computer Science";

The four lines of code above declare and initialize a variable. The variable done stores the primitive value false.  You know that the other three variables hold objects because the data types are classes – the name of the data type begins with a capital letter.

The code above creates two strings. The first string, "Java Software Solutions" is created with a call to the String constructor. The second string is created without a call to the constructor. How is this possible? Strings are so common that Java provides a shortcut for creating strings. Strings are the only objects that may be created without a call to the constructor.

The import Statement

Java organizes classes into packages. When a class uses classes defined in other packages the class must import the class. For example, the Scanner class is defined in a package named java.util (a package of utility classes). Consider the Echo class below, based on Listing 2.8 in the Java Software Solutions textbook. Note the import statement high­lighted in yellow.

import java.util.Scanner;
public class Echo
{
     public static void main(String[] args)
     {
          String message;
          Scanner scan = new Scanner(System.in);
          System.out.print("Enter a line of text: ");
          Message = scan.nextLine();
          System.out.println("You entered: " + message);
     }
}

The import statement in the code above imports one class, the Scanner class, from the package java.util. However, packages contain multiple classes. The import statement below imports all of the classes from the java.util package.

import java.util.*;

Java uses the following convention for importing classes. If only one single class is being imported, name the class in the import statement. If two or more classes are imported from the same package, use the * notation to import all of the classes. In other words, a package should appear in at most one import statement.