160 likes | 257 Views
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.
E N D
Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming
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
Basic Object Syntax • Declaring objects: classname objectname; • Constructing objects:objectname = new classname(params); • Calling object methods:returntype = objectname.methodname(params);
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
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();
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.)
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
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
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
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
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
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”
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
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
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
Java API • Online language reference • List of packages, classes, and methods • Maintained by Sun at http://docs.oracle.com/javase/7/docs/api/