Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.

Similar presentations


Presentation on theme: "CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari."— Presentation transcript:

1 CSC 1051 M.A. Papalaskari, Villanova University 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

2 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

3 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

4 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

5 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 B y y s s t t e e 0 1 2 3 4

6 More String Methods int numOfCharsInName = name. length(); char initial = name. charAt(0); B B y y s s t t e e 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 nickName See also textbook example StringMutation.java

7 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 R R R A A D D A A 0 1 2 3 4 str

8 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.javaPalindromeTester.java (Example from Chapter 5)

9 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.javaPalindromeTester.java (Example from Chapter 5) 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

10 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

11 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 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

12 Creating Objects Another example: The new operator calls the String constructor, which is a 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");

13 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";

14 Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive TypeWrapper Class byteByte shortShort intInteger longLong floatFloat doubleDouble charCharacter booleanBoolean

15 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

16 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;

17 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

18 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;

19 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

20 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After: CSC 1051 M.A. Papalaskari, Villanova University int num1 = 38; int num2 = 96;

21 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 num2 After: 38 CSC 1051 M.A. Papalaskari, Villanova University num1 num2 Before: 38 96 num2 = num1; Integer num1 = 38; Integet num2 = 96;

22 Another example For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs" CSC 1051 M.A. Papalaskari, Villanova University

23 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

24 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


Download ppt "CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari."

Similar presentations


Ads by Google