Architecture of so-ware systems

Similar documents
Effec%ve So*ware. Lecture 9: JVM - Memory Analysis, Data Structures, Object Alloca=on. David Šišlák

Programming Languages and Techniques (CIS120)

Java's Memory Management

Programming Languages and Techniques (CIS120)

Introduction to Programming Using Java (98-388)

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

Garbage collec,on Parameter passing in Java. Sept 21, 2016 Sprenkle - CSCI Assignment 2 Review. public Assign2(int par) { onevar = par; }

CS/B.TECH/CSE(New)/SEM-5/CS-504D/ OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70 GROUP A. (Multiple Choice Type Question)

History of Java. Java was originally developed by Sun Microsystems star:ng in This language was ini:ally called Oak Renamed Java in 1995

CSC Java Programming, Fall Java Data Types and Control Constructs

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

Class, Variable, Constructor, Object, Method Questions

PIC 20A Number, Autoboxing, and Unboxing

Chair of Software Engineering Java and C# in Depth

Language Features. 1. The primitive types int, double, and boolean are part of the AP

type conversion polymorphism (intro only) Class class

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Programming Languages and Techniques (CIS120)

1 Shyam sir JAVA Notes

Encapsula)on, cont d. Polymorphism, Inheritance part 1. COMP 401, Spring 2015 Lecture 7 1/29/2015

301AA - Advanced Programming [AP-2017]

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

Compiling Techniques

Objec,ves. Review: Object-Oriented Programming. Object-oriented programming in Java. What is OO programming? Benefits?

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

INTROSPECTION. We need to begin with a more basic concept called type introspection

Generic programming POLYMORPHISM 10/25/13

Introflection. Dave Landers BEA Systems, Inc.

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

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

Why OO programming? want but aren t. Ø What are its components?

301AA - Advanced Programming

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

Modern Programming Languages. Lecture Java Programming Language. An Introduction

15CS45 : OBJECT ORIENTED CONCEPTS

Short Notes of CS201

Chapter 4: Memory. Taylor & Francis Adair Dingle All Rights Reserved

Programming Languages and Techniques (CIS120)

Genericity. Philippe Collet. Master 1 IFI Interna3onal h9p://dep3nfo.unice.fr/twiki/bin/view/minfo/sofeng1314. P.

CS201 - Introduction to Programming Glossary By

20 Most Important Java Programming Interview Questions. Powered by

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Java Reflection

Assumptions. History

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

JAVA MOCK TEST JAVA MOCK TEST II

The Java Programming Language

Java Security. Compiler. Compiler. Hardware. Interpreter. The virtual machine principle: Abstract Machine Code. Source Code

CSCE 314 Programming Languages

Principles of Programming Languages

301AA - Advanced Programming [AP-2017]

Declarations and Access Control SCJP tips

C++ Overview (1) COS320 Heejin Ahn

Java Primer 1: Types, Classes and Operators

Principles of Programming Languages

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Arrays Classes & Methods, Inheritance

DAD Lab. 1 Introduc7on to C#

Programming Language Concepts: Lecture 10

TeenCoder : Java Programming (ISBN )

Objects and Iterators

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

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

CSE 431S Type Checking. Washington University Spring 2013

From C++ to Java. Duke CPS

Lecture 4: Extending Classes. Concept

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

HAS-A Relationship. If A uses B, then it is an aggregation, stating that B exists independently from A.

Points To Remember for SCJP

CS 61C: Great Ideas in Computer Architecture Strings and Func.ons. Anything can be represented as a number, i.e., data or instruc\ons

B2.52-R3: INTRODUCTION TO OBJECT ORIENTATED PROGRAMMING THROUGH JAVA

Objec+ves. Review. Basics of Java Syntax Java fundamentals. What are quali+es of good sooware? What is Java? How do you compile a Java program?

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CSE Compilers. Reminders/ Announcements. Lecture 15: Seman9c Analysis, Part III Michael Ringenburg Winter 2013

DHANALAKSHMI SRINIVASAN COLLEGE OF ENGINEERING AND TECHNOLOGY ACADEMIC YEAR (ODD SEM)

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CPSC 3740 Programming Languages University of Lethbridge. Data Types

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

Efficient Java (with Stratosphere) Arvid Heise, Large Scale Duplicate Detection

Announcements. Java Review. More Announcements. Today. Assembly Language. Machine Language

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

Java Fundamentals (II)

ITI Introduction to Computing II

Principles of Programming Languages

Example: Count of Points

Lecture 17 Java Remote Method Invoca/on

