PIC 20A Number, Autoboxing, and Unboxing

Similar documents
JAVA WRAPPER CLASSES

ITI Introduction to Computing II

ITI Introduction to Computing II

Class Library java.lang Package. Bok, Jong Soon

Introduction to Inheritance

Unit 3 INFORMATION HIDING & REUSABILITY. Interface:-Multiple Inheritance in Java-Extending interface, Wrapper Class, Auto Boxing

JAVA.LANG.INTEGER CLASS

C08c: A Few Classes in the Java Library (II)

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CSC Java Programming, Fall Java Data Types and Control Constructs

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

Lecture Notes Chapter #9_c Abstract Classes & Interfaces

C# and Java. C# and Java are both modern object-oriented languages

PIC 20A Collections and Data Structures

PIC 20A Streams and I/O

Chapter 9 Abstract Classes and Interfaces

CISC370: Inheritance

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

We now start exploring some key elements of the Java programming language and ways of performing I/O

CS 251 Intermediate Programming Methods and Classes

CS 251 Intermediate Programming Methods and More

Java. Representing Data. Representing data. Primitive data types

Computational Expression

Classes and Objects Miscellany: I/O, Statics, Wrappers & Packages. CMSC 202H (Honors Section) John Park

Chapter 10. Object-Oriented Thinking

A variable is a name for a location in memory A variable must be declared

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Lesson 10A OOP Fundamentals. By John B. Owen All rights reserved 2011, revised 2014

Abstract Classes Interfaces

2. The object-oriented paradigm

CST141 Thinking in Objects Page 1

CSE 1223: Introduction to Computer Programming in Java Chapter 6 ArrayLists

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

type conversion polymorphism (intro only) Class class

Polymorphism. return a.doublevalue() + b.doublevalue();

More About Objects and Methods

More About Objects and Methods. Objectives. Outline. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

Data Types and Variables

Introduction to Java Programming Comprehensive Version

The class Object. Lecture CS1122 Summer 2008

Introduction to Programming Using Java (98-388)

CIS 110: Introduction to Computer Programming

Getting started with Java

Java Programming Lecture 10

C20a: Selected Interfaces in Java API

Operators and Expressions

Formatting Output & Enumerated Types & Wrapper Classes

Introduction to Object-Oriented Programming

ITP 342 Mobile App Dev. Data Types

Chapter 2: Using Data

Assumptions. History

Rules and syntax for inheritance. The boring stuff

PIC 20A Inheritance, Interface, and Polymorphism

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Index COPYRIGHTED MATERIAL

import java.io.*; import javax.swing.*;

New Features in EPICS V4 Release 4.4

PIC 20A The Basics of Java

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

JAVA MOCK TEST JAVA MOCK TEST II

More on variables and methods

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 11 Object-Oriented Design Exception and binary I/O can be covered after Chapter 9

Java Threads and intrinsic locks

Java classes cannot extend multiple superclasses (unlike Python) but classes can implement multiple interfaces.

Chapter 10 Thinking in Objects. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

More About Objects and Methods. Objectives. Outline. Chapter 6

More About Objects and Methods

Chapter 10 Thinking in Objects. Liang, Introduction to Java Programming, Eleventh Edition, (c) 2018 Pearson Education, Ltd. All rights reserved.

core programming Basic Java Syntax Marty Hall, Larry Brown:

Ans: Store s as an expandable array of chars. (Double its size whenever we run out of space.) Cast the final array to a String.

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Generic Collections. Chapter The Object class

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Inheritance. Transitivity

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community

CS162: Introduction to Computer Science II. Primitive Types. Primitive types. Operations on primitive types. Limitations

CS Programming I: ArrayList

More About Objects and Methods

1B1b Classes in Java Part I

Inheritance. Improving Structure with Inheritance. Dr. Siobhán Drohan Mairead Meagher. Produced by:

Java Simple Data Types

Do not turn to the next page until the start of the exam.

Array. Prepared By - Rifat Shahriyar

(A) 99 ** (B) 100 (C) 101 (D) 100 initial integers plus any additional integers required during program execution

Value vs. reference semantics

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Java Primer. CITS2200 Data Structures and Algorithms. Topic 2

