CONTENTS Wrapper Class Enumeration Garbage Collection Import static.

Slides:



Advertisements
Similar presentations
Primitives, References and Wrappers Java has the following types defined as part of the java language; int float double byte char boolean long short These.
Advertisements

9-1 Chapter.8 Classes & Objects: A deeper look –Static Class Members –Passing & Returning Objects from Methods –The toString method –Writing an equals.
Chapter 8: A Second Look at Classes and Objects
More methods and classes, 4 of 4 Math 130 Introduction to Programming Lecture # 18 Monday, October 8, 2007.
© 2010 Pearson Addison-Wesley. All rights reserved. 9-1 Chapter Topics Chapter 9 discusses the following main topics: –Static Class Members –Passing Objects.
Chapter 14 Abstract Classes and Interfaces 1. Objectives To design and use abstract classes (§14.2). To process a calendar using the Calendar and GregorianCalendar.
Java Software Solutions
Mathematical Operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming in.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
1 1 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code to respond.
Fundamental Programming Structures in Java: Strings.
Inheritance and interfaces A class C1 is derived from class C2, then C1 is called subclass, and C2 is called superclass Superclass-parent, base class Subclass.
LESSON 4 - Variables JAVA PROGRAMMING. Variables Variable is a named storage location that stores data and the value of the data may change while the.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
1 Java Console I/O Introduction. 2 Java I/O You may have noticed that all the I/O that we have done has been output The reasons –Java I/O is based on.
1 Chapter 10 Object-Oriented Thinking. 2 Class Abstraction and Encapsulation Class abstraction means to separate class implementation details from the.
9-2  Static Class Members  Passing Objects as Arguments to Methods  Returning Objects from Methods  The toString method  Writing an equals Method.
Slides prepared by Rose Williams, Binghamton University Chapter 5 Defining Classes II.
Comp 248 Introduction to Programming Chapter 4 & 5 Defining Classes Part C Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
 Definition: Accessing child class methods through a parent object  Example: Child class overrides default parent class methods  Example: Child class.
Introduction to Java Lecture Notes 3. Variables l A variable is a name for a location in memory used to hold a value. In Java data declaration is identical.
Programming in Java (COP 2250) Lecture 8 Chengyong Yang Fall, 2005.
Chapter 7: Characters, Strings, and the StringBuilder.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Chapter 9 – Part Tony Gaddis Modified by Elizabeth Adams.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 10 Abstract Classes.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 9 Abstract Classes.
Chapter 7: Class Inheritance F Superclasses and Subclasses F Keywords: super and this F Overriding methods F The Object Class F Modifiers: protected, final.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Abstract Classes and Interfaces.
1 Input/Output. 2 In Java input and output take place through I/O streams – An I/O stream represents an input source or output destination – Streams support.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Wrapper Classes  Java offers a convenient way to incorporate, or wrap, a primitive data type into an object, for example, wrapping int into the class.
Enum public enum Week {Sunday, Moday, Tuesday, Wednesday, Thursday, Friday, Saturday} Week.Sunday.ordinal()  0 Week.Sunday  “Sunday”
1 Enums (Chapter 4) To enumerate is: to name things one after another in a list Java has a type, called an enum, where a programmer specifies a finite.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
1 Chapter 5 Abstract Classes and Interfaces. 2 Objectives u To design and use abstract classes. u To process a calendar using the Calendar and GregorianCalendar.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 4: Object Composition, Inheritance and Polymorphism Michael Hsu CSULA.
Chapter 9 A Second Look at Classes and Objects - 4.
1 1 Chapter 14 Abstract Classes and Interfaces. 22 Motivations You learned how to write simple programs to display GUI components. Can you write the code.
Topic: Classes and Objects
Chapter 11 Abstract Classes and Interfaces
Lecture 4: Object Composition, Inheritance and Polymorphism
Static Members and Methods
Chapter 10 Thinking in Objects
Java Review: Reference Types
CMSC 202 Static Methods.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Chapter 3: Introduction to Objects and Input/Output
More About Objects and Methods
Chapter 10 Thinking in Objects
Classes and Objects Static Methods
Dr. Sampath Jayarathna Cal Poly Pomona
Outline Creating Objects The String Class The Random and Math Classes
Chapter 10 Thinking in Objects Part 2
Chapter 10 Thinking in Objects
Presentation transcript:

CONTENTS Wrapper Class Enumeration Garbage Collection Import static

