Upload
kerrie-oliver
View
222
Download
0
Embed Size (px)
Citation preview
CSC 1051 M.A. Papalaskari, Villanova University
Everyday objects: Strings and Wrappers
CSC 1051 – Data Structures and Algorithms IDr. Mary-Angela Papalaskari
Department of Computing Sciences
Villanova University
Course website:
http://www.csc.villanova.edu/~map/1051/f13
Some slides in this presentation are adapted from the slides accompanying Java Software Solutions by Lewis & Loftus
Overview• Review what we know about objects:
– classes– methods– object creation
• String class– useful methods and examples
• Wrapper classes
CSC 1051 M.A. Papalaskari, Villanova University
Some everyday Objects…
• Strings - defined by the String class:
"This is a string literal.""123 Main Street""X”
• System.out is also an object - it represents a destination (the monitor screen) to which we can send output
CSC 1051 M.A. Papalaskari, Villanova University
Methods• Objects can have methods associated with them
• In Lincoln.java we invoked the println method
System.out.println ("Whatever you are, be a good one.");
object methodname
information provided to the method(parameters)
CSC 1051 M.A. Papalaskari, Villanova University
Invoking Methods• We use the dot operator to invoke an object’s methods
CSC 1051 M.A. Papalaskari, Villanova University
int numOfCharsInName = name.length();
length() is one of the methods of String objects (defined in String class)
String name = scan.nextLine();
nextLine() is one of the methods of Scanner objects (defined in Scanner class)
B yste
0 1 2 3 4
More String Methods
int numOfCharsInName = name.length();
char initial = name.charAt(0);
B yste
0 1 2 3 4
String newName = name.replace('s', 't');
String capsName = name.toUpperCase();
String nickName = name.substring(1, 4);
0 1 2 3 4
name
newName
0 1 2 3 4
capsName
0 1 2 3 4
nickNameSee also textbook example StringMutation.java
Palindrome tester• Input a string, determine whether it is a palindrome,
i.e.:– first char is the same as last char– 2nd char is the same as 2nd last char– and so on…
• How to express this as an algorithm?• How to implement it?
CSC 1051 M.A. Papalaskari, Villanova University
R RADA
0 1 2 3 4str
CSC 1051 M.A. Papalaskari, Villanova University
System.out.println ("Enter a potential palindrome:");str = scan.nextLine();
left = 0;right = str.length() - 1;
while (str.charAt(left) == str.charAt(right) && left < right){ left++; right--;}
if (left < right) System.out.println ("NOT a palindrome");else System.out.println ("palindrome");
PalindromeTester.java (Example from Chapter 5)
CSC 1051 M.A. Papalaskari, Villanova University
System.out.println ("Enter a potential palindrome:");str = scan.nextLine();
left = 0;right = str.length() - 1;
while (str.charAt(left) == str.charAt(right) && left < right){ left++; right--;}
if (left < right) System.out.println ("NOT a palindrome");else System.out.println ("palindrome");
PalindromeTester.java (Example from Chapter 5)
Sample RunEnter a potential palindrome:radarpalindrome
Test another palindrome (y/n)? yEnter a potential palindrome:able was I ere I saw elbapalindrome.
Test another palindrome (y/n)? yEnter a potential palindrome:abracadabraNOT a palindrome.
Test another palindrome (y/n)? n
Declaring Variables, revisited• Examples of variable declarations:
int count = 0;
double mpg;
String title;
Graphics page;
Color aquamarine;
Scanner scan;
• A class name can be used as a type to declare an object reference variable
• The object itself must be created separatelyCSC 1051 M.A. Papalaskari, Villanova University
Creating Objects • We have already seen something like this:
Scanner scan = new Scanner (System.in);
The new operator calls the Scanner constructor, which isa special method that sets up the object
CSC 1051 M.A. Papalaskari, Villanova University
Variable refers to a Scanner object Constructing a new object is called instantiation
an instance of the Scanner class
Creating Objects • Another example:
The new operator calls the String constructor, which isa special method that sets up the object
CSC 1051 M.A. Papalaskari, Villanova University
Variable refers to a String object Constructing a new object is called instantiation
an instance of the String class
String title = new String ("Java Software Solutions");
The String Class is SPECIAL!
• Exception to the use of new operator: Because strings are so common, we don't have to use the new operator to create a String object
• This is special syntax that works only for strings
CSC 1051 M.A. Papalaskari, Villanova University
String title = new String ("Java Software Solutions");
String title = "Java Software Solutions";
Wrapper Classes• The java.lang package contains wrapper
classes that correspond to each primitive type:
Primitive Type Wrapper Classbyte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
Wrapper Classes• The following declaration creates an Integer
object which represents the integer 40 as an object
CSC 1051 M.A. Papalaskari, Villanova University
Integer age = new Integer(25);
age
Wrapper Class methods and constants• Integer.parseInt(): convert String to int
• Double.parseDouble(): convert String to double
• Integer.MIN_VALUE: smallest int value
• Integer. MAX_VALUE: largest int value
Examples:
CSC 1051 M.A. Papalaskari, Villanova University
String str = scan.nextLine();
int num = Integer.parseInt(str);
int currentMin = Integer.MAX_VALUE;
Autoboxing• Autoboxing is the automatic conversion of a
primitive value to a corresponding wrapper object:
Integer obj;int num = 42;obj = num;
• The assignment creates the appropriate Integer object
• The reverse conversion (called unboxing) also occurs automatically as needed
CSC 1051 M.A. Papalaskari, Villanova University
Quick Check
CSC 1051 M.A. Papalaskari, Villanova University
Are the following assignments valid?
Double value = 15.75;
Character ch = new Character('T');
char myChar = ch;
References• Note that a primitive variable contains the value
itself, but an object variable contains the address of the object
• An object reference can be thought of as a pointer to the location of the object
• Rather than dealing with arbitrary addresses, we often depict a reference graphically
"Steve Jobs"name1
num1 38
CSC 1051 M.A. Papalaskari, Villanova University
Assignment Revisited• The act of assignment takes a copy of a value and
stores it in a variable
• For primitive types:
num1 38
num2 96Before:
num2 = num1;
num1 38
num2 38After:
CSC 1051 M.A. Papalaskari, Villanova University
int num1 = 38;int num2 = 96;
Reference Assignment• For objects, the same is true, but what is copied is
the reference to the object (i.e., its address):
• For objects:
num1
num2After:
38
CSC 1051 M.A. Papalaskari, Villanova University
num1
num2Before:
38
96
num2 = num1;
Integer num1 = 38;Integet num2 = 96;
Another example• For object references, assignment copies the
address:
name2 = name1;
name1
name2Before:
"Steve Jobs"
"Steve Wozniak"
name1
name2After:
"Steve Jobs"
CSC 1051 M.A. Papalaskari, Villanova University
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
CSC 1051 M.A. Papalaskari, Villanova University
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
CSC 1051 M.A. Papalaskari, Villanova University