Java Fundamentals Marco Piccioni Course Goals Teach Java to - - PowerPoint PPT Presentation

java fundamentals
SMART_READER_LITE
LIVE PREVIEW

Java Fundamentals Marco Piccioni Course Goals Teach Java to - - PowerPoint PPT Presentation

Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals Marco Piccioni Course Goals Teach Java to programmers having previous programming knowledge/experience Show


slide-1
SLIDE 1

Languages in Depth Series: Java Programming

  • Prof. Dr. Bertrand Meyer

Chair of Software Engineering

Java Fundamentals

Marco Piccioni

slide-2
SLIDE 2

2

Languages in Depth series: Java Programming

Course Goals

  • Teach Java to programmers having previous

programming knowledge/experience

  • Show (hopefully!) useful stuff
  • Illustrate some dark corners
  • Practice (exercise sessions, project)
slide-3
SLIDE 3

3

Languages in Depth series: Java Programming

Roadmap

  • Java language basics
  • GUI
  • Testing framework: JUnit
  • Enterprise framework: Spring
  • Eclipse/Jazz
  • Threads
  • Reflection
  • Sockets
  • Persistence: serialization, RDBMS, OODBMS
  • Dynamic loading, byte code, JIT compilation
  • Java Virtual Machine
  • Middleware
slide-4
SLIDE 4

4

Languages in Depth series: Java Programming

Useful info

Assistant pool

  • Christoph Angerer
  • Tomas Hruz
  • Adriana Ispas
  • Andrei Vancea

Lectures: Thursday 14:15-16:00 Room ML E12 Exercise sessions: Wednesday 16:15-17:00

  • Exercises will be corrected, not graded
  • Please sign-up during the break or at end of lecture
  • Rooms: IFW D42, LFV E41, LFW C4, LFW E15

Final exam: May 29th

  • 50% grade: written exam (yes books/notes, no electronic devices)
  • 50% grade: project
slide-5
SLIDE 5

5

Languages in Depth series: Java Programming

Course Docs

Slides

  • Lecture slides
  • Exercise sessions slides
  • Lecture notes

Course Web page

  • http://se.inf.ethz.ch/teaching/2008-S/0284/index.html
  • Links to reading material

Course wiki

  • http://javacourse-ss2008.origo.ethz.ch/wiki/javacourse-ss2008
  • Q & A
  • Project-related material
  • Create an http://www.origo.ethz.ch/ account and send it to your

assistant

slide-6
SLIDE 6

6

Languages in Depth series: Java Programming

Different isn’t always better, but better is always different

