1.1_introduction to java

Upload: nayeem1952754

Post on 08-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 1.1_Introduction to Java

    1/24

    Introduction to

    Java

  • 8/7/2019 1.1_Introduction to Java

    2/24

    Session Objectives

    Determine Programming Models

    Features Of Object Oriented Programming Model

    Encapsulation

    Data Abstraction

    Inheritance

    Polymorphism

    History of Java

    Features of Java

    Java vs C++

    Types of Java Programs

  • 8/7/2019 1.1_Introduction to Java

    3/24

    Programming Models

    Procedural programming model - Each problem is divided into smallerproblems and solved using specified modules that act on data

    Object-oriented programming model It perceives theentiresoftwaresystem as a collection of objects which contain attributes and behaviors.

  • 8/7/2019 1.1_Introduction to Java

    4/24

    Object Oriented ProgrammingModel

    Encapsulation - Encapsulation hides the implementation details of anobject and thereby, hides its complexity.

    Data Abstraction It hides the implementation of the data belonging toan object.

    Inheritance - Inheritance creates a hierarchy of classes and helps inthe reuse of the attributes and methods of class.

    Polymorphism Polymorphism triggers different reactions in objects inresponse to thesame message.

  • 8/7/2019 1.1_Introduction to Java

    5/24

    Encapsulation

    Data

    Hiding

  • 8/7/2019 1.1_Introduction to Java

    6/24

    Data Abstraction

    Hiding

    Implementation

    details of the

    Data

  • 8/7/2019 1.1_Introduction to Java

    7/24

    Inheritance

    Derived class

    inherits

    features from

    Base Class

  • 8/7/2019 1.1_Introduction to Java

    8/24

    History of Java

    Java began as Programming Language Oak.

    Oak - Developed by members of Green Project, which included James Goslingand 2 more people in 1991 to create products forelectronics market.

    Existing programming languages were not suited for thesame.

    Chief Programmer of Sun Microsystems, James Gosling, was given the task of

    creating software for controlling consumerelectronic devices.The requirement of Software was to run on different kinds of computers,should

    have power of networks and be based on web.

    Members of the Oak team realized that Java would provide the required crossplatform independence that is independent from hardware,network and OS.

  • 8/7/2019 1.1_Introduction to Java

    9/24

    Features of Java

    Java is an Internet programming language:

    Java programs can be used to access data existingacross the network irrespective of thesource platform

    Java is secure:

    Due to thestrong type-checking done by Java anychanges made to the program are tagged aserrors andthe program will not execute. Java is, therefore,secure

  • 8/7/2019 1.1_Introduction to Java

    10/24

    Java is a platform independent language:

    The Java compiler compiles Java code to an intermediate

    bytecode that is understood by the Java environment. The

    bytecode can be executed by the Java interpreter or anyJava-enabled Internet browser

    Due to the bytecode compilation process and its

    interpretation by a browser, Java programs can be executed

    on various hardware and operating systems

  • 8/7/2019 1.1_Introduction to Java

    11/24

    Java is a high-performance language:

    Java programs are comparable in speed to the programswritten in other compiler-based languages like C and C++.

    Java programs are faster than the programs written in otherinterpreter-based languages, such as BASIC

    Java is simple:

    The syntax of Java is similar to C++. Java does not support

    pointers, multiple inheritance, goto statement, and operatoroverloading

    Features that slow down application development cycle havebeen omitted in Java

  • 8/7/2019 1.1_Introduction to Java

    12/24

    Java Memory Management

    Java prevents errors that arise due to improper memory

    usage because in Java, programmers do not need tomanipulate memory

  • 8/7/2019 1.1_Introduction to Java

    13/24

    Java vs. C++:

    Java is a pure object-oriented language because every

    statement in Java is written inside a class. In C++, the

    main() method is always written outside any classIn Java, all the data types except the primitive types are

    objects. Even the primitive data types can be

    encapsulated inside classes

    Data types:

    Java has two additional primitive data types besides the

    ones available in C++

  • 8/7/2019 1.1_Introduction to Java

    14/24

    The byte data type of Java occupies one byte of memory space

    and can store integral numbers

    The other data type boolean can store any one of the two

    boolean values, true and false Java omits pointers and structs that are available in C++

    In Java, the char data type stores characters in Unicodeformat unlike the 8-bit ASCII format in C++. Unicode is a

    16-bit character format that can store Asian languagealphabets

    Data types in Java have a fixed size, regardless of theoperating system used

  • 8/7/2019 1.1_Introduction to Java

    15/24

    Constructs:

    In Java, the constructs such as if...else, while, anddo...while take expressions that result in a boolean value

    The switch construct and the for loop in Java are similar totheir counterparts in C++

    Inheritance:

    Java does not support multiple inheritance

    All classes in Java are derived from Object class

    To inherit from a class, you use the extends keyword

  • 8/7/2019 1.1_Introduction to Java

    16/24

    Method Overloading:

    Java allows method overloading but does not support

    operator overloading

    Arrays:In C++, an array is a collection of elements where as

    in Java, arrays are real objects because you can

    allocate memory to an array using the new operator

    In Java, array accesses are checked to ensure that theindices are within the range of the array

  • 8/7/2019 1.1_Introduction to Java

    17/24

    JavaPrograms

    Java programs fall into two categories, Applications and Applets

    Applications

    Execute from any operating system prompt

    They can beeither window-based applications orconsole applications

    They have moresecurity privileges than applets

    They reside on the hard disk of a local machine

  • 8/7/2019 1.1_Introduction to Java

    18/24

    Java Programs (Contd.)

    Applets

    Execute in a Java-enabled browser

    They are window-based

    They can access the resources of the host machineonly, they cannot access the files on the computer towhich they are downloaded

    They reside on a remote machine

  • 8/7/2019 1.1_Introduction to Java

    19/24

    SampleApplication:- Welcome.java

    public class Welcome

    {

    public static void main(String args[])

    {

    System.out.println(Hello);

    }

    }

  • 8/7/2019 1.1_Introduction to Java

    20/24

    In Java, the main() method and the class in which it is

    defined should be declared public because the Java

    runtime environment has to access the main() method to

    execute a programThe main() method should be declared static because it

    has to exist before any object of the class is created

    The command line parameter is a String type variable

    main(String args[]). The number of arguments is

    determined by the String class object

  • 8/7/2019 1.1_Introduction to Java

    21/24

    The String Object:

    In Java, a String is a real object and not an array of null-

    terminated characters as in C++

    In Java, the String objects are consistent. The way in

    which strings are obtained and the string elements areaccessed is consistent across all systems. They have well-

    defined programming interfaces

    In Java, the String objects are reliable. They do not give

    rise to memory leaks in a program

  • 8/7/2019 1.1_Introduction to Java

    22/24

    Java uses all the system resources such as the display deviceand the keyboard with the help of the System class. This classcontains all the methods that are required to work with the

    system resourcesThe out object encapsulated inside the System classrepresents the standard output device. This object contains the

    println() method

    The println() method displays data on the screen

  • 8/7/2019 1.1_Introduction to Java

    23/24

    Executing a Java Program:

    Java programs are executed by a program called the Java

    Virtual Machine (JVM). The JVM contains the runtime

    environment and the class loader

    You must save a Java source file with the .java extension.

    The name of the file should be the same as the public class

    name

    When you compile a .java file, a .class file is created

  • 8/7/2019 1.1_Introduction to Java

    24/24

    To compile a file, you use thejavac utility.

    To execute a .class file, you use the java utility

    For example, to compile and execute a class Welcome:

    Save the file as Welcome.java

    At the command prompt, type

    javac Welcome.java At the command prompt, type

    java Welcome