Basic Java Objectives o o Understand the basic

Basic Java

Objectives o o Understand the basic concepts of Java Language Features Object Oriented Principles Classes and Objects

Java Architecture - JVM Windows Platform Solaris Platform

Java system overview Java and the JVM 4

Features of Java o o o Object Oriented Interpreted Portability Secure Multithreaded Automatic memory management: Garbage Collection

Basic Concepts in Java

Data Types Primitive Types Type Default Size Range boolean false 1 bit false, true byte 0 8 bits -28 – (28 -1) char u 0000 16 bits -216 – (216 -1) short 0 16 bits -216 – (216 -1) int 0 32 bits -232 – (232 -1) long 0 64 bits -264 – (264 -1) float 0. 0 32 bits -/+(1. 4 e-45 – 3. 4 e+38) double 0. 0 64 bits -/+(4. 94 e-324 – 1. 79 e+308)

Data Types o Wrapper classes around all primitive data types e. g. : Integer class is a wrapper of int. o String o Object to store a sequence of characters o Advanced Data Types o Arrays: to store set of values/objects of similar type o Collections: to store set of objects of different types

o Why do we need Wrapper class for every primitive data type? o This enables the primitive data types to be used in Object Oriented manner o If primitive data types do not support Object Oriented approach then why does Java as a OOP language support them? o It is faster and easier to process primitive data types at the machine level. o These can be used with out impacting the implementation of basic OOP concepts and platform independence of Java.

Operators Operator Use Description + op 1 + op 2 Adds op 1 and op 2 - op 1 – op 2 Subtracts op 2 from op 1 * op 2 Multiplies op 1 and op 2 / op 1 / op 2 Divides op 1 by op 2 % op 1 % op 2 Computes the remainder of dividing op 1 by op 2 we can use % with float data type

Type Casting o Automatic Type Casting o Between compatible types o Destination type is larger size than source type o Casting incompatible types o Explicit usage of casting needed o (target-type) value o e. g. int a; byte b; b = (byte) a;

Control Statements o. If-else statement oif (expression) { statement(s) } else { statement(s) } oif-else-if ladder oif (expression) { statement(s) } else { statement(s) }

Quiz int b=1; if(b) { // if(b==1) System. out. println("This will print"); } else { System. out. println("This will not print"); } What is O/P is this code?

Control Statements (contd. ) owhile statement owhile (expression) { statement(s) } odo-while statement odo { statement(s) } while (expression) ofor statement ofor (initialization; termination; increment) { statement(s) }

Control Statements (contd. ) oswitch-case statement oswitch (integer expression) { case integer expression: statement(s) break; . . . default: statement(s) break; }

Classes o A class is a blueprint, or prototype, that defines the state(variables) and the behavior(methods) common to all items(objects) of a certain kind.

Classes Ex. class student { int roll. No; String name; . . Void get. Data(){ ………} Void show. Data(){ ………}. . } Attributes Methods

Encapsulation in Java o Hides information/implementation about a Java class o The user of an object can view the object as a black box that provides services. o Encapsulation in Java is implemented through objects

How can we create objects? o Constructors: o Special method of a class o Gets called when the class is instantiated – object is created e. g: Student s = new Student(); Constructor o Memory is allocated for the object and a reference to the object is returned

How can we create objects? (cont. ) o Default Constructor: n Java compiler creates a default constructor if no constructor is defined in the class. n Default constructor does not have any arguments and does not implement any functionality. e. g. class Student { Student() {} …. }

Access Specifiers o o private protected public package

Access Specifiers Access Specifier Classes in the same package Classes in other packages Subclasses in in the other same packages package public Yes Yes protected Yes No Yes default Yes No private No No

Keywords: Static n Helps in creating class level members n Methods/variables/inner classes can be static n Static members of a class/interface belong to the class/interface and not to their instances. n The various instances share the static members

Ex. class Static. Demo { static int i; public static void show. I() { System. out. print("n I am in the static Mathod I= "+i); } static { System. out. print("n I am in the static block I= "+i); // Initial value of I i=100; } public static void main(String[] args) { show. I(); } }

Inheritance o Allows programmers to customize a class for a specific purpose, without actually modifying the original class (the superclass) o The derived class (subclass) is allowed to add methods or redefine them o The subclass can add variables, but cannot redefine them

Inheritance Example o Class C is a subclass of class B (its superclass) if its declaration has the form class C extends B { … }

Inheritance in Java (cont. ) n Usage o Super keyword class Student extends Person{ public Student(){ super(); }. . // all the methods and variables of Person are inherited by Student } super keyword in Java allows you to access methods only in the parent class, one level up in the hierarchy. Does not allow access of multi-level hierarchy

o This keyword is used to access the members of current instance. o Final keyword n n n Methods/variables/classes can be final Final methods cannot be over-ridden Final variables cannot be reassigned Final classes cannot be extended Final cannot be used with abstract class

Polymorphism in Java n n One name, multiple behaviors Types: o Overriding n Method signature and name is the same. n Is done during inheritance. The overridden method is the superclass method n late/dynamic binding: Method to be called decided at runtime o Overloading n Method signature is different while the method name is same n Early/static binding: Method to be called decided at compile time void cal. Sal(int emp. ID){ //calculating salary by emp. ID …. } void cal. Sal(String desig){ //calculating salary by designation …. . }

Memory Management in Java o Done by Java Garbage Collector. o Garbage Collector o Frees memory of objects for which no references exist e. g. : Bike b = new Bike(); ……. . b = null; //release reference and gc picks up b o Called by the Java Virtual Machine when running out of memory o Timing and algorithm used to do Garbage Collection is JVM specific o Note: Developers can only request the JVM to run garbage collection by calling gc(), no guarantee of when garbage collection would be run. However it is not recommended for performance reasons

Memory Management in Java (cont. ) o. Finalize method n Called just before the object is garbage collected. n Ultimate last checks on releasing external resources (open N/W or database connections, files etc. ) should be done in the finalize method.

Limitations of Java o Java is slower because of o Type checking o Security o Two stage compilation o Memory manipulation not possible o No access to pointers. All memory access is done internally by JVM

Thank You!
- Slides: 33