java fundamentals
play

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


  1. Chair of Software Engineering Languages in Depth Series: Java Programming Prof. Dr. Bertrand Meyer Java Fundamentals Marco Piccioni

  2. Course Goals  Teach Java to programmers having previous programming knowledge/experience  Show (hopefully!) useful stuff  Illustrate some dark corners  Practice (exercise sessions, project) Languages in Depth series: Java Programming 2

  3. 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 Languages in Depth series: Java Programming 3

  4. 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 Languages in Depth series: Java Programming 4

  5. 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 Languages in Depth series: Java Programming 5

  6. 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 Languages in Depth series: Java Programming 6

  7. Intended language goals  Write Once, Run Anywhere  Built-in security  Automatic memory management  API + documentation generation  Object-Oriented (mostly)  Familiar C/C++ syntax Languages in Depth series: Java Programming 7

  8. Write once, run anywhere .java .class compiler .jar network JVM Class loader H Interpreter Bytecode w verifier JIT compiler Languages in Depth series: Java Programming 8

  9. 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 Languages in Depth series: Java Programming 9

  10. JVM overview .java .class compiler .jar network JVM Class loader H Interpreter w Bytecode verifier JIT compiler Languages in Depth series: Java Programming 10

  11. Built-in security Language 1.  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) Class loaders 2.  Take care of files and file systems  Locate libraries and dynamically load classes  Partition classes into realms and restrict what they can do Verifier checks bytecode using a theorem prover 3.  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 Languages in Depth series: Java Programming 11

  12. JVM: code generation .java .class compiler .jar network JVM Class loader H Interpreter Bytecode w verifier JIT compiler Languages in Depth series: Java Programming 12

  13. 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 Languages in Depth series: Java Programming 13

  14. JVM Overview .java .class compiler .jar network JVM Class loader H Interpreter Bytecode w verifier JIT compiler Languages in Depth series: Java Programming 14

  15. 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 Languages in Depth series: Java Programming 15

  16. 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*/ Languages in Depth series: Java Programming 16

  17. Identifiers  No restriction on length  Case sensitive  Cannot start with a digit  Cannot include “/” or “-”  Cannot be a keyword Languages in Depth series: Java Programming 17

  18. 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 short  Literals null , true , false are also reserved Languages in Depth series: Java Programming 18

  19. 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 ;) Languages in Depth series: Java Programming 19

  20. 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 Languages in Depth series: Java Programming 20

  21. Quiz What will be printed? double double d1=4.0; double double d2=3.1; System. out .println("4.0 - 3.1 =" + (d1 - d2)); Languages in Depth series: Java Programming 21

  22. 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 Languages in Depth series: Java Programming 22

  23. 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 Languages in Depth series: Java Programming 23

  24. 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); Languages in Depth series: Java Programming 24

  25. 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 Languages in Depth series: Java Programming 25

  26. 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 Languages in Depth series: Java Programming 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend