Foundations of Programming Languages Boxing and Unboxing Prof. Dr. Christoph Reichenbach Fachbereich 12 / Institut für Informatik 14. November 2014 Boxing I I Strings, lists etc. as objects on the heap int? double? Separation Objects / Primitive Types Universal Objects (Python, Haskell (partly)) (AttoVM, Java) int i = 0; Object o = new Object (); Advantages: I Higher performance during arithmetic I I Everything is an object Built-in ops for arithmetic Advantages: I I Simple semantics Simple implementation 2/4 Automatic Boxing and Unboxing Example Java (LinkedList): LinkedList<Integer> ll = new LinkedList<>(); 1 ); ll.add( int i = ll.get(0) + 3; 3/4 Automatic Boxing and Unboxing Example Java (LinkedList): LinkedList<Integer> ll = new LinkedList<>(); ll.add(new Integer(1)); int i = ll.get(0).intValue() + 3; 3/4 Summary I I I I Some languages distinguish between boxed and unboxed values Boxed: primitive value is packed into heap object Unboxed: primitive value is stored as literal value Boxing/Unboxing: conversion between the two 4/4