Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
30 views

Lesson 4 Working With The Java Class Library

Uploaded by

Jillian Lobo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views

Lesson 4 Working With The Java Class Library

Uploaded by

Jillian Lobo
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Working with the

Java Class Library


Objectives
 Explain object-oriented programming and some
of its concepts
 Differentiate between classes and objects
 Differentiate between instance
variables/methods and class(static)
variables/methods
 Explain what methods are and how to call and
pass parameters to methods
Introduction to OOP
 Object-Oriented programming or OOP
 Revolves around the concept of objects as
the basic elements of your programs.
 These objects are characterized by their
properties and behaviors.
Introduction to OOP
 Examples of objects

 objects in the physical world can easily be modeled


as software objects using the properties as data
and the behaviors as methods
Encapsulation
 The scheme of hiding implementation
details of a class.
 The caller of the class does not need to
know the implementation details of a
class
 The implementation can change without
affecting the caller of the class
Classes and Objects
 Class
 can be thought of as a template, a prototype or a
blueprint of an object
 is the fundamental structure in object-oriented
programming
 Two types of class members
 Fields (properties, variables)
 specify the data types defined by the class
 Methods (behavior)
 specify the operations
Classes and Objects
 Object
 An object is an instance of a class - we will
call it object instance
 The property values of an object instance is
different from the ones of other object
instances of a same class
 Object instances of a same class share the
same behavior (methods)
Classes and Objects
 The difference between classes and objects.
Classes and Objects
 Classes provide the benefit of reusability.
 Software programmers can use a class
over and over again to create many object
instances.
Instance Variables (Properties)
vs. Class Variables
 Instance Variables
 Belongs to an object instance
 Value of variable of an object instance is different
from the ones of other object object instances
 Class Variables (also called static member
variables)
 variables that belong to the whole class
 This means that they have the same value for all the
object instances in the same class.
Class Variables
 For example:
Creation of Object Instance
 To create an object instance of a class, we use
the new operator.
 For example, if you want to create an instance of
the class String, we write the following code,
 String str2 = new String(“Hello world!”);
or also equivalent to,
 String str2 = "Hello";
 String class is a special (and only) class you can
create an instance without using new keyword
as shown above
Creation of Object Instance
 The new operator
 allocates a memory for that object and returns a
reference of that memory location to you.
 When you create an object, you actually invoke the
class' constructor.
 The constructor
 is a method where you place all the initializations, it
has the same name as the class.
Methods
 Method
 is a separate piece of code that can be called by a
main program or any other method to perform some
specific function.
 The following are characteristics of methods:
 It can return one or no values
 It may accept as many parameters it needs or no
parameter at all. Parameters are also called
arguments.
 After the method has finished execution, it goes
back to the method that called it.
Why Use Methods?
 Methods contain behavior of a class (business
logic)
 The heart of effective problem solving is in problem
decomposition.
 We can do this in Java by creating methods to solve
a specific part of the problem.
 Taking a problem and breaking it into small,
manageable pieces is critical to writing large
programs.
Calling Instance Methods
 To illustrate how to call methods, let's use the
String class as an example.
 You can use the Java API documentation to see
all the available methods in the String class.
 Later on, we will create our own methods, but for
now, let us use what is available.
 To call an instance method, we write the
following,
nameOfObject.nameOfMethod(parameters);
Calling Methods
 Example
String str1 = "Hello";
char x = str1.charAt(0); //will return the
character H
//and store it to variable x
String str2 = "hello";
//this will return a boolean value true
boolean result = str1.equalsIgnoreCase( str2 );
Parameter Passing
 Pass-by-Value
 when a pass-by-value occurs, the method
makes a copy of the value of the variable
passed to the method.
 The method cannot accidentally modify the
original argument even if it modifies the
parameters during calculations.
 all primitive data types when passed to a
method are pass-byvalue.
Pass-by-Value
 Example
Parameter Passing
 Pass-by-Reference
 When a pass-by-reference occurs, the reference to
an object is passed to the calling method. This
means that, the method makes a copy of the
reference of the variable passed to the method.
 However, unlike in pass-by-value, the method can
modify the actual object that the reference is
pointing to, since, although different references are
used in the methods, the location of the data they
are pointing to is the same.
Pass-by-Reference
 Example
Pass-by-Reference
Calling Static Methods
 Static methods
 methods that can be invoked without instantiating a
class (means without invoking the new keyword).
 Static methods belong to the class as a whole and
not to a certain instance (or object) of a class.
 Static methods are distinguished from instance
methods in a class definition by the keyword static.
 To call a static method, just type,
Classname.staticMethodName(params);
Calling Static Methods
 Examples of static methods, we've used
so far in our examples are,
//prints data to screen
System.out.println(“Hello world”);

//converts the String 10, to an integer


int i = Integer.parseInt(“10”);

You might also like