Outline
  Why Java ?
  Classes and methods
  Data types
  Arrays
  Control statements
  Oops concept
  Abstract class
  Interface
  Run Java Program in eclipse
  Hello Word Android App
Procedural vs. Object Oriented


Functional/procedural programming:
program is a list of instructions to the computer

Object-oriented programming
program is composed of a collection objects that
communicate with each other
JVM
• JVM stands for

     Java Virtual Machine

• Unlike other languages, Java “executables” are executed on a
  CPU that does not exist.
Platform Dependent

myprog.c                                 myprog.exe
                              gcc          machine code
  C source code

                                           OS/Hardware



                  Platform Independent

myprog.java                               myprog.class
                              javac           bytecode
 Java source code

                                                JVM

                                            OS/Hardware
Class
 • A template that describes the kinds of state and behavior that
   objects of its type support.
 • object is an instance of class
 • class groups similar objects
   • same (structure of) attributes
   • same services
 • object holds values of its class’s attributes
Objects
• At runtime, when the Java Virtual Machine (JVM) encounters
  the new keyword,

• it will use the appropriate class to make an object which is an
  instance of that class.

• That object will have its own state, and access to all of
 the behaviors defined by its class.
Encapsulation
• Separation between internal state of the object and its external
  aspects

• How ?
  • control access to members of the class
  • interface “type”
What does it buy us ?
• Modularity
  • source code for an object can be written and maintained
    independently of the source code for other objects
  • easier maintainance and reuse
• Information hiding
  • other objects can ignore implementation details
  • security (object has control over its internal state)
• but
  • shared data need special design patterns (e.g., DB)
  • performance overhead
Primitive types

•   int     4 bytes
•   short   2 bytes
•   long    8 bytes
                         Behaviors is
                         exactly as in
•   byte    1 byte
                         C++
•   float   4 bytes
•   double 8 bytes
•   char    Unicode encoding (2 bytes)   Note:
                                         Primitive type
•   boolean {true,false}                 always begin
                                         with lower-case
Primitive types - cont.

• Constants
  37     integer
  37.2   float
  42F    float
  0754   integer (octal)
  0xfe   integer (hexadecimal)
Wrappers

       Java provides Objects which wrap
       primitive types and supply methods.
Example:

           Integer n = new Integer(“4”);
           int m = n.intValue();
Hello World
Hello.java
class Hello {
      public static void main(String[] args) {
            System.out.println(“Hello World !!!”);
      }
}



C:javac Hello.java   ( compilation creates Hello.class )

C:java Hello              (Execution on the local JVM)
Arrays
arrays are objects that store multiple variables of the same type,
or variables that are all subclasses of the same type



Declaring an Array of Primitives

int[] key;    // Square brackets before name (recommended)
int key []; // Square brackets after name
 (legal but less// readable)

Declaring an Array of Object References
Thread[] threads; // Recommended
Thread threads []; // Legal but less readable
Arrays - Multidimensional
  • In C++

        Animal arr[2][2]

  Is:

• In Java
Animal[][] arr=
   new Animal[2][2]



         What is the type of
         the object here ?
Flow control
   Basically, it is exactly like c/c++.

                           do/while
                      int i=5;
                                                 switch
  if/else             do {                char
If(x==4) {              // act1           c=IN.getChar();
  // act1               i--;              switch(c) {
} else {              } while(i!=0);        case ‘a’:
  // act2                                   case ‘b’:
}                              for             // act1
                                               break;
                int j;
                                            default:
                for(int i=0;i<=9;i++)
                                               // act2
                {
                                          }
                  j+=i;
                }
Access Control
• public member (function/data)
  • Can be called/modified from outside.
• protected
  • Can be called/modified from derived classes
• private
  • Can be called/modified only from the current class
• default ( if no access modifier stated )
  • Usually referred to as “Friendly”.
  • Can be called/modified/instantiated from the same package.
Inheritance   class Base {
                  Base(){}
                  Base(int i) {}
                  protected void foo() {…}
              }
    Base
              class Derived extends Base {
                  Derived() {}
                  protected void foo() {…}
                  Derived(int i) {
                    super(i);
   Derived           …
                    super.foo();
                  }
              }
