@ Jasmit Singh 1
Crash Course Review Only Please use online resources @ Jasmit Singh 2
Java is an object- oriented language Structured around objects and methods A method is an action or something you do with the object An object has member variables (Instance variables) that define the different properties of the object Java code is written as.java files Compiled into.class files using javac Java run time environment Virtual Machine java 3
Compile-time Java Source (.java) Run-time Class Loader Java Class Libraries Java Compiler Java Bytecodes move locally or through network Java Interpreter Just in Time Compiler Java Virtual machine Java Bytecode (.class ) Runtime System Operating System Hardware 4
JDK freely download from http://www.oracle.com Any text editors can be used to create a.java file Vi/vim, emacs, notepad, wordpad Eclipse IDE Eclipse http://www.eclipse.org Android Development Tools (ADT) is a plugin for Eclipse 5
Write java class HelloWorld containing a main() method and save in file HelloWorld.java The file name MUST be the same as class name Compile with: javac HelloWorld.java Creates compiled.class file: HelloWorld.class Run the program: java HelloWorld Notice: use the class name directly, no.class! 6
File name: HelloWorld.java /* Our first Java program HelloWorld.java */ public class HelloWorld { //main() } Command line arguments public static void main ( String[] args ) { System.out.println( Hello world!" ); } Standard output, print with new line 7
Step 1 l File > New > Java Project l Project Name : HelloWorld Step 2 l File > New > Class l source folder : HelloWorld/src l Package : com.example l Name : HelloWorld l check "public static void main (String[] args) 8
Step 3 l Add your code System.out.println( Hello world! ); Step 4 l Run > Run As > Java Application 9
Fundamental unit of Java program All java programs are classes A class is a template or blueprint for objects A class describes a set of objects that have identical characteristics (data elements) and behaviors (methods). Existing classes provided by JRE User defined classes Each class defines a set of fields (variables), methods or other classes 10
A class describes the attributes and behaviors of an object. An interface defines behaviors that a class implements. You cannot instantiate an interface. An interface does not contain any constructors. All of the methods in an interface are abstract. An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. An interface is not extended by a class; it is implemented by a class. An interface can extend multiple interfaces. @ Jasmit Singh 11
An interface is implicitly abstract. You do not need to use the abstract keyword when declaring an interface. Each method in an interface is also implicitly abstract, so the abstract keyword is not needed. Methods in an interface are implicitly public. @ Jasmit Singh 12
File name: Animal.java /* File name : Animal.java */ interface Animal { } public void eat(); public void travel(); 13
/* File name : MammalInt.java */ public class MammalInt implements Animal{ public void eat(){ System.out.println("Mammal eats"); } public void travel(){ System.out.println("Mammal travels"); } public int nooflegs(){ return 0; } } public static void main(string args[]){ MammalInt m = new MammalInt(); m.eat(); m.travel(); } @ Jasmit Singh 14
Fields (instance variables) in a class are private and providing access to the fields via public methods. Anyone outside the class cannot directly access or modify the fields. Also referred to as data hiding. Users of a class do not know how the class stores its data. Example? 15
Each sub- class acquires all properties of its super- class Information is made manageable in a hierarchical order Keywords extend and implements Animal Mammal Reptile Human Being Dog Lizard @ Jasmit Singh 16
Interface same despite different data types Ability of an object to take on many forms draw() method Circle Shape Triangle Parallelogram Penatgon Hexagon @ Jasmit Singh 17
Abstract class is one that cannot be instantiated All other functionality of the class still exists fields, methods, and constructors Parent class contains common functionality of a collection of child classes, but the parent class itself is too abstract to be used on its own. @ Jasmit Singh 18
An object is an instance of a class An object has state, behavior and identity Internal variable: store state Method: produce behavior Unique address in memory: identity All objects of the same type share code they all have same object type, but can have different field values. 19
public class Dog{ String breed; int age; String color; void barking(){} void hungry(){} void sleeping(){} } Variables Method class: keyword 20
Dog roxy = new Dog(); //declaration roxy.age= 2; //set properties roxy.breed= Pit Bull ; roxy.color= Brown ; Dog sam = new Dog(); sam.age= 5; //set properties sam.breed= Cocker Spanial ; sam.color= White ; roxy.barking(); sam.sleeping(); // call method 21
roxy breed: Roxy age: 2 color: Brown sam breed: Sam age: 5 color: White References Objects allocated in memory 22
Primitive type Size Minimum Maximum Wrapper type boolean 1-bit Boolean char 16-bit Unicode 0 Unicode 2 16-1 Character byte 8-bit -128 +127 Byte short 16-bit -2 15 +2 15-1 Short int 32-bit -2 31 +2 31-1 Integer long 64-bit -2 63 +2 63-1 Long float 32-bit IEEE754 IEEE754 Float double 64-bit IEEE754 IEEE754 Double 23
A package defines the path to a class Uniquely identifies a class with respect to other classes that may have the same class name - avoid conflicts between classes Packages also reflect the folder structure. Give packages meaningful names. Grouping similar classes together makes it easier to search and utilize different classes when programming. @ Jasmit Singh 24
Default (no modifier): Visible to the package Private: Visible to class only Public: Visible to World Protected: Visible to package and all sub- classes 25
Java methods and variables can be declared static These exist independent of any object static methods can be called even if no objects of that class have been created static data is shared by all instances (i.e., one value per class instead of one per instance 26
Operator Meaning == equal to, as in (a == b)!= not equal to, as in (x!= y) > greater than, as in (z > y) < Less than, as in (z<y) <= Less than equal to, as in (z<=y) >= Greater than equal to, as in (z>=y) ((a==b) (a==c)) // true only if A is equal to B or equal to C && ((a==b)&& (a==c)) // true only if A is equal to B and equal to C @ Jasmit Singh 27
A number of different code paths possible that branch from a single variable value A default option to execute if none other cases apply Each case can be terminated with a break statement @ Jasmit Singh 28
char singlechar= z ; switch(singlechar) { case 'a': case 'e': case 'i': case 'o': case 'u': // singlechar is a vowel! Execute this code! break; default: // singlechar is a consonant! Execute this code instead! break; } @ Jasmit Singh 29
Example of If If else If elseif else switch @ Jasmit Singh 30
Type while //loop continuously provided that a statement is true Example int numitemstoprocess = 3; while(numitemstoprocess > 0){ // process an item numitemstoprocess- - ; } do while // conditional loop expression AFTER the first iteration do{ // check for items to process, update numitemstoprocess as required // process item, updated numitemstoprocess } while (numitemstoprocess > 0); for // loop for a specific number of iterations for(int i = 1; i <=100; i++){ // print i } @ Jasmit Singh 31
Examples of Loops @ Jasmit Singh 32
If, else // check for a condition if(ivar==0) { // variable is zero } else if (ivar > 0) { // variable is a positive number } else { // variable is a negative number } InstanceOf // test if an object is of a specific type void checkfortextview(view v) { if(v instanceof TextView) { // This is a TextView control } } else { // This is not a TextView control } @ Jasmit Singh 33
Data structure to store an ordered list of items char avowels[] = {'a','e','i','o','u'}; Size of array int length = avowels.length; Can store primitive types or Objects Dog a = new Dog(); Dog b = new Dog(); Dog c= new Dog(); Dog dogs[] = {a,b,c}; or Dog[] dogs = new Dogs[3]; dogs[1] = new Dog(); dogs[2] = new Dog(); dogs[3] = new Dog(); @ Jasmit Singh 34
Interface for a collection of objects Having them work similarly with a high degree of interoperability High performance implementations Allow for adaptation by extending easily Examples: Interfaces - List, Maps, Sets, SortedMap Classes LinkedList, ArrayList, TreeSet, HashMap, TreeMap @ Jasmit Singh 35
ArrayList<Dog> dogs= new ArrayList<Dog>(); Dog roxy = new Dog( Roxy, 5); Dog sam = new Dog( Sam, 10); dogs.add(roxy); dogs.add(sam); System.out.println( Number of dogs is +dogs.size()); dogs.remove(sam) @ Jasmit Singh 36
HashMap<String,Student> classgrades= new HashMap<String,Student>(); Student st1 = new Student( Charles,3.8); Student st2 = new Student( Raechel, 2.5); Student st3 = new Student( Andrea, 4.0); classgrade.put( CHARLES, st1); classgrade.put( RAECHEL, st2); classgrade.put( ANDREA, st3); System.out.println( Rachel s class grade is + classgrades.get( RAECHEL ).getgrade()); @ Jasmit Singh 37
38