Você está na página 1de 42

INTRODUCTION TO JAVA

Ms. Asma A. Mokashi

History
2


James Gosling, Mike Sheridan, and Patrick Naughton initiated the Java language project in June 1991. Java was originally designed for interactive television, but it was too advanced for the digital cable television industry at the time. The language was initially called Oak after an oak tree that stood outside Gosling's office. It was later renamed Java, from Java coffee, said to be consumed in large quantities by the language's creators.

Ms. Asma A. Mokashi, Lecturer, MCA

Java Milestones
3

Year 1990

Development Sun decided to developed special software that could be used for electronic devices. A project called Green Project created and head by James Gosling. Team announced a new language named Oak The team demonstrated the application of their new language to control a list of home appliances using a hand held device. The World Wide Web appeared on the Internet and transformed the text-based interface to a graphical rich environment. The team developed Web applets (time programs) that could run on all types of computers connected to the Internet.

1991 1992

1993

Ms. Asma A. Mokashi, Lecturer, MCA

Java Milestones
4

Year 1994

Development The team developed a new Web browsed called Hot Java to locate and run Applets. HotJava gained instance success. Oak was renamed to Java, as it did not survive legal registration. Many companies such as Netscape and Microsoft announced their support for Java Java established itself it self as both 1. the language for Internet programming 2. a general purpose OO language. A class libraries, Community effort and standardization, Enterprise Java, Clustering, etc..

1995

1996

1997-

Ms. Asma A. Mokashi, Lecturer, MCA

Sun white paper defines Java as:


5

       

Simple and Powerful Safe Object Oriented Robust Architecture Neutral and Portable Interpreted and High Performance Threaded Dynamic

Ms. Asma A. Mokashi, Lecturer, MCA

Java Attributes
6

        

Familiar, Simple, Small Compiled and Interpreted Platform-Independent and Portable Object-Oriented Robust and Secure Distributed Multithreaded and Interactive High Performance Dynamic and Extensible

Ms. Asma A. Mokashi, Lecturer, MCA

JAVA Attributes
7

Java Attributes

Ms. Asma A. Mokashi, Lecturer, MCA

Simple , Small and Familiar


8

Many features of C and C++ that are either redundant or sources of reliable code are not part of Java. To make the language look familiar to the existing programmers, it was modelled on C and C++ Languages.

Ms. Asma A. Mokashi, Lecturer, MCA

Java is Compiled and Interpreted


9

Programmer

Hardware and Operating System

Source Code Text Editor .java file Notepad, emacs,vi javac Compiler

Byte Code Interpreter .class file java appletviewer netscape

Ms. Asma A. Mokashi, Lecturer, MCA

Compiled Languages
10

Programmer

Source Code Text Editor .c file Notepad, emacs,vi gcc Compiler

Object Code linker .o file

Executable Code a.out file

Ms. Asma A. Mokashi, Lecturer, MCA

Platform Independent and Portable


11

JAVA COMPILER
(translator)

JAVA BYTE CODE


(same for all platforms)

JAVA INTERPRETER
(one for each different system)

Ms. Asma A. Mokashi, Lecturer, MCA

Windows 95

Macintosh

Solaris

Windows NT

Platform independence
12

Ms. Asma A. Mokashi, Lecturer, MCA

Architecture of Java Applications


13

Java applications are written as text files The java compiler creates platform independent code which is called bytecode. Bytecode can be executed by the java runtime environment. The Java virtual machine is a program which knows how to run the bytecode on the operating system the JRE is installed upon. The JRE translates the bytecode into native code, e.g. the native code for Linux is different then the native code for Windows.

Java code

is compiled to produce

byte code

run by Java Virtual Machine (JVM) to produce results


WS 08/09

BA Asma A. / Referent Lars Vogel Ms.MosbachMokashi, Lecturer, MCA

Architecture Neutral & Portable


14

Java Compiler - Java source code (file with extension .java) to bytecode (file with extension .class) Bytecode - an intermediate form, closer to machine representation A interpreter (virtual machine) on any target platform interprets the bytecode.

Ms. Asma A. Mokashi, Lecturer, MCA

