core java

25
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

description

 

Transcript of core java

Page 1: core java

Outline Why Java ?Classes and methods Data typesArrays Control statements Oops concept Abstract class InterfaceRun Java Program in eclipse Hello Word Android App

Page 2: core java

Procedural vs. Object Oriented

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

Object-oriented programmingprogram is composed of a collection objects that communicate with each other

Page 3: core java

JVM

• JVM stands for

Java Virtual Machine

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

Page 4: core java

OS/Hardware

machine codeC source code

myprog.cgcc

myprog.exe

Platform Dependent

JVM

bytecodeJava source code

myprog.javajavac

myprog.class

OS/Hardware

Platform Independent

Page 5: core java

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

Page 6: core java

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.

Page 7: core java

Encapsulation

• Separation between internal state of the object and its external aspects

• How ?• control access to members of the class• interface “type”

Page 8: core java

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

Page 9: core java

Primitive types

• int 4 bytes

• short 2 bytes

• long 8 bytes

• byte 1 byte

• float 4 bytes

• double 8 bytes

• char Unicode encoding (2 bytes)

• boolean {true,false}

Behaviors is exactly as in C++

Note:Primitive typealways beginwith lower-case

Page 10: core java

• Constants

37 integer

37.2 float

42F float

0754 integer (octal)

0xfe integer (hexadecimal)

Primitive types - cont.

Page 11: core java

Wrappers                

Java provides Objects which wrap primitive types and supply methods.

Example:

Integer n = new Integer(“4”);int m = n.intValue();

Page 12: core java

Hello World

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

System.out.println(“Hello World !!!”); }

}

Hello.java

C:\javac Hello.java

C:\java Hello

( compilation creates Hello.class )

(Execution on the local JVM)

Page 13: core java

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 ReferencesThread[] threads; // RecommendedThread threads []; // Legal but less readable

Page 14: core java

Arrays - Multidimensional• In C++

Animal arr[2][2]

Is:

• In Java

What is the type of the object here ?

Animal[][] arr= new Animal[2][2]

Page 15: core java

Flow control

Basically, it is exactly like c/c++.

if/else

do/while

for

switch

If(x==4) { // act1} else { // act2}

int i=5;do { // act1 i--;} while(i!=0);

int j;for(int i=0;i<=9;i++) { j+=i;}

char c=IN.getChar();switch(c) { case ‘a’: case ‘b’: // act1 break; default: // act2}

Page 16: core java

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.

Page 17: core java

Inheritance

Base

Derived

class Base { Base(){} Base(int i) {} protected void foo() {…}}

class Derived extends Base { Derived() {} protected void foo() {…} Derived(int i) { super(i); … super.foo(); }}

Page 18: core java

Inheritance cont..

Class hierarchypackage book;import cert.*; // Import all classes in the cert packageclass 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"); }•}

Page 19: core java

package certification;public class OtherClass {void testIt() { // No modifier means method has default// accessSystem.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();}}

Inheritance cont..

Page 20: core java

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}

Page 21: core java

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

Page 22: core java

Abstract - Examplepackage 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 ... }}

Page 23: core java

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

Page 24: core java

Interface

Page 25: core java

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 interfacepublic abstract interface Rollable { }