Programming Methodology with JAVA

indexadjustmentInternet and Web Development

Nov 13, 2013 (3 years and 1 month ago)

52 views




Programming Methodology with JAVA


Degree Third Year, First Semester 2012/2013



Introduction to Java Programming



Prepared by Mr. H. A. HAJI

Wednesday,
November 13,
2013

1

Introduction to Java

Contents..



Why Java?


What is Java?


Getting Started With Java Programming


Create, Compile and Running a Java
Application and Applets


Java Primitive data types




Wednesday,
November 13,
2013

2

Why Java?


It’s the current “hot” language


It’s almost entirely object
-
oriented


It has a vast library of predefined objects and
operations


It’s more platform independent


this makes it great for Web programming


It’s more secure


It isn’t C++ or C

Wednesday,
November 13,
2013

3

What Is Java?


History


Characteristics of Java

Wednesday,
November 13,
2013

4

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

Wednesday,
November 13,
2013

5

Characteristics of Java


Java is simple


Java is object
-
oriented


Java is distributed


Java is interpreted


Java is robust


Java is secure


Java is portable


Java’s performance


Java is multithreaded


Java is dynamic


Wednesday,
November 13,
2013

6

Cont..


Java is simple:

Java was designed to be easy to
use and is therefore easy to write, compile,
debug, and learn than other programming
languages. The reason that why Java is much
simpler than C++ is because Java uses automatic
memory allocation and garbage collection where
else C++ requires the programmer to allocate
memory and to collect garbage.


Wednesday,
November 13,
2013

7


Java is object
-
oriented:

Java is object
-
oriented because programming in Java is
centered on creating objects, manipulating
objects, and making objects work together. This
allows you to create modular programs and
reusable code.

Wednesday,
November 13,
2013

8


Java is platform
-
independent:

One of the
most significant advantages of Java is its ability
to move easily from one computer system to
another.



Wednesday,
November 13,
2013

9


The ability to run the same program

on
many different systems is crucial to World Wide
Web software, and Java succeeds at this by being
platform
-
independent at both the source and
binary levels.

Wednesday,
November 13,
2013

10


Java is distributed:

Distributed computing
involves several computers on a network
working together. Java is designed to make
distributed computing easy with the networking
capability that is inherently integrated into it.


Wednesday,
November 13,
2013

11


Writing network programs

in Java is like
sending and receiving data to and from a file.

Wednesday,
November 13,
2013

12



Java is interpreted:

An interpreter is needed
in order to run Java programs. The programs are
compiled into Java Virtual Machine code called
bytecode.



Wednesday,
November 13,
2013

13


The bytecode is machine independent

and
is able to run on any machine that has a Java
interpreter. With Java, the program need only be
compiled once, and the bytecode generated by
the Java compiler can run on any platform.

Wednesday,
November 13,
2013

14



Java is secure:

Java is one of the first
programming languages to consider security as
part of its design. The Java language, compiler,
interpreter, and runtime environment were each
developed with security in mind.


Wednesday,
November 13,
2013

15



Java is robust:

Robust means reliable and no
programming language can really assure
reliability. Java puts a lot of emphasis on early
checking for possible errors, as Java compilers
are able to detect many problems that would
first show up during execution time in other
languages.


Wednesday,
November 13,
2013

16



Java is multithreaded:

Multithreaded is the
capability for a program to perform several tasks
simultaneously within a program. In Java,
multithreaded programming has been smoothly
integrated into it, while in other languages,
operating system
-
specific procedures have to be
called in order to enable multithreading.
Multithreading is a necessity in visual and
network programming.


Wednesday,
November 13,
2013

17

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)


……


Java 2 SDK v 1.7 (a.ka JDK 1.7 2010)

Wednesday,
November 13,
2013

18

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 Lecture uses J2SE to introduce Java programming.


Wednesday,
November 13,
2013

19

Java IDE Tools


Forte by Sun MicroSystems


Borland JBuilder


Microsoft Visual J++


WebGain Café


IBM Visual Age for Java


Jcreator


TextPad

Wednesday,
November 13,
2013

20

Getting Started with Java
Programming


A Simple Java Application


Compiling Programs


Executing Applications

Wednesday,
November 13,
2013

21

A Simple Application

Example 1.1

//This application program prints Welcome

//to Java!

package lecture2;


public class Welcome {



public static void main(String[] args) {


System.out.println("Welcome to Java!");


}

}

Wednesday,
November 13,
2013

22

Creating and Compiling Programs


On command line


javac file.java

Wednesday,
November 13,
2013

23



Source Code

Create/Modify Source Code

Compile Source Code

i.e. javac Welcome.java

Bytecode

Run Byteode

i.e. java Welcome

Result

If compilation errors

If runtime errors or incorrect result

Executing Applications


On command line


java classname

Wednesday,
November 13,
2013

24

Java
Interpreter
on Windows
Java
Interpreter
on Sun Solaris
Java
Interpreter
on
Linux
Bytecode
...
Example

javac Welcome.java


java Welcome


output:...

Wednesday,
November 13,
2013

25

Compiling and Running a Program

Wednesday,
November 13,
2013

26

Where are the files
stored in the
directory?


c:
\
example

chapter1

Welcome.class

Welcome.java

chapter2

.

.

.

Java source files and class files for Chapter 2

chapter19

Java source files and class files for Chapter 19

Welcome.java~

Anatomy of a Java Program


Comments


Package


Reserved words


Modifiers


Statements


Blocks


Classes


Methods


The main method

Wednesday,
November 13,
2013

27

Comments

In Java, comments are preceded by two
slashes (
//
) in a line, or 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
*/
.

Wednesday,
November 13,
2013

28

Package

The second line in the program
(
package lecture2;
) specifies a
package name,
lecture2
, for the class
Welcome
. Forte compiles the source
code in Welcome.java, generates
Welcome.class, and stores
Welcome.class in the lecture2 folder.

Wednesday,
November 13,
2013

29

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 1.1 are
public
,
static
, and
void
.

Wednesday,
November 13,
2013

30

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.

Wednesday,
November 13,
2013

31

Statements

A
statement

represents an action or a
sequence of actions. The statement
System.out.println("Welcome to Java!")

in
the program in Example 1.1 is a
statement to display the greeting
"Welcome to Java!" Every statement in
Java ends with a semicolon (;).

Wednesday,
November 13,
2013

32

Blocks

Wednesday,
November 13,
2013

33

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) {

System.out.println("Welcome to Java!");

}

}


Class block

Method block

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 this book. For now,
though, understand that a program is defined
by using one or more classes.


Wednesday,
November 13,
2013

34

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.


Wednesday,
November 13,
2013

35

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;

}

Wednesday,
November 13,
2013

36

Running Java Program

There are two methods which used to run Java
program:
-


-
Application, using command (cmd)

-
Applets, using interface (GUI)

Wednesday,
November 13,
2013

37

The End.





Questions and Answers..!

Wednesday,
November 13,
2013

38