BA22001 Introduction to Java Instructor: Jingwu He
BA22002 History of Java In 1991, Sun started development of a programming language that should be used for programming of consumer electronics, e.g. Mobile telephones, remote console,... The language was originally named Oak by James Gosling, the creator of the language. The market was not ready for this language and the project goal was changed: the language should be for the web. The browser HotJava was launched in The browser was written in Java and could execute applets (code running inside web-pages) Also in 1995 Netscape decided to make their webbrowser Java enabled.
BA22003 More History The first versions of the language (e.g. Java 1.02 released 1996) was not ready for industry use, they were to immature (e.g. when it comes to making user interface). In December 1996 a much more mature version Java 1.1 was release. (some browser has build in support for JDK and not later versions).
BA22004 è Full application programming language è Additional capability as a Web programming language è OO programming language è Adopts its looks from C++ è Compiled to processor-neutral instruction set then interpreted on each supporting platform è Extremely fast adoption rate! What is Java
BA22005 Terminology è Package A grouping of logically related code (classes). For instance, all user-interface code is in a single package; likewise, all input/output code and all network code. è Class A collection of data and methods that operate on that data. è Method A group of statements in a class that handle a task. è Bytecodes A set of instructions that look like machine code, but are not specific to any processor. è Virtual Machine The environment in which Java runs. The JVM is responsible for executing the bytecodes and has responsibility for the fundamental capabilities of Java.
BA22006 Any Operating System JAVA Application (Bytecodes) JAVA Virtual Machine Any Hardware Platform Java Architecture
BA22007 Platform Independence Java source Java compile Java bytecode -HelloWorld.class -Platform Neutral Java source compiled into intermediary bytecode n Application runs anywhere Java VM is ported n Applet runs in any Java enabled browser test suite class HelloWorld { public static void Main (String args[]){ System.out.println("Hello World"); } javac HelloWorld.java
BA22008 Java Program Patterns Applications –Java programs which run stand alone Web-based Java Applications –Applets Dynamic and interactive programs that can run inside a web page displayed by a browser enabled for Java –Servlets Programs that run inside request/response oriented servers –JavaServer Pages An extension to the servlet architecture. Allow for the separation of the display of web content and the generation of that content
BA22009 Java 2 Platform Editions J2MEJ2EEJ2SE
BA Java 2 Platform Editions –Java 2 Platform, Micro Edition (J2ME) Highly optimized runtime environment Targets consumer products –Pagers, cell phones –Java 2 Platform, Standard Edition (J2SE) Java 2 tools, runtimes, and APIs for developers writing, deploying, and running applets and applications Referred to as Java 2 SDK –Java 2 Platform, Enterprise Edition (J2EE) Targets enterprise-class server-side applications.
BA Object Oriented Programming Conceptualizing a computer program as a set of separate objects that interact with each other. Objects can model real life situations –BankAccount object –Bicycle Objects contain information and the means of accessing and changing that information. Benefits –Reusability –Reliability
BA Class A template for creating objects l A class is a blueprint, or prototype, that defines the variables and the methods common to all objects of a certain kind
BA Working with Objects method data message anObject data encapsulation
BA Bank Account Class name address balance interestRate ytdInterest deposit withdraw changeAddress attributes (variables) methods
BA Instance of a class (object) name address balance interestRate ytdInterest deposit withdraw changeAddress class name address balance interestRate ytdInterest deposit withdraw changeAddress Instance of a class (object) John Smith 1234 Main St new
BA Many instances of a class (objects) name address balance interestRate ytdInterest deposit withdraw changeAddress Instance of a class (object) John Smith 1234 Main St name address balance interestRate ytdInterest deposit withdraw changeAddress Mary Smith 2115 Pine St Instance of a class (object)
BA Messages trigger methods name address balance interestRate ytdInterest deposit withdraw changeAddress Mary Smith 2115 Pine St Instance of a class
BA Bicycles
BA Bicycle Class color currentGear currentSpeed currentCadence numberOfGears brake changeGear changeCadence attributes (variables) methods
BA An instance of a class (object)
BA Many instances (objects)
BA Messages
BA How does in work in Java? Class contains: –Variables To hold values –Methods To execute logic
BA Java Program naming conventions Classes –Start with upper case alphabetic –Must be stored in a file with the same name and a.java extension –Use meaningful names –Examples BankAccount – good Bike - good 113BankAccount – invalid bankAccount – works, but may get a warning Stuff - works, but not meaningful
BA Java Program naming conventions methods –Start with lower case alphabetic –Use meaningful names –Capitalize words –Examples calculateInterest – good changeSpeed - good 123Calculate – invalid CalculateInterest – works, but may get a warning stuff - works, but not meaningful
BA Java Program naming conventions variables –Start with lower case alphabetic, _ or $ –Use meaningful names –Capitalize words –Examples int areaCode – good int currentSpeed - good boolean 123 – invalid float CalculatedInterest – works, but doesn’t follow convention int stuff - works, but not meaningful
BA class MyClass { class variables myMethod1() { method variables statement 1; statement 2; } myMethod2() { method variables statement 1; statement 2; } Java Program structure scope
BA class BankAccount { String name; String address; float balance; float interestRate; changeAddress() { method variables statement 1; statement 2; } withdraw() { method variables statement 1; statement 2; } deposit() { method variables statement 1; statement 2; } Java Program structure BankAccount myAccount = new BankAccount();
BA class Bike { int currentSpeed; int currentCadence; int numberOfGears; int currentGear; changeGears() { method variables statement 1; statement 2; } changeSpeed() { method variables statement 1; statement 2; } brake() { method variables statement 1; statement 2; } Java Program structure
BA Creating Objects new operator –Creates an instance of class (object) Bike myBike = new Bike() –Allocates memory Deallocating objects –Done by Java garbage collection
BA Working with Objects changeSpeed status speed myBike
BA Java Programming H SDK H Java Software Development Toolkit H Available from Sun Microsystems, Inc. H SDK 1.4 is the current version H Also version 5.0 RC (Released Code) H Known as JDK in early versions H Development environments (IDE) H IBM - WebSphere Studio Application Developer H Sun - Forte H Microsoft - Visual J++ H Borland - JBuilder H Oracle - JDeveloper H And others
BA Java Translation Enter/edit the Java program Save Source Compiler Java Bytecode Success Fail Correct errors.java file Java Java Bytecode Interpreter Machine Language Instructions CPU Execution of Machine Language Instructions Data for the Program Program Output.class file JVM Syntax errors Logic errors