Inheritance cont..

 Class hierarchy
 package book;
 import cert.*; // Import all classes in the cert package
 class Goo {
 public static void main(String[] args) {
 Sludge o = new Sludge();
 o.testIt();
 }}
 Now look at the second file:
 package cert;
 public class Sludge {
 public void testIt() { System.out.println("sludge"); }
 •}
Inheritance cont..
package certification;
public class OtherClass {
void testIt() { // No modifier means method has default
// access
System.out.println("OtherClass");
}
}
In another source code file you have the following:
package certfcation;
import certification.OtherClass;
class AccessClass {
static public void main(String[] args) {
OtherClass o = new OtherClass();
o.testIt();
}
}
Polymorphism
• Inheritance creates an “is a” relation:
For example, if B inherits from A, than we say that “B is also an A”.
   Implications are:
class PlayerPiece extends GameShape implements Animatable {
public void movePiece() {
System.out.println("moving game piece");
}
public void animate() {
System.out.println("animating...");
}
// more code}
Abstract
• abstract member function, means that the function
  does not have an implementation.
• abstract class, is class that can not be instantiated.
AbstractTest.java:6: class AbstractTest is an abstract class.
It can't be instantiated.
        new AbstractTest();
        ^
1 error

NOTE:
An abstract class is not required to have an abstract method in it.
But any class that has an abstract method in it or that does
not provide an implementation for any abstract methods declared
in its superclasses must be declared as an abstract class.

                                                  Example
Abstract - Example
package java.lang;
public abstract class Shape {
       public abstract void draw();
       public void move(int x, int y) {
         setColor(BackGroundColor);
         draw();
         setCenter(x,y);
         setColor(ForeGroundColor);
         draw();
       }
}


package java.lang;
public class Circle extends Shape {
       public void draw() {
        // draw the circle ...
      }
}
Interface
Interfaces are useful for the following:
• Capturing similarities among unrelated classes without
  artificially forcing a class relationship.
• Declaring methods that one or more classes are expected to
  implement.
• Revealing an object's programming interface without revealing
  its class.
Interface
When to use an interface ?
    interface methods are abstract, they cannot be
     marked final,

■ An interface can extend one or more other interfaces.

■ An interface cannot extend anything but another
interface.

■ An interface cannot implement another interface or class

■ An interface must be declared with the keyword
interface
public abstract interface Rollable { }

core java

