Session01 (Introduction)

33
Java Programing Language SESSION 01 – Introduction to Java

Transcript of Session01 (Introduction)

Page 1: Session01 (Introduction)

Java Programing Language

SESSION 01 – Introduction to Java

Page 2: Session01 (Introduction)

OBJECTIVES• History of Java• Java Virtual Machine• Features of Java• JRE and JDK• Choosing a development environment• Writing a simple Java program• Using the command-line tools• Using Java NetBeans and Eclipse

Page 3: Session01 (Introduction)

History of Java• Developed and maintained by Sun MicroSystems

(Oracle - 2010)– Since 1991– James Gosling– Originally called Oak

• Aimed at producing an operating environment for networked devices and embedded systems– “Write once, run anywhere”– Java is currently one of the most popular

programming language

Page 4: Session01 (Introduction)

The Java history timeline• 1991: Oak Language• 1995: Name changed from Oak to Java• 1996: JDK 1.0• 1997: JDK 1.1• 1998: J2EE, J2SE, J2ME• 2000: J2SE(EE) 1.3• 2002: J2SE(EE) 1.4• 2004: J2SE(EE) 5 (1.5)• 2006: Java SE(EE) 6• 2008: JavaFX 1.0• 2011: Java SE 7

1.01.1

J2SE

J2ME

J2EE

6.0

1.4

7.0

Page 5: Session01 (Introduction)

Java Virtual Machine• Java is both a compiled and an interpreted language

– Source code is compiled into Java bytecode– bytecode is then interpreted by the Java Virtual Machine

(JVM)

Page 6: Session01 (Introduction)

Java Virtual Machine (cont)Java Code

Byte Code (.class)

JVM

Windows LinuxMac

Page 7: Session01 (Introduction)

Java language for Mobile application development

