Java was designed for object-oriented programming (OOP). Object-oriented programming allows the programmer to define their own data structures through classes which are reusable segments of code. The "reusability" of code results in rapid development and increased productivity. Object-oriented programs, along with good programming practices make for programs that are easier to understand, debug, and update. An object is an instance of a class, meaning that Java has reserved memory for the object, and initialized any instance variables. An analogy that is often used in object-oriented programming is that a class is the set of blueprints describing the construction of an object such as a house, and that instantiation is the built object, meaning a physical home that you can walk into. If a class is called Home, then an object created from this class might be myHome. Some examples of instance variables for myHome could be style, address, and color.
Our first object in Java will be a String object. Java's String class is defined with a collection of constructors and methods which determine the attributes and behaviors of a String object. A constructor is a segment of code similar to a method except that it doesn't have a return type. Constructors are used when an instance of an object is created. Although Java has many constructors defined for the String class we will be using the following technique to instantiate a String.
String color = "yellow";
String is the data type, although not a primitive data type because we are creating a Java object. The variable name is color, and the initial value is a series of characters forming the word yellow. Notice that Strings are wrapped in double quotes, not single quotes like char data types. As always we end each statement with a semicolon.
Another important object that we will use often is a Scanner object. In order to instantiate a Scanner object we must first import its class. Notice at the top of the coding window the line:
import java.util.Scanner; // imports the Scanner class
This imports the Scanner class form the java.util package. If you wanted to import all the classes in the java.util package you would use an asterisk in place of a class name. The asterisk acts as a wildcard.
import java.util.*; // imports all classes and interfaces in the package.
The example depicted below is a simple demonstration of importing and using the Scanner class. The import funtion is a fundamental principal of object-oriented programming. We are importing code that has already been written and reusing it to build upon. Our long term goal is to eventually build our own packages that will be imported so that the code can be reused and integrated into other programs. Reusability is an underlying theme for any object oriented programming course.
First Objects in BlueJ
In the program below notice that the first two lines of the main method are variable declarations and that they are non-primitive data types. The variable fastCar is an Object data type and firstName is a String data type. Notice in the output window that System.out.println(fastCar) and System.out.println(firstName) get very different results. Both objects have a toString() method, but the toString() methods are defined differently for their respective data types using something called method overriding, which you will learn to do as we get more advanced with our Java programming skills. The toString() method for Objects returns the class and some reference data, where the toString() method for Strings returns the sequence of characters stored with the String variable.