1.Java Basics

download 1.Java Basics

of 28

Transcript of 1.Java Basics

  • 8/8/2019 1.Java Basics

    1/28

    Confidential

    JAVA BASICS

  • 8/8/2019 1.Java Basics

    2/28

    Confidential

    What is Java?

    Java is ahigh-level,third generationprogramming language,like C, Fortranandcan be used to write computer applications

    that crunchnumbers, process words, playgames, store data or do any ofthethousandsof other things computer software can do.

    Javatechnology is botha programminglanguageand a platform.

  • 8/8/2019 1.Java Basics

    3/28

    Confidential

    JAVA PLATFORM

    Aplatform is thehardware or software

    environment in whicha program runs.

    The Java platformhas two components:TheJava Virtual Machine

    TheJava Application Programming Interface (API)

  • 8/8/2019 1.Java Basics

    4/28

    Confidential

    JAVA BUZZ WORDS

    OBJECT ORIENTED

    PLATFORM INDEPENDENT

    SIMPLE

    SECURE

    SUITABLE FOR WEB APPLICATION

    ROBUST

    PORTABLE

    EXCEPTION HANDLING

    MULTI THREADING BUILT IN API

    OPEN SOURCE

  • 8/8/2019 1.Java Basics

    5/28

    Confidential

    OBJECT ORIENTED PROGRAMMING

    In object-oriented programs data is represented by

    objects. Objects havetwo sections

    Fields (Instancevariables) tellyou whatan

    object is.

    Methods tellyou whatan object does.

    These fields and methods are closelytied to the

    object's real world characteristics and behavior.

  • 8/8/2019 1.Java Basics

    6/28

    Confidential

    OOP Principles

    ENCAPSULATION

    Binding ofattributes and behaviour insidea class

    INHERITANCE

    Deriving the properties of one class bythe other

    POLYMORPHISM

    Using the samename for differenttypes of

    actions

  • 8/8/2019 1.Java Basics

    7/28

    Confidential

    PLATFORM INDEPENDENT

    Java is compiled to an intermediate form

    called byte-code,a specialnative program

    called the Java interpreter reads the byte code

    and executes the corresponding native

    machine instructions.

  • 8/8/2019 1.Java Basics

    8/28

    Confidential

    COMPILATION

    .JAVA FILE JAVAC.EXE .CLASS FILE

    EXECUTION

    .CLASS FILE JAVA.EXE .EXE FILE

    SOURCE CODE BYTE CODECOMPILER

    MACINE CODEINTERPRETERBYTE CODE

  • 8/8/2019 1.Java Basics

    9/28

    Confidential

    Portability

    Three features makeJava programs portable:

    1. The language. TheJava language is completely

    specified; all data-type sizes and formats are definedas part ofthelanguage.

    2. The library. TheJava class library is available witha

    Java runtime system, becausea portable program isofno use ifyou cannot usethe same class library on

    every platform.

  • 8/8/2019 1.Java Basics

    10/28

    Confidential

    3. The byte code. TheJava runtime system does

    not compileyour source codeirectly into

    machinelanguage Instead,Java programs aretranslated into machine-independent byte

    code. The byte code is easily interpreted and

    therefore can beexecuted onany platform

    having aJava runtime system.

    Portability

  • 8/8/2019 1.Java Basics

    11/28

    Confidential

    Security

    Therearetwo mainlines of defense: Interpreter level:

    No pointer arithmetic

    Garbage collection

    Array bounds checking No illegal data conversions

    Browser level (applies to applets only):

    No local file I/O

    Sockets back to host only

    No calls to nativemethods

  • 8/8/2019 1.Java Basics

    12/28

    Confidential

    Robustness

    TheJava language is robust. Ithas severalfeatures designed to avoid crashes

    during programexecution, including:

    No pointer arithmetic Garbage collection--no bad addresses

    Arrayand string bounds checking

    No jumping to bad method addresses

    Interfaces and exceptions

  • 8/8/2019 1.Java Basics

    13/28

    Confidential

    EXCEPTIONS HANDLING

    Java is having a specialtype ofexceptionhandling

    mechanism whichallows theexecution ofthe

    programevenafter getting a runtime bug.

    It is handled by using a set of classes & keywords like

    try

    catch

    finally

    throw

    throws

  • 8/8/2019 1.Java Basics

    14/28

    Confidential

    JAVA IS MULTITHREADED

    A single Java program canhavemany

    differentthreads executing independently

    and continuously.

    This makes Javavery responsiveto user input.

    For this it uses a built in features like Thread class

    Runnable interface

  • 8/8/2019 1.Java Basics

    15/28

    Confidential

    BUILT IN API

    Javahas a rich set of collection of built in APIs,

    which facilitates the programmer withmuch

    ofthe programming aspects. Like,

    java.lang

    java.io

    java.util

    java.netand so on.,

  • 8/8/2019 1.Java Basics

    16/28

    Confidential

    Coding Hello World Application

    class HelloWorld

    {

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

    }

  • 8/8/2019 1.Java Basics

    17/28

    Confidential

    SAVING

    We can savea javaapplication by

    Taking the class name whichhas themain()

    Taking the class or interface declared as public

    Tip:

    enclosethe filename in double quotes as Notepadadd athreeletter ".txt" extensionto allthe filesthey save withouttelling you. Thus you can

    unexpectedlyend up witha file called"HelloWorld.java.txt."

  • 8/8/2019 1.Java Basics

    18/28

    Confidential

    Compilation & Execution

    Under Windows, it's similar. You just do this ina DOS

    shell.

    C:> javac HelloWorld.java

    C:> java HelloWorld

    Hello World

    C:>

    Noticethatyou usethe .javaextension whencompiling a file, butyou do not usethe .class

    extension when running a file.

  • 8/8/2019 1.Java Basics

    19/28

    Confidential

    JAVA BUILDING BLOCKS

  • 8/8/2019 1.Java Basics

    20/28

    Confidential

    JAVA TOKENS

    WHITE SPACE

    COMMENTS

    IDENTIFIERS

    LITERALS SEPERATORS

    OPERATORS

    DATA TYPES

    CONTROL STATEMENT

    ARRAYS

    CLASSES, OBJECTS, METHODS

    KEYWORDS

  • 8/8/2019 1.Java Basics

    21/28

    Confidential

    WHITE SPACE

    Blank space introduced for proper functioning

    ofa program

    Java is a free formlanguage whichnever caresabout white spaces

  • 8/8/2019 1.Java Basics

    22/28

    Confidential

    COMMENTS

    Its anonexecutable statement provided to

    geta brief ideaaboutthemodules of

    applications

    Types:

    Singleline://comment

    Multi line:/*......comment..*/

    Documentation:/**comment*/

  • 8/8/2019 1.Java Basics

    23/28

    Confidential

    IDENTIFIERS

    Group of uppercase,lowercase characters,

    numbers,and some special characters

    Rules: No special characters other than _ and $

    It should not start withnumbers

    No space in between

  • 8/8/2019 1.Java Basics

    24/28

    Confidential

    LITERALS

    We can createaliteral ofa datatype by creating

    the constant representation of it.

    Char

    a Numbers 100

    Float 10.09f

    Double 10.09 Boolean true, false

  • 8/8/2019 1.Java Basics

    25/28

    Confidential

    SEPERATORS

    {} Braces

    () Parenthesis

    [] Brackets ; Semi colon

    , Comma

    . Access operator

  • 8/8/2019 1.Java Basics

    26/28

    Confidential

    OPERATORS

    Arithmetic

    Bitwise

    Logical Relational

  • 8/8/2019 1.Java Basics

    27/28

    Confidential

    DATA TYPESTYPE KEYWORD SIZE

    CHARACTER char 16

    NUMBERS byte

    short

    int

    long

    8

    16

    32

    64

    FLOATING Pt float

    double

    3264

    BOOLEAN boolean 1

  • 8/8/2019 1.Java Basics

    28/28

    Confidential

    CONTROL STATEMENTS

    Selection

    if, switch

    Iteration while, do-while, for

    Jump

    break, continue, return