1.introduction to java.ppt

53
8/14/2019 1.INTRODUCTION TO JAVA.ppt http://slidepdf.com/reader/full/1introduction-to-javappt 1/53  August 6, 2009 EVENT DRIVEN PROGRAMMING WITH JAVA

Upload: gautham-atluri

Post on 04-Jun-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 1/53

 August 6, 2009

EVENT DRIVENPROGRAMMING WITH

JAVA

Page 2: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 2/53

Page 3: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 3/53

 August 6, 2009

• Java is related to C++, which is a direct descendent

of C • From C, Java derives its syntax • Many of Java’s object-oriented features wereinfluenced by C++ • Java was conceived by James Gosling, Patrick

Naughton, Chris Warth, Ed Frank, and Mike

Sheridan at Sun Microsystems, Inc. in 1991

Page 4: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 4/53

• The primary motivation was the need for aplatform-independent (that is, architecture-

neutral) language that could be used tocreate software to be embedded in variousconsumer electronic devices, such asmicrowave ovens and remote controls. 

• This effort ultimately led to the creation ofJava. 

•  About the time that the details of Java were

being worked out, a second, and ultimatelymore important, factor was emerging thatwould play a crucial role in the future of

Java. 

• This second force was the World Wide Web. 

Page 5: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 5/53

• It became obvious to members of the Javadesign team that the problems of portability

frequently encountered when creating code forembedded controllers are also found whenattempting to create code for the Internet. 

• The same problem that Java was initially

designed to solve on a small scale could alsobe applied to the Internet on a large scale.This realization caused the focus of Java to

switch from consumer electronics to Internetprogramming. So, while the desire for anarchitecture-neutral programming languageprovided the initial spark, the Internetultimately led to Java’s large-scale success.

Page 6: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 6/53

 August 6, 2009

• This language was initially called ―Oak‖ but wasrenamed ―Java‖ in 1995 

• Java can be used to create two types of programs: appl icat ions  and applets  

•  An application is a program that runs on yourcomputer, under the operating system of thatcomputer  

•  An applet is an application designed to be

transmitted over the Internet and executed by aJava-compatible Web browser

Page 7: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 7/53

 August 6, 2009

•  An applet is actually a tiny Java program,

dynamically downloaded across thenetwork, just like an image, sound file, orvideo clip 

•  An applet is a program that can react touser input and dynamically change—not

 just run the same animation or sound overand over  

Page 8: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 8/53

 August 6, 2009

Security

• When you use a Java-compatible Webbrowser, you can safely download Javaapplets without fear of viral infection or

malicious intent • Java achieves this protection by confining

a Java program to the Java executionaccess to other parts of the computerenvironment and not allowing it

Page 9: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 9/53

 August 6, 2009

• Java is portable across many types ofcomputers and operating systems that arein use throughout the world 

Page 10: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 10/53

 August 6, 2009

Java’s Magic: The Bytecode

• The output of a Java compiler is notexecutable code ; rather, it is bytecode  

• Bytecode is a highly optimized set ofinstructions designed to be executed bythe Java run-time system, which is called

the Java Virtual Mach ine  (JVM ) 

Page 11: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 11/53

 August 6, 2009

Page 12: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 12/53

 August 6, 2009

Page 13: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 13/53

 August 6, 2009

Page 14: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 14/53

 August 6, 2009

Page 15: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 15/53

 August 6, 2009

Page 16: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 16/53

 August 6, 2009

JVM is an in terpreter for bytecode  

Page 17: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 17/53

Page 18: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 18/53

 August 6, 2009

• Sun supplies its Just In Time (JIT)compiler for bytecode, which is included inthe Java 2 release 

• When the JIT compiler is part of the JVM,it compiles bytecode into executable codein real time, on a piece-by-piece, demand

basis 

• The JIT compiler compiles code as it isneeded, during execution

Page 19: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 19/53

 August 6, 2009

The Java Buzzwords

