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.

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

IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
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.
L EC. 02: D ATA T YPES AND O PERATORS (2/2) Fall Java Programming.
CERTIFICATION OBJECTIVES Use Class Members Develop Wrapper Code & Autoboxing Code Determine the Effects of Passing Variables into Methods Recognize when.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Objects vs. Primitives Primitive Primitive byte, short, int, long byte, short, int, long float, double float, double char char boolean boolean Object (instance.
15-Jul-15 Generics. ArrayList s and arrays A ArrayList is like an array of Object s, but... Arrays use [ ] syntax; ArrayList s use object syntax An ArrayList.
CSE373 Optional Section Java Collections 11/12/2013 Luyi Lu.
01- Intro-Java-part1 1 Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology June 2008.
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
VARIABLES AND TYPES CITS1001. Types in Java the eight primitive types the unlimited number of object types Values and References The Golden Rule Scope.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
Generics1 Parametrized classes and methods. Generics2 What are generics Generics are classes or interfaces that can be instantiated with a variety of.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
Java Generics Compiled from Core Java Technologies Tech Tips By Billy B. L. Lim.
Wrapper Classes and ArrayList Mrs. C. Furman 9/15/2008.
Types in programming languages1 What are types, and why do we need them?
CS-2851 Dr. Mark L. Hornick 1 Generic Java Classes Implementing your own generic classes.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Abstract Classes and Interfaces Chapter 9 CSCI 1302.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 6: Transition to Java Programming with Alice and Java First Edition.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
The ArrayList Data Structure Standard Arrays at High Speed! More Safety, More Efficient, and Less Overhead!
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Developed at Sun Microsystems in 1991 James Gosling, initially named “OAK” Formally announced java in 1995 Object oriented and cant write procedural.
ArrayLists (and the for-each loop) ArrayList example = new ArrayList (); example.add(4);
1 CSC 2053 New from AutoBoxing 3 Before J2SE 5.0, working with primitive types required the repetitive work of converting between the primitive.
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.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
1 Primitive Types n Four integer types:  byte  short  int (most common)  long n Two floating-point types:  float  double (most common) n One character.
The ArrayList Data Structure Standard Arrays at High Speed!
100e Hosted by vhs © Don Link, Indian Creek School, 2004 Jeopardy.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Integer, Double, and Other Wrapper Classes … Sometimes a primitive value needs to be passed in as an argument, but the method definition creates an object.
Object Oriented Programming Lecture 2: BallWorld.
Lecture 20: Wrapper Classes and More Loops
CIS3931 – Intro to JAVA Lecture Note Set 2 17-May-05.
JAVA MULTIPLE CHOICE QUESTION.
Review of Java … or maybe not.
Java Primer 1: Types, Classes and Operators
Exercise on Java Basics
Preconditions precondition: Something your method assumes is true at the start of its execution. Often documented as a comment on the method's header:
CMSC 202 Static Methods.
Exercise on Java Basics
Explicit and Implicit Type Changes
Principles of Computer Programming (using Java) Chapter 2, Part 1
5 Variables, Data Types.
Unit-2 Objects and Classes
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Computers & Programming Languages
Generics 27-Nov-18.
Wrapper Classes The java.lang package contains wrapper classes that correspond to each primitive type: Primitive Type Wrapper Class byte Byte short Short.
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Classes and Objects 5th Lecture
Java Classes and Objects 3rd Lecture
Arrays of Objects Fall 2012 CS2302: Programming Principles.
Arrays and Collections
Java Programming Review 1
Classes and Objects Static Methods
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
Generics 2-May-19.
Presentation transcript:

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 –The primitive types are not classes (but something more primitive) No methods, no constructors, no inheritance, etc. Each primitive type has a wrapper class –Boolean, Character, Byte, Short, Integer, Long, Float, Double

In and out boxing in old Java In old Java (before Java 5.0) you had to explicitly convert from primitive type to wrapper, and vice versa –int → Integer: new Integer(i) Constructor in class Integer Called “boxing”. The wrapper class is considered a “box”, and you are putting the int into the box. –Integer → int: intValue() Method in class Integer Called “unboxing”. You are taking the int out of the box. Example –List myList = new ArrayList(); –myList.add(47); // illegal –myList.add(new Integer(47)); // explicit boxing –Int a = myList.get(0); // illegal –Integer aObject = myList.get(0); –Int a = aObject.intValue(); // explicit unboxing

Autoboxing on Java 5.0 In Java 5.0 we don’t need to explicitly convert from primitive type to wrapper type. Example –List myList = new ArrayList (); –myList.add(47); // legal: 47 is automatically boxed –int a = myList.get(0); // legal: a is automatically unboxed Result –Fewer lines –Cleaner code

When autoboxing falls short Integer can have a null value, int cannot –Unboxing may result in a NullPointerException Integer a = null; Int res = 45 + a; // throws NullPointerException == has different semantics (meaning) –int b=3, c=3; –Integer d=new Integer(3), e = new Integer(3); –b == c true –d == efalse –d.equals(e)true –b.equals(c) Does not compile: b is primitive and cannot be deferenced.

Performance Autoboxing works well with collections. Don’t use a lot of autoboxing in fast-running aplications –scientific calculations, gaming, etc. –It takes too much time. –Try to stay with the primitive types They are faster than the wrappers. –Don’t do this at home (in a fast-running application) Integer a, b; Integer result = a + b; –Unboxes a and b. Executes plus. Boxes result. –This is what you should do int a, b; int result = a + b; –No autoboxing