240 likes | 364 Vues
This presentation provides an overview of essential concepts related to strings and wrapper classes in Java, tailored for CSC 1051 students at Villanova University. It covers the definition and creation of strings using the String class, showcases useful string methods, and introduces wrapper classes that correspond to primitive types. Students will learn to invoke methods, check for palindromes, and create object references. This resource is designed to enhance understanding of these fundamental data structures and algorithms.
E N D
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 Stringclass: "This is a string literal." "123 Main Street" "X” • System.outis also an object - it represents a destination (the monitor screen) to which we can send output CSC 1051 M.A. Papalaskari, Villanova University
object method name information provided to the method (parameters) 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."); CSC 1051 M.A. Papalaskari, Villanova University
Invoking Methods • We use the dot operatorto invoke an object’s methods • String name = scan.nextLine(); nextLine() is one of the methods of Scannerobjects (defined in Scannerclass) • intnumOfCharsInName = name.length(); length() is one of the methods of String objects (defined in String class) 0 1 2 3 4 B e t s y CSC 1051 M.A. Papalaskari, Villanova University
More String Methods name • intnumOfCharsInName = name.length(); • char initial = name.charAt(0); • String newName = name.replace('s', 't'); newName • String capsName = name.toUpperCase(); capsName 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 0 1 2 3 4 • String nickName = name.substring(1, 4); B e t s y nickName See 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? str 0 1 2 3 4 R A D A R 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) { left++; right--; } if (left < right) System.out.println ("NOT a palindrome"); else System.out.println ("palindrome"); Sample Run Enter a potential palindrome: radar palindrome Test another palindrome (y/n)? y Enter a potential palindrome: able was I ere I saw elba palindrome. Test another palindrome (y/n)? y Enter a potential palindrome: abracadabra NOT a palindrome. Test another palindrome (y/n)? n CSC 1051 M.A. Papalaskari, Villanova University
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 Constructing a new object is called instantiation Variable refers to a Scanner object an instance of the Scanner class CSC 1051 M.A. Papalaskari, Villanova University
Creating Objects • Another example: String title = newString ("Java Software Solutions"); The new operator calls the String constructor, which is a special method that sets up the object Constructing a new object is called instantiation Variable refers to a String object an instance of the String class CSC 1051 M.A. Papalaskari, Villanova University
The String Class is SPECIAL! • Exception to the use of newoperator: 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 String title = new String ("Java Software Solutions"); String title = "Java Software Solutions"; CSC 1051 M.A. Papalaskari, Villanova University
Wrapper Classes • The java.lang package contains wrapper classes that correspond to each primitive type:
Wrapper Classes • The following declaration creates an Integer object which represents the integer 40 as an object • Integer age = new Integer(25); age 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 intvalue Examples: • String str = scan.nextLine(); • intnum = Integer.parseInt(str); • intcurrentMin = 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
38 num1 "Steve Jobs" name1 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 CSC 1051 M.A. Papalaskari, Villanova University
38 num1 Before: 96 num2 38 num1 After: 38 num2 Assignment Revisited • The act of assignment takes a copy of a value and stores it in a variable • For primitive types: int num1 = 38; int num2 = 96; num2 = num1; CSC 1051 M.A. Papalaskari, Villanova University
38 num1 Before: 96 num2 38 num1 After: num2 Reference Assignment • For objects, the same is true, but what is copied is the reference to the object (i.e., its address): • For objects: Integer num1 = 38; Integet num2 = 96; num2 = num1; CSC 1051 M.A. Papalaskari, Villanova University
"Steve Jobs" name1 Before: "Steve Wozniak" name2 "Steve Jobs" name1 After: name2 Another example • For object references, assignment copies the address: name2 = name1; 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