Advances in Programming Languages: Generics, interoperability and implementation

Size: px
Start display at page:

Download "Advances in Programming Languages: Generics, interoperability and implementation"

Transcription

1 Advances in Programming Languages: Generics, interoperability and implementation Stephen Gilmore The University of Edinburgh February 1, 2007 Understanding generic code Generic Java extends Java with generic type variables which allow us to define generic classes and generic methods. Generic C# extends C# similarly. When adding genericity to an objectoriented programming language it is important to do this in a way which does not invalidate existing code. Legacy code (which does not use the generic features of the language) should continue to operate as before and there should be a smooth upgrade path from legacy code to generic code. In Generic Java (Java 1.5) generics are compiled out by the Java compiler. Type variables are replaced by Object and downcasts are inserted automatically by the compiler instead of being coded manually by the programmer. The implications of this are that generic code is never slower than non-generic code (but there is no reason to think that it will be faster) while being more reliable than non-generic code. The way-ahead-of-time optimising compiler gcj will be extended to deal with Java generics when GCC-4.3 is released. The gcj compiler will include the Eclipse Compiler for Java, ecj as its front-end. This extended gcj could produce faster code by eliminating casts. This example shows use of SUN s javac. /* File: programs/java/cell.java */ /* A generic, updateable cell */ class Cell<A> { A value; Cell(A value) { this.value = value; A get() { return value; void set (A value) { this.value = value; The above example is a simple generic cell which holds values. We now try comparing that generic Java class with its compiled bytecode representation. We can inspect the Java bytecode produced by the compiler by using the Java disassembler javap. We use the command javap -c Cell When compiled to Java bytecode we see that the cell is operating on objects of class java.lang.object. 1