Initially was “Oak” (James Gosling, 1991), then “Green”

  • Ruled out by the trademark layers

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 layers

  • #1 was “Silk”
  • Gosling’s favorite was “Lyric” (#3)
  • “Java” was # 4

Version 1.0: 1995

slide-7
SLIDE 7

7

Languages in Depth series: Java Programming

Intended language goals

  • Write Once, Run Anywhere
  • Built-in security
  • Automatic memory management
  • API + documentation generation
  • Object-Oriented (mostly)
  • Familiar C/C++ syntax
slide-8
SLIDE 8

8

Languages in Depth series: Java Programming

Write once, run anywhere

.java compiler .class .jar network H w Class loader Bytecode verifier JIT compiler Interpreter

JVM

slide-9
SLIDE 9

9

Languages in Depth series: Java Programming

Byte code

  • Intermediate format resulting from Java compilation
  • Instruction set of a stack-oriented, capability architecture
  • 1 bytecode instruction = 1 byte
  • Executed by any platform-specific Virtual Machine (VM)
  • Code generation can either happen via an interpreter or

a JIT compiler

slide-10
SLIDE 10

10

Languages in Depth series: Java Programming

JVM overview

.java compiler .class .jar network H w Class loader Bytecode verifier JIT compiler Interpreter

JVM

slide-11
SLIDE 11

11

Languages in Depth series: Java Programming

Built-in security

1.

Language

  • Restricted: no pointers, no explicit memory de-allocation,

checked type casts, enforced array bounds

  • Security API (XML digital signature and cryptographic

services from 6.0)

2.

Class loaders

  • Take care of files and file systems
  • Locate libraries and dynamically load classes
  • Partition classes into realms and restrict what they can do

3.

Verifier checks bytecode using a theorem prover

  • Branches always to valid locations
  • Data always initialized and references always type-safe
  • Data and methods access checked for visibility
  • Arbitrary bit patterns cannot get used as an address
slide-12
SLIDE 12

12

Languages in Depth series: Java Programming

JVM: code generation

.java compiler .class .jar network H w Class loader Bytecode verifier JIT compiler Interpreter

JVM

slide-13
SLIDE 13

13

Languages in Depth series: Java Programming

Code generation: HotSpot

  • An interpreter is the software CPU of the JVM
  • Examines each bytecode and executes a unique action
  • A JIT “compiler” converts the interpreted bytecode into

native code just before running it

  • Keeps a log of the native code that it has to run to interpret

each bytecode

  • Then optimizes substituting a short set of instructions with

a shorter/faster one

  • It’s like the back end of a traditional compiler, the java

compiler being the front end

  • HotSpot is the default SUN JVM since 2000
slide-14
SLIDE 14

14

Languages in Depth series: Java Programming

JVM Overview

.java compiler .class .jar network H w Class loader Bytecode verifier JIT compiler Interpreter

JVM

slide-15
SLIDE 15

15

Languages in Depth series: Java Programming

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
  • JRuby (Ruby)
  • Rhino (JavaScript)
  • Jython (Python)
  • From 6.0 scripting languages can be mixed with Java
slide-16
SLIDE 16

16

Languages in Depth series: Java Programming

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: /* not nested, multi-line

comment*/

  • Javadoc comment: /** extracted by javadoc*/
slide-17
SLIDE 17

17

Languages in Depth series: Java Programming

Identifiers

  • No restriction on length
  • Case sensitive
  • Cannot start with a digit
  • Cannot include “/” or “-”
  • Cannot be a keyword
slide-18
SLIDE 18

18

Languages in Depth series: Java Programming

Keywords

  • Literals null, true, false are also reserved

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

slide-19
SLIDE 19

19

Languages in Depth series: Java Programming

Operators

  • Access, method call: ., [], ()
  • Postfix: expr++, expr--
  • Other unary: ++expr, --expr, +, -, ~, !, new, (aType)
  • Arithmetic: *, /, %
  • Additive: +, -
  • Shift: <<, >>, >>>
  • Relational: <, >, <=, >=, instanceof
  • Equality: ==, !=
  • Logic (precedence from left to right): &, ^, |, &&, ||
  • Ternary: condition ? (expr1):(expr2)
  • Assignment: =, +=, -=, *=, /=. %=, &=, ^=, |=, <<=,

>>=, >>>=

  • Precedence: from top to bottom
  • Tip: don’t learn precedence rules: use parenthesis ;)
slide-20
SLIDE 20

20

Languages in Depth series: Java Programming

