Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
3.1 Objects Data Types Data type. Set of values and operations on those values. Data Type Set of Values Operations boolean true, false not, and, or, xor int -231 to 231 - 1 add, subtract, multiply double any of 264 possible reals add, subtract, multiply Primitive/Built-In types. Usually single values 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 Objects Object. An entity that can take on a data type value. Data Type Set of Values Operations Color 24 bits get red component, brighten Picture 2D array of colors get/set color of pixel (i, j) String sequence of characters length, substring, compare 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. 3 Objects and Arrays are Similar What distinguishes creating arrays from creating variables of basic data types? Explicit memory management Arrays and objects are both “reference” types They are allocated a chunk of memory in the address space The memory needs to be initialized Assigning one object/array to another object/array results in an alias Key difference Arrays are part of the core Java language/standard library. Objects are not. “Class” vs. “Object” Similar to difference between “type” vs. “variable” An object is an instance of a class 4 A Simple First Class Height Data: – Feet (as an integer value) – Inches (as a double value) Methods: – Set height – Display height API: Height(int feet, double inches) //constructor void displayHeight() //instance method 5 Constructors Objects are reference types => Need to initialize their memory Can use the new operator, just like arrays One special instance method in the API: Height(int feet, double inches) //constructor This method is called when you invoke “new” to create a new object You can pass parameters to control initialization. The class’ author defines the logic for this method to carry out proper initialization. 6 Constructors vs. Methods Height(int feet, double inches) How is this signature different from a method signature? No return type The constructor manipulates the data in the object No “return” statement 7 Instance Methods vs. Static Methods Instance methods are called “on” an object (a variable) System.out.println(); // call println on System.out String s = aCharge.toString(); // call toString on aCharge In fileIn = new In(“file.txt”); fileIn.readInt(); Ways to think about this: Send a message to an object. Tell an object to do something. 8 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. 9 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 Using Height.java Compile .java file into a .class file Create objects of that class in other programs Dr. Java Coding Demo 11 Extending the Height Class double getHeightInches() Returns the height in inches Dr. Java Coding Demo 12 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 Height(int setFeet) Set “feet” to “setFeet”; set “inches” to zero Use the “this” keyword to invoke the original constructor Dr. Java Coding Demo 13 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 14 Typical String Processing Code How do we implement a recursive version of this function? 15 String Data Type … http://java.sun.com/j2se/1.5.0/docs/api/java/lang/String.html 16 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