250 likes | 288 Views
Learn about primitive vs. reference types, garbage collection, null references, aliasing of objects, and parameter passing in Java programming. Understand static fields/methods, initialization, interfaces, and Java Applets.
E N D
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.
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
Null Reference • null • A reference does not refer to any object. • The followings are not the same: • String s1 = null; • String s2 = "";
bishop1 bishop2 Alias of Objects • Two or more reference variables refer to the same object. • Example: ChessPiece bishop1 = new ChessPiece(); ChessPiece bishop2 = new ChessPiece();
After Before bishop1 bishop2 bishop1 bishop2 Alias of ObjectsAssignment of Reference Types bishop2 = bishop1;
Before After num1 num1 num2 num2 5 5 12 5 Assignment of Primitive Types num2 = num1;
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.
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 + ""; } }
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"); } }
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"); } }
Example: The Output C:\Courses\CSC224\Examples>java ParameterPassing Before calling changeValues: a1 a2 a3 111 222 333 Before changing the values: f1 f2 f3 111 222 333 After changing the values: f1 f2 f3 999 888 777 After calling changeValues: a1 a2 a3 111 888 333
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.
Initialization of Static Fields • Static fields can be initialized in a static block: public class MyClass { private static int value; static { value = ...; } // ...}
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.
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.
Interface Examples public interface Comparable { public int compareTo(Object o); } public interface Iterator { public boolean hasNext(); public Object next(); public void remove(); }
Java Applet • Java programs that run in web browsers. • Embedded into an HTML file using the <applet> tag. • No main() method • Must extend the Applet class • There are several special methods that serve specific purposes
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
X Y page.drawLine (10, 20, 150, 45); or page.drawLine (150, 45, 10, 20); Drawing a Line 10 150 20 45
X 40 100 Y Drawing a Rectangle 50 20 page.drawRect (50, 20, 100, 40);
X 80 50 Y Drawing an Oval 175 20 bounding rectangle page.drawOval (175, 20, 50, 80);
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
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 } }
Example: HTML Page <HTML> <HEAD> <TITLE>The Snowman Applet</TITLE> </HEAD> <BODY> <center> <H3>The Snowman Applet</H3> <applet code="Snowman.class" width=300 height=225> </applet> </center> </BODY> </HTML>
Compile and Run Applets • Compile: javac Snowman.java • Run: appletviewer Snowman.html In browser: File -> Open page -> Snowman.html