Introduction to
Java
Introduction to Java
What Is Java?
Getting Started With Java Programming
Create, Compile and Running a Java
Application
What Is Java?
History
Characteristics of Java
History
James Gosling and Sun Microsystems
Oak
Java, May 20, 1995, Sun World
HotJava
The first Java-enabled Web browser
JDK Evolutions
J2SE, J2ME, and J2EE (not mentioned in the
book, but could discuss here optionally)
4
Characteristics of Java
Java is simple
Java is object-oriented
Java is distributed
Java is interpreted
Java is robust
Java is secure
Java is architecture-neutral
Java is portable
Javas performance
Java is multithreaded
Java is dynamic
JDK Versions
JDK 1.02 (1995)
JDK 1.1 (1996)
Java 2 SDK v 1.2 (a.k.a JDK 1.2, 1998)
Java 2 SDK v 1.3 (a.k.a JDK 1.3, 2000)
Java 2 SDK v 1.4 (a.k.a JDK 1.4, 2002)
JDK Editions
Java Standard Edition (J2SE)
J2SE can be used to develop client-side standalone
applications or applets.
Java Enterprise Edition (J2EE)
J2EE can be used to develop server-side applications such
as Java servlets and Java ServerPages.
Java Micro Edition (J2ME).
J2ME can be used to develop applications for mobile
devices such as cell phones.
This book uses J2SE to introduce Java programming.
7
Java IDE Tools
Forte by Sun MicroSystems
Borland JBuilder
Microsoft Visual J++
WebGain Caf
IBM Visual Age for Java
Getting Started with Java
Programming
A Simple
Java Application
Compiling
Programs
Executing Applications
A Simple Application
Example 1.1
//This application program prints Welcome
//to Java!
package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
10
Creating and Compiling Programs
On command line
javac file.java
Create/Modify Source Code
Source Code
Compile Source Code
i.e. javac Welcome.java
If compilation errors
Bytecode
Run Byteode
i.e. java Welcome
Result
If runtime errors or incorrect result
11
Executing Applications
On command line
java classname
Bytecode
Java
Interpreter
on Windows
Java
Interpreter
on Linux
...
Java
Interpreter
on Sun Solaris
12
Example
javac Welcome.java
java Welcome
output:...
13
Compiling and Running a Program
Welcome.java
c:\example
chapter1
Welcome.class
Where are the files
stored in the
directory?
Welcome.java~
.
.
.
chapter2
Java source files and class files for Chapter 2
chapter19
Java source files and class files for Chapter 19
14
Anatomy of a Java Program
Comments
Package
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method
15
Comments
InJava,commentsareprecededby
twoslashes(//)inaline,or
enclosedbetween/*and*/inone
ormultiplelines.Whenthe
compilersees//,itignoresall
textafter//inthesameline.
Whenitsees/*,itscansforthe
next*/andignoresanytext
between/*and*/.
16
Package
The second line in the program
(package chapter1;) specifies a package
name, chapter1, for the class Welcome.
Forte compiles the source code in
Welcome.java, generates
Welcome.class, and stores
Welcome.class in the chapter1 folder.
17
Reserved Words
Reservedwordsorkeywordsarewords
thathaveaspecificmeaningtothe
compilerandcannotbeusedforother
purposesintheprogram.Forexample,
whenthecompilerseestheword
class,itunderstandsthattheword
afterclassisthenameforthe
class.Otherreservedwordsin
Example1.1arepublic,static,and
void.Theirusewillbeintroduced
laterinthebook.
18
Modifiers
Java uses certain reserved words called
modifiers that specify the properties of the
data, methods, and classes and how they can
be used. Examples of modifiers are public
and static. Other modifiers are private, final,
abstract, and protected. A public datum,
method, or class can be accessed by other
programs. A private datum or method cannot
be accessed by other programs. Modifiers are
discussed in Chapter 6, "Objects and Classes."
19
Statements
Astatementrepresentsanaction
orasequenceofactions.The
statement
System.out.println("Welcometo
Java!")intheprogramin
Example1.1isastatementto
displaythegreeting"Welcometo
Java!"EverystatementinJava
endswithasemicolon(;).
20
Blocks
Apairofbracesinaprogram
formsablockthatgroups
componentsofaprogram.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Class block
Method block
21
Classes
TheclassistheessentialJava
construct.Aclassisatemplateor
blueprintforobjects.Toprogramin
Java,youmustunderstandclasses
andbeabletowriteandusethem.
Themysteryoftheclasswill
continuetobeunveiledthroughout
thisbook.Fornow,though,
understandthataprogramisdefined
byusingoneormoreclasses.
22
Methods
What is System.out.println? It is a method: a
collection of statements that performs a
sequence of operations to display a message
on the console. It can be used even without
fully understanding the details of how it
works. It is used by invoking a statement
with a string argument. The string argument
is enclosed within parentheses. In this case,
the argument is "Welcome to Java!" You can
call the same println method with a different
argument to print a different message.
23
main Method
Themainmethodprovidesthecontrol
ofprogramflow.TheJavainterpreter
executestheapplicationbyinvoking
themainmethod.
Themainmethodlookslikethis:
publicstaticvoidmain(String[]
args){
//Statements;
}
24
Displaying Text in a Message
Dialog Box
you can use the showMessageDialog
method in the JOptionPane class.
JOptionPane is one of the many
predefined classes in the Java system,
which can be reused rather than
reinventing the wheel.
25
The showMessageDialog Method
JOptionPane.showMessageDialog(null,"Welcometo
Java!",
"Example1.2",JOptionPane.INFORMATION_MESSAGE));
26
The exit Method
Use Exit to terminate the program and stop
all threads.
NOTE: When your program starts, a thread
is spawned to run the program. When the
showMessageDialog is invoked, a separate
thread is spawned to run this method. The
thread is not terminated even you close the
dialog box. To terminate the thread, you have
to invoke the exit method.
27
Prepared By- Prathika
Goswami
IX-A
Submitted
to- Ms.
Nidhi
28