primitive types vs. reference types
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/1.jpg)
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.
![Page 2: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/2.jpg)
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
![Page 3: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/3.jpg)
Null Reference
null
A reference does not refer to any object.The followings are not the same: String s1 = null; String s2 = "";
![Page 4: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/4.jpg)
Alias of Objects
Two or more reference variables refer to the same object.Example:ChessPiece bishop1 = new ChessPiece();ChessPiece bishop2 = new ChessPiece();
bishop1 bishop2
![Page 5: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/5.jpg)
Alias of ObjectsAssignment of Reference Types
Before
bishop1 bishop2
After
bishop1 bishop2
bishop2 = bishop1;
![Page 6: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/6.jpg)
Assignment of Primitive Types
Before
num1
5
num2
12
After
num1
5
num2
5
num2 = num1;
![Page 7: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/7.jpg)
Parameter Passing
Parameters in a Java method are passed by valueThe 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.
![Page 8: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/8.jpg)
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 + ""; }}
![Page 9: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/9.jpg)
Example: ParameterTester.javapublic 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"); }}
![Page 10: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/10.jpg)
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"); }}
![Page 11: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/11.jpg)
Example: The OutputC:\Courses\CSC224\Examples>java ParameterPassingBefore calling changeValues:a1 a2 a3111 222 333
Before changing the values:f1 f2 f3111 222 333
After changing the values:f1 f2 f3999 888 777
After calling changeValues:a1 a2 a3111 888 333
![Page 12: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/12.jpg)
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.staticFieldClassName.staticMethod(parameters, ...)
Static fields are shared among all the instances of the class.
![Page 13: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/13.jpg)
Initialization of Static Fields
Static fields can be initialized in a static block:
public class MyClass { private static int value; static { value = ...; } // ... }
![Page 14: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/14.jpg)
InterfaceA 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.
![Page 15: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/15.jpg)
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.
![Page 16: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/16.jpg)
Interface Examples
public interface Comparable { public int compareTo(Object o); }
public interface Iterator { public boolean hasNext(); public Object next(); public void remove(); }
![Page 17: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/17.jpg)
Java Applet
Java programs that run in web browsers.Embedded into an HTML file using the <applet> tag.No main() methodMust extend the Applet classThere are several special methods that serve specific purposes
![Page 18: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/18.jpg)
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
![Page 19: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/19.jpg)
Drawing a LineX
Y
10
20
150
45
page.drawLine (10, 20, 150, 45);
page.drawLine (150, 45, 10, 20);
oror
![Page 20: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/20.jpg)
Drawing a RectangleX
Y
page.drawRect (50, 20, 100, 40);
50
20
100
40
![Page 21: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/21.jpg)
Drawing an OvalX
Y
page.drawOval (175, 20, 50, 80);
175
20
50
80
boundingboundingrectanglerectangle
![Page 22: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/22.jpg)
Example: Snowman.javaimport 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
![Page 23: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/23.jpg)
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 }}
![Page 24: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/24.jpg)
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>
![Page 25: Primitive Types vs. Reference Types](https://reader036.vdocuments.mx/reader036/viewer/2022062301/5681301b550346895d95968a/html5/thumbnails/25.jpg)
Compile and Run Applets
Compile: javac Snowman.java
Run: appletviewer Snowman.html
In browser: File -> Open page -> Snowman.html