1 / 17

Data Types: Sets of Values and Operations on Those Values

Learn about data types, their operations, and how to define your own data types. Explore the concepts of objects, arrays, and strings in Java.

trull
Download Presentation

Data Types: Sets of Values and Operations on Those Values

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 3.1 Objects

  2. Data Types Data type. Set of values and operations on those values. 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. 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

  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. 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 3

  4. 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

  5. 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

  6. 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.

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

  8. 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.

  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, andthe 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"); } }

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

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

  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 • Height(int setFeet) • Set “feet” to “setFeet”; set “inches” to zero • Use the “this” keyword to invoke the original constructor • Dr. Java Coding Demo

  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. Typical String Processing Code How do we implement a recursive version of this function?

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

More Related