8/13/2019 OOP - Java - Session 1
1/44
8/13/2019 OOP - Java - Session 1
2/44
J2SE Core Java
Objective: To introduce the student to Core Java Prerequisites: Knowledge of objectoriented programming
Evaluation method:
Theory exam (objective/subjective/both) 40% weightage
Lab exam 40% weightage Assignments 20% weightage
List of Books / Other training material
Courseware:
The Complete Reference Java Eight Edition, Herbert Schidt/ TMH
Reference: Beginning Java 2 by Ivor Horton; Wrox Publication
8/13/2019 OOP - Java - Session 1
3/44
Session 1:
Learning Objectives
List and explain Java features
Differentiate between C++ and Java
Write a simple Java program HelloWorld
8/13/2019 OOP - Java - Session 1
4/44
8/13/2019 OOP - Java - Session 1
5/44
Language Translators
Machine language is the only language capable of directly instructingthe CPU.
Every non machine language program instruction must be translatedinto machine language prior to execution.
Language Translators convert High-level code into Machinelanguage.
Interpreters translate one program statement at a time, as theprogram is running.
Compilers translate a complete program into machine language,then the machine language program is executed as needed.
Because compiled programs run faster than programs that aretranslated line by line by an interpreter, programmers usuallychoose compilers to translate frequently run business programs.
8/13/2019 OOP - Java - Session 1
6/44
Java History
Computer language innovation and development occurs for two
fundamental reasons:
1) To adapt to the changing environments and uses
2) To implement improvements in the art of programming
The development of Java was driven by both in equal measures.
Many Java features are inherited from the earlier languages:
C C++ Java
8/13/2019 OOP - Java - Session 1
7/44
Before Java: C
Designed by Dennis Ritchie in 1972.
Before C:
BASIC, COBOL, FORTRAN, PASCAL
C is structured, efficient, high-level language that could replace
assembly code when creating systems programs.
8/13/2019 OOP - Java - Session 1
8/44
Before Java: C++
Designed by Bjarne Stroustrup in 1979.
In response to the increased complexity of programs and respectiveimprovements in the programming paradigms and methods:
1) Assembler languages
2) High-level languages
3) Structured programming
4) Object-oriented programming (OOP)
OOP a methodology that helps organize complex programs through
the use of inheritance, encapsulation and polymorphism.
C++ extends C by adding object-oriented features.
8/13/2019 OOP - Java - Session 1
9/44
Java: History
In 1990, Sun Microsystems started a project called Green.
Objective: To develop software for consumer electronics.
Project was assigned to James Gosling, a veteran of classic networksoftware design. Others included Patrick Naughton, ChrisWarth, Ed
Frank, and Mike Sheridan.
The team started writing programs in C++ for embedding into
Set top boxes
Washing machines
VCRs
- Ovens
Aim was to make these appliances more intelligent.
8/13/2019 OOP - Java - Session 1
10/44
Java: History
C++ is powerful, but also dangerous.
The power and popularity of C derived from the extensive use ofpointers.
Incorrect use of pointers can cause memory leaks, leading the
program to crash.
Replacing pointers by references, and automating memorymanagement was the proposed solution.
8/13/2019 OOP - Java - Session 1
11/44
Java: History
Hence, the team built a new programming language called Oak,whichavoided potentially dangerous constructs in C++, such as
pointers, pointer arithmetic, operator overloading etc.
Introduced automatic memory management, freeing theprogrammer to concentrate on other things.
Architecture neutrality (Platform independence)
Many different CPUs are used as controllers in consumer electronicdevices (They may change as per the new trends in technology).
So, the software and programming language had to be architectureneutral.
8/13/2019 OOP - Java - Session 1
12/44
Java: History It was soon realized that these design goals of consumer electronics
perfectly suited an ideal programming language for the Internet andWWW, which should be:
Object-oriented (& support GUI) Robust Architecture neutral
Internet programming presented a BIG business opportunity. Muchbigger than programming for consumer electronics.
Java was re-targeted for the Internet
The team was expanded to include Bill Joy (developer of Unix), Arthurvan Hoff, Jonathan Payne, Frank Yellin, Tim Lindholm etc.
In 1994, an early web browser called WebRunner was written in Oak.WebRunner was later renamed HotJava.
In 1995, Oak was renamed Java.
A common story is that the name Java relates to the place from where
the developmentteam got its coffee. The name Java survived thetrade mark search.
8/13/2019 OOP - Java - Session 1
13/44
Programming language
Another programming language using which we can develop applets,standalone applications, web applications and enterprise applications.
Platform Independent
A Java program written and compiled on one machine can be executedon any other machine (irrespective of the operating
system/architecture)
Object Oriented
Complies to object oriented programming concepts. Your program isnot object oriented unless you code that way
Compiled and Interpreted
The .java file is compiled to a .class file and the .class file isinterpreted to machine code
What is Java?
8/13/2019 OOP - Java - Session 1
14/44
The Java Buzzwords
The key considerations were summed up by the Java
team in the following list of buzzwords:
Simple
Secure
Portable
Object-oriented Robust
Multithreaded
Architecture-neutral
Interpreted
High performance Distributed
Dynamic
8/13/2019 OOP - Java - Session 1
15/44
SimpleJava is designed to be easy for the professionalprogrammer to learn and use.
Object-oriented:A clean, usable, pragmatic approach to objects
Robust:Restricts the programmer to find the mistakes early,performs compile-time (strong typing) and run-time (exception-handling) checks, manages memory automatically.
Multithreaded:Supports multi-threaded programming for writingprogram that perform concurrent computations
Architecture-neutral and Portable:Java Virtual Machine provides a
platform independent environment for the execution of Java byte codeand can be ported to any machine.
The Java Buzzwords
8/13/2019 OOP - Java - Session 1
16/44
Interpreted and High-performance:Java programs are
compiled into an intermediate representation byte code:
a) Later interpreted by any JVM
b) Translated into the native machine code (JIT)
Distributed:Java handles TCP/IP protocols, accessing a resource
through its URL much like accessing a local file
Dynamic:Substantial amounts of run-time type information toverify and resolve access to objects at run-time.
Secure:Programs are confined to the Java execution environmentand cannot access other parts of the computer.
The Java Buzzwords
8/13/2019 OOP - Java - Session 1
17/44
The Java Platform
A platformis the hardware and software environment in which aprogram runs.
The Java platform differs from most other platforms in that it's asoftware-only platform that runs on top of other, hardware-based platforms.
The Java platform has two components:
The Java Virtual Machine (Java VM)
The Java Application Programming Interface (Java API)
The Java API is a large collection of ready-made software
components that provide many useful capabilities, such asgraphical user interface (GUI) widgets.
The Java API is grouped into libraries (packages) of relatedcomponents which allow you to do various things.
8/13/2019 OOP - Java - Session 1
18/44
Versions of Java
Java Language vs. Java Platform Current version of the languageis 1.7 (JDK 7)
Core language plus additional APIs is called the Java 2platform
Three versions of the Java 2 Platform, targeted at differentuses
Java 2 Standard Edition (J2SE)
The basic platform, which this module will cover Java 2 Enterprise Edition (J2EE)
For business applications, web services, mission-critical systems
Transaction processing, databases, distribution, replication
Java 2 Micro Edition (J2ME)
Very small Java environment for smart cards, mobile phones, andset-top boxes
Subset of the standard Java libraries aimed at limited size andprocessing power
8/13/2019 OOP - Java - Session 1
19/44
The Java SE PlatformDevelop and deploy Java applications on desktops and servers andembedded environments..
8/13/2019 OOP - Java - Session 1
20/44
The Java SE PlatformDevelop and deploy Java applications on desktops and servers andembedded environments..
8/13/2019 OOP - Java - Session 1
21/44
21
Introduction Java Virtual Machine
.class
file
Java Virtual Machine
.java file
Java
Compiler
UNIXMicrosoftMac
8/13/2019 OOP - Java - Session 1
22/44
Java Virtual Machine
Every Java interpreter, whether it's a Java development tool or a Webbrowser that can run Java applets, is an implementation of the Java
VM.The Java VM can also be implemented in hardware.
Java byte codes help make "write once, run anywhere" possible.
We can compile your Java program into byte codes on any platformthat has a Java compiler.
The byte codes can then be run on any implementation of the Java VM.
8/13/2019 OOP - Java - Session 1
23/44
Why Use Java?
Write less code: Comparisons of program metrics (class counts,
method counts, and so on) suggest that a program written in Java canbe four times smaller than the same program in C++.
Write better code: The Java language encourages good codingpractices, and its garbage collection helps you avoid memory leaks.
Develop programs faster: Your development time may be as muchas twice as fast versus writing the same program in C++.
Avoid platform dependencieswith 100% Pure Java
Write once, run anywhere: Because 100% Pure Java programs arecompiled into machine-independent byte codes, they run consistentlyon any Java platform.
Distribute software more easily: You can upgrade applets easilyfrom a central server. Applets take advantage of the Java feature ofallowing new classes to be loaded "on the fly," without recompiling theentire program.
8/13/2019 OOP - Java - Session 1
24/44
Java Programming Environment
Specification :
Java compiler: Transform Java programs into Java byte code
Java byte code: Intermediate representation for Java programs
Java interpreter: Read programs written in Java byte code andexecute them
Java virtual machine: Runtime system that provides variousservices to running programs
Java programming environment: Set of libraries that provideservices such as GUI, data structures, etc.
Java enabled browsers: Browsers that include a JVM + ability to
load programs from remote hosts
8/13/2019 OOP - Java - Session 1
25/44
Object Oriented Languages -A Comparison
Feature C++ Ada Java
Encapsulation Yes Yes YesInheritance Yes No Yes
Multiple Inherit. Yes No NoPolymorphism Yes Yes YesBinding (Early/Late) Both Early LateConcurrency Poor Difficult YesGarbage Collection No No Yes
Class Libraries Yes Limited Yes
C++: OOL ADA: OBL JAVA: Almost Pure OOL
8/13/2019 OOP - Java - Session 1
26/44
Java better than C++ ?
No Typedef, #Defines or Preprocessor
No Global Variables
No goto statements
No Pointers and pointer arithmetic
No Unsafe Structures
No Multiple Inheritance
No Operator Overloading
No Fragile Data Types
8/13/2019 OOP - Java - Session 1
27/44
Removed From C++
Operator overloading
Pointers and array of pointers
Multiple-Inheritance
typedef, #define
Copy constructors, Destructors
Templates
And other stuff....
8/13/2019 OOP - Java - Session 1
28/44
Added or Improved over C++
Interfaces: Type Vs. Class
Garbage collection
Exceptions (More powerful than C++)
Strings
Instance of
Packages
Multi-threading
8/13/2019 OOP - Java - Session 1
29/44
Types of Java Applications
Different ways to write/run a Java codes are:Application- A stand-alone program that can be invoked from
command line . A program that has amain()method
Applet- A program embedded in a web page , to be run whenthe page is browsed .
A program that contains nomainmethod
Application -Java interpreter
Applets- Java enabled web browser (Linked to HTML via tag in .html file)
8/13/2019 OOP - Java - Session 1
30/44
Java Program Structure
The Java programming language:
A program is made up of one or more classes
A class contains one or more methods
A method contains programstatements
A Java application always contains a method calledmain()
8/13/2019 OOP - Java - Session 1
31/44
Java Program Structure
// comments about the class
public class MyProgram
class header
class body
Comments can be added almost anywhere
{
}
8/13/2019 OOP - Java - Session 1
32/44
Java Program Structure
public class MyProgram
{
}
public static void main (String[] args)
{
}
// comments about the class
// comments about the method
method headermethod body
8/13/2019 OOP - Java - Session 1
33/44
Comments
Comments in a program are also called inline documentation They should be included to explain the purpose of the program and describe
processing steps
They do not affect how a program works
Java comments can take two forms:
// this comment runs to the end of the line
/* this comment runs to the terminatingsymbol, even across line breaks */
/** This kind of comment is a specialjavadocstyle comment */
8/13/2019 OOP - Java - Session 1
34/44
Java Program - Example
8/13/2019 OOP - Java - Session 1
35/44
Java Program - Example
8/13/2019 OOP - Java - Session 1
36/44
Java Program - Example
8/13/2019 OOP - Java - Session 1
37/44
A Simple Java Application
Define a class HelloWorld and store it into a file:HelloWorld.java:
public class HelloWorld {
public static void main (String[] args) {
System.out.println(Hello, World);}
}
Compile the program: javac HelloWorld.java
Execute the program: java HelloWorld
Output: Hello, World
8/13/2019 OOP - Java - Session 1
38/44
8/13/2019 OOP - Java - Session 1
39/44
Compiling Programs
On command line
javac file.javaSource Code
Create/Modify Source Code
Compile Source Code
i.e. javac Welcome.java
Bytecode
Run Byteode
i.e. java Welcome
Result
If compilation errors
If runtime errors or incorrect result
8/13/2019 OOP - Java - Session 1
40/44
Executing Applications
On command line
java classname
Java
Interpreter
on Windows
Java
Interpreter
on Sun Solaris
Java
Interpreter
on Linux
Bytecode
...
8/13/2019 OOP - Java - Session 1
41/44
8/13/2019 OOP - Java - Session 1
42/44
Next
basic programming constructs of Java
Classes and Objects in Java
new operator
Constructors
Overloading
8/13/2019 OOP - Java - Session 1
43/44
J2SE Core Java
PG-DAC
Aug-2013
8/13/2019 OOP - Java - Session 1
44/44
J2SE Core Java
Objective: To introduce the student to Core Java Prerequisites: Knowledge of objectoriented programming
Evaluation method:
Theory exam (objective/subjective/both) 40% weightage
Lab exam 40% weightage
Assignments 20% weightage
List of Books / Other training material
Courseware:
The Complete Reference Java Eight Edition, Herbert Schidt/ TMH
Reference: Beginning Java 2 by Ivor Horton; Wrox Publication