A non-uniform type system

  • 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 (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

slide-21
SLIDE 21

21

Languages in Depth series: Java Programming

Quiz

What will be printed? double double d1=4.0; double double d2=3.1; System.out.println("4.0 - 3.1 =" + (d1 - d2));

slide-22
SLIDE 22

22

Languages in Depth series: Java Programming

Quiz solution

double double d1=4.0; double double d2=3.1; System.out.println("4.0 - 3.1 =" + (d1 - d2)); // 4.0-3.1=0.8999999999999999

  • Negative powers of 10 cannot be represented exactly as

finite length binary fractions

  • float and double provide good approximations, but

are not suited for exact calculations

  • Tip: Use BigDecimal instead
slide-23
SLIDE 23

23

Languages in Depth series: Java Programming

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
slide-24
SLIDE 24

24

Languages in Depth series: Java Programming

Quiz

Let’s suppose we are using the 1.5 compiler. What will be printed? Integer i1 = 123; Integer i2 = 123; Integer i3 = 1234; Integer i4 = 1234; System.out.println(i1==i2); System.out.println(i3==i4);

slide-25
SLIDE 25

25

Languages in Depth series: Java Programming

Quiz solution

  • Normally JVM creates a new object.
  • For some special cases, like int between -128 and 127,

the JVM reuses the same object. Integer i1 = 123; Integer i2 = 123; Integer i3 = 1234; Integer i4 = 1234; System.out.println(i1==i2); //true System.out.println(i3==i4); //false

slide-26
SLIDE 26

26

Languages in Depth series: Java Programming

Packages

  • A hierarchical namespace mechanism
  • Map to file system pathnames
  • Influence class visibility
  • Even if a default anonymous package exists, realistic

class names include the package: ch.ethz.inf.se.java.mypkg.MyClass

  • Tip: notice the useful name convention
slide-27
SLIDE 27

27

Languages in Depth series: Java Programming

The statements package and import

  • package declares a package
  • Classes from external packages generally need to be

imported using import

  • Classes from java.lang are automatically imported
  • * imports (dynamically) all classes in a package, but not

in sub-packages package ch.ethz.inf.se.java.mypkg.MyClass import java.awt.* import java.awt.event.*

slide-28
SLIDE 28

28

Languages in Depth series: Java Programming

Core packages in 6.0

  • java.lang (basic language functionality, fundamental

types, automatically imported)

  • java.util (collections and data structures)
  • java.io and java.nio (old/new file operations API)
  • java.math (multi-precision arithmetic)
  • java.net (networking, sockets, DSN lookups)
  • java.security (key generation, encryption)
  • java.sql (database access: JDBC)
  • java.awt (native GUI components)
  • javax.swing (platform independent rich GUI

components)

slide-29
SLIDE 29

29

Languages in Depth series: Java Programming

Quiz

1.

How many package declarations may be contained in

  • ne source file?

2.

How many classes can be contained in one source file?

3.

Is there a visibility restriction for those classes?

slide-30
SLIDE 30

30

Languages in Depth series: Java Programming

Quiz solution

1.

How many package declarations may be contained in

  • ne source file? Max one

2.

How many classes can be contained in one source file? No restriction

3.

Is there a visibility restriction for classes in a source file? Yes, max one top level public class

  • Tip: use one source file per each class declaration
slide-31
SLIDE 31

31

Languages in Depth series: Java Programming

Instance variables

  • Relate to a class instance
  • Declared within the class curly brackets, outside any

method

  • Visible at least within the class scope, within any method
  • f the class
  • Automatically initialized to the default values
  • 0 for numeric types, ‘ ‘ for chars, null for objects
slide-32
SLIDE 32

32

Languages in Depth series: Java Programming

Instance methods

  • Relate to an instance and are declared within the class

curly brackets

  • May have arguments and return values (also void)

boolean

  • olean test(int

nt i, boolean

  • olean b){

// some stuff here return eturn true rue; }

  • Constructors are “special” (more on this later)
slide-33
SLIDE 33

33

Languages in Depth series: Java Programming

Information hiding

Attribute and method visibility

  • public: visible everywhere
  • protected: visible in the same package and in

subclasses (wherever they are)

  • (*): visible in the same package
  • private: visible only in the class in which it was defined

Class visibility

  • Top level classes can only have default or public visibility

(*) No keyword for “package” visibility: it’s the default

slide-34
SLIDE 34

34

Languages in Depth series: Java Programming

Local variables

  • Declared within a method scope (curly brackets)
  • Visible only within the method scope
  • De-allocated at method end
  • Not automatically initialized
slide-35
SLIDE 35

35

Languages in Depth series: Java Programming

The static modifier

When applied to non local variables and methods

  • Relates to a specific class, not to a class instance
  • Shared by every object of a certain class
  • Accessed without creating any class object

MyClass.myStaticAttribute MyClass.myStaticMethod()

  • static methods can only access other static

attributes and methods