• Simple • Secure • Portable • Object-oriented • Robust • Multithreaded •  Architecture-neutral • Interpreted • High performance • Distributed • Dynamic

Page 20: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 20/53

 August 6, 2009

Simple

• If you already understand the basic concepts of

object-oriented programming, learning Java willbe even easier  

• Because Java inherits the C/C++ syntax and

many of the object-oriented features of C++,most programmers have little trouble learningJava 

• Beyond its similarities with C/C++, Java hasanother attribute that makes it easy to learn: itmakes an effort not to have surprising features

Page 21: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 21/53

 August 6, 2009

Object oriented

• Java was not designed to be source-codecompatible with any other language 

• The object model in Java is simple andeasy to extend, while simple types, suchas integers, are kept as high-performance

non-objects 

Page 22: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 22/53

 August 6, 2009

Robust

•  Ability to create robust programs wasgiven a high priority in the design of Java 

• To better understand how Java is robust,consider two of the main reasons forprogram failure: memory management

mistakes  and mishandled except ionalcond i t ions  (that is, run-time errors)

Page 23: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 23/53

 August 6, 2009

• Memory management can be a difficult, tedioustask in traditional programming environments 

• For example, in C/C++, the programmer mustmanually allocate and free all dynamic memory 

• Programmers will either forget to free memorythat has been previously allocated or, worse, tryto free some memory that another part of theircode is still using

Page 24: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 24/53

 August 6, 2009

• Java virtually eliminates these problemsby managing memory allocation anddeallocation for you 

• Java provides object-oriented exceptionhandling

Page 25: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 25/53

 August 6, 2009

Multithreaded

• Java supports multithreadedprogramming, which allows you to writeprograms that do many things

simultaneously • The Java run-time system comes with an

elegant yet sophisticated solution formultiprocess synchronization that enablesyou to construct smoothly runninginteractive systems

Page 26: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 26/53

 August 6, 2009

 Architecture-Neutral

• Operating system upgrades, processorupgrades, and changes in core systemresources can all combine to make a

program malfunction 

• Java designers made several harddecisions in Java language and JavaVirtual Machine in an attempt to alter this

situation • Their goal was ―write once; run anywhere,

any time, forever.‖ 

Interpreted and High

Page 27: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 27/53

 August 6, 2009

Interpreted and HighPerformance

• Java enables the creation of cross-platform

programs by compiling into an intermediaterepresentation called Java bytecode 

• This code can be interpreted on any systemthat provides a Java Virtual Machine 

• the Java bytecode was carefully designed sothat it would be easy to translate directly intonative machine code for very highperformance by using a just-in-time compiler

Page 28: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 28/53

 August 6, 2009

Distributed

• Java is designed for the distributedenvironment of the Internet, because ithandles TCP/IP protocols 

• Remote Method Invocation (RMI ) featureof Java brings an unparalleled level ofabstraction to client/server programming

Page 29: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 29/53

 August 6, 2009

Dynamic

• Java programs carry with them substantialamounts of run-time type information thatis used to verify and resolve accesses to

objects at run time. • Makes it possible to dynamically link codein a safe and expedient manner. 

• Small fragments of bytecode may bedynamically updated on a running system 

Page 30: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 30/53

 August 6, 2009

An Overview of Java

Page 31: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 31/53

 August 6, 2009

• Object-oriented programming is at the

core of Java • all computer programs consist of two

elements: code  and data  •  A program can be conceptually organized

around its code or around its data 

Page 32: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 32/53

 August 6, 2009

• That is, some programs are writtenaround ―what is happening‖ and

others are written around ―who is beingaffected.‖ • The first way is called the process- or iented model  • The process-oriented model can be

thought of as code acting on data

Page 33: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 33/53

Page 34: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 34/53

 August 6, 2009

• Concept of Abstract ion  – focus is onessential features. 

• Humans manage complexity throughabstraction which is through the use of

hierarchical classifications. 

