Presentation is loading. Please wait.

Presentation is loading. Please wait.

Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references.

Similar presentations


Presentation on theme: "Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references."— Presentation transcript:

1 Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references (pointers) to objects, which resides in the garbage- collected heap.

2 Garbage Collection Programmers are not responsible for deallocating memory. When an object is no longer accessible from anywhere in the program, it becomes garbage. The garbage-collector in the Java virtual machine automatically reclaims the memory occupied by garbage. System.gc() suggest to the Java virtual machine to do a garbage- collection finalize() invoked by the Java runtime just before the object is garbage-collected

3 Null Reference null A reference does not refer to any object. The followings are not the same: String s1 = null; String s2 = "";

4 Alias of Objects Two or more reference variables refer to the same object. Example: ChessPiece bishop1 = new ChessPiece(); ChessPiece bishop2 = new ChessPiece(); bishop1 bishop2

5 Alias of Objects Assignment of Reference Types Before bishop1bishop2 After bishop1bishop2 bishop2 = bishop1;

6 Assignment of Primitive Types Before num1 5 num2 12 After num1 5 num2 5 num2 = num1;

7 Parameter Passing Parameters in a Java method are passed by value The actual parameters (the values passed in) are assigned to the formal parameters (declared in the method header) For a parameter of primitive types, the formal parameter is a copy of the actual parameter. For a parameter of reference types (objects), the formal parameter is an alias of the actual parameter.

8 Example: Num.java public class Num { private int value; public Num(int update) { value = update; } public void setValue(int update) { value = update; } public String toString() { return value + ""; }

9 Example: ParameterTester.java public class ParameterTester { public void changeValues (int f1, Num f2, Num f3) { System.out.println("Before changing the values:"); System.out.println("f1\tf2\tf3"); System.out.println(f1 + "\t" + f2 + "\t" + f3 + "\n"); f1 = 999; f2.setValue(888); f3 = new Num (777); System.out.println("After changing the values:"); System.out.println("f1\tf2\tf3"); System.out.println(f1 + "\t" + f2 + "\t" + f3 + "\n"); }

10 Example: ParameterPassing.java public class ParameterPassing { public static void main (String[] args) { ParameterTester tester = new ParameterTester(); int a1 = 111; Num a2 = new Num (222); Num a3 = new Num (333); System.out.println("Before calling changeValues:"); System.out.println("a1\ta2\ta3"); System.out.println(a1 + "\t" + a2 + "\t" + a3 + "\n"); tester.changeValues(a1, a2, a3); System.out.println("After calling changeValues:"); System.out.println("a1\ta2\ta3"); System.out.println(a1 + "\t" + a2 + "\t" + a3 + "\n"); }

11 Example: The Output C:\Courses\CSC224\Examples>java ParameterPassing Before calling changeValues: a1a2a3 111222333 Before changing the values: f1f2f3 111222333 After changing the values: f1f2f3 999888777 After calling changeValues: a1a2a3 111888333

12 Static Fields and Methods Static fields are per class, instead of per instance. Static methods can only use the static fields of the class. Static fields can be used by all methods. Static fields and static methods can be used without creating instances of the class: ClassName.staticField ClassName.staticMethod(parameters,...) Static fields are shared among all the instances of the class.

13 Initialization of Static Fields Static fields can be initialized in a static block: public class MyClass { private static int value; static { value =...; } //... }

14 Interface A Java interface consists of declarations of abstract methods and constants. An abstract method declaration is a method header without a method body. An interface defines a contact without an implementation. An interface cannot be instantiated, since it has no implementation.

15 Interface and Class Interfaces are implemented by classes. A class that implements an interface is responsible for providing implementations of all the abstract methods declared in the interface. An interface provides a client's view of a software component, and a class implementing the interface provides the implementer's view of the software component.

16 Interface Examples public interface Comparable { public int compareTo(Object o); } public interface Iterator { public boolean hasNext(); public Object next(); public void remove(); }

17 Java Applet Java programs that run in web browsers. Embedded into an HTML file using the tag. No main() method Must extend the Applet class There are several special methods that serve specific purposes

18 Drawing public void paint(Graphics page) Automatically executed to draw the applets contents A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes

19 Drawing a Line X Y 10 20 150 45 page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or

20 Drawing a Rectangle X Y page.drawRect (50, 20, 100, 40); 50 20 100 40

21 Drawing an Oval X Y page.drawOval (175, 20, 50, 80); 175 20 50 80 boundingrectangle

22 Example: Snowman.java import java.applet.Applet; import java.awt.*; public class Snowman extends Applet { public void paint (Graphics page) { final int MID = 150; final int TOP = 50; setBackground (Color.cyan); page.setColor (Color.blue); page.fillRect (0, 175, 300, 50); // ground page.setColor (Color.yellow); page.fillOval (-40, -40, 80, 80); // sun page.setColor (Color.white); page.fillOval (MID-20, TOP, 40, 40); // head page.fillOval (MID-35, TOP+35, 70, 50); // torso page.fillOval (MID-50, TOP+80, 100, 60);// torso

23 Example: Snowman.java page.setColor (Color.black); page.fillOval (MID-10, TOP+10, 5, 5);// left eye page.fillOval (MID+5, TOP+10, 5, 5); // right eye page.drawArc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillRect (MID-15, TOP-20, 30, 25); // top of hat }

24 Example: HTML Page The Snowman Applet The Snowman Applet

25 Compile and Run Applets Compile: javac Snowman.java Run: appletviewer Snowman.html In browser: File -> Open page -> Snowman.html


Download ppt "Primitive Types vs. Reference Types Variables of primitive types hold the values of the primitive types directly. Variables of reference types hold references."

Similar presentations


Ads by Google