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

Similar documents
Introduction to Programming Using Java (98-388)

Java: framework overview and in-the-small features

Zhifu Pei CSCI5448 Spring 2011 Prof. Kenneth M. Anderson

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

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

Compiling Techniques

Computational Expression

Praktische Softwaretechnologie

Operators and Expressions

3. Java - Language Constructs I

CS 11 java track: lecture 1

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

Special Topics: Programming Languages

Programming. Syntax and Semantics

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

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Values and Variables 1 / 30

1 Shyam sir JAVA Notes

SOFTWARE ARCHITECTURE 7. JAVA VIRTUAL MACHINE

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

Java Programming. Atul Prakash

Introduction to Programming (Java) 2/12

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

Java Overview An introduction to the Java Programming Language

CSC Java Programming, Fall Java Data Types and Control Constructs

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Java Primer 1: Types, Classes and Operators

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

Language Fundamentals Summary

Pace University. Fundamental Concepts of CS121 1

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

Assumptions. History

Data Types, Variables and Arrays. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

PROGRAMMING FUNDAMENTALS

Program Fundamentals

CSC 1214: Object-Oriented Programming

The Java Programming Language

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

GUJARAT TECHNOLOGICAL UNIVERSITY

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

JAVA MOCK TEST JAVA MOCK TEST II

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Points To Remember for SCJP

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

Strings and Loops CSE moodle.yorku.ca. moodle.yorku.ca CSE 1020

Full file at

Java is a high-level programming language originally developed by Sun Microsystems and released in Java runs on a variety of

CS11 Java. Fall Lecture 1

CSE115 / CSE503 Introduction to Computer Science I. Dr. Carl Alphonce 343 Davis Hall Office hours:

CSc 453 Interpreters & Interpretation

Index COPYRIGHTED MATERIAL

WA1278 Introduction to Java Using Eclipse

Core JAVA Training Syllabus FEE: RS. 8000/-

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Core Java Syllabus. Overview

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

Short Notes of CS201

Full file at

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

CS260 Intro to Java & Android 03.Java Language Basics

Primitive Data Types: Intro

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

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

CS201 - Introduction to Programming Glossary By

Portable Resource Control in Java: Application to Mobile Agent Security

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

Decaf Language Reference Manual

Under the Hood: The Java Virtual Machine. Problem: Too Many Platforms! Compiling for Different Platforms. Compiling for Different Platforms

Objects and Iterators

Static Program Analysis

Type Conversion. and. Statements

Java Notes. 10th ICSE. Saravanan Ganesh

Java Fundamentals (II)

CS/B.TECH/CSE(OLD)/SEM-6/CS-605/2012 OBJECT ORIENTED PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

CMPT 125: Lecture 3 Data and Expressions

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 definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

NOOTAN PADIA ASSIST. PROF. MEFGI, RAJKOT.

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Programming Language Concepts: Lecture 2

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

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

Java Fall 2018 Margaret Reid-Miller

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

IC Language Specification

COMP 202 Java in one week

Programming overview

CS321 Languages and Compiler Design I. Winter 2012 Lecture 2

Class, Variable, Constructor, Object, Method Questions

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

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.

Module - 2 Overview of Java History of Java Technology?

Portable Resource Control in Java The J-SEAL2 Approach

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Language Reference Manual simplicity

Class definition. complete definition. public public class abstract no instance can be created final class cannot be extended

Compaq Interview Questions And Answers

Transcription:

Java TM Introduction Renaud Florquin Isabelle Leclercq FloConsult SPRL http://www.floconsult.be mailto:info@floconsult.be

Java Technical Virtues Write once, run anywhere Get started quickly Write less code Write better code Develop programs more quickly Avoid platform dependencies with 100% pure Java Distribute software more easily 2

Java: History (1) J2ME 1.2 Java 2 Platform... oak Java 1.0 J2EE Java 1.2 J2SE (Java) 1.2 J2SE 1.3 J2EE 1.3 J2SE 1.4 J2EE 1.4 1996 1997 1998 1999 2000 2001 2002 2003 2004 3