• Data can be transformed into componentobjects. A sequence of process steps can

become a collection of messagesbetween these objects - essence of OOP 

Page 35: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 35/53

 August 6, 2009

The Three OOP Principles: • Encapsulat ion  - is the mechanism that

binds together code and the data itmanipulates, and keeps both safe fromoutside interference and misuse – class,member variables and methods 

• Inheri tance  - the process by which one

object acquires the properties of anotherobject 

Page 36: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 36/53

 August 6, 2009

Page 37: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 37/53

 August 6, 2009

Page 38: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 38/53

 August 6, 2009

• Polymorph ism  - is a feature that allows

one interface to be used for a generalclass of actions – ― one interface , multiplemethods‖

• Polymorphism, encapsulation andinheritance works together - every javaprogram involves these.

A First Simple Program

Page 39: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 39/53

 August 6, 2009

 A First Simple Program

/* This is a simple Java program. Call this file "Example.java".*/ class Example { // Your program begins with a call to main(). public static void main(String args[]) { System.out.println("This is a simple Javaprogram."); } }

Page 40: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 40/53

 August 6, 2009

Compiling the Program

• C:\>javac Example.java  • The javac compiler creates a file called

Example.class  that contains thebytecode version of the program 

• The output of javac is not code that canbe directly executed

Page 41: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 41/53

 August 6, 2009

• To actually run the program, you must use

the Java interpreter, called java. • C:\>java Example  • When a class member is preceded by

public, then that member may beaccessed by code outside the class in

which it is declared

• The keyword static allows main( ) to be

Page 42: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 42/53

 August 6, 2009

• The keyword static allows main( ) to becalled without having to instantiate a

particular instance of the class 

• The keyword void simply tells thecompiler that main( ) does not return a

value • String[ ] args declares a parameter

named args, which is an array ofinstances of the class String. args receives any command-line arguments. 

A Second Short Program

Page 43: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 43/53

 August 6, 2009

 A Second Short Program

class Example2 { 

public static void main(String args[]) { int num; // this declares a variable called num num = 100; // this assigns num the value 100 System.out.println("This is num: " + num); num = num * 2; System.out.print("The value of num * 2 is "); System.out.println(num); } }

Two Control Statements

Page 44: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 44/53

 August 6, 2009

Two Control Statements

The if Statement : if(condition) statement ; if(num < 100)System.out.println("num is less than 100");

Th f L

Page 45: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 45/53

 August 6, 2009

The for Loop:  for(initialization; condition; iteration)statement ; class ForTest { public static void main(String args[]) { int x; for(x = 0; x<10; x = x+1) System.out.println("This is x: " + x); } }

Page 46: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 46/53

Lexical Issues

Page 47: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 47/53

 August 6, 2009

Lexical Issues

• Whitespace:  o Java is a free-form language o In Java, whitespace is a space, tab, or newline 

• Identi f iers:  - Identifiers are used for class names,method names, and variable names - Java is case-sensitive

Page 48: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 48/53

Page 49: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 49/53

 August 6, 2009

Literals:  •  A constant value in Java is created by

using a literal representation of it 

Comments

Page 50: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 50/53

 August 6, 2009

• There are three types of comments defined by Java.

• Single-line and multiline • The third type is called a documentation comment  • This type of comment is used to produce an HTML

file that documents your program • The documentation comment begins with a /** and

ends with a */

Separators

Page 51: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 51/53

 August 6, 2009

The Java Keywords

Page 52: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 52/53

 August 6, 2009

e Ja a ey o ds

• There are 50 reserved keywords currentlydefined in the Java language (enum) • Can not be used as names for a variable,

class, or method. • const and goto are reserved but not used •  Also reserves true, false and null – values

defined by Java.

Page 53: 1.INTRODUCTION TO JAVA.ppt

8/14/2019 1.INTRODUCTION TO JAVA.ppt

http://slidepdf.com/reader/full/1introduction-to-javappt 53/53

 August 6, 2009