Download presentation
Presentation is loading. Please wait.
1
Chapter 3 Using Classes and Objects
2
2 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable
3
3 Remember… Primitive Data Types X 5 Primitive Data Type int x = 5; A primitive data type variable contains the value itself, but an object variable contains the address of the object
4
4 References to Objects An object reference variable holds the address of an object String s = “Hello”; //same as: String s = new String(“Hello”); s “Hello” Memory location An object variable contains the address of the object
5
5 Declaring the Object The object itself must be created separately String s; s Memory location
6
6 Creating the Object itself The object itself must be created separately String s; s = “Hello”; s “Hello” Memory location
7
7 Is this assignment? String s; String t; s = “hello”; t = s; s “Hello” Memory location t
8
8 Creating Objects, the formula Generally, we use the new operator to create an object Object objectReference = new Object 0 or more Class name Class name parameters ( ); The identifier or variable name Specifications for the newly created object
9
9 Creating Objects Generally, we use the new operator to create an object String name = new String (”Blanca J. Polo"); This keyword new calls the String constructor That is the one that creates/builds the object Creating an object is called instantiation An object is an instance of a particular class
10
10 Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods int nameLength = 0; nameLength = name.length() A method may return a value The returned value can be used in an assignment or expression
11
11 The main method is void A void method doesn’t return anything Return types can be of any Primitive Data Type Return types can be of any Object kind, either pre-made by JAVA or made by you
12
12 Review: The difference ”Blanca Polo" Srting name int num38
13
13 Primitive Data Type Assignment Review The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:
14
14 Object Assignment Review For object references, assignment copies the address: name2 = name1; name1 name2 Before: ”Tom Hanks" ”Kurt Russell" name1 name2 After: ”Tom Hanks"
15
15 Aliases Two or more references that refer to the same object are called aliases of each other That creates an interesting situation: one object can be accessed using multiple reference variables Aliases can be useful, but should be managed carefully Changing an object through one reference changes it for all of its aliases, because there is really only one object
16
16 Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection
17
17 Class Libraries A class library is a collection of classes that we can use when developing programs The Java standard class library is part of any Java development environment Other class libraries can be obtained through third party vendors, or you can create them yourself
18
18 What is in a Package A package is a directory of files that contain utilities. All classes of the java.lang package are imported automatically into all programs. The String class, and the System.out class are part of this package.
19
19 Packages The classes of the Java standard class library are organized into packages Some of the packages in the standard class library are: Package java.lang java.applet java.awt javax.swing java.net java.util javax.xml.parsers Purpose General support Creating applets for the web Graphics and graphical user interfaces Additional graphics capabilities Network communication Utilities XML document processing
20
20 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, and then use just the class name import java.util.Random; To import all classes in a particular package, you can use the * wildcard character import java.util.*;
21
21 The import Declaration Import statements are always at the top of the program. import java.lang.*; Many of the classes that we will use in this class will need to be imported. Different JAVA programming environments have different libraries We will use the standard libraries that can be found in any programming environment.
22
22 Counting
23
23 The Random Class The Random class is part of the java.util package It provides methods that generate pseudorandom numbers
24
24 Random Class Methods Constructor: Random( ) Random r = new Random( ); float nextFloat( ) Returns a random number between 0.0 and 1.0 inclusive float f; f = r.nextFloat( );
25
25 More Random Class Methods int nextInt( ) Returns a random number that ranges over all possible int values positive and negative int i; i = r.nextInt( ); int nextInt( int num ) Returns a random number between 0 and num-1 (inclusive) int i; i = r.nextInt(5 ); // generates a number between 0 and 4 See Java/Numbers/GenOneRandom.java Java/Numbers/OneRandom.java
26
26 Questions
27
27 Questions?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.