La10 - Java Programming - 01
La10 - Java Programming - 01
(J2EE LC)
Day 1
Course Objective
To illustrate how to make use of standard Java Class Library and create
reusable classes.
Java Applets
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 2
Technologies Ltd Version 1.00
Session Plan
Day 1
– Review of Object Oriented Concepts
– Java architecture
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 3
Technologies Ltd Version 1.00
Class and Object
What is a Class?
What is an Object?
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 4
Technologies Ltd Version 1.00
Example : Objects and Classes
object class
class Student
char name
int rollNo
setName()
Jodie Daria Jane Brittany setRollNo()
R001 R002 R003 R004
calcMarks()
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 5
Technologies Ltd Version 1.00
Features of OOP
Abstraction:
– The process of extracting the essential information and hiding the irrelevant details
Encapsulation:
– The process of binding code and data together in the form of a capsule
Inheritance:
– The feature by which one class acquires the properties and functionalities of another
class
Polymorphism:
– The feature that allows the same interface to be used for a general set of actions
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 6
Technologies Ltd Version 1.00
Object Oriented Program State (Data) is kept
accessible only to a
set of functions.
Behavior of the
object is exposed
using methods.
Data Data
Function Function
An object communicates
with another object by
passing messages
(invoking methods)
Data Data
Function Function
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 7
Technologies Ltd Version 1.00
Introduction to Java
A general-purpose language
High-level language
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 8
Technologies Ltd Version 1.00
Features of Java
Object-oriented
Simpler language
– Compared to earlier OO languages like C++, it is simple
Robust
Secure
– Built -in security features like absence of pointers and confinement of the java program within its
runtime environment
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 9
Technologies Ltd Version 1.00
Features of Java- (Contd…)
Please find more explanation of the previous slide in the notes page
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 10
Technologies Ltd Version 1.00
Platform independence
Once compiled, code will run on any platform without recompiling or any
kind of modification.
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 11
Technologies Ltd Version 1.00
Java Virtual Machine (JVM) (1 of 2)
The source code of Java will be stored in a text file with extension .java
The .class file that is generated is the machine code of this processor.
The interface that the JVM has to the .class file remains the same irrespective of the
underlying platform.
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 12
Technologies Ltd Version 1.00
Java Virtual Machine (JVM) (2 of 2)
The JVM interprets the .class file to the machine language of the underlying
platform.
The underlying platform processes the commands given by the JVM
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 13
Technologies Ltd Version 1.00
Java Architecture:
JVM
Runtime
Hardware
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 14
Technologies Ltd Version 1.00
Java Architecture- (Contd…)
Please find more explanation of the previous slide in the notes page
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 15
Technologies Ltd Version 1.00
Installing and using Java
Before we begin, something on installation
– Java 2 SDK (v1.4 or higher)
– Can be downloaded freely from http://java.sun.com
– Also available in the intranet
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 16
Technologies Ltd Version 1.00
Environment variables used by JVM
JAVA_HOME: Java Installation directory
– This environment variable is used to derive all other env. variables used by JVM
– In Windows: set JAVA_HOME=C:\jdk1.4.3
– In UNIX: export JAVA_HOME=/var/usr/java
CLASSPATH
– In Windows: set PATH=%PATH%;%JAVA_HOME%\lib\tools.jar;.
– In UNIX: set PATH=$PATH:$JAVA_HOME/lib/tools.jar:.
PATH
– Path variable is used by OS to locate executable files
– In Windows: set PATH=%PATH%;%JAVA_HOME%\bin
– In UNIX: set PATH=$PATH:$JAVA_HOME/bin
This approach helps in managing multiple versions of Java – Changing JAVA_HOME will
reflect on CLASSPATH and PATH as well
Set these environment variables on the command prompt and type ‘javac’
– Displays all the options of using ‘javac’
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 17
Technologies Ltd Version 1.00
Source File Layout - Hello World
We will have the source code first
Important :
– Take care!! cAsE of file name matters
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 18
Technologies Ltd Version 1.00
To Compile
– If it does not say anything, and you get the prompt, then the compilation was
successful.
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 19
Technologies Ltd Version 1.00
To execute
java HelloWorldApp
The result
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 20
Technologies Ltd Version 1.00
Compilation & Execution
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 21
Technologies Ltd Version 1.00
Best Practices
One .java file must contain only one class declaration
The name of the file must always be same as the name of the class
Stand alone Java program must have a public static void main defined
– it is the starting point of the program.
– Not all classes require public static void main
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 22
Technologies Ltd Version 1.00
Java Keywords – (For Reference Only)
The reserved keywords defined in the Java language
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 23
Technologies Ltd Version 1.00
Data Types in Java
– Reference type
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 24
Technologies Ltd Version 1.00
Primitive Data Types in Java
Integer data types Notes:
byte (1 byte) All numeric data types are signed
short (2 bytes) The size of data types remain the
int (4 bytes) same on all platforms (standardized)
Logical
boolean (1 byte) (true/false)
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 25
Technologies Ltd Version 1.00
Reference Types in Java (1 of 3)
Objects, Arrays are accessed using reference variables in Java
Java does not support the explicit use of addresses like other languages
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 26
Technologies Ltd Version 1.00
Reference Types in Java (2 of 3)
A reference type can be assigned ‘null’ to show that it is not referring to any
object
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 27
Technologies Ltd Version 1.00
Reference Types (3 of 3)
ChangeChannel(6)
ChangeVolume(INCREASE)
TV (object)
Remote Control
(reference variable)
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 28
Technologies Ltd Version 1.00
Comment entry in Java
comment
in Java */
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 29
Technologies Ltd Version 1.00
Variables in Java
Using primitive data types is similar to other languages
int count;
int max=100;
In Java, if a local variable is used without initializing it, the compiler will show an error
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 30
Technologies Ltd Version 1.00
Variables -- declaration and initialization (1 of 2)
int count
name
type
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 31
Technologies Ltd Version 1.00
Variables -- declaration and assignment (2 of 2)
Assigning a value to the variable
count =10;
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 32
Technologies Ltd Version 1.00
Give it a try….
What will be the output of the following code snippet when you try to compile
and run it?
class Sample{
public static void main (String args[]){
args[]){
int count;
System.out.println(count);
System.out.println(count);
}
}
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 33
Technologies Ltd Version 1.00
Typecasting of primitive data types
int i = 10;
double d;
d = i;
int i;
i = (int) d;
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 34
Technologies Ltd Version 1.00
Typecasting of primitive data types –(Contd…)
Please find more explanation of the previous slide in the notes page
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 35
Technologies Ltd Version 1.00
Operators and Assignments
Arithmetic Operators:
Relational Operators:
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 36
Technologies Ltd Version 1.00
Operators and Assignments –(Contd…)
Please find more explanation of the previous slide in the notes page
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 37
Technologies Ltd Version 1.00
Operators and Assignments –(Contd…)
Please find more explanation of the previous slide in the notes page
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 38
Technologies Ltd Version 1.00
Constituents of a Class
public class Student {
private int rollNo;
rollNo;
Data Members
private String name;
(State)
Student(){
//initialize data members
} Constructor
Student(String nameParam){
nameParam){
name = nameParam;
nameParam;
}
public int getrollNo (){
return rollNo;
rollNo;
Method (Behavior)
}
}
The main method may or may not be present depending on whether the class is a starter class
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 39
Technologies Ltd Version 1.00
Access Modifiers – private and public
The methods which expose the behavior of the object are kept public
– However, we can have helper methods which are private
Access modifiers (public, private etc) will be covered in more details in the later
slides
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 40
Technologies Ltd Version 1.00
Creating Objects in Java
The new operator creates the object and returns a reference to it
Or
new keyword creates
Student obj2 = new Student(“Jack”); an object and returns
a reference to it
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 41
Technologies Ltd Version 1.00
Constructors (1 of 2)
Can be used to initialize the objects to required or default values at the time of
object creation
It is not mandatory for the coder to write a constructor for the class
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 42
Technologies Ltd Version 1.00
Constructors (2 of 2)
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 43
Technologies Ltd Version 1.00
Lifetime of objects (1 of 2)
obj1
-- References: 2
obj2
-- Objects: 2
obj1
obj2
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 44
Technologies Ltd Version 1.00
Lifetime of objects (2 of 2)
obj1
obj3 = obj1;
1
-- References: 3 ob3 heap
2
-- Objects: 2
obj2
obj2 = null;
-- Active References: 2
obj1
-- null references: 1
1
-- Reachable Objects: 1 ob3 heap
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 45
Technologies Ltd Version 1.00
Garbage Collection
In C language, it is the programmer’s responsibility to de-allocate memory
allocated dynamically
– free() function is used to de-allocate
• Other way of explicitly releasing the reference is to set the reference variable to null
• Setting a reference variable to null means that it is not referring to any object
– Stored in the heap area along with the object to which they belong
– Stored in the program stack along with method calls and live until the call ends
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 47
Technologies Ltd Version 1.00
Variables and their scope (2 of 3)
String name;
int x=z+10;
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 48
Technologies Ltd Version 1.00
Variables and their scope (3 of 3)
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 49
Technologies Ltd Version 1.00
Strings in Java
Unlike C, String is a system defined class in Java
Declaring “Hello World” in code will create and object of type string with data “Hello
World” and returns a reference to it.
Unlike C, the string is of fixed length and memory for the string is managed totally by the
String class
– Prevents buffer overruns
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 50
Technologies Ltd Version 1.00
Control Structures in Java
Conditionals
– if-else, else if statements
Loops
– for
– while
– do-while
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 51
Technologies Ltd Version 1.00
Coding standards and Best practices for naming
classes and variables
Class name should begin with uppercase and camel casing
– Eg. Student, ArrayList
Variable name should start with lower case and to follow camel casing
– Eg. int numberOfStudents;
Method names should begin with lowercase and follow camel casing
– Eg. void displayUserChoice()
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 52
Technologies Ltd Version 1.00
Commenting code in Java (1 of 3)
File header
– Description of the file
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 53
Technologies Ltd Version 1.00
Commenting code in Java (2 of 3)
Class header
– description of the class
– Date
– @ author
– @ version
/**
* This class contains a method to print sum of two numbers.
* Date: 12-Jan-2005
* @author E&R Dept, Infosys Technologies Limited
* @version 1.0
*/
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 54
Technologies Ltd Version 1.00
Commenting code in Java (3 of 3)
Method header
– description of the method
– @param
– @return
/**
* Computes the sum of the two integer variables passed
* as parameters
* @param number1 The First number
* @param number2 The Second number
* @return the sum of the two numbers passed as arguments
*/
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 55
Technologies Ltd Version 1.00
Arrays in Java (1 of 7)
Arrays in Java are objects and can be of primitive data types or reference
variable type
20 50 45 100 70
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 56
Technologies Ltd Version 1.00
Arrays in Java (2 of 7)
Declaring Array Variables
<elementType>[] <arrayName>;
or
<elementType> <arrayName>[];
where <elementType> can be any primitive data type or reference type
Example:
int intArray[];
Pizza[] mediumPizza, largePizza;
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 57
Technologies Ltd Version 1.00
Arrays in Java (3 of 7)
Constructing an Array
<arrayName> = new <elementType>[<noOfElements>];
Example:
int intArray[];
Pizza mediumPizza[], largePizza[];
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 58
Technologies Ltd Version 1.00
Arrays in Java (4 of 7)
Example:
int intArray[] = {1, 2, 3, 4};
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 59
Technologies Ltd Version 1.00
Arrays in Java (5 of 7)
If x is a reference to an array, x.length will give you the length of the array
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 60
Technologies Ltd Version 1.00
Arrays in Java (6 of 7)
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 61
Technologies Ltd Version 1.00
Arrays in Java (7 of 7)
When you allocate memory for a multidimensional array, you need only specify
the memory for the first (leftmost) dimension
Ex:
– int twoD[][] = new int[4][];
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 62
Technologies Ltd Version 1.00
this keyword(1 of 2)
class Counter{ class CounterTest{
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 63
Technologies Ltd Version 1.00
this reference (2 of 2)
The ‘this’ reference will always refer to the object that called the method
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 64
Technologies Ltd Version 1.00
static (1 of 4)
– For methods
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 65
Technologies Ltd Version 1.00
static (2 of 4)
static variable
– It is a variable which belongs to the class
static method
– It is a class method
– A static method can only access other static data and methods. It cannot access non-static
members
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 66
Technologies Ltd Version 1.00
static (3 of 4) The static duckCount variable is
initialised to 0, ONLY when the
class is first loaded, NOT each time
Class Duck { a new instance is made
Compilation
error
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 67
Technologies Ltd Version 1.00
static (4 of 4)
static block
– The static block is a block of statement inside a Java class that will be executed
when a class is first loaded and initialized
• A class is loaded typically after the JVM starts
class Test{
static {
//Code
//Code goes here
}
}
– A static block helps to initialize the static data members just like constructors help to
initialize instance members
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 68
Technologies Ltd Version 1.00
Java Native API (JNI)
Such methods are qualified with the keyword native, to show that it is native
code and not implemented using Java
The library has to be loaded in the static block using the method call
System.loadLibrary
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 69
Technologies Ltd Version 1.00
Referring to Java documentation
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 70
Technologies Ltd Version 1.00
Summary:
– Review of Object Oriented Concepts
– Java architecture
ER/CORP/CRS/LA10/003
Copyright © 2005, Infosys 71
Technologies Ltd Version 1.00