Chapter:Objects - Introduction
Object, objects, objects in java, creating objects, using objects, arraylist, api, java's api, javadoc, java api documentation, introduction to objects
- 1 Introduction to Objects in Java
- 2 Meta information about this chapter
- 3 Videos
- 4 Links
Introduction to Objects in Java
Creating and using objects are done all the time in Java. This chapter shows the basic cases of how to do this. We start by creating some objects from some of the common classes in the Java API (the Java class library that comes with the JDK).
Chapters about Objects
Here are the chapters about objects in this book:
- Chapter:Objects_-_Introduction « you are here!
Meta information about this chapter
Expand using link to the right to see the full content.
The lectures for this topic introduce objects and as such serves as a foundation for the coming lectures on related topics.
The purpose of the Objects chapters, is getting the student familiar with creating objects and invoking (object/instance) methods. Getting the students' hands dirty by typing in some code creating objects from the API classes, and later on invoking methods on the objects. This, so that the student is well prepared for the chapters about Classes.
Purpose of this specific chapter
This specific chapter, Objects - Introduction, is to give a smooth introduction to the concept of objects in Java programming.
Goals of the Objects-chapters
After completing all the chapters on Objects, the student shall be able to:
- create an object with no arguments (using new and a call of a constructor)
- create an object with arguments (using new and a call of a constructor)
- get a reference to an object using other means (like calling a method that creates an object and returns the reference to it)
String upperCaseName = name.toUpperCase();
- invoke a method returning void - and understanding the meaning and use of doing so
- invoke a non-void method and store the returned data
- invoke a method with arguments
- understand the concept of a reference, and that many instances of the same class can exist
- describe what the Java API is
- describe what the Java API documentation is and knowing the basics of how to read it
Also, after completing the chapters on Objects, the student should be well prepared for taking the chapters about classes.
- Objects/instances (of a class)
- Constructor, creating an object
- Reference (to an object/an instance)
- invoking a method
- return/return value, return type
- Classes (in relation to objects - soft introduction to classes as types and blueprints)
- Passing references as arguments to methods ("objects using other objects")
Instructions to the teacher
Students must learn the concept of name spaces (even if we don't use that term) as in the fully qualified names of classes. A source of confusion could be the fact that classes in the java.lang package don't have to be qualified or imported.
Another source of confusion for students is the different role they can take when writing code. Sometimes a programmer uses predefined classes (classes from the standard Java API or classes from colleagues or third party vendors). Other times, the task of the Java programmer is to also identify classes during the analysis and design phases and writing code for defining such classes. We have seen that these two different tasks can be confusing to the novice student. Our approach in this course is to have the students first using classes someone else have written (perhaps provided by the authors, or classes included in the standard Java API). Only after having created objects and having used them (by invoking methods offered by the objects) we turn to the practice of writing our own classes for custom objects. Writing classes is a topic for chapters after the chapters on Objects.
Make sure that you don't make the mistake of calling reference variables "objects". It is easy to slip and say stuff like "Here we pass the object
name to the println method" (where "name" is a reference to a String, for instance). It is better, albeit cumbersome, to say: "Here we pass the reference variable name to the println method". We have seen many students (new to programming) struggle with understanding references to objects as opposed to the primitive type variables. We should help them by thinking carefully about what words we choose when explaining code to them. Strings are particularly tricky in this regard since it's easy to think "I'm assigning the String 'abc' to the variable 's'" when we actually are assigning a reference (to some String object) to the variable s.
We start by creating and using objects of the often used String class. This opens for a minefield of pitfalls, since String objects are a little different from some of the typical objects created from classes written by the students during an introductory course. Some of the things to consider when using and introducing the String class, include:
- Strings can be created using the double quote construct (rather than being confined to using only calls to constructors and the new operator) - this is an exception - only String objects can be created this way (even though "autoboxing" of the wrapper classes show a similar behavior)
- Strings created using the double quote construct are put in the String constants pool, which means such Strings are re-used and will have the same reference "address" (also something which is similar to the wrapper classes means of re-use for certain values)
- Strings created using the new operator will not be put in the constants pool and will be separate objects which are not re-used
- Strings are immutable, which is a good practice, but may cause confusion such as when calling an instance method such as toLowerCase(), you get a reference to a completely new String object, when the intuition of the students might tell them that the actual instance for which the method is called will change its state...
- String is a class in the java.lang package (as mentioned above) which makes uses of unqualified naming of the class legal and normal - as soon as they learn to use classes from other packages, qualification or import statements are required
- Using double quotes to create a new String makes the String type look like a primitive type - make sure you don't mislead the students into thinking it is! String is a reference type (and a class) and nothing else
Despite all the pitfalls above, we decided to start off by using String, since it is a very common type of objects. Heck, even the signature of the main method includes the
String args parameter, so even "Hello world" will expose the students to the String class. Just be careful what you say and how you express yourself when using String as an example type for objects, and these pitfalls can be handled. Also, answer honestly if you get questions about the properties of Strings which make them a little "special" but refer to the fact that theses "exceptions to the way other objects work" will be explained more in detail in lectures further down the road.
Another thing to think about is the nature of the simple programs students typically write and read early on in an introductory course. It is very common to create a few objects by passing hard coded values to their constructors and then writing the objects state to the standard out. This is not very typical activities in real programs for many reasons. First, real programs tend to avoid having hard coded values (and rather gather data from external sources during runtime). Second, real programs don't normally create objects solely for the purpose of immediately printing them. Third, real programs usually don't do all their work in the main method alone. The problem we have to handle is, that students don't have enough knowledge early on in the course to write bigger and more typical programs. So we have to compromise with this principle in order to start with very simple programs. It is up to us teachers, however, to handle how we present the small typical course book example programs, and to make them at least a little interesting. As soon as we introduce user supplied data for the state of objects, the programs become much more interesting (and more similar to "real programs"). One of the fascinating things about programs, is that we can write one program to solve a general task, and it will solve it the same way regardless of what data we feed it.
Think about a calculator, for instance. Isn't it much more fascinating that a calculator can multiply two arbitrary numbers that we supply? Compare that to the typical "learning operators exercise" where the student creates two variables a and b, and assigns 10 to a, and 20 to b. Then the program prints something like "10 * 20 = 200". At least some students will silently think "But I knew that already".
Objects - a short introductory video (and presentation)
The video lecture for this section is just one short introduction to objects. Therefore, there's no playlist link.
Where to go next
Next page has the exercises for this chapter: Objects - Introduction - Exercises