Features
JVM
Architecture
Security
Data Types
Class and Object
Program
First Java Application
Declaring and creating objects
Arrays
Constructors
this keyword
static keyword
Inheritance
Overriding
Access Modifiers
super keyword
final keyword
abstract classes
interfaces
Exceptions (error handling)
User Defining exceptions
2. Why World Needs Java ?
Dynamic Web pages.
Platform Independent language.
Replacement of C++.
A collection of more wings.
The new programming language from Sun Microsystems.
Allows anyone to create a web page with Java code in it.
Platform Independent language
Java developer James Gosling, Arthur Van , and others.
Oak, The root of Java.
Java is “C++ -- ++ “.
3. Sun : Java Features
Simple and Powerful with High Performance.
Object Oriented approach.
Portable and scalable.
Independent Architecture.
Distributed based.
Multi-threaded.
Robust, Secure/Safe, No network connectivity.
Interpreted Source code.
Dynamic programming platform.
5. Java as Best Object Oriented Program
Object is the key of program.
Simple and Familiar: “C++ Lite”
No Pointers overhead to the developer.
Garbage Collector.
Dynamic Binding.
Single & Multilevel Inheritance with “Interfaces”.
6. Java : JVM
Unicode character.
Unlike other language compilers, Java complier generates code (byte
codes) for Universal Machine.
Java Virtual Machine (JVM): Interprets bytecodes at runtime.
Independent Architecture.
No Linker.
No Loader.
No Preprocessor.
Higher Level Portable Features for GUI : AWT, Applet, Swing.
7. Total Platform Independence
JAVA COMPILER
JAVA BYTE CODE
JAVA INTERPRETER
Windows 95 Macintosh Solaris Windows NT
(translator)
(same for all platforms)
(one for each different system)
9. Java : Architecture
Java Compiler – Name javac, Converts Java source code to bytecode.
Bytecode - Machine representation Intermediate form of Source code &
native code.
JVM-A virtual machine on any target platform interprets the bytecode.
Porting the java system to any new platform involves writing an interpreter
that supports the Java Virtual Machine.
The interpreter will figure out what the equivalent machine dependent
code to run.
10. Java : High Performance Distributed Computing
JVM uses bytecodes.
Small binary class files.
Just-in-time Compilers.
Multithreading.
Native Methods.
Class Loader.
Lightweight Binary Class Files.
Dynamic.
Good communication constructs.
Secure.
12. 12
Definition of Java ?
A programming language:
Object oriented (no friends, all functions are members of classes, no function
libraries -- just class libraries).
Simple (no pointer arithmetic, no need for programmer to deallocate
memory).
Platform Independent.
Dynamic.
Interpreted.
13. 13
Java Data Types
Eight basic types
4 integers (byte, short, int, short) [ example : int a; ]
2 floating point (float, double) [example : double a;]
1 character (char) [example : char a; ]
1 boolean (boolean) [example : boolean a; ]
Everything else is an object
String s;
StringBuffer, StringBuilder, StringTokenizer
14. 14
Java : Class and Object
Declaring a class
class MyClass {
member variables;
…
member functions () ;
…
} // end class MyClass
15. 15
Java Program
Two kinds
Applications
have main() method.
run from the OS prompt (DOS OR SHELL through classpath).
Applets
have init(), start(), stop(), paint(), update(), repaint(), destroy() method.
run from within a web page using HTML <applet> tag.
16. 16
First Java Application
class MyClass {
public static void main(String s [ ] ) {
System.out.println(“Hello World”);
}
} // end class MyClass
17. 17
Declaring and creating objects
Declare a reference
String s;
Create/Define an object
s = new String (“Hello”); Object
Hello
StringPool
18. 18
Java : Arrays
Arrays are objects in Java.
It is a derive object i.e. Integer based or double etc.
A homogeneous contiguous collection of elements of specific datatype.
It’s index starts with 0.
Declaration of Array:
int x [ ] ; // 1-dimensional
int [ ] x ; // 1-dimensional
int [ ] y [ ]; // 2-dimensional
int y [ ][ ];// 2-dimensional
Syntax of Array for allocate space
x = new int [5];
y = new int [5][10];
19. 19
Java : Arrays length
It is used to retrieve the size of an array.
int a [ ] = new int [7]; // 1-dimensional
System.out.println(a.length); //output print ‘7’
int b [ ] [ ] = new int [7] [11];
System.out.println(a.length); //output print ‘7’
System.out.println(b.length * b[0].length); //output print ‘77’
Let int [][][][] array = new int [5][10][12[20] , then …
array.length * array[3rd dimensional].length * array[][2nd dimensional].length *
array[][][1st dimensional].length is 5 x 10 x 12 x 20
20. 20
Java : Constructors
All objects are created through constructors.
Assign Object.
They are invoked automatically.
Return class type.
Mainly public for access anywhere but also private (Singleton class).
Two types: default & parameterize.
class Matter_Weight {
int lb; int oz;
public Matter_Weight (int m, int n ) {
lb = m; oz = n;
}
}
parameterize
21. 21
Java : this keyword
Refer to as “this” local object (object in which it is used).
use:
with an instance variable or method of “this” class.
as a function inside a constructor of “this” class.
as “this” object, when passed as local parameter.
22. 22
Java : this use Example as variable
Refers to “this” object’s data member.
class Rectangle {
int length; int breath;
public Weight (int length, int breath ) {
this. length = length; this. breath = breath;
}
}
23. 23
Java : this use Example as method
Refers to another method of “this” class.
class Rectangle {
public int m1 (int x) {
int a = this.m2(x);
return a;
}
public int m2(int y) {
return y*7 ;
}
}// class close
24. 24
Java : this use Example as function
It must be used with a constructor.
class Rectangle {
int length, breath;
public Rectangle (int l, int b)
{
length = a; breath = b;
}
}
public Rectangle(int m) { this( m, 0); }
}
Constructor is also overloaded
(Java allows overloading of all
methods, including constructors).
25. 25
Java : this use Example as function object,
when passed as parameter
Refers to the object that used to call the calling method.
class MyClass {
int a;
public static void main(String [] s )
{
(new MyClass ()).Method1();
}
public void Method1()
{
Method2(this);
}
public void Method2(MyClass obj)
{ obj.a = 75; }
}
26. 26
Java : static keyword
It means “GLOBAL” for all objects refer to the same storage.
It applies to variables or methods throughout the Program.
use:
with an instance variable of a class.
with a method of a class.
27. 27
Java : static keyword (with variables)
class Order {
private static int OrderCount; // shared by all objects of this class throughout the program
public static void main(String [] s ) {
Order obj1 = new Order();
obj1.updateOdCount();
}
public void updateOdCount()
{
OrderCount++;
}
}
28. 28
Java : static keyword (without methods)
class Math {
public static double sqrt(double m) {
// calculate
return result;
}
}
class MyClass{
public static void main(String [] s ) {
double d;
d = Math.sqrt(9.34);
}
}
30. 30
Manager acts as sub/derived-class of
Employee
class Manager extends Employee {
private double bonus;
public void setBonus(double bb) {
bonus = salary * bb/100;
}
public Manager ( … ) { … }
}
31. 31
Java : Overriding (methods)
class Manager extends Employee {
private double bonus;
public void setSalary(double bb) { …}
public void cal(double dd) {
salary += salary * dd/100 + bonus;
}
public Manager ( … ) { … }
}
Keyword for Inheritance
32. 32
class First {
public First() { System.out.println(“ First class “); }
}
public class Second extends First {
public Second() { System.out.println(“Second class”); }
}
public class Third extends Second {
public Third() {System.out.println(“Third class”);}
}
Java : Role of Constructors in
Inheritance
First class
Second class
Third class
Topmost class constructor is invoked first
(like us …grandparent-->parent-->child->)
33. 33
Java : Access Modifiers
private
Same class only
public
Everywhere
protected
Same class, Same package, any subclass
(default)
Same class, Same package
34. 34
Java : super keyword
Refers to the superclass (base class)
use:
with a variable or method (most common with a method)
as a function inside a constructor of the subclass
35. 35
Java : super with a method
class Manager extends Employee {
private double bonus;
public void setSalary(double bb) { …}
public void cal(double dd) { //overrides cal() of Employee
super.cal(dd); // call Employee’s cal()
salary += bonus;
}
public Manager ( … ) { … }
}
36. 36
Java : super function inside a constructor of the subclass
class Manager extends Employee {
private double bonus;
public void setSalary(double bb) { …}
public Manager ( String name, double salary, double bonus )
{
super(name, salary);
this.bonus = bonus;
}
}
37. 37
Java : final keyword
It means “constant”.
It applies to
variables (makes a constant variable), or
methods (makes a non-overridable or final method)
or
classes (makes a class non-overridable or final means
“objects cannot be created”).
38. 38
Java : final keyword with a variable
class Math {
public final double pi = 3.141;
public static double cal(double x) {
double x = pi * pi;
}
}
note: variable pi is made “Not Overriden”
39. 39
Java : final keyword with a method
class Employee {
protected String name;
protected double salary;
public final void cal(double dd) {
salary += salary * dd/100;
}
public Employee ( … ) { … }
}
Cannot override final method cal() inside the Manager
class
40. 40
Java : final keyword with a class
final class Employee {
protected String name;
protected double salary;
public void cal(double dd) {
salary += salary * dd/100;
}
public Employee ( … ) { … }
}
Not create class Manager as a subclass of class Employee
(all are equal)
41. 41
Java : abstract classes and interfaces
abstract classes
may have both implemented and non-implemented methods.
interfaces
have only non-implemented methods.
(concrete classes or pure classes)
have all their methods implemented.
42. 42
Java : abstract class
abstract class Figure {
public abstract double area();
public abstract double perimeter();
public abstract void print();
public void setOutColor(Color cc) {
// code to set the color
}
public void setInColor(Color cc) {
// code to set the color
}
}
KEYWORD FOR
ABSTRACT
CLASS
43. 43
Java : interface
interface MouseClick {
public void Down();
public void Up();
public void DoubleClick();
}
class PureClick implements Mouse Click {
// all above methods implemented here
}
KEYWORD FOR
INTERFACE
CLASS
44. 44
Java : Exceptions (error handling)
code without exceptions:
...
int x = 7, y = 0, result;
if ( y != 0) {
result = x/y;
}
else {
System.out.println(“y is zero”);
}
...
code with exceptions:
...
int x = 7, y = 0, result;
try {
result = x/y;
}
catch (ArithmeticException ex )
{
System.out.println(“y is zero”);
}
...
A nice way to handle errors in Java programs
45. 45
Java : Exceptions (try-catch-finally block )
import java.io.*;
class Test{
public static void main(String args[]){
int x = 7, y = 0, result;
try {
result = x/y;
/// more code .. Main operations
}
catch (ArithmeticException ex1 ) {
System.out.println(“y is zero”);
}
catch (IOException ex2 ) {
System.out.println(“Can’t read Format error”);
}
finally {
System.out.println(“Closing file”);
/// code to close file, release memory.
}
}}
KEYWORD FOR
Exception CLASS
Package FOR
Exception
CLASS
46. 46
Java : Using Throwing Exceptions
public int divide (int x, int y ) throws ArithmeticException {
if (y == 0 ) {
throw new ArithmeticException();
}
else {
return x/y ;
}
} // end divide()
KEYWORD FOR
Exception CLASS
47. 47
Java : User Defining exceptions
public int divide (int x, int y ) throws MyException {
if (y == 0 ) {
throw new MyException();
}
else {
return a/b ;
}
} // end divide()
}
import java.io.*;
class MyException extends ArithmeticException
{
KEYWORD FOR
Exception CLASS
Package FOR
Exception
CLASS
inherit
Exception
CLASS