Java Technology The Basic Constructs in Java
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Session Plan
Concept of Object Oriented Programming
Java architecture
Rules of Java
Data types
Variables
Operators
Control statements
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
2
References
Patrick Naughton & Herbert Schildt, Java2: The Complete Reference 3rd Ed., Tata McGraw-ill Publishing Company Limited, New Delhi.
Eckel & Bruce, Thinking in Java, 3rd Ed., Addison-Wesley.
"The Java Tutorial", found online at http://java.sun.com/docs/books/tutorial/
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
3
History of Java
Java is an object-oriented programming language developed by James Gosling and colleagues at Sun Microsystems in the early 1990s.
Java was started as a project called "Oak" by James Gosling in June 1991.
The first public implementation was Java 1.0 in 1995.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
4
Introduction To Java
A language developed at Sun Microsystems
A general-purpose language
High-level language
Developed initially for consumer devices
Helps in building a dynamic Web
Supported today by most of the big players like IBM, Oracle etc.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
5
Features of Java
Object-oriented
Simple
Robust
Secure
Architecture Neutral / Portable
Multithreaded
Distributed
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
6
What is Java?
Object Oriented Language
Platform Independent
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
7
What is an Object?
Objects are things
PC Monitor, Employee, Space Shuttle, Complex Number, (X+Yi), Bank Account, Bicycle
Objects may be simple or complex
Objects may be real or imaginary
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
8
Object Oriented Program Data
Data
Function
Function
Data
Data
Function
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Function
9
Object Oriented Concepts Abstraction
Encapsulation
Oops concepts
Polymorphism
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
10
Inheritance
Object Oriented Concepts
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 single unit called class.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
11
Inheritance - Inheriting the properties of one object by another object
Inheritance
Single Inheritance
MultiLevel Inheritance
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
Hybrid Inheritance
12
Multiple Inheritance
Hierarchical Inheritance
Polymorphism - A form taking more than one action. Polymorphism
Run-Time Polymorphism
Compile Time Polymorphism
Overriding
Overloading
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
13
Java Platform Independent
A platform is the hardware or software environment in which a program runs
Once compiled, code will run on any platform without recompiling or any kind of modification. “Write Once Run Anywhere”
This is made possible by making use of a Java Virtual Machine (JVM)
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
14
Java Platform Independent
The Java VM is base for Java platform and is ported onto various hardware-based and OS based platforms.
The JVM converts machine code into byte code and vice versa. This makes platform independence possible.
The Java API is a large collection of ready-made, frequently used class libraries, stored in packages.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
15
The Java Environment Java Program JavaAPI API Java Java Virtual Machine
Native OS Platform
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
16
Java Platfor m
Execution Plan: Source code File Compiler Byte code File Interpreter
JVM
JVM
JVM
Windows
Linux
Sun Solaris
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
17
Structure of a Java Program Package Declaration Import Statements Class declaration Method Definitions Main Method
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
18
A simple Java Program class hello { public static void main(String args[]) { System.out.println(“Welcome to Java”); } }
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
19
Compilation,Execution of program
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
20
Rules of Java
The name of the file must always be the name of the “public class”
It is 100% case sensitive
You can have only one public class in a file (i.e. in one .java file)
Every “stand alone” Java program must have a public static void main defined
it is the starting point of the program.
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
21
Java Keywords abstract
const
finally
implements
public
this
boolean
continue
for
instanceof
throw
transient
break
float
if
null
short
void
byte
default
import
int
super
volatile
case
do
false
return
switch
while
catch
double
interface
package
synchronized
char
else
long
private
static
class
extends
goto
protected
try
true
final
new
native
throws
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
22
Data Types in Java
Java is a Strongly typed language
Two types of variables
Primitive type
Reference type
null is a special type
Reference types cannot be cast to primitive types
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
23
Primitive Data Types in Java Type
In bits
Range
Byte
8
-27 to 27-1
Short
16
-215 to 215-1
Int
32
-231 to 231-1
Long
64
-263 to 263-1
Float
32
3.4e-038 to 3.4e+038
Double
64
1.7e-308 to 1.7e+308
Boolean
8
True/False
Char
16
0 to 65,636 (216-1)
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
24
Reference Types in Java
Arrays, classes, and interfaces are reference types
A reference is called a pointer, or a memory address in other languages The Java programming language does not support the explicit use of addresses like other languages do
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
25
Variables in Java
The variable is the basic unit of storage in a Java program
The variables have a scope, which defines their visibility and lifetime
The variable name is a name given to the memory location in a computer and not for the value present in it.
Member Variables
Automatic Variables
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
26
Variables in Java
Object Reference Variables
Type Conversion and Casting
Implicit (Automatic, non-explicit type changing is known as Conversion)
Explicit (Casting)
Widening conversions
Narrowing conversions are allowed in java
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
27
Variable Types: Local Variables
Variables
Static variables
Instance variables
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
28
Java Operators
Arithmetic Operators =,-,*,/,% Unary Arithmetic operators ++,-Relational operators >,<,>=,<=,==,!= Logical and Bitwise operators &&, ||, !, >>, <<, >>>, &, | Ternary operator ()?():()
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
29
Control Structures in Java
Similar to the “C” Programming Language
Decision making - if-else, switch-case
Loop - for, while, do-while
Miscellaneous - Break, continue, label, return
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
30
Arrays in Java
Java arrays are objects
Array contains like-typed values
Array can contain both primitive and reference data types
When used with reference data types, reference (memory location) is only stored in arrays. They are nothing but ‘array of arrays’
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
31
Arrays in Java
Array Declaration -int my Integers[]; -int[] my Integers;
Array memory allocation -int myIntegers[]=new int[10];
Array Initialization -int my Integers[]={1,2,3,4,5};
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
32
Summary
OO Concepts
Features of Java
Architecture of Java
Structure of a Java Program
Compiling a Java Program
Executing a Java Program
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
33
Summary
Basic Language Rules
Syntax
Keywords in Java
Variables in Java
Operators in Java
Control Structures in Java
Nace Solutions (P) Ltd. Copyright © 2008,All Rights Reserved.
34