Sie sind auf Seite 1von 37

Chapter 2: Introduction to

JAVA

Session Plan (Week 2):


To understand:
Java Framework
Java runtime environment
Term API, IDE and JDK
History of JAVA
 James Gosling and Sun Microsystems
 Early name for Java - Oak
 Java, May 20, 1995, Sun World
 HotJava
• The first Java-enabled Web browser

 Early History Website:


http://java.sun.com/features/1998/05/birthday.html

TMK 3102-Chap 1: Introduction to 2


JAVA
Why Java?
The answer is that Java enables users to deploy applications
on the Internet for servers, desktop computers, and small
hand-held devices. The future of computing will be
profoundly influenced by the Internet, and Java promises
to remain a big part of that future.

 Java is a general purpose programming language.


 Java is the Internet programming language.

TMK 3102-Chap 1: Introduction to 3


JAVA
Java, Web, and Beyond
 Java can be used to develop Web
applications.
 Java Applets
 Java Servlets and JavaServer Pages
(JSP)
 Java can also be used to develop
applications for hand-held devices
such as Palm and cell phones

TMK 3102-Chap 1: Introduction to 4


JAVA
Characteristics of Java
 Java Is Simple Java is partially modeled
 Java Is Object-Oriented on C++, but greatly
 Java Is Distributed simplified and improved.
 Java Is Interpreted Some people refer to Java
 Java Is Robust as "C++--" because it is
 Java Is Secure like C++ but with more
 Java Is Architecture-Neutral functionality and fewer
 Java Is Portable negative aspects.
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 5


JAVA
Characteristics of Java
 Java Is Simple Java is inherently object-oriented.
Although many object-oriented languages
 Java Is Object-Oriented began strictly as procedural languages,
 Java Is Distributed Java was designed from the start to be
object-oriented. Object-oriented
 Java Is Interpreted programming (OOP) is a popular
 Java Is Robust programming approach that is replacing
traditional procedural programming
 Java Is Secure techniques.
 Java Is Architecture-Neutral
One of the central issues in software
 Java Is Portable development is how to reuse code.
 Java's Performance Object-oriented programming provides
great flexibility, modularity, clarity, and
 Java Is Multithreaded reusability through encapsulation,
 Java Is Dynamic inheritance, and polymorphism.

TMK 3102-Chap 1: Introduction to 6


JAVA
Characteristics of Java
 Java Is Simple Distributed computing involves
 Java Is Object-Oriented several computers working together
 Java Is Distributed on a network. Java is designed to
make distributed computing easy.
 Java Is Interpreted Since networking capability is
 Java Is Robust inherently integrated into Java,
 Java Is Secure writing network programs is like
 Java Is Architecture-Neutral sending and receiving data to and
 Java Is Portable from a file.
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 7


JAVA
Characteristics of Java
 Java Is Simple You need an interpreter to run Java
 Java Is Object-Oriented programs. The programs are
 Java Is Distributed compiled into the Java Virtual
Machine code called bytecode. The
 Java Is Interpreted bytecode is machine-independent
 Java Is Robust and can run on any machine that
 Java Is Secure has a Java interpreter, which is part
 Java Is Architecture-Neutral of the Java Virtual Machine (JVM).
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 8


JAVA
Characteristics of Java
 Java Is Simple Java compilers can detect many
 Java Is Object-Oriented problems that would first show up at
 Java Is Distributed execution time in other languages.
 Java Is Interpreted Java has eliminated certain types of
 Java Is Robust error-prone programming constructs
 Java Is Secure found in other languages.
 Java Is Architecture-Neutral
 Java Is Portable Java has a runtime exception-
handling feature to provide
 Java's Performance programming support for
 Java Is Multithreaded robustness.
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 9


JAVA
Characteristics of Java
 Java Is Simple Java implements several security
 Java Is Object-Oriented mechanisms to protect your system
 Java Is Distributed against harm caused by stray
programs.
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 10


JAVA
Characteristics of Java
 Java Is Simple Write once, run anywhere
 Java Is Object-Oriented
With a Java Virtual Machine (JVM),
 Java Is Distributed you can write one program that will
 Java Is Interpreted run on any platform.
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 11


JAVA
Characteristics of Java
 Java Is Simple Because Java is architecture
 Java Is Object-Oriented neutral, Java programs are
 Java Is Distributed portable. They can be run on any
platform without being recompiled.
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 12


JAVA
Characteristics of Java
 Java Is Simple Java can execute very fast with the
 Java Is Object-Oriented support of multithreading, clustering
 Java Is Distributed and load balancing.
 Java Is Interpreted
 Java Is Robust
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 13


JAVA
Characteristics of Java
 Java Is Simple Multithread programming is
 Java Is Object-Oriented smoothly integrated in Java,
 Java Is Distributed whereas in other languages you
have to call procedures specific to
 Java Is Interpreted the operating system to enable
 Java Is Robust multithreading.
 Java Is Secure
 Java Is Architecture-Neutral
 Java Is Portable
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 14


JAVA
Characteristics of Java
 Java Is Simple Java was designed to adapt to an
 Java Is Object-Oriented evolving environment. New code
 Java Is Distributed can be loaded on the fly without
recompilation. There is no need for
 Java Is Interpreted developers to create, and for users
 Java Is Robust to install, major new software
 Java Is Secure versions. New features can be
 Java Is Architecture-Neutral incorporated transparently as
 Java Is Portable needed.
 Java's Performance
 Java Is Multithreaded
 Java Is Dynamic

TMK 3102-Chap 1: Introduction to 15


JAVA
JDK
 Java Development Kit (JDK)
 JDK for
