core java
Upcoming SlideShare
Loading in...5

Like this? Share it with your network

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 149 122 12 8 3 2 2

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. 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
  • 2. Procedural vs. Object OrientedFunctional/procedural programming:program is a list of instructions to the computerObject-oriented programmingprogram is composed of a collection objects thatcommunicate with each other
  • 3. JVM• JVM stands for Java Virtual Machine• Unlike other languages, Java “executables” are executed on a CPU that does not exist.
  • 4. Platform Dependentmyprog.c myprog.exe gcc machine code C source code OS/Hardware Platform myprog.class javac bytecode Java source code JVM OS/Hardware
  • 5. 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
  • 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 between internal state of the object and its external aspects• How ? • control access to members of the class • interface “type”
  • 8. 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
  • 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 WorldHello.javaclass Hello { public static void main(String[] args) { System.out.println(“Hello World !!!”); }}C:javac ( compilation creates Hello.class )C:java Hello (Execution on the local JVM)
  • 13. Arraysarrays are objects that store multiple variables of the same type,or variables that are all subclasses of the same typeDeclaring an Array of Primitivesint[] 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
  • 14. Arrays - Multidimensional • In C++ Animal arr[2][2] Is:• In JavaAnimal[][] 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 { charIf(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• 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.
  • 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 …; } }
  • 18. 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"); } •}
  • 19. Inheritance cont..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();}}
  • 20. 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}
  • 21. Abstract• abstract member function, means that the function does not have an implementation.• abstract class, is class that can not be class AbstractTest is an abstract class.It cant be instantiated. new AbstractTest(); ^1 errorNOTE: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 doesnot provide an implementation for any abstract methods declaredin its superclasses must be declared as an abstract class. Example
  • 22. 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 ... }}
  • 23. 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 objects programming interface without revealing its class.
  • 24. Interface
  • 25. 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 anotherinterface.■ An interface cannot implement another interface or class■ An interface must be declared with the keywordinterfacepublic abstract interface Rollable { }