ERJEEML100 - JavaProgramming Part 1 PDF
ERJEEML100 - JavaProgramming Part 1 PDF
ERJEEML100 - JavaProgramming Part 1 PDF
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Course Objective
• To introduce Java Architecture & appreciate basic
syntax in Java Language
• To apply Object Oriented Concepts using Java
• To illustrate how to make use of standard Java Class
Library and create reusable classes.
• To learn what is required to package and deploy a Java
application
• To introduce Exception Handling in Java
• To introduce concepts of Java Runtime Environment to
under stand the important constituents of JVM .
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Learning Outcomes
• To refresh Object Oriented Concepts
• What is an Object?
– An object is a representative or specimen of a class.
Software objects are often used to model real-world
objects you find in everyday life.
Data Data
Function Function
An object communicates
with another object by
passing messages
(invoking methods)
Data Data
Function Function
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Learning Outcomes
• After completion of the module you will be able to
Understand
– The Java Architecture
– How to write a simple program using Java
– How to compile and run a program written using Java
• A general-purpose language
• High-level language
extension .java
this processor.
software.
• The interface that the JVM has to the .class file remains the same
Runtime
Hardware
– In UNIX:
– set PATH=$PATH:$JAVA_HOME/lib/tools.jar:.
• The result
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Learning Outcomes
• After completion of the module you will be able to
– Understand the basic constructs in Java
– Know how to use the modifiers available in Java
– Work with Constructors
– Understand the concept of Method Overloading
– Program using the concept of inheritance.
– Understand the concept of inner classes.
double d;
d = i;
• Whenever a larger type is converted to aType
smaller type,
cast operator
int i;
i = (int) d;
– This prevents accidental loss of data
We enable you to leverage knowledge
38
anytime, anywhere!
Widening and Narrowing
• Widening conversions permitted in Java
– From a byte to a short, an int, a long, a float, or a double
– From a short to an int, a long, a float, or a double
– From an int to a long, a float, or a double
– From a long to a float, or a double
– From a float to a double
• Char -----------------|
• Int ----------- long ----------- float -------
---- double
• Byte ------ Short -|
OPERATOR OPERATION
~ Bit wise Unary NOT
& Bit wise And
| Bit wise OR
^ Bit wise exclusive OR
>> Shift right (Performs a signed shift)
>>> Shift right zero fill (Performs a unsigned shift)
<< Shift left
&= Bit wise AND assignment
|= Bit wise OR assignment
^= Bit wise exclusive OR assignment
>>= Shift right assignment
>>>= Shift right zero fill assignment
<<= Shift left assignment
We enable you to leverage knowledge
42
anytime, anywhere!
Operators and Assignments (Contd..)
• Relational Operators:
OPERATOR OPERATION
== Equal to
!= Not Equal to
> Greater than
< Lesser than
>= Greater than or equal to
<= Less than or equal to
Syntax:
for(declaration : expression)
Which prints:
1234
• Syntax:
• public void method(int … var){ //Definition}
• E.g.-
Constructor
Student(){
//initialize data members
}
Student(String
nameParam){
name =
nameParam;
} Method (Behavior)
public int getrollNo (){
return rollNo;
}
}
The main method may or may not be present depending on whether the class is a starter class
Example:
IntArray = new int[10];
mediumPizza = new Pizza[5];
largePizza = new Pizza[2];
Initializing an Array
<elementType>[] <arayName> =
{<arrayInitializerCode>};
Example:
int IntArray[] = {1, 2, 3, 4};
char charArray[] = {‘a’, ‘b’, ‘c’};
Object obj[] = {new Pizza(), new Pizza()};
String pets[] = {“cats”, “dogs”};
Overloaded methods
• Ex.
void add (int a, int b)
void add (int a, float b)
void add (float a, int b)
void add (int a, int b, float c)
Not overloading. Compiler
error.
class Person{
/*attributes and functionality of Person defined*/
} Student “is a” Person and is more
specialized
class Student extends Person{
/*inherits the attributes and functionalities
of Person and in addition add its own
specialties*/
}
• A subclass cannot access the private members of its super class.
• Inheritance leads to reusability of code
We enable you to leverage knowledge
72
anytime, anywhere!
Inheritance
• Multi-level inheritance is allowed in Java but not multiple
inheritance
GrandParent Paternal Maternal
Parent
Child Child
1)Surgeon Doctor
overrides 1)FamilyDoctor
worksAtHospital
treatPatient adds one new
method ie gives a instance
new definition to variable
the method treatPatient()
2) Adds one
2) Adds one new new method
method
FamilyDoctor
Surgeon makesHouseCalls
subclasses
treatPatient() giveAdvice()
makeIncision()
Calls treatPatient
Redefining a super class method in a sub class method of the
is called method overriding Doctor class
Doctor doctorObj = new Doctor();
doctorObj.treatPatient()
Surgeon surgeonObj = new Surgeon(); Calls treatPatient
method of the
surgeonObj.treatPatient() Surgeon class
We enable you to leverage knowledge
74
anytime, anywhere!
Dynamic Binding
• Can we do this?
– Doctor obj = new Surgeon();
• A reference to a super class can refer to a sub class object
• Now when we say obj.treatPatient(), which version of the
method is called?
– It calls Surgeon’s version of the treatPatient method as the
reference is pointing to a Surgeon object
• If a base class reference is used to call a method, the method
to be invoked is decided by the JVM, depending on the object
the reference is pointing to
– For example, even though obj is a reference to Doctor, it
calls the method of Surgeon, as it points to a Surgeon
object
• This is decided during run-time and termed dynamic or run-
time polymorphism
specific stuff
}
STATIC DYNAMIC
Circle Rectangle
We enable you to leverage knowledge
78
anytime, anywhere!
Abstract (Contd…)
• The abstract keyword can be used for method and class
• An abstract method signifies it has no body
(implementation), only declaration of the method followed
by a semicolon
abstract public double calculateArea();
• If a class has one or more abstract methods declared inside
it, the class must be declared abstract
abstract class Shape{
//Class definition}
• Note:
– An abstract class may also have concrete (complete)
methods
– For design purpose, a class can be declared abstract
even if it does not contain any abstract methods
– Constructors
– Static methods
– Private methods
Animal Pet
Dog
extends implements
Dog
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Learning Outcomes
• After completion of the module you will be able to
– Understand the concept of packages in Java
– Create and use your own packages
– Understand the standard packages available in Java
– Work with Wrapper Classes
– Work with Collections framework.
other packages.
together as groups.
– import mypackage.mysubpackage.MyClass;
LEGAL ILLEGAL
import java.awt.*;
import java.uti.*;
import statements are optional. There
can be any no of import statements.
class MyClass{
//attributes and
functions
}
public class Demo{
Any no of classes and interfaces but
//attributes and there can be only one public class in a
functions given .java file
}
boolean equals(Object)
Class getClass() Represents an unique
ID for the object
int hashCode()
String toString()
Representsana string
Represents unique
message withobject
name of
ID for the
the class that describe
the object
Wrapper Wrapper
Data Type Data Type
Class Class
Set List
(interface) (interface)
SortedSet ArrayList
(interface)
LinkedList Vector
extends
Map
implements
(interface)
SortedMap
(interface)
Properties
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Learning Outcomes
statement(s)
statement(s)
} finally {
statement(s)
Object
Throwable
Error Exception
….. ……
System.out.println("ArithmeticExceptio
n:"+e);
}
finally{
We enable you to leverage knowledge
System.out.println("inside
147
anytime, anywhere!
Throwing Exceptions
• Use the throw clause to throw an exception
• Exceptions in Java are compulsorily of type Throwable
• If you attempt to throw an object that is not throwable, the
compiler refuses to compile your program
• Can also be used to re-throw an exception
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Learning Outcomes
• After completion of the module you will be able to
– Understand the Java Runtime Execution and Memory
allocation to different part of your program.
Conclusion
ER/CORP/CRS/LA10/003
ER/CORP/CRS/ERJEEML100/003
Version 1.00
Conclusion
• Basic programming constructs
• Interfaces & packages
• Exception Handling
• Java Runtime Environment
“© 2007 Infosys Technologies Ltd. All rights reserved. Copyright in the whole and any part of this
document belongs to Infosys Technologies Ltd. This work may not be used, sold, transferred, adapted,
abridged, copied or reproduced in whole or in part, in any manner or form, or in any media, without the
prior written consent of Infosys Technologies Ltd.”