03 Java - LN01startingpoint
Transcript of 03 Java - LN01startingpoint
-
8/18/2019 03 Java - LN01startingpoint
1/29
Object Orientated Programming
An Introduction to Java
-
8/18/2019 03 Java - LN01startingpoint
2/29
• For the most accurate and up-to-date
tutorials, please access the latest version
from Suns official !ebsite for the Java S"
#utorials $%ast &pdated '()*()++', !hich
can be found at http((java.sun.com
(docs(boo/s(tutorial.
http://java.sun.com/docs/books/tutorial/information/history.htmlhttp://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/http://java.sun.com/docs/books/tutorial/information/history.html
-
8/18/2019 03 Java - LN01startingpoint
3/29
Features of OOP
• 0lasses
• Objects
• Instantiation• Inheritance
• 1embership Functions
-
8/18/2019 03 Java - LN01startingpoint
4/29
1ore OOP Features
• 2ata "ncapsulation
• Pol3morphism
• Operator Overloading• 1essage Passing
• Soft!are 4e-&se
-
8/18/2019 03 Java - LN01startingpoint
5/29
5istor3 of Java
• Java
• Java, first released by Sun Microsystems in 1994
– Originally for intelligent consumer-electronic devices
– Ten used for creating !eb "ages #it dynamic content
– $o# also used for%
• &evelo" large-scale enter"rise a""lications
• 'nance !!! server functionality
• (rovide a""lications for consumer devices )cell "ones, etc*+
-
8/18/2019 03 Java - LN01startingpoint
6/29
#he "mergence of Java
• In the past decade Java has emerged in !ide
use partiall3 because of its similarit3 to 0 and to
066
• Perhaps more important is its implementationusing a virtual machine that is intended to run
code unchanged on man3 different platforms.
• #his last feature has made it ver3 attractive to
larger development shops !ith heterogeneous
environments.
-
8/18/2019 03 Java - LN01startingpoint
7/29
7e3 8enefits of Java
• Java is #rite once, run any#ere
– arcitecture neutral – "ortable across different "latforms
– &ue to Java .irtual Macine )J.M+
• Security features
– igly configurable security levels "revent any "iece of Java code doing armto te ost system
• $et#or/-centric "latform
– easy to #or/ #it resources across a net#or/ and to create net#or/ baseda""lications
• Ob0ect Oriented
– an interacting collection of inde"endent soft#are com"onents
– dynamic etensible "rograms
-
8/18/2019 03 Java - LN01startingpoint
8/29
7e3 8enefits of Java
• Internationalisation 9 uses :; bit &nicode characters that represents the phonetic
and ideographic character sets of the entire !orld
• Performance
9 although an interpreted language Java programs run almostas fast as native 0, 066 programs
• Simple and eas3 to develop
9 po!erful < !ell designed set of APIs
-
8/18/2019 03 Java - LN01startingpoint
9/29
Java 0lass %ibraries
• 0lasses
9 Include methods that perform tas/s
• 4eturn information after tas/ completion
9 &sed to build Java programs
• Java contains class libraries
9 7no!n as Java APIs $Application Programming Interfaces
-
8/18/2019 03 Java - LN01startingpoint
10/29
8asics of a #3pical Java "nvironment
• Java programs normall3 undergo five phases
9 "dit
• Programmer !rites program $and stores program on dis/
9 0ompile
• 0ompiler creates bytecodes from program
9 %oad
• 0lass loader stores b3tecodes in memor3
9 =erif3
• =erifier ensures b3tecodes do not violate securit3re>uirements
9 "?ecute
• Interpreter translates b3tecodes into machine language
-
8/18/2019 03 Java - LN01startingpoint
11/29
Typical Java environment.
Primar3
1emor3
.
.
.
.
.
.
2is/
2is/
2is/
"ditor
0ompiler
0lass %oader
Program is created in an editorand stored on dis/ in a file ending!ith .java.
0ompiler creates b3tecodes andstores them on dis/ in a file ending!ith .class.
0lass loader reads .class files containing b3tecodesfrom dis/ and puts thoseb3tecodes in memor3.
Phase :
Phase )
Phase *
Primar3
1emor3
.
.
.
.
.
.
83tecode =erifier 83tecode verifier confirms thatall b3tecodes are valid and donot violate Java@s securit3restrictions.
Phase
Primar3
1emor3
.
.
.
.
.
.
Interpreter Interpreter reads b3tecodesand translates them into alanguage that the computercan understand, possibl3storing data values as theprogram e?ecutes.
Phase B
-
8/18/2019 03 Java - LN01startingpoint
12/29
Java
In discussing java it is important to distinguish bet!een
:. #he Java Programming %anguage
). #he Java =irtual 1achine
*. #he Java Platform
-
8/18/2019 03 Java - LN01startingpoint
13/29
Java
:. #he Java Programming Language is the language in
!hich Java applications, applets , servlets andcomponents are !ritten. It is an object orientatedlanguage li/e 066.
). #he Java Virtual Machine, $or java interpreter is the
crucial piece of ever3 Java installation. Java applicationsare portable, but the3 are onl3 portable to platforms to!hich a java virtual machine $ or java interpreter hasbeen ported.
*. #he Java Platform . All programs !ritten in Java rel3 onthe set of predefined classes that comprise the javaplatform. #hese classes are organised into relatedgroups /no!n as packages. #he java platform definespac/ages for functionalit3 such as input(output,net!or/ing, graphics, user interface creation.
-
8/18/2019 03 Java - LN01startingpoint
14/29
J=1
class my2ode 3
5
myCode.java
Compiled by Java
compiler
Application
runs
Application
runs
Interpreted
by JVM
Source
Code
1661166161661
myCode.class
Bytecode
-
8/18/2019 03 Java - LN01startingpoint
15/29
J=1
• J=1 provides the run time environment for the b3tecode
$Java 4untime "nvironment J4"
9 e?ecutes the b3tecode and causes native machine
code instructions to e?ecute on the 0P& that the J=1
is on
each target platform needs an implementation of the
J=1
-
8/18/2019 03 Java - LN01startingpoint
16/29
Java 0ompiler
class my2ode 3
5
myCode.java
Source
Code
Errors
2om"iler 7yte 2ode
8nter"reter for
2om"uter
(JVM)
8nter"reter for
2om"uter 7
(JVM)
8nter"reter for
2om"uter 2
(JVM)
-
8/18/2019 03 Java - LN01startingpoint
17/29
Operation of the J4"$Java 4untime "nvironment
::::::
::::::::
myCode.java
Compile Class Loader
Bytecode verifier
Interpreter
::::::
::::::::
myCode.class
javac
RunTime
Hardare
%oad from
hard dis/,
net!or/ or
other source
Runtime
java
-
8/18/2019 03 Java - LN01startingpoint
18/29
J4"
• At runtime the java b3tecode that ma/es up a java soft!are program
are
9 %oaded, chec/ed and run in an interpreter
9 In the case of applets 3ou can do!nload the b3tecode and the3 are then
interpreted b3 the J=1 built into the bro!ser.
• 83tecode verifier chec/s
9 Attributes correctl3 t3ped
9 8od3 of the methods chec/
9 #3pe chec/ing 9 4eferences to other t3pes chec/ed
-
8/18/2019 03 Java - LN01startingpoint
19/29
Java Applications vs Java Applets• #here are t!o categories of Java Programs
9 A Java Application 9
• #hese are stand alone programs in the traditional sense
• 4un independentl3
• 4un under the java interpreter
9 Java applets
• #hese are programs designed to run from bro!sers such asCetscape or Sun@s 5otJava.
• #he3 don@t run independentl3
• Programs that can be included in !eb documents.
-
8/18/2019 03 Java - LN01startingpoint
20/29
First Java Program $Application
• A class definition is the basic bloc/ of a Java program
• "ach program must have a main method to tell it!here to start e?ecuting
// First Program HelloWorld
public class HelloWorld {
public static void main(String args [!{
System.out.println("Hello #orld...$!%&
&
-
8/18/2019 03 Java - LN01startingpoint
21/29
Cotes
• // First Program HelloWorld
• 0omments begin !ith ((
• 0an also use 0 st3le comments• /* First Program HelloWorld */
-
8/18/2019 03 Java - LN01startingpoint
22/29
Cotes )
• public class HelloWorld
• This begins a class deinition or class
HelloWorld
• These classes are !no"n as Programmerdeined classes or #ser deined classes
• The public and class terms are java
!ey"ords "hich are al"ays lo"ercase• HelloWorld is the class identiier "hich
al"ays start "ith an uppercase letter
-
8/18/2019 03 Java - LN01startingpoint
23/29
Cotes *
• Dhen 3ou save 3our class definition in a
file the filename must be the same as the
class identifier !ith a .java e?tension
• So the 5elloDorld class !ould be stored
in a 5elloDorld.java file
-
8/18/2019 03 Java - LN01startingpoint
24/29
Cotes
• public static void main$%tring args &'(
• Java applications automatically begin e)ecuting atmain
• Parentheses $( ater main indicate a program piece
called a method "hich deine some operationalcomponent o the class.
• oid indicates that the method "ill perorm a tas!"ithout returning any inormation "hen it completes
• %tring args &' are used as input arguments to themethod. +n this case "e don,t use input but javare-uires it any"ay
-
8/18/2019 03 Java - LN01startingpoint
25/29
Cotes B
• %ystem.out.println$Hello "orld...(0• %ystem.out is called the standard output object• This "ill display a line o te)t in the command
"indo"
• +n Java 1 any source and destination or +/2 isconsidered a stream o bytes or characters.• To perorm output "e insert bytes or characters
into a stream. To perorm input "e e)tract bytes orcharacters rom a stream.
• java.lang.%ystem class contains three predeinedstreams• %ystem.out• %ystem.err or errors• %ystem.in
-
8/18/2019 03 Java - LN01startingpoint
26/29
A First Java Program
• #his is a Java application not a Java applet 9
indicated b3 presence of main
• Source code e?ists in a file !ith same name asthe class and an e?tension of .java. So for the
previous e?ample it is 5elloDorld.java
• Java is case sensitive $li/e 0.
-
8/18/2019 03 Java - LN01startingpoint
27/29
&sing the J27
• 0reate source files for
each class in 3our
program• #he name of source file
should be the same as
the name of class
pu!lic class myCode "
#
#
#
#
$
myCode.java
Source File
-
8/18/2019 03 Java - LN01startingpoint
28/29
0ompiling 3our source code
• 0ompile each class source
file into b3tecode $class files
• #o compile a java source file
javac m30ode.java
• #his creates a classfile called
m30ode.class
%&&%%&%&&%%%&%&%&%%
#
#
#
#
myCode.class
Class File
-
8/18/2019 03 Java - LN01startingpoint
29/29
#o run 3our program
• #o start 3our program running 3ou run the
b3tecode of the program control class
• #he program control class has the main method
• #o run b3tecode 9 pass it to the J=1 java classFileCame
e.g. java m3Prog
note no .class included