CS Programming I: ArrayList

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Imports. Lexicon. Java/Lespérance 1. PROF. Y. LESPÉRANCE Dept. of Electrical Engineering & Computer Science

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

F1 A Java program. Ch 1 in PPIJ. Introduction to the course. The computer and its workings The algorithm concept

ITI Introduction to Computing II

Conversions and Overloading : Overloading

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

Java: framework overview and in-the-small features

The Proxy Pattern. Design Patterns In Java Bob Tarr

1. Java is a... language. A. moderate typed B. strogly typed C. weakly typed D. none of these. Answer: B

Chapter 1 Getting Started

Instructor: Randy H. Katz hap://inst.eecs.berkeley.edu/~cs61c/fa13. Fall Lecture #7. Warehouse Scale Computer

Array. Prepared By - Rifat Shahriyar

Transcription:

Architecture of so-ware systems Lecture 13: Class/object ini<aliza<on, class loaders, reflec<on, data structures David Šišlák david.sislak@fel.cvut.cz

Java virtual machine start-up» create ini<al class» must be present in bootstrap class loader» links the ini<al class» cause loading, linking and invoca<on of other classes» ini<alize class (class vs. instance ini<aliza<on!)» start execu<ng public void main (String[]) 2

Loading and Linking» Loading» finding binary form of class or interface (e.g. compu<ng on the fly)» form Class» implemented by ClassLoader» can cache binary representa<ons (can decrypt, verify dig. signature)» prefetch them based on expected usage» load group of related classes together» Linking» binary form into run-<me state in JVM» verifica<on structural check (correct opcodes, branches, )» prepara<on» create sta<c fields, fill default values (no ini<alizers!)» precompute addi<onal data structures (e.g. method table)» resolu<on of symbolic references valida<on, direct reference 3

Class ini9aliza9on» class is being ini9alized in the following cases:» instance of class has to be created» sta<c method of class is invoked» non-constant sta<c field of class is used» sub-class is ini<alized» invoca<on of reflec<ve methods over class» it is ini<al class for start-up» class is not ini9alized» when sta<c final field is ini<alized with compile-<me constant» class ini9aliza9on sequence» super class ini<aliza<on» ini<aliza<on in declara<on order (you cannot use values a-er, compiled into "<clinit>:()v") :» user class sta<c ini<alizers» ini<alizers sta<c fields (class + interfaces by default public sta<c final) 4

