Java: framework overview and in-the-small features

Similar documents
C#: framework overview and in-the-small features

Introduction to Programming Using Java (98-388)

Java language. Part 1. Java fundamentals. Yevhen Berkunskyi, NUoS

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

Java TM Introduction. Renaud Florquin Isabelle Leclercq. FloConsult SPRL.

Compiling Techniques

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

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Computer Components. Software{ User Programs. Operating System. Hardware

Index COPYRIGHTED MATERIAL

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

Run-time Program Management. Hwansoo Han

CSC 1214: Object-Oriented Programming

3. Java - Language Constructs I

CSc 453 Interpreters & Interpretation

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

Java Programming. Atul Prakash

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

Compiler construction 2009

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Operators and Expressions

Java Overview An introduction to the Java Programming Language

2 rd class Department of Programming. OOP with Java Programming

WA1278 Introduction to Java Using Eclipse

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Static Program Analysis

Java Fundamentals (II)

1 Shyam sir JAVA Notes

Computer Components. Software{ User Programs. Operating System. Hardware

Introduction to Programming (Java) 2/12

COMP 202 Java in one week

B.V. Patel Institute of BMC & IT, UTU 2014

CS313D: ADVANCED PROGRAMMING LANGUAGE

Core Java - SCJP. Q2Technologies, Rajajinagar. Course content

Language Fundamentals Summary

CSC Java Programming, Fall Java Data Types and Control Constructs

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Program Fundamentals

3. Java - Language Constructs I

Objectives. Problem Solving. Introduction. An overview of object-oriented concepts. Programming and programming languages An introduction to Java

More Programming Constructs -- Introduction

Assumptions. History

High-Level Language VMs

Short Notes of CS201

Core JAVA Training Syllabus FEE: RS. 8000/-

Programming. Syntax and Semantics

Java: introduction to object-oriented features

CS201 - Introduction to Programming Glossary By

: Primitive data types Variables Operators if, if-else do-while, while, for. // // First Java Program. public class Hello {

Java Primer 1: Types, Classes and Operators

CSCE 314 Programming Languages

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Introduce C# as Object Oriented programming language. Explain, tokens,

CS 231 Data Structures and Algorithms, Fall 2016

CS11 Java. Fall Lecture 1

Java Notes. 10th ICSE. Saravanan Ganesh

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

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

The Sun s Java Certification and its Possible Role in the Joint Teaching Material

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

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

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

Java Fundamentals p. 1 The Origins of Java p. 2 How Java Relates to C and C++ p. 3 How Java Relates to C# p. 4 Java's Contribution to the Internet p.

PROGRAMMING FUNDAMENTALS

Java Language Basics: Introduction To Java, Basic Features, Java Virtual Machine Concepts, Primitive Data Type And Variables, Java Operators,

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

STRUCTURING OF PROGRAM

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

Programming Lecture 3

Java Class Loading and Bytecode Verification

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

02 B The Java Virtual Machine

Pace University. Fundamental Concepts of CS121 1

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

CHETTINAD COLLEGE OF ENGINEERING & TECHNOLOGY JAVA

Question. 1 Features of Java.

Chapter 1 GETTING STARTED. SYS-ED/ Computer Education Techniques, Inc.

CS5015 Object-oriented Software Development. Lecture: Overview of Java Platform. A. O Riordan, 2010 Most recent revision, 2014 updated for Java 8

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Objects and Iterators

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

CMPSC 497: Java Security

Chapter 2. Elementary Programming

Core Java Syllabus. Overview

CSE 452: Programming Languages. Outline of Today s Lecture. Expressions. Expressions and Control Flow

DM550 / DM857 Introduction to Programming. Peter Schneider-Kamp

A web-based IDE for Java

.Net Technologies. Components of.net Framework

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: reflection

15CS45 : OBJECT ORIENTED CONCEPTS

Running class Timing on Java HotSpot VM, 1

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

Course information. Petr Hnětynka 2/2 Zk/Z

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

Review of the C Programming Language for Principles of Operating Systems

The Java Language Implementation

Transcription:

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: framework overview and in-the-small features

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: framework overview

What s in a name Initially was Oak (James Gosling, 1991), then Green Ruled out by the trademark lawyers Twelve people locked in a room together with a naming consultant How does this thing make you feel? What else makes you feel that way? After listing and sorting, 12 names were sent to the lawyers #1 was Silk Gosling s favorite was Lyric (#3) Java was # 4 Version 1.0: 1995, latest stable version: 7 Update 51 (14.1.14) Coming next (Java SE 8, 18.3.2014): lambda expressions (closures) embedded JavaScript 3

Java platform goals Write Once, Run Anywhere Built-in security Automatic memory management API + documentation generation Object-Oriented Familiar C/C++ syntax 4

Write once, run anywhere.java compiler.class/.jar JVM network Class loader Bytecode verifier Interpreter JIT compiler exec H w 5

Bytecode Intermediate format resulting from Java compilation Instruction set of an architecture that is stack-oriented (no registers) provides capability (object access rights) 1 bytecode instruction = 1 byte Executed by any platform-specific Virtual Machine (VM) 6

Bytecode format JVM loads class file gets a stream of bytecodes One bytecode instruction: opcode + 0 operands Each opcode is associated with a mnemonic 03 iconst_0 // pushes int 0 on stack 3b istore_0 // pops int from stack to local in pos 0 84 00 01 iinc 0, 1 // increments local in pos 0 by 1 1a iload_0 // pushes int from local in pos 0 on stack 05 iconst_2 // pushes int 2 on stack 68 imul // pops 2 int values, multiplies them and puts the result on the stack 7

Example of bytecode translation class SimpleMath{ byte inflexible_add(){ byte x = 2; byte y = 2; byte z = (byte) (x + y); return z; } } 8

Bytecode example Opcode mnemonics iconst_2 istore_1 iconst_2 istore_2 iload_1 iload_2 iadd int2byte istore_3 iload_3 ireturn Meaning push an integer constant 2 into the stack pop into local in pos 1 (x) push an integer constant 2 into the stack pop into local in pos 2 (y) push x into the stack push y into the stack sum the two top values on the stack and push the result convert result into byte pop into local in pos 3 (z) push z into the stack return result (z) 9

JVM overview.java compiler.class.jar JVM network Class loader Bytecode verifier Interpreter JIT compiler exec H w 10

Security: language restrictions and support No pointers, no explicit memory de-allocation Checked type casts (at compile time and runtime) Enforced array bounds (at runtime) Security APIs SecurityManager (standard security) XML digital signature, Public Key Infrastructure, cryptographic services, authentication 11

Security: class loaders Take care of files and file systems Locate libraries and dynamically load classes Partition classes into realms (e.g. local machine, local network, all the rest) and restrict what they can do 12

Security: Bytecode verifier Verifier checks bytecode using a theorem prover Branches always to valid locations Data always initialized Types of parameters of bytecode instructions always correct Data and methods access checked for visibility Arbitrary bit patterns cannot get used as an address No operand stack overflows and underflows 13

JVM: code generation.java compiler.class.jar JVM network Class loader Bytecode verifier Interpreter JIT compiler exec H w 14

Code generation: HotSpot The interpreter is the software CPU of the JVM Examines each bytecode and executes a unique native procedure No native code is produced A JIT compiler converts the bytecode into native code just before running it Keeps a log (cache) of the native code that it has to run to execute each bytecode May optimize substituting often occurring short sets of instructions ( hot spots ) with shorter/faster ones Like the back-end of a traditional compiler, the java compiler being the front-end HotSpot is the default SUN JVM since 2000 15

HotSpot client and server HotSpot client VM For platforms typically used for client applications (e.g. GUI) Tuned for reducing start-up time and memory footprint Invoked by using client when launching an app HotSpot server VM For all platforms Tuned for max program execution speed Invoked by using server when launching an app Both use an interpreter to launch applications, and an adaptive compiler optimizing code hot spots They use different code inline policies and heap defaults 16

JVM Overview.java compiler.class.jar JVM network Class loader Bytecode verifier Interpreter JIT compiler exec H w 17

JVM: more features Automated exception handling Provides root cause debugging info for every exception Responsible for garbage collection Ships as JRE (VM + libraries) Can have other languages run on top of it, e.g. JRuby (Ruby) Rhino (JavaScript) Jython (Python) Scala From 6.0 scripting languages can be mixed with Java code 18

Command-line Java Compile javac MainClass.java Execute java MainClass Generate documentation javadoc MainClass.java Generate an archive from.class files in current dir jar cf myarchive.jar *.class 19

Chair of Software Engineering Carlo A. Furia, Marco Piccioni, Bertrand Meyer Java: in-the-small language features

Encoding and formatting Uses unicode as encoding system: www.unicode.org Free format Blanks, tabs, new lines, form feeds are only used to keep tokens separate Comments Single line: //Single line comment Multiple lines: /* non-nested, multi-line comment*/ Javadoc comment: /** processed by javadoc */ 21

Identifiers No restriction on length Case sensitive Cannot start with a digit Cannot include / or - Cannot be a keyword 22

Annotations Meta-data about programs Compiler flags e.g: @Deprecated, @Override, @SuppressWarnings Information that can be used for compilation (or other forms of code analysis) e.g.: @Inherited, application-defined such as @RevisionId Some runtime processing e.g.: application-defined 23

Keywords abstract double int super boolean else interface switch break extends long synchronized byte final native this case finally new throw catch float package throws char for private transient class (goto) protected try (const) if public void continue implements return volatile default import short while do instanceof Literals null, true, false are also reserved 24

Operators Access, method call:., [], () Postfix: expr++, expr-- (R to L) Other unary: ++expr, --expr, +, -, ~,!, new, (atype) Arithmetic: *, /, % Additive: +, - Shift: <<, >>, >>> Relational: <, >, <=, >=, instanceof Equality: ==,!= Logical (L to R): &, ^,, &&, Ternary: condition? (expr1):(expr2) (R to L) Assignment: =, +=, -=, *=, /=, %=, &=, ^=, =, <<=, >>=, >>>= Precedence: from top to bottom Tip: don t rely too much on precedence rules: use parentheses 25

Type system basics Primitive types boolean, byte, short, int, long, char, float, double Reference types class, interface, [] null Automatic widening conversions (no precision loss) byte to short to int to long char to int, int to double, float to double Automatic widening conversions (possible precision loss) int to float, long to float, long to double A cast is required for narrowing conversions int i = 3; long j = 5; i = (int) j 26

Widening conversions with precision loss float g(int x){ return x; }... int i = 1234567890; float f = g(i); System.out.println(i - (int)f) // output: -46... 27

Wrapper types and autoboxing For each primitive type there is a wrapper type Boolean, Byte, Short, Integer, Long, Character, Float, Double Starting from 5.0, autoboxing provides automatic conversions between primitive and wrapper types Pro: reduces code complexity Cons: not efficient, sometimes unexpected behavior 28

Some surprises of autoboxing new Integer(7).equals(7) // true new Long(7).equals(7) //false. True if equals(7l) new Integer(7).equals(new (Long(7))) // false new Integer(7) == 7 // true new Long(7) == 7 // true new Integer(7) == new Long(7) // compiler error 29

Control flow: conditional branch Same syntax as in C/C++ if (booleanexpr) { // do something } else // else is optional { // do something else } 30

Control flow: loops while (booleanexpr) { // execute body // until booleanexpr becomes false } do { // execute body (at least once) // until booleanexpr becomes false } while (booleanexpr); 31

Control flow: for loop for (int i=0; i < n; i++) { // execute loop body n times } // equivalent to the following int i=0; while (i < n) { // executes loop body n times i++; } 32

Control flow: enhanced for loop Introduced in Java 5.0 for (variable : collection) { // loop body } collection is an array or an object of a class that implements interface Iterable more on classes and interfaces later Executes the loop body for every element of the collection, assigned iteratively to variable 33

Control flow: switch selector switch (Expr) { case Value1: instructions; break; case Value2: instructions; break; //... default: instructions; } Expr can be of type: byte, short, int, char (or wrapped counterparts) enum types String (compared with equals) (new in Java 7) 34

Breaking the control flow: break label: [while do for] Identifies a loop (Or a code block) break optionallabel; Within a loop or a switch No label: exit the loop or switch With label: within loop: jump out of the loop to label optionallabel within switch: jump out of switch block to label optionallabel 35

Breaking the control flow: continue label: [while do for] Identifies a loop (Or a code block) continue optionallabel; Within a loop No label: skip the remainder of the current iteration and continue with the next iteration With label: skip the remainder of the current iteration and continue with the next iteration of the loop with label optionallabel 36