• Standard Edition (SE)
• Enterprise Edition (EE)
• Mobile Edition (ME)
 Use for producing JAVA program
 Can be downloaded from
http://www.java.sun.com

TMK 3102-Chap 1: Introduction to 16


JAVA
JDK Editions
 Java Standard Edition (J2SE or Java SE)
• J2SE/Java SE can be used to develop client-side
standalone applications or applets.
 Java Enterprise Edition (J2EE or Java EE)
• J2EE/Java EE can be used to develop server-side
applications such as Java servlets and Java
ServerPages.
 Java Micro Edition (J2ME or Java ME).
• J2ME/Java ME can be used to develop applications
for mobile devices such as cell phones.

TMK 3102-Chap 1: Introduction to 17


JAVA
Questions
 Why are we using JAVA?
 List the characteristics of JAVA
Programming Language
 What is JVM? What it is for?
 What is JDK? What is for?
 Give example of JDKs.

TMK 3102-Chap 1: Introduction to 18


JAVA
First Java Program

Example 1.1
//This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

TMK 3102-Chap 1: Introduction to 19


JAVA
Create & Edit Java Source Code

 To use Notepad, type


• notepad Welcome.java
 from the DOS prompt.

TMK 3102-Chap 1: Introduction to 20


JAVA
Create & Edit Java Source Code
To use WordPad, type
write Welcome.java
from the DOS prompt.

TMK 3102-Chap 1: Introduction to 21


JAVA
Creating, Compiling, and Running Programs

Create/Modify Source Code

Source code (developed by the programmer)


Saved on the disk
package chapter1;
public class Welcome {
public static void main(String[] args) { Source Code
System.out.println("Welcome to Java!");
}
}

Compile Source Code


Byte code (generated by the compiler for JVM i.e., javac Welcome.java
to read and interpret, not for you to understand)

Method Welcome() If compilation errors
0 aload_0 stored on the disk

Bytecode
Method void main(java.lang.String[])
0 getstatic #2 …
3 ldc #3 <String "Welcome to
Java!">
5 invokevirtual #4 …
8 return Run Byteode
i.e., java Welcome

Result

If runtime errors or incorrect result

TMK 3102-Chap 1: Introduction to 22


JAVA
Compiling and Running Java
from the Command Window
 Set path to JDK bin directory
• set path=c:\Program Files\java\jdk1.5.0\bin
 Set classpath to include the current directory
• set classpath=.
 Compile
• javac Welcome.java
 Run
• java Welcome

TMK 3102-Chap 1: Introduction to 23


JAVA
Anatomy of a Java Program
 Comments
 Package
 Reserved words
 Modifiers
 Statements
 Blocks
 Classes
 Methods
 The main method

TMK 3102-Chap 1: Introduction to 24


JAVA
Comments
In Java, comments are preceded by (1) two slashes (//) in a line
(2) enclosed between /* and */ in one or multiple lines. When
the compiler sees //, it ignores all text after // in the same
line. When it sees /*, it scans for the next */ and ignores any
text between /* and */.

//This program prints Welcome to Java!


package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

TMK 3102-Chap 1: Introduction to 25


JAVA
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.
//This program prints Welcome to Java!
package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

TMK 3102-Chap 1: Introduction to 26


JAVA
Reserved Words
 Reserved words or keywords are words that have a specific
meaning to the compiler and cannot be used for other
purposes in the program. For example, when the compiler
sees the word class, it understands that the word after class
is the name for the class. Other reserved words in example
program are public, static, and void.
//This program prints Welcome to Java!
package chapter1;
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}

TMK 3102-Chap 1: Introduction to 27


JAVA
Java Keywords
do new abstract break synchronized
if default assert private implements
int goto this throw protected
try return import public instanceof
byte else throws double transient
case void boolean short package
for final interface static extends
char long finally strictfp volatile
class float native super while
const catch continue switch
TMK 3102-Chap 1: Introduction to 28
JAVA
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.

TMK 3102-Chap 1: Introduction to 29


JAVA
Statements
A statement represents an action or a
sequence of actions. The statement
System.out.println("Welcome to
Java!") in the example program is a
statement to display the greeting
"Welcome to Java!“
Every statement in Java ends with a
semicolon (;).

TMK 3102-Chap 1: Introduction to 30


JAVA
Blocks
 A pair of braces in a program forms a block that
groups components of a program

public class Test {


public static void main(String[] args) { Class block
System.out.println("Welcome to Java!"); Method block
}
}

TMK 3102-Chap 1: Introduction to 31


JAVA
Classes
The class is the essential Java construct. A
class is a template or blueprint for objects.
To program in Java, you must understand
classes and be able to write and use them.
The mystery of the class will continue to be
unveiled throughout subject. For now,
though, understand that a program is
defined by using one or more classes.

TMK 3102-Chap 1: Introduction to 32


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

TMK 3102-Chap 1: Introduction to 33


JAVA
main Method
The main method provides the control of program flow.
The Java interpreter executes the application by
invoking the main method.

The main method looks like this:

public static void main(String[] args) {


// Statements;
}

TMK 3102-Chap 1: Introduction to 34


JAVA
Java Program Structure
import and package section;
Class Section
public class classIdentifier {
Global Declaration
Main method section
public static void main (String[] args) {
Local Declaration
Java Statement

}
TMK 3102-Chap 1: Introduction to 35
JAVA
Questions
 List the anatomy of JAVA program.
 Give example of JAVA keywords.
 What is the meaning of modifiers?
 Give example of modifiers.

TMK 3102-Chap 1: Introduction to 36


JAVA
Thank you.

TMK 3102-Chap 1: Introduction to 37


JAVA

Das könnte Ihnen auch gefallen