• Android (Java)• Bada (C++)• Black Berry (Java)• iPhone, iPad (Objective-C)• Java Me (Java)• WebOs (C , C++, Javascript, HTML)• Symbian (C++)• Windows Mobile (VC++,C++)• Windows Phone (C#)

Page 8: Session01 (Introduction)

Features of Java• Simple• Object-Oriented• Platform-independent• Robust• Secure• Distributed• Multi-threaded

Page 9: Session01 (Introduction)

Features of Java (cont)• Simple:

– The syntax is mostly derived from C and C++– Java has simplified C++ programming by remove

some of complicated and difficult from C++• Goto statement• Overload operator• Pointer• Header• Union, struct• …

Page 10: Session01 (Introduction)

Features of Java (cont)• Object-Oriented:

– Except for primitive data types, everything in Java is an object

– There are no global functions in Java: all functions are invoked through an object

– Java’s support for Object-Orientation includes inheritance, polymorphism, data access levels…

– Not support multiple inheritance

Page 11: Session01 (Introduction)

Features of Java (cont)• Platform-independent:

– Java was conceived with the concept of WORA: “Write once, Run anywhere"

– Java code (.java) is compiled a byte code which is independent of the system

– Java byte code (.class) can run on any device equipped with a Java Virtual Machine

Page 12: Session01 (Introduction)

Features of Java (cont)• Robust: Java can be used to solve some very

complex programming problems with the supporting of:– Variety of data types– Distributed– Multithreaded– Garbage collection– …

Page 13: Session01 (Introduction)

Features of Java (cont)• Secure:

– No pointers are forged. – No illegal object casts are performed. – There will be no operand stack overflows or

underflows. – All parameters passed to functions are of the

proper types. – Rules regarding private, protected, and public

class membership are followed.

Page 14: Session01 (Introduction)

Features of Java (cont)• Distributed:

– Java facilitates the building of distributed applications by a collection of classes for use in networked applications.

– By using Java's URL class, an application can easily access a remote server. Classes also are provided for establishing socket-level connections.

• Multi-threaded:– Multiple, synchronized threads is built directly into the

Java language and runtime environment– Synchronized threads are useful in creating distributed,

network-aware applications

Page 15: Session01 (Introduction)

Java compared with other languages

• Java and C/C++• Java and C#• Java and script language such as PHP, Perl,

Python, Ruby• Java and JavaScript

Page 16: Session01 (Introduction)

The popularity of JavaProgramming

LanguagePosition

May 2011Position

May 2006Position

May 1996Position

May 1986Java 1 1 3 -

C 2 2 1 1C++ 3 3 2 5C# 4 7 - -

PHP 5 4 - -Objective-C 6 43 - -

Python 7 8 - -Visual Basic 8 5 4 6

Perl 9 6 6 -Ruby 10 18 - -Lisp 15 14 9 3Ada 16 16 8 2

Page 17: Session01 (Introduction)

JDK and JRE• The two principal products in the Java SE

platform: Java Development Kit (JDK) and Java Runtime Environment (JRE).– The JRE provides the libraries, Java virtual machine,

and other components necessary for running applets and applications written in the Java programming language

– The JDK includes the JRE plus development tools such as compilers and debuggers that are necessary or useful for developing applets and applications

Page 18: Session01 (Introduction)

JDK and JRE

Page 19: Session01 (Introduction)

Download and install JDK• The JDK is a development environment for building

applications, applets, and components using the Java programming language.

• JDK can be download at http://www.oracle.com• After downloading the JDK, follow the platform-

dependent installation directions. At the time of this writing, they were available at http://java.sun.com/javase/6/webnotes/install/index.html.

Page 20: Session01 (Introduction)

Navigating the Java Directories• The JDK has the directory structure shown

below:JDK 1.6.0_<version>

Readme.html

jre

bin lib

include

lib

bin

LICENSE

COPYRIGHT

README.txt

demo

Page 21: Session01 (Introduction)

Setting the Execution Path1. Click Start > Control Panel > System on

Windows XP or Start > Settings > Control Panel > System (on Windows XP.)

2. Click Advanced > Environment Variables.3. Add the location of bin folder of JDK

installation for PATH in User Variables and System Variables. A typical value for PATH is: C:\Program Files\Java\jdk1.6.0_<version>\bin

Page 22: Session01 (Introduction)

Write a simple Java programThe first application will simply display the greeting "Hello world!"

• Step 1: Create a source file public class Helloworld {

public static void main(String[] args){System.out.println("Hello world");

}}

• Step 2: Compile the source file into a .class file• Step 3: Run the program

Page 23: Session01 (Introduction)

Write a simple Java programWrite Java code using an editor

javac Helloworld.java

java Helloworld

Java code: Helloworld.java

Bytecode: Helloworld.class

Text Editor

Output

Save the file with a .java extension

Run the Java compiler 'javac'

Execute the bytecode with the command 'java'

This creates a file of bytecode with a .class extension

Page 24: Session01 (Introduction)

Using the Command-Line Tools• Open a shell indow. Go to directory where you

store your Helloworld.java and use javac

Page 25: Session01 (Introduction)

Using the Command-Line Tools• The javac has a set of standard options:

– classpath classpath– d directory– encoding encoding – g – help – nowarn – source release – sourcepath sourcepath – target version – …

Page 26: Session01 (Introduction)

Choosing a Development Environment

• NetBeans• Jbuilder• Eclipse

Page 27: Session01 (Introduction)

Using Java NetBeans

Page 28: Session01 (Introduction)

Using Eclipse

Page 29: Session01 (Introduction)

• Create a Java Project:

Using Eclipse

Page 30: Session01 (Introduction)

• Write a simple program:

Using Eclipse

Page 31: Session01 (Introduction)

Using Eclipse• Program’s output in Console window:

Page 32: Session01 (Introduction)

Comments• Java has three ways of marking comments:

– The most commonmethod is a //– Use the /* and */ comment to block off a longer

comment– Use a /** to start and a */ to end in order to

generate documentation automatically

Page 33: Session01 (Introduction)

Summary• In this session, we learnt:

– History of Java– Java Virtual Machine– Types of Java programs– Features of Java– JRE and JDK– Choosing a development environment– Writing a simple Java program– Using the command-line tools– Using Java NetBeans and Eclipse– Basic of Java