Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Java platform


Published on

Introduction to the Java platform, Java virtual machine, loading classes, Java API

Published in: Technology

Java platform

  1. 1. Introduction to Java Programming - internals
  2. 2. Contents <ul><li>Computer Programming Overview </li></ul><ul><li>Your First Java Program </li></ul><ul><li>Java Technology Overview </li></ul><ul><li>Eclipse IDE </li></ul>
  3. 3. Contents <ul><li>The Structure of Java Programs </li></ul><ul><li>Keywords and Identifiers </li></ul><ul><li>Data Types </li></ul><ul><ul><li>Integral, Textual, Floating-Point </li></ul></ul><ul><ul><li>Enumerations </li></ul></ul><ul><li>Variables, Declarations, Assignments, Operators </li></ul>
  4. 4. Contents <ul><li>Expressions and Statements </li></ul><ul><ul><li>Logical Statements </li></ul></ul><ul><ul><li>Loop Statements </li></ul></ul><ul><li>Console Input and Output </li></ul><ul><li>Arrays and Array Manipulation </li></ul><ul><li>Using the Java API Documentation </li></ul>
  5. 5. What is Computer Programming?
  6. 6. Define: Computer Programming <ul><li>Computer Programming: creating a sequence of instructions to enable the computer to do something </li></ul>Definition by Google
  7. 7. Programming Phases <ul><li>Define a task/problem </li></ul><ul><li>Plan your solution </li></ul><ul><ul><li>Find suitable algorithm to solve it </li></ul></ul><ul><ul><li>Find suitable data structures to use </li></ul></ul><ul><li>Write code </li></ul><ul><li>Fix program error (bugs) </li></ul><ul><li>Make your customer happy </li></ul> = Specification = Design = Implementation = Testing & Debugging = Deployment
  8. 8. Your First Java Program
  9. 9. First Look at Java Code Sample Java Source code: public class HelloJava { public static void main(String args[]) { System.out.println(&quot;Hello, Java&quot;); } }
  10. 10. Java Code – How It Works? public class HelloJava { public static void main(String args[]){ System.out.println(&quot;Hello, Java&quot;); } } Define a class called &quot; HelloJava &quot; Define the main() method – the program entry point Print a text on the console calling the method &quot; println() &quot; of the system's standard output
  11. 11. Java Is Case Sensitive! public class HelloJava { public static void Main(String args[]){ system.out.PrintLn(&quot;Hello, Java&quot;); } } The keyword class should be lowercase The class System should be in &quot; Pascal Case &quot; The method println() should be in &quot; camel Case &quot; The correct method name is main()
  12. 12. Java Code Should Be Well Formatted public class HelloJava { public static void main(String args[]) { System.out.println(&quot;Hello, Java&quot;); } } The { symbol should be on the same line. The block after the { symbol should be indented by a TAB Class names should start with a CAPITAL letter The { symbol should be on the same line. The } symbol should be under the beginning of the line with corresponding {
  13. 13. Example of Bad Formatting public class HelloJava {public static void main(String args[]){ System.out.println (&quot;Hello, Java&quot;); System.out.println(&quot;Hello, Java&quot;);} } Such formatting makes the code unreadable
  14. 14. File Names Match Class Names! <ul><li>In Java all public classes should be in a file name matching their name </li></ul><ul><ul><li>For example the class HelloJava should be in the file </li></ul></ul>public class HelloJava { public static void main(String args[]){ System.out.println(&quot;Hello, Java&quot;); } }
  15. 15. Your First Java Program Live Demo
  16. 16. Welcome to Java Technology
  17. 17. Why Java? <ul><li>Meets the emerging software development challenges of its time </li></ul><ul><ul><li>Platform independence </li></ul></ul><ul><ul><ul><li>Run on wide variety of hardware </li></ul></ul></ul><ul><ul><ul><li>Desktop, server-side, embedded </li></ul></ul></ul><ul><ul><ul><li>Easier to develop, administer, maintain </li></ul></ul></ul><ul><ul><li>Object-oriented approach </li></ul></ul><ul><ul><li>Built-in security </li></ul></ul><ul><ul><li>Network mobility (mobile code) </li></ul></ul>
  18. 18. History of Java <ul><li>Project “Oak” began in mid 80’s at Sun Microsystems by James Gosling </li></ul><ul><li>Java 1.0 – released Jan 1996 </li></ul><ul><li>Java 1.1 – released Feb 1997 </li></ul><ul><ul><li>Reflection, inner classes </li></ul></ul><ul><ul><li>Serialization, AWT, JavaBeans </li></ul></ul><ul><li>Java 1.2 – released Dec 1998 </li></ul><ul><ul><li>Also known as &quot;Java 2 Platform&quot; </li></ul></ul><ul><ul><li>Significant improvement </li></ul></ul><ul><ul><li>Swing GUI, performance and security improvements </li></ul></ul>
  19. 19. History of Java <ul><li>Java 2 splits -> J2SE, J2EE, J2ME – 1999 </li></ul><ul><li>J2SE 1.3 – released May 2000 </li></ul><ul><ul><li>CORBA, RMI, Sound API, many enhancements </li></ul></ul><ul><li>J2SE 1.4 – released Feb 2002 </li></ul><ul><ul><li>Assertions, non-blocking I/O, XML parser </li></ul></ul><ul><li>J2SE 1.5 (5.0) – released Sep 2004 </li></ul><ul><ul><li>Lots of new language features </li></ul></ul><ul><ul><li>Generics, enhanced for loop, variable arguments list, auto boxing/unboxing, ... </li></ul></ul><ul><li>Java SE 6.0 – December 2006 </li></ul><ul><ul><li>Better performance, scripting support, new APIs </li></ul></ul>
  20. 20. What is the Java Technology? <ul><li>Java technology is: </li></ul><ul><ul><li>A programming language </li></ul></ul><ul><ul><li>A development environment </li></ul></ul><ul><ul><li>An application environment </li></ul></ul><ul><ul><li>A deployment environment </li></ul></ul><ul><li>It is similar in syntax to C++ and C# </li></ul><ul><li>Used for developing applets and applications (standalone, server-side, Web services, mobile, embedded, ...) </li></ul>
  21. 21. Java Platform Architecture
  22. 22. The Java Platform Architecture <ul><li>Consists of four distinct, but interrelated technologies </li></ul><ul><ul><li>The Java virtual machine (JVM) </li></ul></ul><ul><ul><li>Class loaders and class files </li></ul></ul><ul><ul><li>The Java programming language </li></ul></ul><ul><ul><li>The Java API </li></ul></ul><ul><li>Writing Java programs is related to all of these technologies </li></ul>
  23. 23. Java, JVM and OS
  24. 24. Java 5 Architecture
  25. 25. Java Compilation and Execution public class HelloJava { public static void main(String args[]) { System.out.println( &quot;Hello, Java&quot;); } } Compilation 0010011101011101110101110101011101110101101101101110101011010101010101010101010101101110100011010000001011010111101101110101001110100110101010101101011111101010111010100101011101010100101001111101101010101110101010001010101001011000101010011101010100110101110110111110101 HelloJava.class Execution
  26. 26. The Java Programming Environment
  27. 27. Architectural Tradeoffs <ul><li>Not “the right tool for any job” </li></ul>Platform independence Productivity Execution speed Lowest common subset of features Garbage collection Lack of control of memory management and thread scheduling Dynamic linking Symbolic references Security
  28. 28. Java Platform Editions
  29. 29. Java Platform Editions <ul><li>The Java platform has several editions: </li></ul><ul><ul><li>J2SE (Java SE) </li></ul></ul><ul><ul><li>J2EE (Java EE) </li></ul></ul><ul><ul><li>J2ME </li></ul></ul>
  30. 30. J2SE, J2EE, J2ME <ul><li>Java 2 Standard Edition (J2SE, Java SE) </li></ul><ul><ul><li>Used to write standalone Java Applications and Applets </li></ul></ul><ul><li>Java 2 Enterprise Edition (J2EE, Java EE) </li></ul><ul><ul><li>A set of API’s and server specifications built on top of J2SE </li></ul></ul><ul><ul><li>Used for building Enterprise, Web applications and Web services </li></ul></ul><ul><li>Java 2 Micro Edition (J2ME) </li></ul><ul><ul><li>A pared down version of J2SE and API’s for wireless and embedded devices </li></ul></ul>
  31. 31. Java Virtual Machine (JVM)
  32. 32. The Java Virtual Machine <ul><li>Main features </li></ul><ul><ul><li>Load class files </li></ul></ul><ul><ul><li>Execute bytecodes they contain </li></ul></ul><ul><li>Loose features specification </li></ul><ul><ul><li>Use any technique to execute bytecode </li></ul></ul><ul><ul><li>Software/hardware implementation </li></ul></ul><ul><ul><li>Can be implemented on a wide variety of computers and devices </li></ul></ul>
  33. 33. The Java Virtual Machine <ul><li>JVM provides definitions for the: </li></ul><ul><ul><li>Instruction set (virtual CPU) </li></ul></ul><ul><ul><li>Register set </li></ul></ul><ul><ul><li>Class file format </li></ul></ul><ul><ul><li>Stack </li></ul></ul><ul><ul><li>Garbage-collected heap </li></ul></ul><ul><ul><li>Memory area </li></ul></ul>
  34. 34. Garbage Collection <ul><li>Allocated memory that is no longer needed is automatically deallocated </li></ul><ul><li>The Java programming language provides a system level thread to track memory allocation </li></ul><ul><li>Garbage collection: </li></ul><ul><ul><li>Checks for and frees memory no longer needed </li></ul></ul><ul><ul><li>Is done automatically </li></ul></ul><ul><ul><li>Can vary dramatically across JVM implementations </li></ul></ul>
  35. 35. The JVM and Host Operating Systems <ul><li>Java methods </li></ul><ul><ul><li>Written in Java, compiled to bytecodes </li></ul></ul><ul><ul><li>Stored in class files ( .class ) </li></ul></ul><ul><li>Native methods </li></ul><ul><ul><li>Written in other languages (C, C++, …) </li></ul></ul><ul><ul><li>Compiled to native machine code </li></ul></ul><ul><ul><li>Dynamic libraries </li></ul></ul><ul><li>Java Native Interface (JNI) </li></ul>
  36. 36. The JVM and Host Operating Systems
  37. 37. Classes and Class Loaders
  38. 38. Classes and Class Loaders <ul><li>The “bootstrap” class loader </li></ul><ul><ul><li>Only one </li></ul></ul><ul><ul><li>Part of the JVM implementation </li></ul></ul><ul><ul><li>Loads classes in some default way </li></ul></ul><ul><li>User-defined class loaders </li></ul><ul><ul><li>Written and compiled in Java </li></ul></ul><ul><ul><li>Installed at runtime </li></ul></ul><ul><ul><li>Load classes in custom ways </li></ul></ul><ul><ul><li>Not part of the JVM implementation </li></ul></ul>
  39. 39. Class Loaders Architecture
  40. 40. Class Loaders <ul><li>Load classes over networks, from DB, … </li></ul><ul><li>Keep track of loaded classes </li></ul><ul><ul><li>Class namespaces </li></ul></ul><ul><ul><li>Access between class namespaces </li></ul></ul><ul><li>Advantages </li></ul><ul><ul><li>Security </li></ul></ul><ul><ul><li>Mobility </li></ul></ul><ul><ul><li>Extensibility </li></ul></ul>
  41. 41. Java Class Files <ul><li>Java classes, translated to “bytecodes” </li></ul><ul><ul><li>Stored with .class file extension </li></ul></ul><ul><li>Platform independent binary format </li></ul><ul><ul><li>Consistent byte order of integers </li></ul></ul><ul><li>Designed to be compact </li></ul><ul><ul><li>Network mobility </li></ul></ul><ul><li>Can be downloaded as needed </li></ul><ul><ul><li>Dynamic linking </li></ul></ul>
  42. 42. Classpath <ul><li>The CLASSPATH environment variable </li></ul><ul><ul><li>Third-party and user-defined classes </li></ul></ul><ul><ul><li>Can be overridden using the “-classpath” Java command-line argument </li></ul></ul><ul><li>Classpath entries can be </li></ul><ul><ul><li>Directories </li></ul></ul><ul><ul><li>Archive files (.jar and .zip) </li></ul></ul><ul><li>Classes are loaded in the order of appearance </li></ul>
  43. 43. JAR Files <ul><li>Java programs are compiled to .class files (Java bytecode + class metadata) </li></ul><ul><ul><li>Class files are packed in JAR archives </li></ul></ul><ul><li>JAR files (short for J ava AR chive) are </li></ul><ul><ul><li>Standard ZIP files </li></ul></ul><ul><ul><ul><li>Can use compression or not </li></ul></ul></ul><ul><ul><li>Used to distribute a set of compiled Java classes, associated metadata and resources </li></ul></ul>
  44. 44. JAR Files <ul><li>Can be created and extracted with jar command line tool </li></ul><ul><ul><li>Creating .jar file: </li></ul></ul><ul><ul><li>Extracting .jar files </li></ul></ul><ul><li>Can be created and extracted with WinZip or other ZIP manipulation tool </li></ul>jar -cf MyJarArchive.jar *.class jar -xf MyJarArchive.jar *.class
  45. 45. Eclipse Compiling, R unning and D ebugging Java P rograms
  46. 46. Creating New Java Application <ul><li>Window  Open Perspective  Java </li></ul><ul><li>File  New  Project </li></ul><ul><li>Choose Java Project </li></ul><ul><li>Choose project name </li></ul><ul><li>Click Finish </li></ul>
  47. 47. Creating New Java Application (2) <ul><li>File  New  Class </li></ul><ul><li>Choose the project you just made </li></ul><ul><li>Choose class name </li></ul><ul><li>Enable “public static void main (String args[])” check box </li></ul><ul><li>Click Finish </li></ul>
  48. 48. <ul><li>Eclipse creates some source code for you. </li></ul>Creating New Java Application (3)
  49. 49. Compiling Source Code <ul><li>Compilation process includes: </li></ul><ul><ul><li>Syntactic checks </li></ul></ul><ul><ul><li>Type safety checks </li></ul></ul><ul><ul><li>Translation of the source code to Java bytecode </li></ul></ul><ul><li>In Eclipse compilation is made automatically </li></ul><ul><ul><li>As you type, the program is checked for errors and is compiled </li></ul></ul><ul><ul><li>Saving the file (Ctrl+S) forces compilation </li></ul></ul>
  50. 50. Running Programs <ul><li>Running process includes: </li></ul><ul><ul><li>Compiling (if project not compiled) </li></ul></ul><ul><ul><li>Starting the application </li></ul></ul><ul><li>You can run application by: </li></ul><ul><ul><li>Using Run As->Java Application popup menu </li></ul></ul><ul><li>* NOTE: Not all types of projects are able to be run! </li></ul>
  51. 51. Debugging The Code <ul><li>Debugging process includes: </li></ul><ul><ul><li>Spotting an error </li></ul></ul><ul><ul><li>Finding the code that causes the error </li></ul></ul><ul><ul><li>Fixing the code </li></ul></ul><ul><ul><li>Testing to see if the error is gone and no errors are introduced </li></ul></ul><ul><li>This process is iterative and continuous </li></ul>
  52. 52. Debugging in Eclipse <ul><li>Eclipse has built-in debugger </li></ul><ul><li>It provides: </li></ul><ul><ul><li>Breakpoints </li></ul></ul><ul><ul><li>Ability to trace the code execution </li></ul></ul><ul><ul><li>Ability to inspect variables at runtime </li></ul></ul>
  53. 53. Eclipse Compiling, R unning and D ebugging Java P rograms Live Demo