Presentation is loading. Please wait.

Presentation is loading. Please wait.

Objectives Learn about objects and reference variables Explore how to use predefined methods in a program.

Similar presentations


Presentation on theme: "Objectives Learn about objects and reference variables Explore how to use predefined methods in a program."— Presentation transcript:

1 Objectives Learn about objects and reference variables Explore how to use predefined methods in a program

2 Object and Reference Variables Primitive variables: directly store data into their memory space Reference variables: store the address of the object containing the data

3 Object and Reference Variables Declare a reference variable of a class type Use the operator new to: –Allocate memory space for data –Instantiate an object of that class type Store the address of the object in a reference variable

4 The Operator new Statement: Integer num; num = new Integer(78); Result:

5 Garbage Collection Change value of num: num = new Integer(50); Old memory space reclaimed

6 Using Predefined Classes and Methods in a Program Many predefined packages, classes, and methods in Java Library: Collection of packages Package: Contains several classes Class: Contains several methods Method: Set of instructions

7 Using Predefined Classes and Methods in a Program To use a method you must know: –Name of class containing method (Math) –Name of package containing class (java.lang) –Name of method (pow), its parameters (int a, int b), and function (a^b)

8 Using Predefined Classes and Methods in a Program Example method call: import java.lang; //imports package Math.pow(2,3); //calls power method in //class Math (Dot). Operator: used to access the method in the class


Download ppt "Objectives Learn about objects and reference variables Explore how to use predefined methods in a program."

Similar presentations


Ads by Google