Unit I Java Pro

Embed Size (px)

Citation preview

  • 8/3/2019 Unit I Java Pro

    1/42

    INTRODUCTION TO JAVA

    Ms. Asma A. Mokashi

  • 8/3/2019 Unit I Java Pro

    2/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    2

    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.

  • 8/3/2019 Unit I Java Pro

    3/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    3

    Java Milestones

    Year Development

    1990 Sun decided to developed special software that could be usedfor electronic devices. A project called Green Project created

    and head by James Gosling.

    1991 Team announced a new language named Oak

    1992 The team demonstrated the application of their new languageto control a list of home appliances using a hand held device.

    1993 The World Wide Web appeared on the Internet andtransformed the text-based interface to a graphical richenvironment. The team developed Web applets (timeprograms) that could run on all types of computers connectedto the Internet.

  • 8/3/2019 Unit I Java Pro

    4/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    4

    Java Milestones

    Year Development

    1994 The team developed a new Web browsed called Hot Java tolocate and run Applets. HotJava gained instance success.

    1995 Oak was renamed to Java, as it did not survive legalregistration. Many companies such as Netscape and Microsoftannounced their support for Java

    1996 Java established itself it self as both 1. the language for

    Internet programming 2. a general purpose OO language.

    1997- A class libraries, Community effort and standardization,Enterprise Java, Clustering, etc..

  • 8/3/2019 Unit I Java Pro

    5/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    5

    Sun white paper defines Java as:

    Simple and Powerful

    Safe

    Object Oriented Robust

    Architecture Neutral and Portable

    Interpreted and High Performance Threaded

    Dynamic

  • 8/3/2019 Unit I Java Pro

    6/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    6

    Java Attributes

    Familiar, Simple, Small

    Compiled and Interpreted

    Platform-Independent and Portable

    Object-Oriented

    Robust and Secure

    Distributed

    Multithreaded and Interactive

    High Performance

    Dynamic and Extensible

  • 8/3/2019 Unit I Java Pro

    7/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    7

    Java Attributes7

    JAVA AttributesJAVA Attributes

  • 8/3/2019 Unit I Java Pro

    8/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    8

    Simple , Small and Familiar

    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.

  • 8/3/2019 Unit I Java Pro

    9/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    9

    Java is Compiled and Interpreted

    Text Editor Compiler Interpreter

    Programmer

    Source Code

    .java file

    Byte Code

    .class file

    Hardware andOperating System

    Notepad,emacs,vi

    javac javaappletviewernetscape

  • 8/3/2019 Unit I Java Pro

    10/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    10

    Compiled Languages

    Text Editor Compiler linker

    Programmer

    Source Code

    .c file

    ObjectCode

    .o file

    Notepad,emacs,vi

    gcc

    ExecutableCode

    a.out file

  • 8/3/2019 Unit I Java Pro

    11/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    11

    Platform Independent and Portable

    JAVA COMPILERJAVA COMPILER

    JAVA BYTE CODEJAVA BYTE CODE

    JAVA INTERPRETERJAVA INTERPRETER

    Windows 95 Macintosh Solaris Windows NT

    (translator)

    (same forall platforms)

    (one foreach differentsystem)

  • 8/3/2019 Unit I Java Pro

    12/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    12

    Platform independence

  • 8/3/2019 Unit I Java Pro

    13/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    13

    Architecture of Java Applications

    13

    Java applications are written astext files

    The java compiler createsplatform independent code whichis called bytecode.

    Bytecode can be executed by the

    java runtime environment. The Java virtual machine is a

    program which knows how to runthe bytecode on the operatingsystem the JRE is installed upon.

    The JRE translates the bytecodeinto native code, e.g. the native

    code for Linux is different then thenative code for Windows.

    Java code

    is compiled

    to produce

    byte code

    run by Java

    Virtual Machine

    (JVM) to produce

    results

    BA Mosbach / Referent Lars Vogel WS 08/09

  • 8/3/2019 Unit I Java Pro

    14/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    14

    Architecture Neutral & Portable

    Java Compiler - Javasource code (file withextension .java) to bytecode (file with extension.class)

    Bytecode - an intermediate form, closer tomachine representation

    A interpreter (virtual machine) on any targetplatform interprets thebytecode.

  • 8/3/2019 Unit I Java Pro

    15/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    15

    Architecture Neutral & Portable

    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

  • 8/3/2019 Unit I Java Pro

    16/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    16

    Rich Class Environment

    Core Classes

    language

    Utilities

    Input/Output

    Low-Level Networking

    Abstract Graphical User Interface

    Internet Classes

    TCP/IP Networking

    WWW and HTML

    Distributed Programs

  • 8/3/2019 Unit I Java Pro

    17/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    17

    Object Oriented

    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. Encapsulation5. Inheritance

    6. Polymorphism

    7. Binding

  • 8/3/2019 Unit I Java Pro

    18/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    18

    Object Oriented Languages -A Comparison

    Feature C++ ObjectiveC

    Ada Java

    Encapsulation Yes Yes Yes Yes

    Inheritance

    Yes Yes No YesMultiple Inherit. Yes Yes No No

    Polymorphism Yes Yes Yes Yes

    Binding(Earlyor Late) Both Both Early Late

    Concurrency Poor Poor Difficult Yes

    Garbage Collection No Yes No Yes

    Genericity Yes No Yes Limited

    Class Libraries Yes Yes Limited Yes

  • 8/3/2019 Unit I Java Pro

    19/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    19

    Robust & Secure

    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.

  • 8/3/2019 Unit I Java Pro

    20/42

    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.

  • 8/3/2019 Unit I Java Pro

    21/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    21

    Distributed

    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 togetheron a single project.

  • 8/3/2019 Unit I Java Pro

    22/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    22

    Multithreaded and Interactive

    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.

  • 8/3/2019 Unit I Java Pro

    23/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    23

    High Performance

    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.

  • 8/3/2019 Unit I Java Pro

    24/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    24

    Dyanamic & Extensible

    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.

  • 8/3/2019 Unit I Java Pro

    25/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    25

  • 8/3/2019 Unit I Java Pro

    26/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    26

    Hello Java!26

    A simple Java Program

    The virtual machine will start the main method of this class if called via javaHelloWorld

    The filename must be equal to the class name. The extension must be.java.

    class HelloWorld {public static void main (String[] args) {

    System.out.println(Hello Java!);

    }

    }

    BA Mosbach / Referent Lars Vogel WS 08/09

  • 8/3/2019 Unit I Java Pro

    27/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    27

    Example java program// 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 & thefilename must match the classname exactly(case-sensitive)

  • 8/3/2019 Unit I Java Pro

    28/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    28

    Deconstructing a Java program

    // My First Java Program

    There are 3 kinds ofcomments in Java

    1. //

    2. /* */

    1. /** */

    Implementation comments: tells aprogrammer reading your codeabout your implementation

    Documentation comments: used byjavadoc to generate info for users ofyour classes

  • 8/3/2019 Unit I Java Pro

    29/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    29

    Deconstructing a java program// My First Java Program

    import java.lang.*;

    public class HelloWorld {

    public static void main(String[] args) {

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

    }

    }

    Similar to #include inC/C++;

    but only loads a classdynamically, if needed atruntime

    Never import the java.lang API

    it is always automatically imported

  • 8/3/2019 Unit I Java Pro

    30/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    30

    Deconstructing a java program

    public class HelloWorld {

    Defines a new class. The source file must be theexact same name followed by extension .java

    A file can contain more than one class definition, butonly one can be public.

  • 8/3/2019 Unit I Java Pro

    31/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    31

    Deconstructing a Java program

    public static void main(String[] args) {

    public the main method is publicly visible

    The main program must have this exact header:

    static allows the main program to be called withoutcreating an object

    void no value is returned

    String[] array of Strings allows info to be given to themain program ( String args[] is also OK )

  • 8/3/2019 Unit I Java Pro

    32/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    32

    Deconstructing a java program

    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

  • 8/3/2019 Unit I Java Pro

    33/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    33

    Compiling & Running a Java Program

    Step 1: compile into bytecodes

    javacHelloWorld.java

    Step 2: run

    javaHelloWorld

  • 8/3/2019 Unit I Java Pro

    34/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    34

    Compiling & Running a Java Program

    Step 1: compile into bytecodes

    javacHelloWorld.java

    java compiler createsHelloWorld.class file

    (machine independent

    bytecodes)

    must include the.java extension

  • 8/3/2019 Unit I Java Pro

    35/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    35

    Compiling & Running a Java Program

    Step 2: run

    javaHelloWorld

    loads the classes from HelloDate.class, fromjava.lang, and java.util.Date

    verifies the bytecodes

    runs the main program

  • 8/3/2019 Unit I Java Pro

    36/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    36

    The Java Virtual Machine

    Java programs are notcompiled to machine code in thesame way as conventional programming language.

    To support safe execution of compiled code on multipleplatforms (portability, security), they are compiled toinstructions for an abstract machine called theJavaVirtualMachine (JVM).

    The JVM is a specification originally published by SunMicrosystems.

    JVM instructions are called Java byte codes. They are storedin a class file.

    The JVM is a program that runs on a real computer. So

  • 8/3/2019 Unit I Java Pro

    37/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    37

    JIT

    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 theminteractively into machine language so the program

    can run faster. Java performs runtime checks on

    various sections of the code and this is the reason

    the entire code is not compiled at once.[

  • 8/3/2019 Unit I Java Pro

    38/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    38

    Java Tokens

    Keywords

    Identifiers

    Literals

    Operator

    Separators

  • 8/3/2019 Unit I Java Pro

    39/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    39

    Keywords

    abstract boolean break byte case catch

    char class const * continue default do

    double else extends final finally floatfor 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

  • 8/3/2019 Unit I Java Pro

    40/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    40

  • 8/3/2019 Unit I Java Pro

    41/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    41

    Data types

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

    short: The short data type is a 16-bit signed two's

  • 8/3/2019 Unit I Java Pro

    42/42

    Ms. Asma A. Mokashi, Lecturer, MCA

    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

    useful classes rovided b the Java latform