Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming.

Similar presentations


Presentation on theme: "Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming."— Presentation transcript:

1 Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming

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

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; t 37A4 Memory for t ’s attributes (text, font, size, etc.) 37A4

7 Memory Allocation Initially t points to null –Will get NullPointerException if attempt to use Must allocate memory with new t = 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(); Now refer to same location in memory t1 37A4 4E15 t2 37A4

9 Memory Deallocation Manual deallocation ( delete in C++): –Potentially dangerous delete 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 t1 37A44E15 t2 Memory at location 4E15 inaccessible – should be reallocated for future use

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: length int total = 0; for (int i = 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: int num = 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 boolean equals method: if (s1.equals(s2)) {…} –Each class has an equals method –String version does character-by-character comparison Evaluates to false since s1 and s2 are different objects s1 Fred s2

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/


Download ppt "Introduction to Java Objects CSIS 3701: Advanced Object Oriented Programming."

Similar presentations


Ads by Google