1 / 11

CSCI 1100/1202

CSCI 1100/1202. January 21, 2002. Creating Objects. A variable either holds a primitive type, or it holds a reference to an object A class name can be used as a type to declare an object reference variable String title; No object has been created with this declaration

tinak
Download Presentation

CSCI 1100/1202

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCI 1100/1202 January 21, 2002

  2. Creating Objects • A variable either holds a primitive type, or it holds a reference to an object • A class name can be used as a type to declare an object reference variable String title; • No object has been created with this declaration • An object reference variable holds the address of an object • The object itself must be created separately

  3. Creating Objects • We use the newoperator to create an object title = new String ("Java Software Solutions"); This calls the Stringconstructor, which is a special method that sets up the object • Creating an object is called instantiation • An object is an instance of a particular class • Objects can be declared and instantiated at the • same time: • String title = new String (“Hello World!”);

  4. Creating Objects • Because strings are so common, we don't have to use the new operator to create a Stringobject title = "Java Software Solutions"; • This is special syntax that only works for strings • Once an object has been instantiated, we can use the dot operator to invoke its methods title.length()

  5. String Methods • The String class has several methods that are useful for manipulating strings • Many of the methods return a value, such as an integer or a new String object • String toLowerCase() String lowerCase = phrase.toLowerCase(); • Some of the methods require parameters • char charAt (int index) char first = phrase.charAt(0);

  6. The String Class • See a more complete list of String methods on page 75 and in Appendix M • String (String str) • int length () • String concat (String str) • String toUpperCase () • String replace (char oldChar, char newChar) • String substring (int offset, int endIndex) • See StringMutation.java

  7. Class Libraries • A class library is a collection of classes that we can use when developing programs • There is a Java standard class library that is part of any Java development environment • These classes are not part of the Java language per se, but we rely on them heavily • The Systemclass and the String class are part of the Java standard class library • Other class libraries can be obtained through third party vendors, or you can create them yourself

  8. Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities and components Network communication Utilities Package java.lang java.applet java.awt javax.swing java.net java.util Packages • The classes of the Java standard class library are organized into packages • Some of the packages in the standard class library are:

  9. The import Declaration • When you want to use a class from a package, you could use its fully qualified name java.util.Random • Or you can import the class, then just use the class name import java.util.Random; • To import all classes in a particular package, you can use the * wildcard character import java.util.*;

  10. The import Declaration • All classes of the java.lang package are automatically imported into all programs • As if import java.lang.* was in each program • That's why we don’t have to explicitly import the System or String classes • The Random class is part of the java.util package • It provides methods that generate pseudo-random numbers • We often have to scale and shifta number into an appropriate range for a particular purpose

  11. Random Class • Some methods of the Random Class: • Random() • Constructor: creates a new pseudorandom number generator • Float nextFloat() • Returns a random number between 0.0 (inclusive) and 1.0 (exclusive) • Int nextInt() • Returns a random number that ranges over all possible int values (positive and negative • See RandomNumbers.java

More Related