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

Slides:



Advertisements
Similar presentations
Based on Java Software Development, 5th Ed. By Lewis &Loftus
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
CSCI 1100/ , 6.2, 6.4 April 12, 15, 17.
Java Software Solutions
Lecture 6 b Last time: array declaration and instantiationarray declaration and instantiation array referencearray reference bounds checkingbounds checking.
© 2004 Pearson Addison-Wesley. All rights reserved2-1 Introduction to Graphics The last few sections of each chapter of the textbook focus on graphics.
Client Side Programming Using Java Applet Outcomes: You will be expected to know: – Java Applets and HTML file; –bytecode and platform independent programs;
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
Abstract Classes.
COMP 14: Applets June 21, 2000 Nick Vallidis. Announcements zP6 is due Friday.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 5 Applets and Graphics.
INF 523Q Chapter 5: Enhancing Classes (Examples).
Reference … and Misc Other Topics Clark Savage Turner, J.D., Ph.D. Some lecture slides have been adapted from those developed.
Chapter 5 Graphics. Topics Applets Classes used for graphics –Graphics –Point –Dimension –Color.
Java Data Types  Everything is an Object  Except Primitive Data Types  For efficiency  Platform independent  Portable  “slow”  Objects are often.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
INF 523Q Chapter 5: Enhancing Classes. 2 b We can now explore various aspects of classes and objects in more detail b Chapter 5 focuses on: object references.
OOP Languages: Java vs C++
Java Programming, 2E Introductory Concepts and Techniques Chapter 2 Creating a Java Application and Applet.
NSF North Mississippi GK-8 Program ‘Til You Drop Brenteria Travis NSF North Mississippi GK-8.
Programming Languages and Paradigms Object-Oriented Programming.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Java Applets What is an Applet? How do you create.
© 2004 Pearson Addison-Wesley. All rights reserved Lecture 2  Chapter 2 CS0401: Intermediate Java Programming.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
JAVA APPLETS By: Ms. Humaira Siddiqui. Java and the Internet Java is considered to be a language well suited to be used in the internet. In contrast with.
Chapter 5: Enhancing Classes Presentation slides for Java Software Solutions Foundations of Program Design Second Edition by John Lewis and William Loftus.
Applets. What is an applet? Why create applets instead of applications? – Applets are Java programs that can be embedded in an HTML document – In contrast,
Chapter 2: Color and Applets Coming up: Introduction to Graphics.
Chapter 2 Data and Expressions Part Two. 2-2/30 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive.
Graphics & Applets CSC 1051 – Data Structures and Algorithms I
Object Oriented Programming Examples: C++, Java Advantages: 1. reusibility of code 2. ability to adapt (extend) previously written code.
Intro to Applets. Applet Applets run within the Web browser environment Applets bring dynamic interaction and live animation to an otherwise static HTML.
1 Enhancing Classes  Now we can explore various aspects of classes and objects in more detail  Chapter 5 focuses on: object references and aliases passing.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Classes. Constructor A constructor is a special method whose purpose is to construct and initialize objects. Constructor name must be the same as the.
Chapter 5 Introduction to Defining Classes
Chapter 2 Data and Expressions. Chapter 2 2 Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: character.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Today… “Hello World” ritual. Brief History of Java & How Java Works. Introduction to Java class structure. But first, next slide shows Java is No. 1 programming.
Creating a Java Application and Applet
CSI 3125, Preliminaries, page 1 Applet. CSI 3125, Preliminaries, page 2 Applet An applet is a Java program that runs in a Web browser. An applet can be.
1 Introduction to Graphics b The last one or two sections of each chapter of the textbook focus on graphical issues b Most computer programs have graphical.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Please open JCreator and follow these steps: 1)Configure  Options 2)JDK Tools 3)Choose Run Applet from drop-down 4)Click Default, then Edit 5)Click the.
Memory Management in Java Mr. Gerb Computer Science 4.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
 It is a pure oops language and a high level language.  It was developed at sun microsystems by James Gosling.
Objects and Classes. F OO Programming Concepts F Creating Objects and Object Reference Variables –Differences between primitive data type and object type.
Object Lifetime and Pointers
Chapter 02 Data and Expressions.
Chapter 5: Enhancing Classes
Chapter 5: Enhancing Classes
CSC 1051 – Data Structures and Algorithms I
title = "Java Software Solutions";
CSI 1102 Introduction to Software Design
The this Reference The this reference allows an object to refer to itself That is, the this reference, used inside a method, refers to the object through.
Data and Expressions Chapter 2.
Chapter 5: Enhancing Classes
CSE 214 – Computer Science I More on Linked Lists
Chap 1 Chap 2 Chap 3 Chap 5 Surprise Me
Java Classes Aliases & Null & This
Defining Classes and Methods
Data and Expressions Part Two
Outline Character Strings Variables and Assignment
Corresponds with Chapter 5
Drawing Shapes (Graphics Class) Creating Objects Packages
Presentation transcript:

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 = "";

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

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

Assignment of Primitive Types Before num1 5 num2 12 After num1 5 num2 5 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: a1a2a Before changing the values: f1f2f After changing the values: f1f2f After calling changeValues: a1a2a

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 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

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

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

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

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 The Snowman Applet The Snowman Applet

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