1 / 16

Introduction to Java Objects

Introduction to Java Objects. CSIS 3701: Advanced Object Oriented Programming. Object-Oriented Programming. Object. Methods to access member variables Constructors to set initial value of member variables. Member variables representing current state of object.

naif
Download Presentation

Introduction to Java Objects

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. Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming

  2. Object-Oriented Programming Object Methods toaccess membervariables Constructors to set initial value of member variables Member variables representing current stateof object “Client programmer”: Programmer who uses class in their own code

  3. Basic Object Syntax • Declaring objects: classname objectname; • Constructing objects:objectname = new classname(params); • Calling object methods:returntype = objectname.methodname(params);

  4. Basic Object Syntax • Example Stack s; s = new Stack(); s.push(“Larry”); s.push(“Curley”); if (!s.isEmpty()) { System.out.println(s.pop()); } object in Stack class constructed (no parameters) method calls on this stack object

  5. Basic Object Syntax • Example: JTextField class • Declare and construct JTextField 10 chars wide:JTextField t = new JTextField(10); • Use method to store “Fred” in textfield:t.setText(“Fred”); • Use method to get text in textfield:String name = t.getText();

  6. Objects and Memory • Objects represented as a reference to memory • Must be dynamically allocated • JTextField t; 37A4 t 37A4 Memory for t’s attributes (text, font, size, etc.)

  7. Memory Allocation • Initially t points to null • Will get NullPointerException if attempt to use • Must allocate memory with newt = new JTextField(); • Allocates memory for object(size of state variables) • Invokes constructor(initializes state variables) • Returns memory location for storage in reference t

  8. Memory Management • Avoid using assignment with objects: JTextField t1 = new JTextField(); JtextField t2 = new JTextField(); t2 = t1; • Many classes have clone method that create copy of any state variables • t1 = t2.clone(); 37A4 4E15 Now refer to same location in memory t1 37A4 t2 37A4

  9. Memory Deallocation 37A4 4E15 Memory at location 4E15 inaccessible – should be reallocated for future use • Manual deallocation (delete in C++): • Potentially dangerousdelete t2;t1.setText(“Barney”); • “Garbage collection” in Java • JVM periodically scans sandbox for unreachable memory and reallocates back to OS • Safer, but takes time tradeoff t2 t1

  10. Arrays in Java • Arrays are treated as reference types • Must be allocated with new int[] A; A = new int[5]; • Arrays have object-like attributes • Example: lengthint total = 0;for (inti = 0; i < A.length; i++) total += A[i]; A refers to null A refers to newly allocated array of 5 ints (160 bits) Evaluates to 5

  11. Strings in Java • Objects of String class • Not array of characters • Cannot change individual characters in string • Construct: String name = new String(“Barney”); • Reassign new values: name = new String(“Fred”); • “Barney” eventually garbage-collected

  12. Strings in Java • Manipulate with methods:char c = name.charAt(3); • 0-based indices, so this returns ‘n’ • Useful method: concat: • String full = name.concat(“ Flintstone”); • Casts parameter to String if necessary:intnum = 29;String s = name.concat(num); “Fred Flintstone” “Fred29”

  13. Strings in Java • Shortcuts provided for strings: • Construction: String name = “Barney”; • Assignment: name = “Fred”; • Append: Can use + operator as shortcut String full = name + “ Flintstone”; • Casts second operand to String if necessary • Common tradeoff in most languages:Paradigm principles vs. ease of programming

  14. References and Comparison • Example:String s1 = “hello”; String s2 = “hello”; if (s1 == s2) {…} • Must use booleanequals method:if (s1.equals(s2)) {…} • Each class has an equals method • String version does character-by-character comparison Fred Fred s1 s2 Evaluates to false since s1 and s2 are different objects

  15. Classes and Packages • Classes organized into packages • Like libraries in C/C++ • Actual language small • Import packages with needed classes • import packagename.classname; for single class • import packagename.*; for all classes in package • Examples: • java.util Utility classes (such as Vector) • javax.swing Visual classes (such as JTextField) • java.awt.event Event handling classes

  16. Java API • Online language reference • List of packages, classes, and methods • Maintained by Sun at http://docs.oracle.com/javase/7/docs/api/

More Related