1、外文原文 Working with Objects Lets start todays lesson with an obvious statement: because Java is an object-oriented language, youre going to be dealing with a lot of objects. Youll create them, modify them, move them around, change their variables, call their methods, combine them with other objectsand
2、, of course, develop classes and use your own objects in the mix. Today, therefore, youll learn all about the Java object in its natural habitat. Todays topics include: Creating instances of classes Testing and modifying class and instance variables in your new instance Calling methods in that objec
3、t Casting (converting) objects and other data types from one class to another Other odds and ends about working with objects An overview of the Java class libraries Creating New Objects When you write a Java program, you define a set of classes. As you learned on Day 2, classes are templates for obj
4、ects; for the most part, you merely use the class to create instances and then work with those instances. In this section, therefore, youll learn how to create a new object from any given class. Remember strings from yesterday? You learned that using a string literala series of characters enclosed i
5、n double-quotescreates a new instance of the class String with the value of that string. The String class is unusual in that respectalthough its a class, theres an easy way to create instances of that class using a literal. The other classes dont have that shortcut; to create instances of those clas
6、ses you have to do so explicitly by using the new operator. Note:What about the literals for numbers and characters? Dont they create objects, too? Actually, they dont. The primitive data types for numbers and characters create numbers and characters, but for efficiency, they arent actually objects. You can put object-wrappers around them if you need to treat them like objects (youll learn how to do this later). Using new To create a new object, you use new with th