Java Primer 1: Types, Classes and Operators

Do not turn to the next page until the start of the exam.

ECE 122. Engineering Problem Solving with Java

Chapter 5. Defining Classes II. Slides prepared by Rose Williams, Binghamton University

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

CS/ENGRD 2110 FALL Lecture 7: Interfaces and Abstract Classes

CS162: Introduction to Computer Science II

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSSE 220 Day 8. Arrays, ArrayLists, Wrapper Classes, Auto-boxing, Enhanced for loop. Check out ArraysAndLists from SVN

COE318 Lecture Notes Week 5 (Oct 3, 2011)

Transcription:

PIC 20A Number, Autoboxing, and Unboxing Ernest Ryu UCLA Mathematics Last edited: October 27, 2017

Illustrative example Consider the function that can take in any object. public static void printclassandobj ( Object obj ) { System. out. println ( obj. getclass ()); System. out. println ( obj ); } This method unfortunately doesn t work with primitives. Or does it? (We ll talk about getclass() once we learn about generics.) 2

Illustrative example public static void main ( String... args ) { Complex c1 = new Complex ( 3.3, 2.9 ); int i1 = 2; printclassandobj ( c1 ); printclassandobj ( i1 ); } The output is class Complex 3.3+2.9i class java.lang.integer 2 Why did this work? 3

Illustrative example In this line, printclassandobj ( i1 ); the int i1 was autoboxed into an object of type Integer. The Java language provides their respective wrapper classes for the 8 primitive types: boolean, byte, char, float, int, long, short, and double correspond to Boolean, Byte, Character, Float, Integer, Long, Short, and Double. 4

class Number The class java.lang.number and its subclasses has the following inheritance hierarchy. Object Number Double Integer Float Long BigInteger (This figure is not complete. There are other subclasses of Number like Short or BigDecimal.) 5

class Number The class Number is an abstract class designed to be inherited by classes representing numbers. Number has the following abstract methods public abstract double doublevalue () public abstract float floatvalue () public abstract int intvalue () public abstract long longvalue () Any concrete subclass must provide an implementation of thses methods. https://docs.oracle.com/javase/8/docs/api/java/lang/number.html 6

class Number Number provides the following concrete methods public byte bytevalue () public short shortvalue () Subclasses of Number don t have to override these methods. 7

class Number The implementations of bytevalue and shortvalue reveal why they can be provided as concrete methods. public byte bytevalue () { return ( byte ) intvalue (); } public short shortvalue () { return ( short ) intvalue (); } http://www.docjar.com/html/api/java/lang/number.java.html 8

class Number Out of the 8 wrapper classes for primitive types, 6 inherit Number: Byte, Float, Integer, Long, Short, and Double. (Boolean and Charater do not inherit Number.) These provide many useful features in addition to being an object that contains the primitive data type. 9

class Character The class Character only inherits from Object. In addition to being the wrapper class of char, Character provides other useful features. System. out. println ( Character. touppercase ( c )); (touppercase is a static method.) 10

class Boolean The class java.lang.boolean only inherits from Object. It s not very useful aside from being the wrapper class of boolean. 11

Autoboxing and unboxing The Java language provides special support to the 8 wrapper classes in the form of autoboxing and unboxing. Autoboxing allows you to convert a primitive type into its respective wrapper class type, implicitly or explicitly. boolean b1 = false ; Boolean b2 = b1; Boolean b3 = ( Boolean ) b1; 12

Autoboxing and unboxing Unboxing allows you to convert a wrapper class type into its respective primitive type, implicitly or explicitly. Integer i1 = new Integer ( 3); int i2 = i1; int i3 = ( int ) i1; Autoboxing and unboxing are special features provided by the language; you cannot make classes you write support autoboxing and unboxing. 13

Conclusion Autoboxing and unboxing is slower than using primitive types but they can provide certain convenience. import java. util.*; public class Test { public static void main ( String [] args ) { ArrayList < Character > l = new ArrayList < >(); //. add works because of autoboxing l. add ( a ); l. add ( b ); l. add ( c ); Collections. shuffle (l); } } for ( int i=0; i<3; i ++) System. out. println (l. get (i )); You can use Collections with Objects but not with primitive types. 14