chapter 1.pdf

Upload: labhnesh-jindal

Post on 09-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

  • It is an object-oriented language developed by Sun Microsystem in

    the mid 1990s.

    Original language called Oak

    Intended for embedded systems

    Sun Microsystem describes it as

    "A simple, object-oriented, distributed, interpreted, robust, secure,

    architecture neutral, portable, high-performance, multi-threaded

    and dynamic language.

    What is Java?

  • Java Features

    Simple

    Robust

    Automatic allocation/deallocation

    Multithreaded

    Architecture-neutral

    Operating system upgrade(WORA-Write once run anywhere, any time, forever.)

    Secure

    Portable

    Object-oriented

    Distributed

    Client server programming

    Dynamic

  • Object-Oriented

    Designed to support Object-Oriented concepts

    Distributed

    Applications are constructed using objects. Objects can be

    distributed in multiple locations within a network environment.

    Interpreted

    Java compiles to byte-code (not machine code). Byte code is

    interpreted.

    What is Java? (cont)

  • Robust

    Memory management is done automatically

    Secure

    All Java code subject to security model.

    Architecture-Neutral/Portable

    Compiled Java (byte code) will run on any platform which has a

    Java Virtual Machine

    The Java Virtual Machine is available for almost all platforms...

    Even mainframes.

    What is Java? (cont)

  • High-Performance

    Multi-Threaded

    Processes contain multiple threads of execution.

    Similar to multi-tasking but all threads share the same memory

    space.

    Dynamic

    Classes can be dynamically loaded at any time.

    What is Java? (cont)

  • Java has been described as WORA (Write once, Run Anywhere)

    Because Java source code is compiled to byte code and the byte code is

    interpreted, Java code can be executed anywhere an interpreter is

    available.

    The "Interpreter" is call the Java Virtual Machine

    Platform Independence. How does Java do it?

  • Traditionally, source code had to be compiled for the target hardware and OS platform:

    The Java Virtual Machine.

    Source.cpp

    i386 binary

    SPARC binary

    PPC binary

    Windows

    Compiler

    Solaris

    Compiler

    Mac

    Compiler

  • Java source files (.java) are compiled to Java bytecode (.class)

    Bytecode is interpreted on the target platform within a Java Virtual Machine

    The Java Virtual Machine.

    Source.java

    i386 VM

    SPARC VM

    PPC VM

    Java

    Compiler

    Java

    Bytecode

    Source.class

  • Class

  • Class

    At least one class should exist in java program.

    A java program always consists a number of classes.

    A java source file must also have extension .java.

    Always save the program as class name.java.

  • Class

    class classname{

    type instance-variable1;

    type instance-variable2;

    //

    type instance-variableN;

    type methodname1(parameter-list){

    //body

    }

    type methodname2(parameter-list){

    //body

    }

    type methodnameN(parameter-list){

    //body

    }

    }

  • Program

    class example1{

    public static void main(String args[]){

    System.out.println(Example);

    }

    }

    Compiling and running the program

    Javac example1.java

    Java example1

  • Program

    class example1{

    public static void main(String args[]){

    System.out.println(Example);

    }

    }

    Public: Makes it to accessible to all other classes.

    Static: Interpreter uses this method before any

    objects are created.

    Void: main method does not return any value.

  • Operators and expressions

    Arithmetic operators

    Relational operators

    Logical operators

    Assignment operators

    Increment operators

    Conditional operators

    Bitwise operators

    Special operators

  • Operators and expressions

    Arithmetic operators

    Operator Meaning

    + Addition or unary plus

    - Subtraction or unary minus

    * Multiplication

    / Division

    % Modulo division

  • Operators and expressions

    Relational operators

    Operator Meaning

    < Is less than

    Is greater than

    >= Is greater than or equal to

    == Is equal to

    != Is not equal to

  • Relational operatorsclass relationalOperator{

    public static void main(String args[]){

    float a=15.0,b=20.75,c=15.0;

    System.out.println("a=" + a);

    System.out.println("b=" + b);

    System.out.println("c=" + c);

    System.out.println("a>b is " + (a>b));

    System.out.println("a==c is " + (a==c));

    System.out.println("b!=c " + (b!=c));

    }

    }

  • Relational operatorsclass relationalOperator{

    public static void main(String args[]){

    float a=15.0f,b=20.75f,c=15.0f;

    System.out.println("a=" + a);

    System.out.println("b=" + b);

    System.out.println("c=" + c);

    System.out.println("a>b is " + (a>b));

    System.out.println("a==c is " + (a==c));

    System.out.println("b!=c " + (b!=c));

    }

    }

    a=15.0b=20.75c=15.0a>b is falsea==c is trueb!=c true

  • Logical operator

    Operator Meaning

    && Logical AND

    || Logical OR

    ! Logical NOT

  • Increment and decrement operator

    class example1{

    public static void main(String[] args) {

    int a=10,b=20;

    System.out.println("++a=" + ++a);

    System.out.println("b++=" +b++);

    }

    }

  • Increment and decrement operator

    class example1{

    public static void main(String[] args) {

    int a=10,b=20;

    System.out.println("++a=" + ++a);

    System.out.println("b++=" +b++);

    }

    }

    ++a=11b++=20

  • Conditional operator

    class example1{

    public static void main(String[] args) {

    int a=10,b=20;

    c=a>b?a:b;

    System.out.println("c="+ c);

    }

    }

  • Conditional operator

    lass person{}

    class example1{

    public static void main(String[] args) {

    person p=new person();

    boolean b=p instanceof person;

    System.out.println("p is an object of class

    person " + b);

    }

    }

    c=20

  • Special operator

    1. instanceof

    2. . (dot) operator

  • Special operator

    class person{}

    class example1{

    public static void main(String[] args) {

    person p=new person();

    boolean b=p instanceof person;

    System.out.println("p is an object of class

    person " + b); }

    }

  • Special operator

    class person{}

    class example1{

    public static void main(String[] args) {

    person p=new person();

    boolean b=p instanceof person;

    System.out.println("p is an object of class person

    " + b); }

    }

    p is an object of class person true

  • Objects

    Object

    Using new operator objects are created at run time.

  • Class vs object

    Class Object

    A class creates a new data type that

    can be used to create objects

    Instance of that class.

    Logical construct Physical reality

  • Objects

    Box newbox;//declare reference to Box object

    newbox = new Box();//allocate a Box object

    Box newbox; newbox

    newbox = new Box(); newbox

    null

    Width

    Height

    Depth

  • TPS activity

    Think [~1-2 Min]

    Pair [~2-10 Min]

    Share [~2-10 Min]

  • TPS activity(1)

    public class NewClass {

    int x;

    void test(NewClass ob1)

    {

    ob1. x=100;

    }

    }

  • TPS activity(1)

    public class JavaApplication6 {

    public static void main(String[] args) {

    NewClass n=new NewClass();

    System.out.println ("x=" + n.x);

    n.test(n);

    System.out.println(Now x=" + n.x);

    }

    }

    O/P??

  • TPS activity(1)

    public class JavaApplication6 {

    public static void main(String[] args) {

    NewClass n=new NewClass();

    System.out.println ("x=" + n.x);

    n.test(n);

    System.out.println(Now x=" + n.x);

    }

    }

    O/P

    0

    100

  • Objects

    Box b1=new box();

    Box b2=b1;

    b1

    b2

    Width

    Height

    Depth

  • public class NewClass {

    int x;

    void change()

    {

    x=x+10;

    }

    }

  • Objects

    public static void main(String[] args) {

    NewClass n=new NewClass();

    NewClass n1=n;

    n.change();

    System.out.println("value of x=" + n.x);

    System.out.println("value of x=" + n1.x);

    }

  • Access Protection

    Anything declared public may seen accessed from anywhere.

    Anything declared private cannot be seen outside of its class.

    When a member does not have an explicit access specification,it is visible to subclasses as well as to other classes in the same

    package. This is the default access.

    If you want to allow an element to be seen outside your currentpackage , but only to classes that subclass your class directly,

    then declare that element protected.

  • Class Access Levels

    A class has only two possible access levels.

    Default.

    Public.

    When a class is declared as public, it isaccessible by any other code.

    If a class has default access, then it can only beaccessed by other code within its same

    package.

  • Access Protection

    Private No modifier Protected Public

    Same class yes Yes Yes Yes

    Same package

    subclass

    No Yes Yes Yes

    Same package

    non-subclass

    No Yes Yes Yes

    Different

    package

    subclass

    No No Yes yes

    Different

    package non-

    subclass

    No No No yes

  • class test1

    {

    int y;

    int x;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • class test1

    {

    int y;

    int x;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("x=" + t.x);//0

    System.out.println("y=" + t.y);//0

    }

    }

  • class test1

    {

    private int y;

    private int x;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • class test1

    {

    private int y;

    private int x;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("x=" + t.x);//Error

    System.out.println("y=" + t.y);//Error

    }

    }

  • class test1

    {

    int y;

    int x;

    static int a;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("a="+ t.a);

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • class test1

    {

    int y;

    int x;

    static int a;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("a="+ t.a);//0

    System.out.println("x=" + t.x);//0

    System.out.println("y=" + t.y);//0

    }

    }

  • class test1

    {

    int y;

    int x;

    static int a;

    }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("a="+ test1.a);

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • Overloading methods

    Overloaded methods must differ in type and/ or number of their parameters.

    class overloadex{

    void test(){

    System.out.println(Called with no parameters);

    }

    void test(int a)

    {

    System.out.println(called with one parameter);

    }}

  • Overloaded methods (TPS activity)public class Overloadmethods {

    public static void main(String[] args) {

    NewClass n=new NewClass();

    n.test();

    n.test('c');

    }

    }

  • public class NewClass {

    void test()

    {

    System.out.println("called without parameters");

    }

    void test(double d)

    {

    System.out.println("called with double parameter");

    }

    void test(int a)

    {

    System.out.println("called with integer parameters");

    }

    }

    TPS activity(2)

  • public class NewClass {

    void test()

    {

    System.out.println("called without parameters");

    }

    void test(double d)

    {

    System.out.println("called with double parameter");

    }

    void test(int a)

    {

    System.out.println("called with integer parameters");

    }

    }

    TPS activity(2)

  • public class NewClass {

    void test()

    {

    System.out.println("called without parameters");

    }

    void test(double d)

    {

    System.out.println("called with double parameter");

    }

    }

    TPS activity (3)

  • public class NewClass {

    void test()

    {

    System.out.println("called without parameters");

    }

    void test(double d)

    {

    System.out.println("called with double parameter");

    }

    }

    TPS activity (3)

    called without parameters

    called with double parameter

  • public class NewClass {

    void test()

    {

    System.out.println("called without parameters");

    }

    void test(char d)

    {

    System.out.println("called with char parameter");

    }

    }

    TPS activity (4)

  • public class Overloadmethods {

    public static void main(String[] args) {

    NewClass n=new NewClass();

    n.test();

    n.test(1);

    }

    }

    TPS activity (4)

  • public class Overloadmethods {

    public static void main(String[] args) {

    NewClass n=new NewClass();

    n.test();

    n.test(1);

    }

    }

    TPS activity (4)

  • Constructor A constructor initializes an object immediately upon

    creation. It has same name as the class in which it

    resides and is syntactically similar to a method. Once

    defined the constructor is automatically called

    immediately after the object is created, before the

    new operator completes.

    They have no return type, not even void.

  • class test1

    {

    int y;

    int x;

    static int a;

    test1() {

    System.out.println("default constructor called");

    y=10;

    x=10;

    } }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • Parameterized constructors

  • class test1

    {

    int y;

    int x;

    test1(int x1, int y1) {

    x=x1;

    y=y1; } }

    class default1{

    public static void main(String args[]){

    test1 t=new test1(2,3);

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • class test1

    {

    int y;

    int x;

    test1(int x1, int y1) {

    x=x1;

    y=y1; } }

    class default1{

    public static void main(String args[]){

    test1 t=new test1();

    System.out.println("x=" + t.x);

    System.out.println("y=" + t.y);

    }

    }

  • Example :

    class box{

    double width ;

    double height;

    double depth;

    box(){

    System.out.println(constructing Box);

    width=10;

    height=10;

    depth=10;

    }

    double volume(){

    return width*height*depth;}}

  • class mainbox{

    public static void main(String args[]){

    box mybox1=new box();

    box mybox2=new box();

    double vol;

    vol=mybox1.volume();

    System.out.println(volume is +vol);

    vol=mybox2.volume();

    System.out.println(volume is +vol);

    }

    }

    Output:

    Constructing box

    Constructing box

    Volume is 1000.0

    Volume is 1000.0

  • TPS activity 5public class example {

    int x;

    int y;

    example(){

    x=10;

    y=10;

    }

    example(int a, int b)

    {

    x=a;

    y=b;

    }

    void change()

    {

    x=0;

    y=0;

    }

  • public class Constructor {

    public static void main(String[] args) {

    example ob1=new example();

    example ob2=ob1;

    System.out.println("x=" +ob1.x + "y=" +ob1.y);

    System.out.println("x=" +ob2.x + "y=" +ob2.y);

    ob2.change();

    System.out.println("x=" +ob1.x + " y=" +ob1.y);

    System.out.println("x=" +ob2.x + " y=" +ob2.y);

    }

    }

  • public class Constructor {

    public static void main(String[] args) {

    example ob1=new example();

    example ob2=ob1;

    System.out.println("x=" +ob1.x + "y=" +ob1.y);

    System.out.println("x=" +ob2.x + "y=" +ob2.y);

    ob2.change();

    System.out.println("x=" +ob1.x + " y=" +ob1.y);

    System.out.println("x=" +ob2.x + " y=" +ob2.y);

    }

    }

  • public class example {

    int x;

    int y;

    example(){

    x=10;

    y=10;

    }

    example(int a,int b)

    {

    x=a;

    y=b;

    }

    example(example e)

    {

    x=e.x;

    y=e.y;

    }

    void change()

    {

    x=0;

    y=0;

    }

    }

  • Understanding static

    When objects of class are declared no copy of a static variable is made.

    All instances of the class share the same static variable.

  • Understanding static

    Methods declared as static have several restrictions

    They can only call other static methods.

    They must only access static data.

    They cannot refer to this or super in any way.

  • Understanding static(Example 1)

    public class Staticex {

    static int a=3;

    static int b;

    static void method1(int x)

    {

    System.out.println("x=" + x);

    System.out.println("a=" + a);

    System.out.println("b=" + b);

    }

  • Understanding static(Example 1)

    static

    {

    System.out.println("Static block initialized");

    b=a*4;

    System.out.println("a=" +a);

    }

    public static void main(String[] args) {

    System.out.println("Inside main");

    method1(56);

    }

    }

  • Understanding static(Example 1)

    static

    {

    System.out.println("Static block initialized");

    b=a*4;

    System.out.println("a=" +a);

    }

    public static void main(String[] args) {

    System.out.println("Inside main");

    method1(56);

    }

    }

    Static block initialized

    a=3

    Inside main

    x=56

    a=3

  • Understanding static(Example 2)

    public class Staticex {

    static int a=3;

    static int b;

    int c;

    static void method1(int x)

    {

    System.out.println("x=" + x);

    System.out.println("a=" + a);

    System.out.println("b=" + b);

    }

  • Understanding static(Example 2)

    static

    {

    System.out.println("Static block initialized");

    b=a*4;

    System.out.println(c=" +c);

    }

    public static void main(String[] args) {

    System.out.println("Inside main");

    method1(56);

    }

    }

  • Understanding static(Example 2)

    static

    {

    System.out.println("Static block initialized");

    b=a*4;

    System.out.println(c=" +c);

    }

    public static void main(String[] args) {

    System.out.println("Inside main");

    method1(56);

    }

    }

    Error.

  • Understanding static(Example 3)

    public class Staticex {

    static int a=3;

    static int b;

    static void meth(int x)

    {

    System.out.println("x=" + x);

    System.out.println("a=" + a);

    System.out.println("b=" + b);

    }

  • Understanding static(Example 3)static

    {

    System.out.println("Static block initialized");

    b=a*4;

    System.out.println("a=" +a);

    }

    static

    {

    System.out.println("Another static block");

    }

    public static void main(String[] args) {

    System.out.println("Inside main");

    meth(56);

    } }

  • Garbage collection

    Since object dynamically allocate using new operator and ithandles deallocation automatically.

    The technique that accomplish this is called garbagecollection.

    When no references to an object exists, that object is assumedto be no longer needed and the memory by the object is

    reclaimed.

    So there is no explicit need to destroy objects as in C++.