0% found this document useful (0 votes)
43 views

What Is Java? Getting Started With Java Programming: - Create, Compile and Running A Java Application

This document provides an introduction to Java programming, including a brief history of Java and its key characteristics. It discusses creating, compiling, and running a simple Java application that prints "Welcome to Java!". The document also describes Java programming concepts like packages, classes, methods, and the main method. It provides an example of using the showMessageDialog method to display a message in a dialog box and the exit method to terminate the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

What Is Java? Getting Started With Java Programming: - Create, Compile and Running A Java Application

This document provides an introduction to Java programming, including a brief history of Java and its key characteristics. It discusses creating, compiling, and running a simple Java application that prints "Welcome to Java!". The document also describes Java programming concepts like packages, classes, methods, and the main method. It provides an example of using the showMessageDialog method to display a message in a dialog box and the exit method to terminate the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 26

Chapter 1 Introduction to Java

and Forte
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)
3

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.


6

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!");
}
}
Source

Run
NOTE: To run the program,
install slide files on hard
disk.

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

10

Executing Applications

On command line
java classname
Bytecode

Java
Interpreter
on Windows

Java
Interpreter
on Linux

...

Java
Interpreter
on Sun Solaris

11

Example
javac Welcome.java
java Welcome
output:...

12

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

13

Anatomy of a Java Program


Comments
Package
Reserved words
Modifiers
Statements
Blocks
Classes
Methods
The main method

14

Comments

InJava,commentsareprecededby
twoslashes(//)inaline,or
enclosedbetween/*and*/inone
ormultiplelines.Whenthe
compilersees//,itignoresall
textafter//inthesameline.
Whenitsees/*,itscansforthe
next*/andignoresanytext
between/*and*/.
15

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.
16

Reserved Words

Reservedwordsorkeywordsarewords
thathaveaspecificmeaningtothe
compilerandcannotbeusedforother
purposesintheprogram.Forexample,
whenthecompilerseestheword
class,itunderstandsthattheword
afterclassisthenameforthe
class.Otherreservedwordsin
Example1.1arepublic,static,and
void.Theirusewillbeintroduced
laterinthebook.
17

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."
18

Statements

Astatementrepresentsanaction
orasequenceofactions.The
statement
System.out.println("Welcometo
Java!")intheprogramin
Example1.1isastatementto
displaythegreeting"Welcometo
Java!"EverystatementinJava
endswithasemicolon(;).
19

Blocks

Apairofbracesinaprogram
formsablockthatgroups
componentsofaprogram.
public class Test {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

Class block
Method block

20

Classes
TheclassistheessentialJava
construct.Aclassisatemplateor
blueprintforobjects.Toprogramin
Java,youmustunderstandclasses
andbeabletowriteandusethem.
Themysteryoftheclasswill
continuetobeunveiledthroughout
thisbook.Fornow,though,
understandthataprogramisdefined
byusingoneormoreclasses.
21

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.
22

main Method

Themainmethodprovidesthecontrol
ofprogramflow.TheJavainterpreter
executestheapplicationbyinvoking
themainmethod.

Themainmethodlookslikethis:

publicstaticvoidmain(String[]
args){
//Statements;
}
23

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.
Source

Run
24

The showMessageDialog Method


JOptionPane.showMessageDialog(null,"Welcometo
Java!",
"Example1.2",JOptionPane.INFORMATION_MESSAGE));

25

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.
26

You might also like