Presentation is loading. Please wait.

Presentation is loading. Please wait.

3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.

Similar presentations


Presentation on theme: "3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can."— Presentation transcript:

1 3.1 Objects

2 Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can still store only the same type of data You don’t have to know HOW the data types and their operations are implemented -> Abstraction You may want to define your own data types. 2 Data Types OperationsSet of ValuesData Type not, and, or, xor true, false boolean double int add, subtract, multiplyany of 2 64 possible reals add, subtract, multiply -2 31 to 2 31 - 1

3 3 Objects Object. An entity that can take on a data type value. An object’s “value” can be returned to a client or be changed by one of the data type’s operations. Impact. Enables us to create our own data types; define operations on them; and integrate into our programs. length, substring, comparesequence of characters String OperationsSet of ValuesData Type get red component, brighten24 bits Color Picture get/set color of pixel (i, j)2D array of colors

4 What distinguishes creating arrays from creating variables of basic data types? n Explicit memory management Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n The memory needs to be initialized n Assigning one object/array to another object/array results in an alias Key difference n Arrays are part of the core Java language/standard library. Objects are not. “Class” vs. “Object” n Similar to difference between “type” vs. “variable” n An object is an instance of a class 4 Objects and Arrays are Similar

5 A Simple First Class Height n Data: – Feet (as an integer value) – Inches (as a double value) n Methods: – Set height – Display height API: n Height(int feet, double inches) //constructor n void displayHeight() //instance method 5

6 Objects are reference types => Need to initialize their memory Can use the new operator, just like arrays One special instance method in the API: n Height(int feet, double inches) //constructor n This method is called when you invoke “new” to create a new object n You can pass parameters to control initialization. n The class’ author defines the logic for this method to carry out proper initialization. 6 Constructors

7 Constructors vs. Methods Height(int feet, double inches) n How is this signature different from a method signature? No return type n The constructor manipulates the data in the object n No “return” statement 7

8 Instance methods are called “on” an object (a variable) n System.out.println(); // call println on System.out n String s = aCharge.toString(); // call toString on aCharge n In fileIn = new In(“file.txt”); fileIn.readInt(); Ways to think about this: n Send a message to an object. n Tell an object to do something. 8 Instance Methods vs. Static Methods

9 9 Constructors and Methods To construct a new object: Use keyword new and name of data type. To apply an operation: Use name of object, the dot operator, and the name of the method.

10 Height.java public class Height { // data int feet; double inches; // constructor Height(int setFeet, double setInches) { feet = setFeet; inches = setInches; } //method void displayHeight() { System.out.println("Height: "+feet+" feet and "+inches+ " inches"); } } 10

11 Using Height.java Compile.java file into a.class file Create objects of that class in other programs Dr. Java Coding Demo 11

12 Extending the Height Class double getHeightInches() n Returns the height in inches Dr. Java Coding Demo 12

13 Adding More Constructors May want to initialize an object in more than one way E.g., allow initialization when only the feet value is provided n Height(int setFeet) n Set “feet” to “setFeet”; set “inches” to zero Use the “this” keyword to invoke the original constructor Dr. Java Coding Demo 13

14 14 String Data Type String data type. Basis for text processing. Set of values. Sequence of Unicode characters. API. http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html …

15 15 Typical String Processing Code How do we implement a recursive version of this function?

16 16 String Data Type http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html …

17 Recursive Palindrome Check static boolean isPalindrome(String s) { if(s.length() < 1) //base return true; if(s.charAt(0) == s.charAt(s.length()-1)) //reduction return isPalindrome(s.substring(1,s.length()-1)); return false; } 17


Download ppt "3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can."

Similar presentations


Ads by Google