Wrap – associate with present! When God want to give us a PRESENT, He WRAPS it in a CHALLENGE, the BIGGER is the CHALLENGE, the is the PRESENT.

Wrapper Classes Boolean Integer Character Long Short Float Byte Double The wrapper classes do not have no-arg constructors. The instances of all wrapper classes are immutable, i.e., their internal values cannot be changed once the objects are created. Boolean Character Short Byte Integer Long Float Double

The Integer and Double Classes

The Integer and Double Class Constructors Class Constants MAX_VALUE, MIN_VALUE Conversion Methods

Numeric Wrapper Class Constructors You can construct a wrapper object either from a primitive data type value or from a string representing the numeric value. The constructors for Integer and Double are: public Integer(int value) public Integer(String s) public Double(double value) public Double(String s)

Numeric Wrapper Class Constants Each numerical wrapper class has the constants MAX_VALUE and MIN_VALUE. MAX_VALUE represents the maximum value of the corresponding primitive data type. For Byte, Short, Integer, and Long, MIN_VALUE represents the minimum byte, short, int, and long values. For Float and Double, MIN_VALUE represents the minimum positive float and double values.

To access Constant System.out.println(Integer. MAX_VALUE); the maximum integer (2,147,483,647), System.out.println(Float. MIN_VALUE); the minimum positive float (1.4E-45), and the maximum double floating-point number (1.79769313486231570e+308d).

The Static valueOf Methods The numeric wrapper classes have a useful class method, valueOf(String s). This method creates a new object initialized to the value represented by the specified string. For example:   Double doubleObject = Double.valueOf("12.4"); Integer integerObject = Integer.valueOf("12");

The Methods for Parsing Strings into Numbers parseInt method in the Integer class to parse a numeric string into an int value parseDouble method in the Double class to parse a numeric string into a double value. Each numeric wrapper class has two overloaded parsing methods to parse a numeric string into an appropriate numeric value.

Converting Strings to Numbers Integer class Part of java.lang Automatically imported into programs Convert String to integer parseInt() method Takes String argument Returns its integer value Wrapper Class or object “wrapped around” simpler element

Converting Strings to Numbers (continued) Integer class valueOf() method Convert String to Integer class object Integer class intValue() method Extract simple integer from wrapper class Double class Wrapper class Imported into programs automatically parseDouble() method Takes String argument Returns its double value

The ConvertStringToInteger Application