Architecture Neutral & Portable


15

Porting the java system to any new platform involves writing an interpreter. The interpreter will figure out what the equivalent machine dependent code to run. Size of primitive data types are machine independent

Ms. Asma A. Mokashi, Lecturer, MCA

Rich Class Environment


16

Core Classes
language Utilities Input/Output Low-Level Networking Abstract Graphical User Interface

Internet Classes
TCP/IP Networking WWW and HTML Distributed Programs

Ms. Asma A. Mokashi, Lecturer, MCA

Object Oriented
17

Java is a true object oriented language . Almost everything in Java is an Object Features of Object Oriented Programming :
1. Object 2. Class 3. Data Abstraction 4. Encapsulation 5. Inheritance 6. Polymorphism 7. Binding
Ms. Asma A. Mokashi, Lecturer, MCA

Object Oriented Languages -A Comparison


18

Feature
Encapsulation Inheritance Multiple Inherit. Polymorphism Binding (Early or Late) Concurrency Garbage Collection Genericity Class Libraries

C++ Yes Yes Yes Yes Both Poor No Yes Yes

Objective C Yes Yes Yes Yes Both Poor Yes No Yes

Ada Yes No No Yes Early Difficult No Yes Limited

Java Yes Yes No Yes Late Yes Yes Limited Yes

Ms. Asma A. Mokashi, Lecturer, MCA

Robust & Secure


19

Robust :
1. It has strict compile time and runtime checking for data types . 2. Garbage Collection- Relieving programmers virtually all memory management problems 3. Exception Handling Which captures series of errors and eliminates any risk of crashing the system.

Ms. Asma A. Mokashi, Lecturer, MCA

20

Secure :
1. Java system verify all memory access 2. Also ensues that no viruses are communicated with an applet. 3. The absence of pointers in Java ensures that programs cannot gain access to memory locations without proper authorization.

Ms. Asma A. Mokashi, Lecturer, MCA

Distributed
21

Java is designed as a language for creating applications on networks. It has the ability to share both data and programs . Java applications can open and access remote objects on internet . This enables multiple programmers at multiple remote locations to collborates and work together on a single project.
Ms. Asma A. Mokashi, Lecturer, MCA

Multithreaded and Interactive


22

Multithreaded means handling multiple task simultaneously. This means that we need not wait for the application to finish one task before beginning another. This features greatly improves the interactive performance of graphical applications.

Ms. Asma A. Mokashi, Lecturer, MCA

High Performance
23

Java performance is impressive for an interpreted langauge , mainly due to the use of intermediate bytecode. Incorporation of multithreading enhances the overall execution speed of Java Programs.

Ms. Asma A. Mokashi, Lecturer, MCA

Dyanamic & Extensible


24

Java is capable of dynamically linking in new class libraries, methods, and objects. Java programs support functions written in other languages such as C and C++. these function are known as native methods Native mthods are linked dynamically at runtime.

Ms. Asma A. Mokashi, Lecturer, MCA

25

Ms. Asma A. Mokashi, Lecturer, MCA

Hello Java!
26

A simple Java Program The virtual machine will start the main method of this class if called via java HelloWorld

class HelloWorld { public static void main (String[] args) { System.out.println(Hello Java!); } }
The filename must be equal to the class name. The extension must be .java.

BA Asma A. / Referent Lars Vogel Ms.MosbachMokashi, Lecturer, MCA

WS 08/09

Example java program


27

// My first Java Program

import java.util.*; public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, Java "); } }

source file: HelloWorld.java

All code must be inside a named class & the filename must match the classname exactly (case-sensitive)

Ms. Asma A. Mokashi, Lecturer, MCA

Deconstructing a Java program


28

// My First Java Program


There are 3 kinds of comments in Java

1. // 2. /* */

Implementation comments: tells a programmer reading your code about your implementation Documentation comments: used by javadoc to generate info for users of your classes

1. /** */

Ms. Asma A. Mokashi, Lecturer, MCA

Deconstructing a java program


29

Similar to #include in import java.lang.*; C/C++; but only loads a class public class HelloWorld { dynamically, if needed at public static void main(String[] args) { runtime

