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.

Slides:



Advertisements
Similar presentations
STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Advertisements

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.
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.
CSM-Java Programming-I Spring,2005 Exceptions Lesson - 7.
1 LECTURE#7: Console Input Overview l Introduction to Wrapper classes. l Introduction to Exceptions (Java run-time errors). l Console input using the BufferedReader.
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.
1 Introduction to Console Input  Primitive Type Wrapper Classes  Converting Strings to Numbers  System.in Stream  Wrapping System.in in a Buffered.
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.
Static Class Members Wrapper Classes Autoboxing Unboxing.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 1 Introduction to Computers,
Chapter 11 Abstract Classes and Interfaces 1. Abstract method New modifier for class and method: abstract An abstract method has no body Compare: abstract.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
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.
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.
Autoboxing A new feature in Java 5.0. Primitive types and classes In Java we have primitive types –boolean, char, byte, short, int, long, float, double.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Strings Java Methods A & AB Object-Oriented Programming and Data Structures Maria Litvin ● Gary Litvin Copyright © 2006 by Maria Litvin, Gary Litvin, and.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Package and Some Classes Declaration of Package Usage of Package Package of Java Language.
Using Classes and Objects. We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on: Object creation.
CSC 1051 M.A. Papalaskari, Villanova University Everyday objects: Strings and Wrappers CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari.
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.
© 2004 Pearson Addison-Wesley. All rights reserved September 7, 2007 Formatting Output & Enumerated Types & Wrapper Classes ComS 207: Programming I (in.
CONTENTS Wrapper Class Enumeration Garbage Collection Import static.
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.
More than just a song and dance.. They are objects that store primitive variable values: Integer – stores an int Double – stores a double Character –
Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images.
CPRG 215 Introduction to Object-Oriented Programming with Java Module 2- Using Java Built-in Classes Topic 2.4 Using Java Built-in Classes Produced by.
Java Software Solutions Foundations of Program Design Sixth Edition by Lewis & Loftus Chapter 3: Using Classes and Objects Coming up: Creating Objects.
Chapter 5 Defining Classes II Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 11 Abstract Classes.
InterestRate Create an InterestRate class and InterestRateViewer client class to do the following: A person is purchasing an item with their credit card.
A data type in a programming language is a set of data with values having predefined characteristics.data The language usually specifies:  the range.
Casting, Wrapper Classes, Static Methods, JOptionPane Class.
Wrapper Classes Use wrapper objects in Collections when you can’t use primitive types Primitive TypeWrapper Class byteByte shortShort intInteger longLong.
Java Programming. Objects  Java is an object-oriented programming language –use objects to define both the data type and the operations that can be applied.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 10 Thinking in Objects.
CS202 Java Object Oriented Programming Introduction to Collection Classes Chengyu Sun California State University, Los Angeles.
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.
1 CS1110 Classes, wrapper classes, Vectors. 10 Feb 2012 Miscellaneous points about classes. Discussion of wrapper classes and class Vector Use the text.
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.
Chapter 11 Abstract Classes and Interfaces
Outline Creating Objects The String Class Packages Formatting Output
Static Members and Methods
Chapter 10 Thinking in Objects
CS1110 Wrapper classes, stepwise refinement 23 Feb
CMSC 202 Static Methods.
Unit-2 Objects and Classes
Chapter 3, cont Sept 20, 2004.
The Language Package.
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Java Programming Review 1
Classes and Objects Static Methods
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:

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 Integer, and double into the class Double. F The correspondent class is called a wrapper class.  All the wrappers classes are grouped in the java.lang package.

Wrapper Classes F Boolean F Character F Short F Byte F Integer F Long F Float F Double

Byte, Short, Integer and Long F Class Constants MAX_VALUE, MIN_VALUE, TYPE F Constructors public Byte (byte number); public Byte(String s) throws NumberFormatException;... public Long(long number); public Long(String s) throws NumberFormatException;

Examples Integer a = new Integer(5); Integer b = new Integer(“5”); Double b = new Double(“3.14”);

Classes Double and Float F Class Constants MAX_VALUE, MIN_VALUE, TYPE, NaN, POSITIVE_INFINITY, NEGATIVE_INFINITY F Constructors public Float(float number); public Float (String s) throws NumberFormatexception; public Double(double number); public Double(String s) throws NumberFormatexception;

Abstract Class Number Abstract methods “convert” objects into primitive data type values: double doubleValue(); float floatValue(); byte byteValue(); short shortValue(); int intValue(); long longValue(); Example: Integer a = new Integer(5); int k = a.intValue(); // k is 5;

Conversion from String static byte parseByte(String str) throws NumberFormatexception static short parseShort(String str) throws NumberFormatexception static int parseInt(String str) throws NumberFormatexception static long parseLong(String str) throws NumberFormatexception static double parseDouble(String str) throws NumberFormatexception static float parseFloat(String str) throws NumberFormatexception

Conversion from String, cont. Examples: String str1 = “51”, str2 = “ab”; try { int i = Integer.parseInt(str1); // i is 51; } catch (NumberFormatException ex){ System.out.println(“Wrong format ” + str1); } try { int j = Integer.parseInt(str2); // j is ?; } catch (NumberFormatException ex){ System.out.println(“Wrong format ” + str2); }

Conversion to String static String toString(int num); static String toString(int num, int radix); static String toBinaryString(int num); static String toOctalString(int num); static String toHexadecimalString(int num); Example: int num =64; String str = Integer.toString(num); // “64” String str = Integer.toBinaryString(num); // “ ” String str = Integer.toOctalString(num); // “80” String str = Integer.toHexadecimalString(num); // “40”