2 UG4 Advances in Programming Languages 2005/ Compiled from "Cell.java" class Cell extends java.lang.object{ java.lang.object value; Cell(java.lang.Object); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/object."<init>":()v 4: aload_0 5: aload_1 6: putfield #2; //Field value:ljava/lang/object; 9: return java.lang.object get(); Code: 0: aload_0 1: getfield #2; //Field value:ljava/lang/object; 4: areturn void set(java.lang.object); Code: 0: aload_0 1: aload_1 2: putfield #2; //Field value:ljava/lang/object; 5: return That was an example of how generic classes are defined. How are they used? /* File: programs/java/celltest.java */ class CellTest { public static void main (String[] args) { Cell<String> sc1 = new Cell<String>("First"); String s1 = sc1.get(); /* No cast needed */ System.out.println(s1); As usual with generic classes, a cast is not written by the application developer, but it is automatically inserted by the compiler. Compiled from "CellTest.java" class CellTest extends java.lang.object{ CellTest(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/object."<init>":()v 4: return public static void main(java.lang.string[]);

3 UG4 Advances in Programming Languages 2005/ Code: 0: new #2; //class Cell 3: dup 4: ldc #3; //String First 6: invokespecial #4; //Method Cell."<init>":(Ljava/lang/Object;)V 9: astore_1 10: aload_1 11: invokevirtual #5; //Method Cell.get:()Ljava/lang/Object; 14: checkcast #6; //class String 17: astore_2 18: getstatic #7; //Field java/lang/system.out:ljava/io/printstream; 21: aload_2 22: invokevirtual #8; //Method java/io/printstream.println:(ljava/lang/string;)v 25: return Perhaps the overriding motivation for taking this approach to implementing generics in Java is that the Java Virtual Machine should be unaffected by this approach. From the JVM point of view the bytecode which it executes would look just like it came from a (typesafe) object polymorphic method. Because many manufacturers have implemented Java Virtual Machines it was a sensible design constraint to contain this change to the Java language within the compiler. Unfortunately SUN s Java 1.5 compiler does not correctly fulfil the intended design because it compiles only for the 1.5 virtual machine, not for earlier ones. This largely undermines the approach of compilation by erasure and means that generics could have been supported in the JVM. A separate tool, Retroweaver, can convert Java 1.5 and higher class files to Java 1.4, 1.3 or 1.2, but depending on a third-party tool to repair the compiled class files is not attractive to most developers. We demonstrate how the problem is experienced by users trying to run modern bytecode on older JVMs. [scap]stg: export ALT=/etc/alternatives [scap]stg: alias java6c=$alt/java_sdk_1.6.0/bin/javac [scap]stg: java6c -version javac [scap]stg: java6c GenericStack.java [scap]stg: alias java4=$alt/jre_1.4.2/bin/java [beetgreens]stg: java4 -version java version "1.4.2" gij (GNU libgcj) version (Red Hat ) Copyright (C) 2006 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. [scap]stg: java4 GenericStack Exception in thread "main" java.lang.classformaterror: GenericStack (unrecognized class file version) An alternative is to ask the Java compiler to generate Java 1.4 compliant bytecode. [scap]stg: java6c GenericStack.java [scap]stg: file GenericStack.class

4 UG4 Advances in Programming Languages 2005/ GenericStack.class: compiled Java class data, version 50.0 [scap]stg: java6c -target jsr14 GenericStack.java [scap]stg: file GenericStack.class GenericStack.class: compiled Java class data, version 48.0 [scap]stg: java4 GenericStack c b a Unfortunately it transpires that this use of the target flag is not recommended by the SUN compiler developers. I should not mention the javac flag -target jsr14, which causes javac to generate maximally-1.4-compatible code with the new language features enabled. That flag is useful for bootstrapping and testing javac, but you should not depend on it. If it doesn t work as you want and you report a bug about it, you can expect the flag to be removed. Neal Gafter, SUN lead Java compiler developer Some of the complications are that, because of separate compilation, it is necessary for class files to carry generic type variable information. This is added to the metadata in the class file, specifically as a new Signature attribute for classes, methods and fields. Generics in C# are supported down into the Common Language Runtime, thus their implementation does not have the impact on typing which erasure does and can improve the performance of compiled code by removing casts and unnecessary boxing (compilation by erasure does not give any performance improvement). Polymorphic classes in Objective Caml Generic Java allows developers to parameterise classes by a generic class variable the parameter must be a reference type, not one of the built-in ground types (such as int, boolean, float or double). Polymorphic classes in Objective Caml are parameterised by a generic type variable. Classes can be parametric over a reference type or one of the ground types (such as int, bool or float). We now see how we would re-implement the simple generic cells from our Java example in Objective Caml. (* File: programs/caml/cell.ml *) class [ a] cell (value_init : a) = object val mutable value = value_init method get = value method set (new_value) = value <- new_value end ;; This class definition is parameterised by a type variable a, and by the parameter to the constructor of the class. Caml does not support overloading so a class has only a single constructor, unlike Java. The cell class is used in the Ocaml top-level loop thus. # let c = new cell(3);; (* create a new cell *) val c : int cell = <obj>

5 UG4 Advances in Programming Languages 2005/ # c#get ;; (* invoke the get method *) - : int = 3 # c#set(4) ;; (* invoke the set method *) - : unit = () # c#get;; (* invoke the get method again *) - : int = 4 Subclassing and specialisation If we found that we used cells of strings almost exclusively in our application it might be worthwhile to subclass the cell class and specialise it to a cell of strings. This can be done in a very similar fashion in Ocaml and Generic Java. (* File: programs/caml/stringcell.ml *) class stringcell (s : string) = object inherit [string] cell s end ;; /* File: programs/java/stringcell.java */ class StringCell extends Cell<String> { StringCell(String s) { super(s); Bounded polymorphic methods So far, we have used Java s generic methods to define methods which are parameterised on a generic class variable. We know that in Java s object polymorphism we can define methods which are parameterised on a specific class (and all of its subclasses). We can combine these to define methods where the generic class parameter is bounded above by a specific superclass. For example, class C<A extends B> defines a generic class C whose parameter (A) is a subclass of B. As an example we will define a class of sorted lists, whose elements must be comparable. Before we progress to defining such a class we first implement a non-generic version, discover a problem with it, and then progress to defining a generic version. The non-generic version is called OrderedList and the generic version is called SortedList.

6 UG4 Advances in Programming Languages 2005/ /* File: programs/java/orderedlist.java */ /* A non-generic version */ public class OrderedList { /* Fields to hold the head of the list and the tail */ public Comparable head; public OrderedList tail; /* A constructor for sorted lists */ public OrderedList(Comparable head, OrderedList tail){ this.head = head; this.tail = tail; /* Insert a new element, maintaining ordering */ public void insert (Comparable e) { OrderedList l = this; while(l.tail!= null && l.head.compareto(e) < 0) { l = l.tail; ; if (l.head.compareto(e) >= 0) { l.tail = new OrderedList(l.head, l.tail); l.head = e; else { l.tail = new OrderedList(e, l.tail); The non-generic version of this class has the problem that ordered lists are inherently heterogeneous, as OO collections typically are. Thus it is possible to construct a list all of whose elements are comparable, but just not with each other. A list containing an integer, a string, a double and a float would be one such example. The classes Integer, String, Double and Float all implement java.lang.comparable, but they are not pairwise comparable. /* File: programs/java/orderedlisttest.java */ public class OrderedListTest { public static void main (String[] args) { OrderedList s = new OrderedList(new Integer(3), null); s.insert("a string"); s.insert(new Double(3.0d)); s.insert(new Float(3.1415)); for( ; s!= null ; s = s.tail) { System.out.println(s.head); This program compiles without warnings but fails at run-time with a class cast exception.

7 UG4 Advances in Programming Languages 2005/ [slim]stg: java OrderedListTest Exception in thread "main" java.lang.classcastexception at java.lang.integer.compareto(integer.java:955) at OrderedList.insert(OrderedList.java:19) at OrderedListTest.main(OrderedListTest.java:6) The compareto method throws this exception when it is applied to an object of an inappropriate type. This problem does not arise with generic classes. Comparable compared Non-generic Java defines a Comparable interface with only a single method: public abstract int compareto(object o) In generic Java this interface is replaced by a generic interface. Thus, if we want to have a class E whose instances are comparable to other instances of class E we have E extends Comparable<E> In our example we want a list of such elements. /* File: programs/java/sortedlist.java */ /* A bounded polymorphic sorted list class with generic class variable "E" */ public class SortedList<E extends Comparable<E>> { /* Fields to hold the head of the list and the tail */ public E head; public SortedList<E> tail; /* A constructor for sorted lists */ public SortedList(E head, SortedList<E> tail){ this.head = head; this.tail = tail; /* Insert a new element, maintaining ordering */ public void insert (E e) { SortedList<E> l = this; while(l.tail!= null && l.head.compareto(e) < 0) { l = l.tail; ; if (l.head.compareto(e) >= 0) { l.tail = new SortedList<E>(l.head, l.tail); l.head = e; else { l.tail = new SortedList<E>(e, l.tail); This class is used as in the example below.

8 UG4 Advances in Programming Languages 2005/ /* File: programs/java/sortedlisttest.java */ public class SortedListTest { public static void main (String[] args) { SortedList<Integer> s = new SortedList<Integer>(new Integer(3), null); s.insert(new Integer(1)); s.insert(new Integer(5)); s.insert(new Integer(7)); s.insert(new Integer(2)); for( ; s!= null ; s = s.tail) { System.out.println(s.head); // prints 1, 2, 3, 5, 7 Java 1.5 s autoboxing feature allows the constructor invocations to be omitted in the source file (they are automatically inserted by the compiler). /* File: programs/java/sortedlisttest3.java */ public class SortedListTest3 { public static void main (String[] args) { SortedList<Integer> s = new SortedList<Integer>(3, null); s.insert(1); // autoboxing inserts s.insert(5); // the omitted uses s.insert(7); // of the Integer s.insert(2); // constructor. for( ; s!= null ; s = s.tail) { System.out.println(s.head); // prints 1, 2, 3, 5, 7 Sorted lists in Objective Caml The generic sorted list class exercises the expressiveness of an object-oriented language quite thoroughly. Classes must be parameterised by a class variable (or a type variable) but this must support a comparison operator (such as the compareto method). We now revisit this example in Objective Caml. The first step is to define the abstract (noninstantiable) class for comparable objects. Abstract classes are labelled virtual in Objective Caml. (* File: programs/caml/comparable.ml *) class virtual comparable = object(self : a) method virtual compareto : a -> int end ;; This example introduces some new notation. The first is the labelling of classes and methods as virtual (meaning the same as abstract in Java). The second is the reference of the object to itself (as self or any other identifier whereas we would use the reserved word this in Java).

9 UG4 Advances in Programming Languages 2005/ The third is the use of the type variable a to force the parameter of compareto to be the same as the type of the object itself. Constraints and open types Where Java uses inheritance in requiring E to extend Comparable<E>, Caml takes a different approach in generalising the comparable type to an open type, #comparable and then constraining the type parameter of the class to that. Caml infers the types of expressions, even expressions which use objects and method invocation. Consider the following function. let lessthan(a,b) = a#compareto(b) < 0;; Caml infers the following type for this function. lessthan : < compareto : a -> int;.. > * a -> bool Here the double dots (.. ) in the type of the left operand indicate that the type remains open. We know that these objects have a compareto method but we do not know all of their other methods. This is the definition of sorted lists in Caml. (* File: programs/caml/sortedlist.ml *) class [ a] sortedlist (initial_value : a) = object val mutable elements = [initial_value] constraint a = #comparable method insert e = let rec ins e = function [] -> [e] (head::tail) -> if head#compareto(e) >= 0 then e::(head::tail) else head::(ins e tail) in elements <- ins e elements method getelements = elements end;; Now that we have sorted lists we define a class whose instances can be stored in these lists. We define a small integer class similar to Integer in Java. Our class has an intvalue method to obtain the int value stored in an integer object. Our class implements compareto as it is implemented in java.lang.integer. (* File: programs/caml/integer.ml *) class integer (initialvalue : int) = object(self) inherit comparable val i = initialvalue method intvalue = i method compareto anotherinteger = let anotherval = anotherinteger#intvalue in let selfval = self#intvalue in if (selfval < anotherval) then -1 else if (selfval = anotherval) then 0 else 1 end;;

10 UG4 Advances in Programming Languages 2005/ Finally we can apply this definition to a small collection of abitrarily-chosen integers, as before. (* File : programs/caml/sortedlisttest.ml *) let i = new integer(3);; let s = new sortedlist(i);; s#insert(new integer(1));; s#insert(new integer(5));; s#insert(new integer(7));; s#insert(new integer(2));; let l = s#getelements;; List.iter (fun i -> print_int(i#intvalue); print_newline()) l;; Summary Polymorphic classes are implemented differently in Java and Objective Caml, but both have the capability of defining bounded polymorphic classes and ensuring type safety in operation. The implementation decisions made in the Java 1.5 compiler have been criticised by some. We took a sorted generic list class as our example and implemented this in both of these languages.

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls

JVML Instruction Set. How to get more than 256 local variables! Method Calls. Example. Method Calls CS6: Program and Data Representation University of Virginia Computer Science Spring 006 David Evans Lecture 8: Code Safety and Virtual Machines (Duke suicide picture by Gary McGraw) pushing constants JVML

More information

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II

CS2110 Fall 2011 Lecture 25. Under the Hood: The Java Virtual Machine, Part II CS2110 Fall 2011 Lecture 25 Under the Hood: The Java Virtual Machine, Part II 1 Java program last time Java compiler Java bytecode (.class files) Compile for platform with JIT Interpret with JVM run native

More information

Static Analysis of Dynamic Languages. Jennifer Strater

Static Analysis of Dynamic Languages. Jennifer Strater Static Analysis of Dynamic Languages Jennifer Strater 2017-06-01 Table of Contents Introduction............................................................................... 1 The Three Compiler Options...............................................................

More information

Space Exploration EECS /25

Space Exploration EECS /25 1/25 Space Exploration EECS 4315 www.eecs.yorku.ca/course/4315/ Nondeterminism 2/25 Nondeterministic code is code that, even for the same input, can exhibit different behaviours on different runs, as opposed

More information

Run-time Program Management. Hwansoo Han

Run-time Program Management. Hwansoo Han Run-time Program Management Hwansoo Han Run-time System Run-time system refers to Set of libraries needed for correct operation of language implementation Some parts obtain all the information from subroutine

More information

Shared Mutable State SWEN-220

Shared Mutable State SWEN-220 Shared Mutable State SWEN-220 The Ultimate Culprit - Shared, Mutable State Most of your development has been in imperative languages. The fundamental operation is assignment to change state. Assignable

More information

02 B The Java Virtual Machine

02 B The Java Virtual Machine 02 B The Java Virtual Machine CS1102S: Data Structures and Algorithms Martin Henz January 22, 2010 Generated on Friday 22 nd January, 2010, 09:46 CS1102S: Data Structures and Algorithms 02 B The Java Virtual

More information

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

CSCI-142 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community CSCI-12 Exam 1 Review September 25, 2016 Presented by the RIT Computer Science Community http://csc.cs.rit.edu 1. Provide a detailed explanation of what the following code does: 1 public boolean checkstring

More information

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion

Course Overview. PART I: overview material. PART II: inside a compiler. PART III: conclusion Course Overview PART I: overview material 1 Introduction (today) 2 Language Processors (basic terminology, tombstone diagrams, bootstrapping) 3 The architecture of a Compiler PART II: inside a compiler

More information

Java byte code verification

Java byte code verification Java byte code verification SOS Master Science Informatique U. Rennes 1 Thomas Jensen SOS Java byte code verification 1 / 26 Java security architecture Java: programming applications with code from different

More information

VIRTUAL FUNCTIONS Chapter 10

VIRTUAL FUNCTIONS Chapter 10 1 VIRTUAL FUNCTIONS Chapter 10 OBJECTIVES Polymorphism in C++ Pointers to derived classes Important point on inheritance Introduction to virtual functions Virtual destructors More about virtual functions

More information

BASICS.

BASICS. BASICS http://www.flickr.com/photos/oskay/472097903/ CSCI 135 - Fundamentals of Computer Science I 2 Outline Computer Basics Programs and Languages Introduction to the Eclipse IDE Our First Program Comments

More information

Improving Java Performance

Improving Java Performance Improving Java Performance #perfmatters Raimon Ràfols ...or the mumbo-jumbo behind the java compiler Agenda - Disclaimer - Who am I? - Our friend the java compiler - Language additions & things to consider

More information

The Java Language Implementation

The Java Language Implementation CS 242 2012 The Java Language Implementation Reading Chapter 13, sections 13.4 and 13.5 Optimizing Dynamically-Typed Object-Oriented Languages With Polymorphic Inline Caches, pages 1 5. Outline Java virtual

More information

Introduction to Inheritance

Introduction to Inheritance Introduction to Inheritance James Brucker These slides cover only the basics of inheritance. What is Inheritance? One class incorporates all the attributes and behavior from another class -- it inherits

More information

Advances in Programming Languages: Efficiency

Advances in Programming Languages: Efficiency Advances in Programming Languages: Efficiency Stephen Gilmore The University of Edinburgh March 1, 2007 Overview Computer programs should execute efficiently. The art and skill of computer programming

More information

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008

Under the Hood: The Java Virtual Machine. Lecture 23 CS2110 Fall 2008 Under the Hood: The Java Virtual Machine Lecture 23 CS2110 Fall 2008 Compiling for Different Platforms Program written in some high-level language (C, Fortran, ML,...) Compiled to intermediate form Optimized

More information

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are built on top of that. CMSC131 Inheritance Object When we talked about Object, I mentioned that all Java classes are "built" on top of that. This came up when talking about the Java standard equals operator: boolean equals(object

More information

Rules and syntax for inheritance. The boring stuff

Rules and syntax for inheritance. The boring stuff Rules and syntax for inheritance The boring stuff The compiler adds a call to super() Unless you explicitly call the constructor of the superclass, using super(), the compiler will add such a call for

More information

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format

JVM. What This Topic is About. Course Overview. Recap: Interpretive Compilers. Abstract Machines. Abstract Machines. Class Files and Class File Format Course Overview What This Topic is About PART I: overview material 1 Introduction 2 Language processors (tombstone diagrams, bootstrapping) 3 Architecture of a compiler PART II: inside a compiler 4 Syntax

More information

Java Fundamentals (II)

Java Fundamentals (II) Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals (II) Marco Piccioni static imports Introduced in 5.0 Imported static members of a class

More information

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal Version date:

Java Brand Generics. Advanced Topics in Java. Khalid Azim Mughal  Version date: Java Brand Generics Advanced Topics in Java Khalid Azim Mughal khalid@ii.uib.no http://www.ii.uib.no/~khalid/atij/ Version date: 2005-03-03 Khalid Azim Mughal Java Brand Generics 1/40 Overview Introduction

More information

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1

CSE P 501 Compilers. Java Implementation JVMs, JITs &c Hal Perkins Winter /11/ Hal Perkins & UW CSE V-1 CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Winter 2008 3/11/2008 2002-08 Hal Perkins & UW CSE V-1 Agenda Java virtual machine architecture.class files Class loading Execution engines

More information

CMSC 341. Nilanjan Banerjee

CMSC 341. Nilanjan Banerjee CMSC 341 Nilanjan Banerjee http://www.csee.umbc.edu/~nilanb/teaching/341/ Announcements Just when you thought Shawn was going to teach this course! On a serious note: register on Piazza I like my classes

More information

Atelier Java - J1. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Atelier Java - J1. Marwan Burelle.  EPITA Première Année Cycle Ingénieur. marwan.burelle@lse.epita.fr http://wiki-prog.kh405.net Plan 1 2 Plan 3 4 Plan 1 2 3 4 A Bit of History JAVA was created in 1991 by James Gosling of SUN. The first public implementation (v1.0) in 1995.

More information

301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] 301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-05: The

More information

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

Contents. I. Classes, Superclasses, and Subclasses. Topic 04 - Inheritance Contents Topic 04 - Inheritance I. Classes, Superclasses, and Subclasses - Inheritance Hierarchies Controlling Access to Members (public, no modifier, private, protected) Calling constructors of superclass

More information

Introduction Basic elements of Java

Introduction Basic elements of Java Software and Programming I Introduction Basic elements of Java Roman Kontchakov / Carsten Fuhs Birkbeck, University of London Module Information Time: Thursdays in the Spring term Lectures: MAL B04: 2

More information

TeenCoder : Java Programming (ISBN )

TeenCoder : Java Programming (ISBN ) TeenCoder : Java Programming (ISBN 978-0-9887070-2-3) and the AP * Computer Science A Exam Requirements (Alignment to Tennessee AP CS A course code 3635) Updated March, 2015 Contains the new 2014-2015+

More information

Program Dynamic Analysis. Overview

Program Dynamic Analysis. Overview Program Dynamic Analysis Overview Dynamic Analysis JVM & Java Bytecode [2] A Java bytecode engineering library: ASM [1] 2 1 What is dynamic analysis? [3] The investigation of the properties of a running

More information

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3]

3/15/18. Overview. Program Dynamic Analysis. What is dynamic analysis? [3] Why dynamic analysis? Why dynamic analysis? [3] Overview Program Dynamic Analysis Dynamic Analysis JVM & Java Bytecode [2] A Java bytecode engineering library: ASM [1] 2 What is dynamic analysis? [3] The investigation of the properties of a running

More information

Concepts of Programming Languages

Concepts of Programming Languages Concepts of Programming Languages Lecture 10 - Object-Oriented Programming Patrick Donnelly Montana State University Spring 2014 Patrick Donnelly (Montana State University) Concepts of Programming Languages

More information

1 Shyam sir JAVA Notes

1 Shyam sir JAVA Notes 1 Shyam sir JAVA Notes 1. What is the most important feature of Java? Java is a platform independent language. 2. What do you mean by platform independence? Platform independence means that we can write

More information

PIC 20A Number, Autoboxing, and Unboxing

PIC 20A Number, Autoboxing, and Unboxing 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

More information

Improving Java Code Performance. Make your Java/Dalvik VM happier

Improving Java Code Performance. Make your Java/Dalvik VM happier Improving Java Code Performance Make your Java/Dalvik VM happier Agenda - Who am I - Java vs optimizing compilers - Java & Dalvik - Examples - Do & dont's - Tooling Who am I? (Mobile) Software Engineering

More information

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department

CS263: Runtime Systems Lecture: High-level language virtual machines. Part 1 of 2. Chandra Krintz UCSB Computer Science Department CS263: Runtime Systems Lecture: High-level language virtual machines Part 1 of 2 Chandra Krintz UCSB Computer Science Department Portable, Mobile, OO Execution Model Execution model embodied by recent

More information

COP 3330 Final Exam Review

COP 3330 Final Exam Review COP 3330 Final Exam Review I. The Basics (Chapters 2, 5, 6) a. comments b. identifiers, reserved words c. white space d. compilers vs. interpreters e. syntax, semantics f. errors i. syntax ii. run-time

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE 1 SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ Java Programming Language Java Introduced in 1995 Object-oriented programming

More information

Problem: Too Many Platforms!

Problem: Too Many Platforms! Compiling for Different Platforms 2 Program written in some high-level language (C, Fortran, ML,...) Compiled to intermediate form Optimized UNDE THE HOOD: THE JAVA VITUAL MACHINE Code generated for various

More information

Exercise 7 Bytecode Verification self-study exercise sheet

Exercise 7 Bytecode Verification self-study exercise sheet Concepts of ObjectOriented Programming AS 2018 Exercise 7 Bytecode Verification selfstudy exercise sheet NOTE: There will not be a regular exercise session on 9th of November, because you will take the

More information

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism

Polymorphism. CMSC 330: Organization of Programming Languages. Two Kinds of Polymorphism. Polymorphism Overview. Polymorphism CMSC 330: Organization of Programming Languages Polymorphism Polymorphism Definition Feature that allows values of different data types to be handled using a uniform interface Applicable to Functions Ø

More information

Chapter 1 Introduction to Java

Chapter 1 Introduction to Java Chapter 1 Introduction to Java 1 Why Java? The answer is that Java enables users to develop and deploy applications on the Internet for servers, desktop computers, and small hand-held devices. The future

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Java and C II. CSE 351 Spring Instructor: Ruth Anderson

Java and C II. CSE 351 Spring Instructor: Ruth Anderson Java and C II CSE 351 Spring 2017 Instructor: Ruth Anderson Teaching Assistants: Dylan Johnson Kevin Bi Linxing Preston Jiang Cody Ohlsen Yufang Sun Joshua Curtis Administrivia Lab 5 Due TONIGHT! Fri 6/2

More information

CLASS DESIGN. Objectives MODULE 4

CLASS DESIGN. Objectives MODULE 4 MODULE 4 CLASS DESIGN Objectives > After completing this lesson, you should be able to do the following: Use access levels: private, protected, default, and public. Override methods Overload constructors

More information

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II

Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Advanced Programming - JAVA Lecture 4 OOP Concepts in JAVA PART II Mahmoud El-Gayyar elgayyar@ci.suez.edu.eg Ad hoc-polymorphism Outline Method overloading Sub-type Polymorphism Method overriding Dynamic

More information

Java Class Loading and Bytecode Verification

Java Class Loading and Bytecode Verification Java Class Loading and Bytecode Verification Every object is a member of some class. The Class class: its members are the (definitions of) various classes that the JVM knows about. The classes can be dynamically

More information

CSC 4181 Handout : JVM

CSC 4181 Handout : JVM CSC 4181 Handout : JVM Note: This handout provides you with the basic information about JVM. Although we tried to be accurate about the description, there may be errors. Feel free to check your compiler

More information

On the Algorithm for Specializing Java Programs with Generic Types

On the Algorithm for Specializing Java Programs with Generic Types On the Algorithm for Specializing Java Programs with Generic Types Daniel Selifonov, Nathan Dahlberg, Elena Machkasova Computer Science Discipline University of Minnesota Morris Morris MN, 56267 selif004,dahlb061,elenam@umn.edu

More information

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time CS2110 Fall 2011 Lecture 25 Java program last time Java compiler Java bytecode (.class files) Compile for platform with JIT Interpret with JVM Under the Hood: The Java Virtual Machine, Part II 1 run native

More information

Code Profiling. CSE260, Computer Science B: Honors Stony Brook University

Code Profiling. CSE260, Computer Science B: Honors Stony Brook University Code Profiling CSE260, Computer Science B: Honors Stony Brook University http://www.cs.stonybrook.edu/~cse260 Performance Programs should: solve a problem correctly be readable be flexible (for future

More information

CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 21, 2018 Last Class Types terminology Haskell s type system Currying Defining types Value constructors Algebraic data

More information

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix PGJC4_JSE8_OCA.book Page ix Monday, June 20, 2016 2:31 PM Contents Figures Tables Examples Foreword Preface xix xxi xxiii xxvii xxix 1 Basics of Java Programming 1 1.1 Introduction 2 1.2 Classes 2 Declaring

More information

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub Lebanese University Faculty of Science Computer Science BS Degree Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub 2 Crash Course in JAVA Classes A Java

More information

Collections, Maps and Generics

Collections, Maps and Generics Collections API Collections, Maps and Generics You've already used ArrayList for exercises from the previous semester, but ArrayList is just one part of much larger Collections API that Java provides.

More information

JSR 292 Cookbook: Fresh Recipes with New Ingredients

JSR 292 Cookbook: Fresh Recipes with New Ingredients JSR 292 Cookbook: Fresh Recipes with New Ingredients John Rose Christian Thalinger Sun Microsystems Overview Got a language cooking on the JVM? JSR 292, a set of major changes to the JVM architecture,

More information

Compilation 2012 Code Generation

Compilation 2012 Code Generation Compilation 2012 Jan Midtgaard Michael I. Schwartzbach Aarhus University Phases Computing resources, such as: layout of data structures offsets register allocation Generating an internal representation

More information

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI

Programming Kotlin. Familiarize yourself with all of Kotlin s features with this in-depth guide. Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI Programming Kotlin Familiarize yourself with all of Kotlin s features with this in-depth guide Stephen Samuel Stefan Bocutiu BIRMINGHAM - MUMBAI Programming Kotlin Copyright 2017 Packt Publishing First

More information

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs.

Argument Passing All primitive data types (int etc.) are passed by value and all reference types (arrays, strings, objects) are used through refs. Local Variable Initialization Unlike instance vars, local vars must be initialized before they can be used. Eg. void mymethod() { int foo = 42; int bar; bar = bar + 1; //compile error bar = 99; bar = bar

More information

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac.

Over-view. CSc Java programs. Java programs. Logging on, and logging o. Slides by Michael Weeks Copyright Unix basics. javac. Over-view CSc 3210 Slides by Michael Weeks Copyright 2015 Unix basics javac java.j files javap 1 2 jasmin converting from javap to jasmin classfile structure calling methods adding line numbers Java programs

More information

Java 8 Programming for OO Experienced Developers

Java 8 Programming for OO Experienced Developers www.peaklearningllc.com Java 8 Programming for OO Experienced Developers (5 Days) This course is geared for developers who have prior working knowledge of object-oriented programming languages such as

More information

generic programming alberto ferrari university of parma

generic programming alberto ferrari university of parma generic programming alberto ferrari university of parma contents generic programming java generic programming methods & generic programming classes & generic programming java with generics generic methods

More information

Collections Algorithms

Collections Algorithms Collections Algorithms 1 / 11 The Collections Framework A collection is an object that represents a group of objects. The collections framework allows different kinds of collections to be dealt with in

More information

ITI Introduction to Computing II

ITI Introduction to Computing II ITI 1121. Introduction to Computing II Marcel Turcotte School of Electrical Engineering and Computer Science Interface Abstract data types Version of January 26, 2013 Abstract These lecture notes are meant

More information

Roadmap. Java: Assembly language: OS: Machine code: Computer system:

Roadmap. Java: Assembly language: OS: Machine code: Computer system: Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: Computer system: get_mpg: pushq movq... popq ret %rbp %rsp, %rbp

More information

Mnemonics Type-Safe Bytecode Generation in Scala

Mnemonics Type-Safe Bytecode Generation in Scala Mnemonics Type-Safe Bytecode Generation in Scala Johannes Rudolph CleverSoft GmbH, Munich Peter Thiemann Albert-Ludwigs-Universität Freiburg, Germany Scala Days 2010, Lausanne, 15.04.2010 Existing bytecode

More information

The Java Virtual Machine

The Java Virtual Machine Virtual Machines in Compilation Abstract Syntax Tree Compilation 2007 The compile Virtual Machine Code interpret compile Native Binary Code Michael I. Schwartzbach BRICS, University of Aarhus 2 Virtual

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

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

Polymorphism. return a.doublevalue() + b.doublevalue(); Outline Class hierarchy and inheritance Method overriding or overloading, polymorphism Abstract classes Casting and instanceof/getclass Class Object Exception class hierarchy Some Reminders Interfaces

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2 CS321 Languages and Compiler Design I Winter 2012 Lecture 2 1 A (RE-)INTRODUCTION TO JAVA FOR C++/C PROGRAMMERS Why Java? Developed by Sun Microsystems (now Oracle) beginning in 1995. Conceived as a better,

More information

Generic programming POLYMORPHISM 10/25/13

Generic programming POLYMORPHISM 10/25/13 POLYMORPHISM Generic programming! Code reuse: an algorithm can be applicable to many objects! Goal is to avoid rewri:ng as much as possible! Example: int sqr(int i, int j) { return i*j; double sqr(double

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 2 Code generation 1: Generating Jasmin code JVM and Java bytecode Jasmin Naive code generation The Java Virtual Machine Data types Primitive types, including integer

More information

Tutorial 3: Code Generation

Tutorial 3: Code Generation S C I E N C E P A S S I O N T E C H N O L O G Y Tutorial 3: Code Generation Univ.-Prof. Dr. Franz Wotawa, DI Roxane Koitz, Stephan Frühwirt, Christopher Liebmann, Martin Zimmermann Institute for Software

More information

The Java Virtual Machine

The Java Virtual Machine The Java Virtual Machine Norman Matloff and Thomas Fifield University of California at Davis c 2001-2007, N. Matloff December 11, 2006 Contents 1 Background Needed 3 2 Goal 3 3 Why Is It a Virtual Machine?

More information

Advances in Programming Languages: Type accuracy

Advances in Programming Languages: Type accuracy Advances in Programming Languages: Type accuracy Stephen Gilmore The University of Edinburgh January 29, 2007 Introduction Strongly typed programming languages catch programmer errors, leading to program

More information

Class, Variable, Constructor, Object, Method Questions

Class, Variable, Constructor, Object, Method Questions Class, Variable, Constructor, Object, Method Questions http://www.wideskills.com/java-interview-questions/java-classes-andobjects-interview-questions https://www.careerride.com/java-objects-classes-methods.aspx

More information

DrStrangebrac<E,T> Or how I learned to stop worrying, and love generics...

DrStrangebrac<E,T> Or how I learned to stop worrying, and love generics... DrStrangebrac Or how I learned to stop worrying, and love generics... a presentation to The Seattle Java Users Group by Wilhelm Fizpatrick http://www.agileinformatics.com/ Java is Sun Microsystems,

More information

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview

Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3. Course Code: GK1965. Overview Fast Track to Core Java 8 Programming for OO Developers (TT2101-J8) Day(s): 3 Course Code: GK1965 Overview Java 8 Essentials for OO Developers is a three-day, fast-paced, quick start to Java 8 training

More information

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08 CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly

More information

JAVA BASICS II. Example: FIFO

JAVA BASICS II. Example: FIFO JAVA BASICS II Example: FIFO To show how simple data structures are built without pointers, we ll build a doubly-linked list ListItem class has some user data first refers to that ListItem object at the

More information

Compiling for Different Platforms. Problem: Too Many Platforms! Dream: Platform Independence. Java Platform 5/3/2011

Compiling for Different Platforms. Problem: Too Many Platforms! Dream: Platform Independence. Java Platform 5/3/2011 CS/ENGD 2110 Object-Oriented Programming and Data Structures Spring 2011 Thorsten Joachims Lecture 24: Java Virtual Machine Compiling for Different Platforms Program written in some high-level language

More information

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1

Agenda. CSE P 501 Compilers. Java Implementation Overview. JVM Architecture. JVM Runtime Data Areas (1) JVM Data Types. CSE P 501 Su04 T-1 Agenda CSE P 501 Compilers Java Implementation JVMs, JITs &c Hal Perkins Summer 2004 Java virtual machine architecture.class files Class loading Execution engines Interpreters & JITs various strategies

More information

Note that this is essentially the same UML as the State pattern! The intent of each of the two patterns is quite different however:

Note that this is essentially the same UML as the State pattern! The intent of each of the two patterns is quite different however: Note that this is essentially the same UML as the State pattern! The intent of each of the two patterns is quite different however: State is about encapsulating behaviour that is linked to specific internal

More information

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University

Day 4. COMP1006/1406 Summer M. Jason Hinek Carleton University Day 4 COMP1006/1406 Summer 2016 M. Jason Hinek Carleton University today s agenda assignments questions about assignment 2 a quick look back constructors signatures and overloading encapsulation / information

More information

15CS45 : OBJECT ORIENTED CONCEPTS

15CS45 : OBJECT ORIENTED CONCEPTS 15CS45 : OBJECT ORIENTED CONCEPTS QUESTION BANK: What do you know about Java? What are the supported platforms by Java Programming Language? List any five features of Java? Why is Java Architectural Neutral?

More information

Exercise 8 Parametric polymorphism November 18, 2016

Exercise 8 Parametric polymorphism November 18, 2016 Concepts of Object-Oriented Programming AS 2016 Exercise 8 Parametric polymorphism November 18, 2016 Task 1 Consider the following Scala classes: class A class B extends A class P1[+T] class P2[T

More information

invokedynamic IN 45 MINUTES!!! Wednesday, February 6, 13

invokedynamic IN 45 MINUTES!!! Wednesday, February 6, 13 invokedynamic IN 45 MINUTES!!! Me Charles Oliver Nutter headius@headius.com, @headius blog.headius.com JRuby Guy at Sun, Engine Yard, Red Hat JVM enthusiast, educator, contributor Earliest adopter of invokedynamic

More information

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Weiss Chapter 1 terminology (parenthesized numbers are page numbers) Weiss Chapter 1 terminology (parenthesized numbers are page numbers) assignment operators In Java, used to alter the value of a variable. These operators include =, +=, -=, *=, and /=. (9) autoincrement

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview Advanced Language Features and Foundations Lecturer : Chin Wei Ngan Email : chinwn@comp.nus.edu.sg Office : S15 06-01 CS6202

More information

SSA Based Mobile Code: Construction and Empirical Evaluation

SSA Based Mobile Code: Construction and Empirical Evaluation SSA Based Mobile Code: Construction and Empirical Evaluation Wolfram Amme Friedrich Schiller University Jena, Germany Michael Franz Universit of California, Irvine, USA Jeffery von Ronne Universtity of

More information

Generic programming in OO Languages

Generic programming in OO Languages CS 242 2012 Generic programming in OO Languages Reading Text: Sections 9.4.1 and 9.4.3 J Koskinen, Metaprogramming in C++, Sections 2 5 Gilad Bracha, Generics in the Java Programming Language Questions

More information

Java: introduction to object-oriented features

Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: introduction to object-oriented features Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer

More information

type conversion polymorphism (intro only) Class class

type conversion polymorphism (intro only) Class class COMP 250 Lecture 33 type conversion polymorphism (intro only) Class class Nov. 24, 2017 1 Primitive Type Conversion double float long int short char byte boolean non-integers integers In COMP 273, you

More information

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles,

Chapter 11 Inheritance and Polymorphism. Motivations. Suppose you will define classes to model circles, Chapter 11 Inheritance and Polymorphism 1 Motivations Suppose you will define classes to model circles, rectangles, and triangles. These classes have many common features. What is the best way to design

More information

5/23/2015. Core Java Syllabus. VikRam ShaRma

5/23/2015. Core Java Syllabus. VikRam ShaRma 5/23/2015 Core Java Syllabus VikRam ShaRma Basic Concepts of Core Java 1 Introduction to Java 1.1 Need of java i.e. History 1.2 What is java? 1.3 Java Buzzwords 1.4 JDK JRE JVM JIT - Java Compiler 1.5

More information

Specialization of Java Generic Types

Specialization of Java Generic Types Specialization of Java Generic Types Sam BeVier, Elena Machkasova University of Minnesota, Morris July 17, 2006 Contents 1 Overview 1 1.1 Introduction.............................. 1 1.2 Specializing Type

More information

Inheritance & Polymorphism

Inheritance & Polymorphism E H U N I V E R S I T Y T O H F R G E D I N B U Murray Cole Classifying Things 1 Hierarchies help us to classify things and understand their similarities and differences Some aspects are common to everything

More information

CSE 431S Final Review. Washington University Spring 2013

CSE 431S Final Review. Washington University Spring 2013 CSE 431S Final Review Washington University Spring 2013 What You Should Know The six stages of a compiler and what each stage does. The input to and output of each compilation stage (especially the back-end).

More information