Java Programming - Day 1: Long Cycle - JEE
Java Programming - Day 1: Long Cycle - JEE
Course Objectives
To revise Object Oriented Programming concepts To introduce Java architecture and the basic syntax of Java To introduce the implementation of object oriented concepts using Java To introduce the Java library To introduce exception handling in Java To introduce annotations in Java To introduce JUnit To introduce QA4J
Day 2
Day 4
References
Herbert Schildt, The Complete Reference, Java J2SE 5 Edition, Tata McGraw Edition Sierra, Kathy and Bates, Bert, Head First Java, 2nd Edition, Shroff Publishers
Course Pre-Requisites
The participants should have knowledge of C as a programming language
Expectations
At the end of this course, the participants are expected to be proficient in the following
Object Oriented Programming using Java
10
Software Complexity
There are two categories of software
Software developed for individuals for their own use
12
14
procedures operate
The entire problem was divided into a number of smaller units
Functions/Procedures
All these units need to work on a data item to produce the result
The data need to be global Global data made the code complex
15
other
An object is a self-contained entity that contains attributes (data) and behaviors (functions)
Car, Telephone, Pen
For using an object one needs to just invoke a method (function) on the object
No need to know the internal details (data) of the object
16
Behavior
Bark
Wag Tail
Eat
17
Examples
Animal, Human Being, Automobiles, Bank Account, Customer
18
19
(BEHAVIOR)
A e cc r le e at
Number of Gears 5 Seating Capacity 7 Number of Doors 4 Current Speed 45 km/h Current Gear 3
(STATE)
ak Do e (S w lo n) w
Class Car
20
Br
ge an Ch ea G r
object
class
Class Student name rollNo
setName()
setRollNo()
Jodie R001
Daria R002
Jane R003
Brittany R004
getMarks()
21
Abstraction (1/2)
The process of exposing the relevant details and hiding the irrelevant details is called Abstraction
Helps simplify the understanding and using of any complex system One does not have to understand how the engine works to drive a car Similarly one does not have to understand the internal implementation of a
Engine
Driving
22
Abstraction (2/2)
Consider a Stack
The Stack can be implemented using an array or a linked list One need not know this to use the Stack object Just invoke the push method and pop method to make the Stack object work
23
Encapsulation
Encapsulate = En + Capsulate; En = In a; Encapsulate = In a Capsule
Localization of information of knowledge within an object.
Information hiding A cars dashboard hides the complexity and internal workings of its engine.
24
Typically in a class
State is private (not accessible externally)
By enforcing this restriction, object oriented programming allows isolation of complexity in a manageable way
25
26
Polymorphism
Refers to an objects ability to behave differently depending on its type
Poly = many morph = form
Method Overloading is a way to achieve polymorphism Practice of using same method name to denote several different operations
27
Polymorphism
For example, consider a class String which is designed to simplify string operations Append functions are overloaded to accept different types of data One Append function appends an integer value to string, another Append function appends a float value
void Append(int) void Append(float)
The appropriate function will be invoked based on the type of the argument used in the function call Any number of functions can have the same name as long as they differ in any one of the following
Type of arguments Number of arguments
28
UML and UML Class Diagrams Unified Modeling Language (UML) is a set of diagrams which pictorially represent object oriented design UML is extensively used by software engineers to communicate design In OO Design
Pictures are easier to understand than textual explanation
UML Class diagram is a technique to represent classes and their relationships pictorially
29
Employee -name : string -age : int -employeeNumber : int -basicSalary : float -allowances : float +getName() : string +getAge() : int +getEmployeeNumber() : int +getBasicSalary() : float +getAllowances() : float +getTotalSalary() : float UML Class Diagram Representation of Employee class
Some notations in UML + before a member indicates public - before a member indicates private
Member Functions
30
Relationships
Different types of relationships can exist between classes Identifying relationships helps design the objects better
Analogous to relations between entities in RDBMS design
Uses-A
Driver uses a Car
31
32
Summary
Classes and Objects Abstraction Encapsulation Polymorphism UML Class relationships
33
Java Programming
Introduction to Java
Java is a language developed by Sun Microsystems Java was developed initially for consumer devices Now it is a popular platform to develop web based enterprise applications
35
Architecture Neutral/Portable
Example: Java code compiled on Windows can be run on Unix without recompilation Write Once, Run Anywhere
Secure
Built -in security features like absence of pointers
36
Platform Independence
Java is platform independent. A Java program that is written and compiled in one platform can run on any other platform without any recompilation or modification
Write Once Run Anywhere
37
Java Compiler
The source code of Java will be stored in a text file with extension
.java The Java compiler compiles a .java file into byte code
Byte code will be in a file with extension .class Languages like C compiles the program into the machine language format of the hardware platform on which it is running Byte Code is NOT in the machine language format of the hardware platform on which the code is compiled The hardware processor cannot understand the byte code
38
JVM (1/2)
The byte code is in the machine language format of a machine known
as the Java Virtual Machine or the JVM
Needs a JVM to execute the byte code JVM is not a real machine, it is just a virtual machine; implemented in software
39
JVM (2/2)
JVM is platform dependant; that is there is one JVM for Windows, another one for UNIX, yet another one for Mainframe etc All JVMs accept the same input, the byte code Each JVM interprets the byte code into the machine language format of the platform on which it is running The same byte code can be run on any platform, if the JVM for that platform is available
The JVMs for all platforms are available free (http://java.sun.com/) and hence we say that the byte code runs in all platforms
40
Java Architecture
JVM
Compiler (javac)
41
Save the file as HelloWorld.java Take care; case of file name matters
42
To Compile
Open a command prompt Go to the directory in which the source file is saved Type the following command
javac HelloWorld.java
The Java compiler will convert the source code into the byte code
HelloWorld.class
43
To execute
Use the following command to execute the bytecode
java HelloWorld
44
Java Program(.java)
Java Compiler(javac)
Byte Code(.class)
Interpreter(java)
Interpreter(java)
Interpreter(java)
Win32
Linux
Mac
45
All numeric data types are signed The size of data types remain the same on all platforms
short (2 bytes)
int (4 bytes) long (8 bytes)
Floating Type
float (4 bytes) double (8 bytes)
46
The char data type in Java is 2 bytes because it uses UNICODE character set to support internationalization UNICODE is a character set which covers all known scripts and language in the world
Logical
boolean (1 byte) (true/false)
47
Comments in Java
A single line comment in Java will start with //
// This is a single line comment in Java
in Java */
48
49
BEST PRACTICE: Declare a variable in program only when required. Do not declare variables upfront like in C.
50
Local Variables
In Java, if a local variable is used without initializing it, the compiler will show an error
class Sample{ public static void main (String [] args){ int count; System.out.println(count);//Error } }
51
Whenever a larger type is converted to a smaller type, the typecast operator has to be explicitly specified
double d = 10; int i; i = (int) d;
Type cast operator
52
Operators
Operators in Java are very similar to operators in C
Assignment Operators Arithmetic Operators Relational Operators Logical Operators
53
Control Statements
The syntax of the control statements in Java are very similar to that of C language
if if-else for while do-while switch break continue
54
Methods in Java
The syntax of writing methods in Java is similar to that of functions in C Unlike C
All methods in Java should be written inside a class There is no default return type for a Java method
55
Arrays in Java
In Java, all arrays are created dynamically The operator new is used for dynamic memory allocation The following statement creates an array of 5 integers
new int[5]
56
58
Printing a pointer will print the Printing a reference will NOT address stored in it print the address of the object referred by it Pointer arithmetic like incrementing a pointer is valid in the case of a pointer A pointer has to be dereferenced using the * operator to get the value pointed by it We cannot use arithmetic operators on references A reference is automatically de-referenced to give the data referred by it and no special operator is required for this
59
int [] x = null;
60
61
If x is a reference to an array, x.length will give you the length of the array
The for loops can be set up as follows
62
Multidimensional Arrays
Multidimensional arrays are arrays of arrays. To declare a multidimensional array variable, specify each additional index using another set of square brackets.
int [][] x; //x is a reference to an array of int arrays x = new int[3][4]; /*Create 3 new int arrays, each having 4 elements x[0] refers to the first int array, x[1] to the second etc x[0][0] is the first element of the first array
x.length will be 3
x[0].length, x[1].length and x[2].length will be 4 */
63
64
Summary:
Review of Object Oriented Concepts Java architecture The basic constructs in Java Arrays in Java
65
Thank You
The contents of this document are proprietary and confidential to Infosys Technologies Ltd. and may not be disclosed in whole or in part at any time, to any third party without the prior written consent of Infosys Technologies Ltd. 2006 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.