// My First Java Program

System.out.println("Hello, Java "); } }


Never import the java.lang API it is always automatically imported
Ms. Asma A. Mokashi, Lecturer, MCA

Deconstructing a java program


30

public class HelloWorld {


Defines a new class. The source file must be the exact same name followed by extension .java A file can contain more than one class definition, but only one can be public.
Ms. Asma A. Mokashi, Lecturer, MCA

Deconstructing a Java program


31

The main program must have this exact header:

public static void main(String[] args) {


public the main method is publicly visible static allows the main program to be called without creating an object void no value is returned String[] array of Strings allows info to be given to the main program ( String args[] is also OK )
Ms. Asma A. Mokashi, Lecturer, MCA

Deconstructing a java program


32

System.out.println("Hello, it's: ");


Prints a string to the standard output stream System is a java class out is an instance of an OutputStream; defaults to the console
Ms. Asma A. Mokashi, Lecturer, MCA

Compiling & Running a Java Program


33

Step 1: compile into bytecodes javac HelloWorld.java Step 2: run java HelloWorld

Ms. Asma A. Mokashi, Lecturer, MCA

Compiling & Running a Java Program


34

Step 1: compile into bytecodes javac HelloWorld.java


java compiler creates HelloWorld.class file (machine independent bytecodes) must include the .java extension

Ms. Asma A. Mokashi, Lecturer, MCA

Compiling & Running a Java Program


35

Step 2: run java HelloWorld


loads the classes from HelloDate.class, from java.lang, and java.util.Date verifies the bytecodes runs the main program

Ms. Asma A. Mokashi, Lecturer, MCA

The Java Virtual Machine


36

Java programs are not compiled to machine code in the same way as conventional programming language. To support safe execution of compiled code on multiple platforms (portability, security), they are compiled to instructions for an abstract machine called the Java Virtual Machine (JVM).
The JVM is a specification originally published by Sun Microsystems.  JVM instructions are called Java byte codes. They are stored in a class file. Ms. Asma A. Mokashi, Lecturer, MCA  The JVM is a program that runs on a real computer. So any compiled Java program (class file) can be run on any


JIT
37

In one form of Just-In-Time compilation, methods may be compiled to machine code immediately before they are executed for the first time. Then subsequent calls to the method just involve jumping into the machine code. The JIT compiler reads the bytecodes in many sections (or in full rarely) and compiles them interactively into machine language so the program can run faster. Java performs runtime checks on various sections of the code and this is the reason Ms. Asma A. Mokashi, Lecturer, MCA the entire code is not compiled at once.[

Java Tokens
38

    

Keywords Identifiers Literals Operator Separators

Ms. Asma A. Mokashi, Lecturer, MCA

Keywords
39

abstract boolean break byte case catch char class const * continue default do double else extends final finally float for goto * if implements import instanceof int interface long native new null package private protected public return short static super switch synchronized this throw throws transient try void volatile while
Ms. Asma A. Mokashi, Lecturer, MCA

40

Ms. Asma A. Mokashi, Lecturer, MCA

Data types
41

byte: The byte data type is an 8-bit signed two's complement integer. It has a minimum value of 128 and a maximum value of 127 (inclusive). The byte data type can be useful for saving memory in large arrays, where the memory savings actually matters. They can also be used in place of int where their limits help to clarify your code; the fact that a variable's range is limited can serve as a form of documentation.
Ms. Asma A. Mokashi, Lecturer, MCA

short: The short data type is a 16-bit signed two's complement integer. It has a minimum value of -

42

float: The float data type is a single-precision 32-bit IEEE 754 floating point. Its range of values is beyond the scope of this discussion, but is specified in section 4.2.3 of the Java Language Specification. As with the recommendations for byte and short, use a float (instead of double) if you need to save memory in large arrays of floating point numbers. This data type should never be used for precise values, such as currency. For that, you will need to use the java.math.BigDecimal class instead. Numbers and Strings covers BigDecimal and other Ms. Asma A. Mokashi, Lecturer, MCA useful classes provided by the Java platform.

Você também pode gostar