Download String - Villanova Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Everyday objects: Strings and
Wrappers
CSC 1051 – Data Structures and Algorithms I
Dr. 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
CSC 1051 M.A. Papalaskari, Villanova University
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
method
name
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
String name = scan.nextLine();
nextLine() is one of the
methods of Scanner objects
(defined in Scanner class)
int numOfCharsInName = name.length();
0
1
2
3
4
B
e
t
s
y
length() is one of the
methods of String objects
(defined in String class)
CSC 1051 M.A. Papalaskari, Villanova University
More String Methods
name
0
1
2
3
4
B
e
t
s
y
int numOfCharsInName = name.length();
char initial = name.charAt(0);
String newName = name.replace('s', 't');
0
1
2
3
4
newName
String capsName = name.toUpperCase();
0
1
2
3
4
capsName
String nickName = name.substring(1, 4);
0
See also textbook example StringMutation.java
nickName
1
2
3
4
Palindrome tester
• Input a string, determine whether it is a palindrome,
0
1
2
3
i.e.:
str
– first char is the same as last char
– 2nd char is the same as 2nd last char
– and so on…
R
A
D
A
4
R
• How to express this as an algorithm?
• How to implement it?
CSC 1051 M.A. Papalaskari, Villanova University
PalindromeTester.java (Example from Chapter 5)
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");
CSC 1051 M.A. Papalaskari, Villanova University
PalindromeTester.java (Example from Chapter 5)
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)
{
Sample Run
left++;
right--;
Enter a potential palindrome:
}
radar
palindrome
if (left < right)
Test another palindrome (y/n)? y
System.out.println ("NOT a palindrome");
Enter a potential palindrome:
else
System.out.println ("palindrome");able was I ere I saw elba
palindrome.
Test another palindrome (y/n)? y
Enter a potential palindrome:
abracadabra
NOT a palindrome.
CSC 1051 M.A.palindrome
Papalaskari, Villanova
University n
Test another
(y/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 separately
CSC 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 is
a special method that sets up the object
Variable refers to a Scanner object
Constructing a new object is
called instantiation
an instance of the Scanner class
CSC 1051 M.A. Papalaskari, Villanova University
Creating Objects
• Another example:
String title = new String ("Java Software Solutions");
The new operator calls the String constructor, which is
a special method that sets up the object
Variable refers to a String object
Constructing a new object is
called instantiation
an instance of the String class
CSC 1051 M.A. Papalaskari, Villanova University
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
String title = new String ("Java Software Solutions");
String title = "Java Software Solutions";
• This is special syntax that works only for strings
CSC 1051 M.A. Papalaskari, Villanova University
Wrapper Classes
• The java.lang package contains wrapper
classes that correspond to each primitive type:
Primitive Type
byte
short
Wrapper Class
Byte
Short
int
long
float
double
Integer
Long
Float
Double
char
boolean
Character
Boolean
Wrapper Classes
• The following declaration creates an Integer
object which represents the integer 40 as an object
Integer age = new Integer(25);
CSC 1051 M.A. Papalaskari, Villanova University
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:
String str = scan.nextLine();
int num = Integer.parseInt(str);
int currentMin = Integer.MAX_VALUE;
CSC 1051 M.A. Papalaskari, Villanova University
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
Are the following assignments valid?
Double value = 15.75;
Character ch = new Character('T');
char myChar = ch;
CSC 1051 M.A. Papalaskari, Villanova University
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
num1
name1
38
"Steve Jobs"
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:
Before:
int num1 = 38;
int num2 = 96;
num1
38
num2
96
num1
38
num2
38
num2 = num1;
After:
CSC 1051 M.A. Papalaskari, Villanova University
Reference Assignment
• For objects, the same is true, but what is copied is
the reference to the object (i.e., its address):
Integer num1 = 38;
Integet num2 = 96;
• For objects:
Before:
num1
38
num2
96
num2 = num1;
num1
After:
38
num2
CSC 1051 M.A. Papalaskari, Villanova University
Another example
• For object references, assignment copies the
address:
Before:
name1
"Steve Jobs"
name2
"Steve Wozniak"
name1
"Steve Jobs"
name2 = name1;
After:
name2
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