Class ini9aliza9on vs. Class instance ini9aliza9on» requires careful synchroniza<on (synchronized on class object)» Class object state» verified and prepared» being ini<alized by some thread» fully ini<alized and ready for use» in error state verifica<on failed, ini<aliza<on failed (throws NoClassDefFoundError)» Class instance ini<aliza<on» memory alloca<on (fields in class + superclasses) -> OutOfMemoryError» all variables are set to default values (0, false, null)» prepare args for other/super constructor invoca<on (follow this( ) and follow super( ))» execute instance ini<alizers + field ini<alizers in declara<on order (compiled into "<init>:()v )» execute the rest of the body of constructor 5

Ini9alizer block» what is the output?? 7

Ini9alizer block» what is the output? 2 3 1 8

Ini9alizer block - alterna9ve» what is the output?? 9

Ini9alizer block - alterna9ve» what is the output? 2 1 10

Ini9alizer block sta9c variant 11

Ini9alizer block sta9c example» what is the output?? 12

Ini9alizer block sta9c example» what is the output?» throws NullPointerExcep<on due to recursive class ini<aliza<on, and auto-unboxing» correct: 24/05/17 A4B77ASS Course 2 13

Classloader» classloader types:» bootstrap class loader» system class loader searches run<me, inst. extension, class path» user-defined class loader» extrac<on from encrypted file, verify digital signature» loading from non-standard sources (e.g. network)» generate on the fly» each class has» defining class loader finally define Class» ini<a<ng class loader ini<ate class loading (e.g. through other CL)» class is uniquely iden<fied by pair!» fully qualified name» defining class loader 14

Classloader» referenced classes from X are loaded by its defining CL» each class is loaded only once if it is not previously unloaded» method Class loadclass(string) - qualified name» a cache implemented by Class findloadedclass(string)» get raw bytes from class from somewhere» if ok, define class from array of bytes using Class defineclass( )» if failed, delegate loading to other class loader» e.g. Class findsystemclass(string)» e.g. getparent().loadclass(string) CL which creates the current one» if s<ll no class, throw ClassNotFoundExcep9on» if resolve is required call void resolveclass(class) to link class» Class Class.forName(String), Class.getSystemClassLoader().loadClass(String)» cl.loadclass(string), c.newinstance(), constructor.newinstance( ) 15

Reflec9on» can examine or modify the run-<me behavior» create external classes by qualified name (through Class loaders) do not need to have class during compila<on» class browser enumerate members» debugger examine private members» BUT» performance overhead dynamic resolu<on, slower» security restric<ons security context, e.g. Applet» unexpected side-effects access private fields and methods 16

Reflec9on» retrieve Class object» Class getclass() returns instance Class representa<on» XXX.class from type, no instance» e.g. aa.class» Class Class.forName(String), CL.loadClass(String)» Class.getSuperClass(), Class.getClasses(), Class.getDeclaredClasses(), Class.getEnclosingClass(), {Field Method Constructor}.getDeclaringClass()» examine class modifiers and types» Class.getModifiers()» Class.getTypeParameters() get Generic types» Class.getGenericInterfaces()» Class.getSuperclass()» Class.getAnnota9ons() 17

Reflec9on» discovering class members 18

Reflec9on» Fields» get field types, generic types» get field modifiers» get and set field value (private if no security manager)» Methods» get method types including apributes» get method modifiers» invoke method» Constructors» find constructor with specific parameters» get constructor modifiers» create new class instance» Arrays (through java.lang.reflect.array)» get array types» create new array» get/set array components 19

Reflec9on» Method call example: Class<T> c = Class.forName("MyClass"); // Class<T> c = MyClass.class; Method m = c.getmethod( mymethod ); Object retval = m.invoke(object, );» Field usage example: Class<T> c = MyClass.class; Field f = c.getfield( myfield ); Object value = f.get(object); 20

Data structures» primi<ves: boolean(1), byte(1), char(2), int(4), long(8), float(4), double(8)» without implicit alloca<on» placed in frame in variables or operand stack» objects (object header structure overhead)» every object is descendant of Object by default» methods clone(), equals, getclass(), hashcode(), wait( ), no<fy ( ), finalize()» objects for primi<ves: Boolean, Byte, Character, Integer, Long, Float, Double; can be null; all are immutable objects (final values)» other objects» arrays» special data structure which store a number of items of the same type in linear order; have the defined limit» JAVA automa<cally check limita<ons» allocated on the heap» mul<-dimensional arrays = arrays of arrays; ragged array 21

Autoboxing, Unboxing» automa<c conversion from primi<ve to object representa<on and vice versa» since JAVA 5» for example» autoboxing for Integer is based on valueof(int) and intvalue() methods? 23

Autoboxing, Unboxing» automa<c conversion from primi<ve to object representa<on and vice versa» since JAVA 5» for example» autoboxing for Integer is based on valueof(int) and intvalue() methods» works only during assignment or parameter passing? 24

Autoboxing, Unboxing» automa<c conversion from primi<ve to object representa<on and vice versa» since JAVA 5» for example» autoboxing for Integer is based on valueof(int) and intvalue() methods» works only during assignment or parameter passing» example: count word frequency/histogram» boxing and un-boxing brings inefficiencies! 25

Example» what is the output? and what is the output for i=2000 and j=2000?? 26

Example» what is the output? and what is the output for i=2000 and j=2000? true true true true false true» but not a-er serializa<on, there is no readresolve! A4B77ASS Lecture 13 24/05/17 27

Integer usage of iden9ty seman9cs» similar concept as in mul<ton; Integer itself is Immutable (final) 28

Integer 29

Widening vs. autoboxing» what are the outputs?? 30

Widening vs. autoboxing» what are the outputs? long Integer» why? prefer widening cannot use autoboxing before autoboxing to widen primi<ves -> error if no hello(integer) method 31

Example» what is the outputs?? 32

Example» what is the outputs? 100 - because we are removing Integers instead of Short!!» correct: 33

Method overloading» method is iden<fied by its signature» can be compiled and what is the output?? 34

Method overloading» method is iden<fied by its signature» can be compiled and what is the output? YES no ambiguity method with parameter type String» due to JLS specifica<on: The Java programming language uses the rule that the most specific method is chosen. 35

Method overloading» can be compiled and what is the output?? 36

Method overloading» can be compiled and what is the output?» NO cannot find most specific, both are sub-classes of Object but not in the same inheritance hierarchy 37

Method overloading» can be compiled and what is the output?? 38

Method overloading» can be compiled and what is the output? YES method with param types String, Object 39

Method overloading» BUT» this cannot be compiled cannot iden<fy most specific 40

Method overloading» can be compiled and what is the output?? 41

Method overloading» can be compiled and what is the output? YES Collec9on - compile <me resolu<on not run-<me type 42