  • 1.
    Outline WhyJava ? Classes and methods Data types Arrays Control statements Oops concept Abstract class Interface Run Java Program in eclipse Hello Word Android App
  • 2.
    Procedural vs. ObjectOriented Functional/procedural programming: program is a list of instructions to the computer Object-oriented programming program is composed of a collection objects that communicate with each other
  • 3.
    JVM • JVM standsfor Java Virtual Machine • Unlike other languages, Java “executables” are executed on a CPU that does not exist.
  • 4.
    Platform Dependent myprog.c myprog.exe gcc machine code C source code OS/Hardware Platform Independent myprog.java myprog.class javac bytecode Java source code JVM OS/Hardware
  • 5.
    Class • Atemplate that describes the kinds of state and behavior that objects of its type support. • object is an instance of class • class groups similar objects • same (structure of) attributes • same services • object holds values of its class’s attributes
  • 6.
    Objects • At runtime,when the Java Virtual Machine (JVM) encounters the new keyword, • it will use the appropriate class to make an object which is an instance of that class. • That object will have its own state, and access to all of the behaviors defined by its class.
  • 7.
    Encapsulation • Separation betweeninternal state of the object and its external aspects • How ? • control access to members of the class • interface “type”
  • 8.
    What does itbuy us ? • Modularity • source code for an object can be written and maintained independently of the source code for other objects • easier maintainance and reuse • Information hiding • other objects can ignore implementation details • security (object has control over its internal state) • but • shared data need special design patterns (e.g., DB) • performance overhead
  • 9.
    Primitive types • int 4 bytes • short 2 bytes • long 8 bytes Behaviors is exactly as in • byte 1 byte C++ • float 4 bytes • double 8 bytes • char Unicode encoding (2 bytes) Note: Primitive type • boolean {true,false} always begin with lower-case
  • 10.
    Primitive types -cont. • Constants 37 integer 37.2 float 42F float 0754 integer (octal) 0xfe integer (hexadecimal)
  • 11.
    Wrappers Java provides Objects which wrap primitive types and supply methods. Example: Integer n = new Integer(“4”); int m = n.intValue();
  • 12.
    Hello World Hello.java class Hello{ public static void main(String[] args) { System.out.println(“Hello World !!!”); } } C:javac Hello.java ( compilation creates Hello.class ) C:java Hello (Execution on the local JVM)
  • 13.
    Arrays arrays are objectsthat store multiple variables of the same type, or variables that are all subclasses of the same type Declaring an Array of Primitives int[] key; // Square brackets before name (recommended) int key []; // Square brackets after name (legal but less// readable) Declaring an Array of Object References Thread[] threads; // Recommended Thread threads []; // Legal but less readable
  • 14.
    Arrays - Multidimensional • In C++ Animal arr[2][2] Is: • In Java Animal[][] arr= new Animal[2][2] What is the type of the object here ?
  • 15.
    Flow control Basically, it is exactly like c/c++. do/while int i=5; switch if/else do { char If(x==4) { // act1 c=IN.getChar(); // act1 i--; switch(c) { } else { } while(i!=0); case ‘a’: // act2 case ‘b’: } for // act1 break; int j; default: for(int i=0;i<=9;i++) // act2 { } j+=i; }
  • 16.
    Access Control • publicmember (function/data) • Can be called/modified from outside. • protected • Can be called/modified from derived classes • private • Can be called/modified only from the current class • default ( if no access modifier stated ) • Usually referred to as “Friendly”. • Can be called/modified/instantiated from the same package.
  • 17.
    Inheritance class Base { Base(){} Base(int i) {} protected void foo() {…} } Base class Derived extends Base { Derived() {} protected void foo() {…} Derived(int i) { super(i); Derived … super.foo(); } }
  • 18.
    Inheritance cont.. Classhierarchy package book; import cert.*; // Import all classes in the cert package class Goo { public static void main(String[] args) { Sludge o = new Sludge(); o.testIt(); }} Now look at the second file: package cert; public class Sludge { public void testIt() { System.out.println("sludge"); } •}
  • 19.
    Inheritance cont.. package certification; publicclass OtherClass { void testIt() { // No modifier means method has default // access System.out.println("OtherClass"); } } In another source code file you have the following: package certfcation; import certification.OtherClass; class AccessClass { static public void main(String[] args) { OtherClass o = new OtherClass(); o.testIt(); } }
  • 20.
    Polymorphism • Inheritance createsan “is a” relation: For example, if B inherits from A, than we say that “B is also an A”. Implications are: class PlayerPiece extends GameShape implements Animatable { public void movePiece() { System.out.println("moving game piece"); } public void animate() { System.out.println("animating..."); } // more code}
  • 21.
    Abstract • abstract memberfunction, means that the function does not have an implementation. • abstract class, is class that can not be instantiated. AbstractTest.java:6: class AbstractTest is an abstract class. It can't be instantiated. new AbstractTest(); ^ 1 error NOTE: An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses must be declared as an abstract class. Example
  • 22.
    Abstract - Example packagejava.lang; public abstract class Shape { public abstract void draw(); public void move(int x, int y) { setColor(BackGroundColor); draw(); setCenter(x,y); setColor(ForeGroundColor); draw(); } } package java.lang; public class Circle extends Shape { public void draw() { // draw the circle ... } }
  • 23.
    Interface Interfaces are usefulfor the following: • Capturing similarities among unrelated classes without artificially forcing a class relationship. • Declaring methods that one or more classes are expected to implement. • Revealing an object's programming interface without revealing its class.
  • 24.
  • 25.
    When to usean interface ?  interface methods are abstract, they cannot be marked final, ■ An interface can extend one or more other interfaces. ■ An interface cannot extend anything but another interface. ■ An interface cannot implement another interface or class ■ An interface must be declared with the keyword interface public abstract interface Rollable { }