Java: Application production Compile AND interpret Compiler makes Java Bytecodes (*.class) Interpreter (JVM) executes Bytecodes myprogram.java class myprogram {... } javac java Java Virtual Machine myprogram.class 03 3b 84 00 01 1a 05 68 3b a7 ff f9 istore_0 iinc 0, 1 iload_0 iconst_2 imul istore_0 goto -7 OS 4

Java Virtual Machine Interprets Java Byte Code myprogram.class 03 3b 84 00 01 1a 05 68 3b a7 ff f9 Class loader Class verifier JIT istore_0 iinc 0, 1 iload_0 iconst_2 imul istore_0 goto -7 Primordial Class loader Heap Execution Engine Native Method Loader Native Method Area Security Manager OS 5

Compilation phases myprogram.java class myprogram {... } tokens Parser Syntax tree Scanner Constrainer Code Generator Compiler Symbol Table position int...... myprogram.class 03 3b 84 00 01 1a 05 68 3b a7 ff f9 istore_0 iinc 0, 1 iload_0 iconst_2 imul istore_0 goto -7 6

Compilation phases selection-statement if (logical-expression) statement expression ; if ( logical-expression ) expression-statement Parser assignment-expression ; a <= 10 val = a Scanner if ( a <= 10 ) val = a ; i f ( a < = 1 0 ) v a l = a ; 7

Types Java programming language is a strongly typed language Everything that has a value also has a type. In Java there are two kinds of types: Primitive Types (Built-in data types). Reference Types (Everything else, including objects) 8

Primitive Types Primitive boolean char byte Description Either true or false 16-bit Unicode character ('\u0000'.. '\uffff') 8-bit signed two's complement integer [-2 7.. +2 7-1] Wrapper class Boolean Character Byte short int long float double 16-bit signed two's complement integer[-2 15.. +2 15-1] 32-bit signed two's complement integer[-2 31.. +2 31-1] 64-bit signed two's complement integer[-2 63.. +2 63-1] 32-bit IEEE 754-1985 floating-point number 64-bit IEEE 754-1985 floating-point number Short Integer Long Float Double 9

Reference Types Everything that is not a primitive type is a reference type Three kinds of reference types: Classes String java.lang.integer Interfaces Serializable Arrays int[] String[] 10

Array of What? You can have an array of anything, but all the elements of an array are of the same type. any primitive type. any reference type. You need to declare the type of an array (the type of each element). You also need to explicitly create an array just declaring the type is not enough. 11

Creating an Array Use the new operator: foo = new int[4]; We can combine the declaration and creation int[] foo = new int[4]; foo foo[0] foo[1] foo[2] foo[3] 12

Creating an Array Reference type: names = new String[4]; // or String[] names = new String[4]; How many objects are created? names names[0] names[1] names[2] names[3] null null null null 13

Array Length The length of an array is available as a field of the array. byte[] buffer = new byte[100]; for (int i=0; i < buffer.length; i++) { } buffer[i] = i; 14

Array Initializers You can use an array literal to create an array you assign the literal to the array variable. Example: Array Initialializers int[] foo = {4, 3, 2, 1, 0}; for (int i =0; i < foo.length; i++) { System.out.println("Foo[" + i + "]=" + foo[i]); } int[][] pascalstriangle = { { 1 }, { 1, 1}, { 1, 2, 1}, { 1, 3, 3, 1}, }; 15

Initial Values of Variables For the class variables and instance variables: boolean char byte, short, int, long float false '\u0000' Object reference null Parameters Provided by the invoker 0 +0.0f double +0.0 Local Variable Must be explicit (verified by the compiler) 16

Variables public static final int maxnumber = 42 ; modifiers type identifier initializer public void foo(final int maxnumber ) {...} 17

Assignment Expressions General form lvalue = expression Overwrites the current value of an object (lvalue, l standing for left) with a new value (expression) The type of the expression must match the type of the object (caution: implicit conversion) The type and the value of an Assignment expression are the type and the value of the lvalue 18

Assignment Expressions Compound assignment int i = 0; lvalue op= expression Equivalent to: lvalue = lvalue op expression Example: i = i + 3; i += 2; // shortcut assignment a = b = 2; 19

Widening Implicit conversions double 64 bits float 32 bits long 64 bits int 32 bits short 16 bits byte 8 bits Incompatible char 16 bits No conversion for booleans 20