ANSWER EXERCISE 2 NO. 7 import javax.swing.JOptionPane; public class Addition { public static void main( String args[] ) { String first,second,third; int number1,number2,sum; final double PI = 3.14; double radius,area; first=JOptionPane.showInputDialog("Enter 1st int" ); second=JOptionPane.showInputDialog("Enter 2nd int"); third=JOptionPane.showInputDialog("Enter radius"); number1 = Integer.parseInt(first); number2 = Integer.parseInt(second); radius = Double.parseDouble(third); sum = number1 + number2; area = PI*radius*radius; JOptionPane.showMessageDialog(null,"The sum is: " + sum, "Results", JOptionPane.PLAIN_MESSAGE ); JOptionPane.showMessageDialog(null,"Area is: " + area, "Area of Circle", JOptionPane.PLAIN_MESSAGE ); System.exit(0); }

convert objects to primitive type values In class Number, methods; doubleValue, floatValue, intValue, longValue, shortValue, and byteValue “convert” objects into primitive type values.

e.g //Program 4.13 class Test { public static void main(String[] args) { Object x = new Integer(2); System.out.println(x.intValue()); }

toString, equals, and hashCode Methods - in the Object class Each wrapper class overrides the toString, equals, and hashCode Since all the numeric wrapper classes and the Character class implement the Comparable interface, the compareTo method is implemented in these classes.

e.g //Program 4.12 class Test { public static void main(String[] args) { Object x = new Integer(2); System.out.println(x.toString()); }

enumerated

Enumerated Types Known as an enum, requires declaration and definition like a class Syntax: enum typeName { one or more enum constants } Definition: enum Day { SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } Declaration: Day WorkDay; // creates a Day enum Assignment: Day WorkDay = Day.WEDNESDAY;

Enumerated Types An enum is a specialized class Each are objects of type Day, a specialized class Day.SUNDAY Day workDay = Day.WEDNESDAY; The workDay variable holds the address of the Day.WEDNESDAY object Day.MONDAY Day.TUESDAY address Day.WEDNESDAY Day.THURSDAY Day.FRIDAY Day.SATURDAY

DECLARATION enum outside A CLASS enum CoffeeSize { BIG, HUGE, OVERWHELMING } ; class Coffee { CoffeeSize size; } public class CoffeeTest1 public static void main(String[] args) Coffee drink = new Coffee(); drink.size = CoffeeSize.BIG; semicolon is optional

DECLARATION - inside a class class Coffee2 { enum CoffeeSize {BIG, HUGE, OVERWHELMING } CoffeeSize size; } public class CoffeeTest2 public static void main (String[] args) { Coffee2 drink = new Coffee2(); drink.size = Coffee2.CoffeeSize.BIG; } enclosing class name required

CANNOT DECLARE ENUM IN METHOD public class CoffeeTestl { public static void main(String[] args) enum CoffeeSize { BIG, HUGE, OVERWHELMING } Coffee drink = new Coffee(); drink.size = CoffeeSize.BIG; }

Constructors, Methods&Variables IN ENUM enum CoffeeSize { private int ounces; BIG(8), HUGE(10), OVERWHELMING(16); CoffeeSize(int ounces) { this.ounces = ounces; } public int getOunces() { return ounces; VARIABLE CONSTRUCTORS METHODS

end with ; start with { close with } enum CoffeeSize { BIG(8), HUGE(10), OVERWHELMING(16) public String getLidCode() return "A"; } }; . start with { close with } end with ;

Enumerated Types - Switch Java allows you to test an enum constant with a switch statement. Refer example from lab module page 71-71(Program 4.11) switch (satay) { case MUTTON: System.out.println("Mutton Satay - Fabulous."); System.out.println("Price is RM0.60/each"); break; case OSTRICH: System.out.print("Ostrich Satay "); System.out.println("For Low Cholestrol Diet."); System.out.println("Price is RM1.00/each");

Enumerated Types - Methods toString – returns name of calling constant ordinal – returns the zero-based position of the constant in the enum. For example the ordinal for Day.THURSDAY is 4 equals – accepts an object as an argument and returns true if the argument is equal to the calling enum constant compareTo - accepts an object as an argument and returns a negative integer if the calling constant’s ordinal < than the argument’s ordinal, a positive integer if the calling constant’s ordinal > than the argument’s ordinal and zero if the calling constant’s ordinal == the argument’s ordinal.

Garbage Collection

Garbage Collection When objects are no longer needed they should be destroyed. This frees up the memory that they consumed. Java handles all of the memory operations for you. Simply set the reference to null and Java will reclaim the memory.

Garbage Collection BankAccount account1 = new BankAccount(500.0); The Java Virtual Machine has a process that runs in the background that reclaims memory from released objects. The garbage collector will reclaim memory from any object that no longer has a valid reference pointing to it. BankAccount account1 = new BankAccount(500.0); BankAccount account2 = account1; This sets account1 and account2 to point to the same object.

Garbage Collection A BankAccount object account1 account2 Balance: account1 Address 500.0 account2 Address Here, both account1 and account2 point to the same instance of the BankAccount class.

Garbage Collection A BankAccount object account1 account2 Balance: null 500.0 account2 Address However, by running the statement: account1 = null; only account2 will be pointing to the object.

Garbage Collection A BankAccount object account1 account2 Balance: null 500.0 Since there are no valid references to this object, it is now available for the garbage collector to reclaim. account2 null If we now run the statement: account2 = null; neither account1 or account2 will be pointing to the object.

Garbage Collection A BankAccount object account1 account2 Balance: The garbage collector reclaims the memory the next time it runs in the background. Balance: account1 null 500.0 account2 null

The finalize Method If a method with the signature: public void finalize(){…} is included in a class, it will run just prior to the garbage collector reclaiming its memory. The garbage collector is a background thread that runs periodically. It cannot be determined when the finalize method will actually be run.

Refer to page

import static import static java.lang.Math.*; public class StaticImportTest { public static void main(String args[]) System.out.printf("sqrt(9.0)\t= %.1f\n",sqrt(9.0)); System.out.printf("ceil(-9.7)\t= %.1f\n",ceil(-9.7)); System.out.printf("\tlog(E)\t\t= %.1f\n",log(E)); System.out.printf("cos(0.0 \t= %.1f\n",cos(0.0)); }