Conversion during numeric ops For unary operators: ++, --: no conversion +, -, ^: byte, short, char promoted to int For binary operators: If one operand double: other promoted double before operation Else, if one is float: other promoted float before operation Else, if one is long: other promoted long before operation Else, both promoted int before operation 21

Object ref. implicit conversion Super interface Super class Interface Sub class Interface Class 22

Conversion to String All objects may be converted to a String Implicit call of tostring() e.g. String s = + 2 s = 2, type String 23

Expression evaluation Operator Precedence []. () expr++ expr-- st priority ++expr --expr +expr -expr ~! new * / %... = +=... st priority 3 + 4/2 * 10 =? (3 + 4/2) * 10 =? 24

Compound Statements Example 1 2 3 4 public class Test { public void amethod() { int j = 2; // j is defined for level 2-3-4 { int i = 0; // i is defined for level 3-4 while (i < 10) { System.out.println("Value: " + i*j); i++; } } } } 25

Class Definition Class Members Fields (= attributes) Methods Nested classes and interfaces Class Modifiers public: publicly accessible otherwise only accessible within its own package abstract: incomplete class (no instance) final: cannot be subclassed Window + position: Point = (0,0) - visible: boolean + display() + create(position: Point) 26

Access control (2) private Default (package) protected public 27

Modifiers Modifiers Class Package Subclass private No specifier protected public World static final transient Volatile Class variable (shared by every instance) The variable's value cannot be changed The variable is not part of the persistent state (should not be serialized). The variable can be changed asynchronously. 28

static Fields One instance of a field shared by all objects of a class (class variable) How to access a static field? Via the class name Via a reference to an object Example public class Window { public static nbrwindow;... } System.out.println(Window.nbrWindow); Window w = new Window(); System.out.println(w.nbrWindow); 29

final Fields Immutable property of a class (static+final) Initialization Via Initializer Via static initialization block (see further) Immutable property of an object (final). Initialization Via Initializer Constructor Via initialization block (see further) 30

Inheritance Example (1) Example class Node { private final String name; private Object value = null; public Node(String name, Object value) {...} public String getname() { return name; }... } class ColorNode extends Node { private Color c; public ColorNode(String name, Object value, Color c) { super(name, value); } this.c = c; } public Color getcolor() { return c; }... Inheritance Superclass constructor invocation 31

Inheritance Example (2) ColorNode adds attributes and methods to Node: Node + name:string + value : Object + getname(): String ColorNode ColorNode + c: Color + getcolor(): Color + name:string + value : Object + c: Color + getname(): String + getcolor(): Color 32

No multiple inheritance No multiple inheritance of classes in Java But: multiple inheritance of interfaces allowed <<Interface>> 33

Interface Keywords summary <<interface>> Window + position: Point = (0,0) Visibility: public (implicit), (package) Others: abstract (implicit) Visibility: public (implicit) Others: static (implicit), final (implicit) + display() + create(position: Point) Visibility: public (implicit) Others: abstract (implicit) 34

Inner Classes Example public class Stack { private Vector items;... public Iterator getiterator() { return new StackIterator(); } class StackIterator implements Iterator { int currentitem = items.size() - 1; public boolean hasnext() {...}... } } astack:stack :StackIterator Iterator 35

Bibliography The Java Program m ing Language 3 rd Edition by Ken Arnold, James Gosling, David Holmes; 705 p, June 2000, Addison-Wesley, ISBN 0-20170-433-1 Java(TM) Language Specification 2 d Edition by Bill Joy, Guy Steele, James Gosling, Gilad Bracha; 544 p, June 2000, Addison-Wesley, ISBN 0-20131-008-2 The Java Tutorial 3 d Edition by Mary Campione and Kathy Walrath; 580 p, January 2000, Addison-Wesley, ISBN 0-20170-393-9 The JDK 1.4 Tutorial by Gregory Travis; 408 p, March 2002, Manning publications Company, ISBN 1930110456 The Java(TM) Developers Alm anach 4 th Edition, volum e 1 by Patrick Chan; 1024 p, March 2002, Addison-Wesley, ISBN 0-20175-280-8 36